Skip to content

Commit

Permalink
fix(git): gracefully handle a file in the index but not in the filesy…
Browse files Browse the repository at this point in the history
…stem

This can happen if a user removes a file from the filesystem and that change hasn't been staged yet.

We log a warning when this happens and continue.

Signed-off-by: Brian McGee <[email protected]>
  • Loading branch information
brianmcgee committed Aug 8, 2024
1 parent 990655f commit f9bc8b0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 6 additions & 2 deletions cli/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,14 +530,18 @@ func TestGitWorktree(t *testing.T) {
as.NoError(err)
run(32, 32, 32, 0)

// remove python directory
// remove python directory from the worktree
as.NoError(wt.RemoveGlob("python/*"))
run(28, 28, 28, 0)

// remove nixpkgs.toml from the filesystem but leave it in the index
as.NoError(os.Remove(filepath.Join(tempDir, "nixpkgs.toml")))
run(27, 27, 27, 0)

// walk with filesystem instead of git
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--walk", "filesystem")
as.NoError(err)
assertStats(t, as, 61, 61, 61, 0)
assertStats(t, as, 60, 60, 60, 0)
}

func TestPathsArg(t *testing.T) {
Expand Down
8 changes: 7 additions & 1 deletion walk/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

type gitWalker struct {
log *log.Logger
root string
paths chan string
repo *git.Repository
Expand Down Expand Up @@ -59,7 +60,11 @@ func (g gitWalker) Walk(ctx context.Context, fn WalkFunc) error {
path := filepath.Join(g.root, entry.Name)

info, err := os.Lstat(path)
if err != nil {
if os.IsNotExist(err) {
// the underlying file might have been removed without the change being staged yet
g.log.Warnf("Path %s is in the index but appears to have been removed from the filesystem", path)
continue
} else if err != nil {
return fmt.Errorf("failed to stat %s: %w", path, err)
}

Expand Down Expand Up @@ -136,6 +141,7 @@ func NewGit(root string, paths chan string) (Walker, error) {
return nil, fmt.Errorf("failed to open git repo: %w", err)
}
return &gitWalker{
log: log.WithPrefix("walker[git]"),
root: root,
paths: paths,
repo: repo,
Expand Down

0 comments on commit f9bc8b0

Please sign in to comment.