@@ -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
30213044func 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