Skip to content

Commit

Permalink
do not remove untracked working directory files when switching branch
Browse files Browse the repository at this point in the history
  • Loading branch information
richardjennings committed Jan 28, 2024
1 parent 754ba8f commit cbd06ff
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions internal/mygit/mygit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cbd06ff

Please sign in to comment.