Skip to content

Commit afb4476

Browse files
committed
handle lock file deletion before checkoputRevision
Signed-off-by: reggie-k <regina.voloshin@codefresh.io>
1 parent 226ccc5 commit afb4476

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

reposerver/repository/repository.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,7 +2533,25 @@ func fetch(gitClient git.Client, targetRevisions []string) error {
25332533
return nil
25342534
}
25352535

2536+
// removeStaleGitLocks best-effort removes common stale git lock files in the repository.
2537+
// It is safe to call while holding the per-repo lock and before running any git commands.
2538+
2539+
func removeStaleGitLocks(repoRoot string) {
2540+
path := filepath.Join(repoRoot, ".git", "HEAD.lock")
2541+
log.Infof("Checking whether HEAD.lock exists %s", path)
2542+
if _, err := os.Stat(path); err == nil {
2543+
log.Warnf("HEAD.lock present in git repository %s, removing it", repoRoot)
2544+
if rmErr := os.Remove(path); rmErr == nil {
2545+
log.Infof("Removed stale git lock %s", path)
2546+
} else if !os.IsNotExist(rmErr) {
2547+
log.Warnf("Failed to remove git lock %s: %v", path, rmErr)
2548+
}
2549+
}
2550+
}
2551+
25362552
func checkoutRevision(gitClient git.Client, revision string, submoduleEnabled bool) error {
2553+
removeStaleGitLocks(gitClient.Root())
2554+
25372555
err := gitClient.Init()
25382556
if err != nil {
25392557
return status.Errorf(codes.Internal, "Failed to initialize git repo: %v", err)

reposerver/repository/repository_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,6 +3016,29 @@ func TestInit(t *testing.T) {
30163016
initGitRepo(t, newGitRepoOptions{path: path.Join(dir, "repo2"), remote: "https://github.com/argo-cd/test-repo2", createPath: true, addEmptyCommit: false})
30173017
}
30183018

3019+
func TestRemoveStaleGitHeadLock(t *testing.T) {
3020+
dir := t.TempDir()
3021+
// create a fake repo structure with .git/HEAD.lock
3022+
gitDir := path.Join(dir, ".git")
3023+
require.NoError(t, os.MkdirAll(gitDir, 0o755))
3024+
headLock := path.Join(gitDir, "HEAD.lock")
3025+
require.NoError(t, os.WriteFile(headLock, []byte("test"), 0o644))
3026+
3027+
// sanity: lock exists before
3028+
_, err := os.Stat(headLock)
3029+
require.NoError(t, err)
3030+
3031+
removeStaleGitLocks(dir)
3032+
3033+
// lock should be gone after cleanup
3034+
_, err = os.Stat(headLock)
3035+
require.Error(t, err)
3036+
require.True(t, os.IsNotExist(err))
3037+
3038+
// calling again should be a no-op (no error)
3039+
removeStaleGitLocks(dir)
3040+
}
3041+
30193042
// TestCheckoutRevisionCanGetNonstandardRefs shows that we can fetch a revision that points to a non-standard ref. In
30203043
// other words, we haven't regressed and caused this issue again: https://github.com/argoproj/argo-cd/issues/4935
30213044
func TestCheckoutRevisionCanGetNonstandardRefs(t *testing.T) {
@@ -3057,6 +3080,7 @@ func TestCheckoutRevisionPresentSkipFetch(t *testing.T) {
30573080

30583081
gitClient := &gitmocks.Client{}
30593082
gitClient.On("Init").Return(nil)
3083+
gitClient.On("Root").Return("<repo-root>")
30603084
gitClient.On("IsRevisionPresent", revision).Return(true)
30613085
gitClient.On("Checkout", revision, mock.Anything).Return("", nil)
30623086

@@ -3069,6 +3093,7 @@ func TestCheckoutRevisionNotPresentCallFetch(t *testing.T) {
30693093

30703094
gitClient := &gitmocks.Client{}
30713095
gitClient.On("Init").Return(nil)
3096+
gitClient.On("Root").Return("<repo-root>")
30723097
gitClient.On("IsRevisionPresent", revision).Return(false)
30733098
gitClient.On("Fetch", "").Return(nil)
30743099
gitClient.On("Checkout", revision, mock.Anything).Return("", nil)
@@ -3083,6 +3108,7 @@ func TestFetch(t *testing.T) {
30833108

30843109
gitClient := &gitmocks.Client{}
30853110
gitClient.On("Init").Return(nil)
3111+
gitClient.On("Root").Return("<repo-root>")
30863112
gitClient.On("IsRevisionPresent", revision1).Once().Return(true)
30873113
gitClient.On("IsRevisionPresent", revision2).Once().Return(false)
30883114
gitClient.On("Fetch", "").Return(nil)

0 commit comments

Comments
 (0)