From cbd06ff059d5c4eaf49ad00efad812436df19ab1 Mon Sep 17 00:00:00 2001 From: Richard Jennings Date: Sun, 28 Jan 2024 18:02:38 +0000 Subject: [PATCH] do not remove untracked working directory files when switching branch --- internal/mygit/mygit.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/internal/mygit/mygit.go b/internal/mygit/mygit.go index eb9573a..a27a723 100644 --- a/internal/mygit/mygit.go +++ b/internal/mygit/mygit.go @@ -289,16 +289,23 @@ func SwitchBranch(name string) error { if err != nil { return err } - // delete any files not in branch + idxFiles := idx.Files() + idxMap := make(map[string]*fs.File) + for _, v := range idxFiles { + idxMap[v.Path] = v + } + // delete any files not in branch - and, not in index, for _, v := range files { if v.Status == fs.StatusAdded { - if err := os.Remove(filepath.Join(config.Path(), v.Path)); err != nil { - return err + if _, ok := idxMap[v.Path]; ok { + if err := os.Remove(filepath.Join(config.Path(), v.Path)); err != nil { + return err + } } } } // check for index changes - idxFiles := idx.Files() + files, err = index.CompareAsCommit(commitFiles, idxFiles) if err != nil { return err