Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,20 @@ func (git *repoSync) removeStaleWorktrees() (int, error) {
return count, nil
}

func hasGitLockFile(gitRoot absPath) (string, error) {
gitLockFiles := []string{"shallow.lock"}
for _, lockFile := range gitLockFiles {
lockFilePath := gitRoot.Join(".git", lockFile).String()
_, err := os.Stat(lockFilePath)
if err == nil {
return lockFilePath, nil
} else if !errors.Is(err, os.ErrNotExist) {
return lockFilePath, err
}
}
return "", nil
}

// sanityCheckRepo tries to make sure that the repo dir is a valid git repository.
func (git *repoSync) sanityCheckRepo(ctx context.Context) bool {
git.log.V(3).Info("sanity-checking git repo", "repo", git.root)
Expand Down Expand Up @@ -1258,6 +1272,16 @@ func (git *repoSync) sanityCheckRepo(ctx context.Context) bool {
return false
}

// Check if the repository contains an unreleased lock file. This can happen if
// a previous git invocation crashed.
if lockFile, err := hasGitLockFile(git.root); err != nil {
git.log.Error(err, "error calling stat on file", "path", lockFile)
return false
} else if len(lockFile) > 0 {
git.log.Error(nil, "repo contains lock file", "path", lockFile)
return false
}

return true
}

Expand Down
40 changes: 40 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,43 @@ func TestTouch(t *testing.T) {
t.Errorf("touch(newfile) mtime %v is not after %v", newfileInfo.ModTime(), stamp)
}
}

func TestHasGitLockFile(t *testing.T) {
testCases := map[string]struct {
inputFilePath []string
expectLockFile bool
}{
"missing .git directory": {
expectLockFile: false,
},
"has git directory but no lock files": {
inputFilePath: []string{".git", "HEAD"},
expectLockFile: false,
},
"shallow.lock file": {
inputFilePath: []string{".git", "shallow.lock"},
expectLockFile: true,
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
root := absPath(t.TempDir())

if len(tc.inputFilePath) > 0 {
if err := touch(root.Join(tc.inputFilePath...)); err != nil {
t.Fatal(err)
}
}

lockFile, err := hasGitLockFile(root)
if err != nil {
t.Fatal(err)
}
hasLock := len(lockFile) > 0
if hasLock != tc.expectLockFile {
t.Fatalf("expected hasGitLockFile to return %v, but got %v", tc.expectLockFile, hasLock)
}
})
}
}