Skip to content

Commit cc33fd4

Browse files
committed
delete index.lock file upon repository init
Signed-off-by: reggie-k <regina.voloshin@codefresh.io>
1 parent 7159a9a commit cc33fd4

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

reposerver/repository/repository.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,30 @@ func (s *Service) Init() error {
165165
fullPath := filepath.Join(s.rootDir, file.Name())
166166
closer := s.gitRepoInitializer(fullPath)
167167
if repo, err := gogit.PlainOpen(fullPath); err == nil {
168+
indexFile := filepath.Join(fullPath, ".git", "index")
169+
indexLockFile := filepath.Join(fullPath, ".git", "index.lock")
170+
if _, err = os.Stat(indexLockFile); err == nil {
171+
log.Warnf("Lock file present in git repository %s, removing it", fullPath)
172+
if err = os.Remove(indexLockFile); err != nil {
173+
log.Errorf("Failed to remove lock file %s: %v", indexLockFile, err)
174+
}
175+
if err = os.Remove(indexFile); err != nil {
176+
log.Errorf("Failed to remove index file %s: %v", indexFile, err)
177+
}
178+
179+
if err == nil {
180+
wt, _ := repo.Worktree()
181+
headRef, _ := repo.Head()
182+
err = wt.Reset(&gogit.ResetOptions{
183+
Mode: gogit.MixedReset,
184+
Commit: headRef.Hash(),
185+
})
186+
}
187+
188+
if err != nil {
189+
log.Errorf("Failed to reset git repo %s: %v", fullPath, err)
190+
}
191+
}
168192
if remotes, err := repo.Remotes(); err == nil && len(remotes) > 0 && len(remotes[0].Config().URLs) > 0 {
169193
s.gitRepoPaths.Add(git.NormalizeGitURL(remotes[0].Config().URLs[0]), fullPath)
170194
}

reposerver/repository/repository_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3014,6 +3014,21 @@ func TestInit(t *testing.T) {
30143014
_, err := os.ReadDir(dir)
30153015
require.Error(t, err)
30163016
initGitRepo(t, newGitRepoOptions{path: path.Join(dir, "repo2"), remote: "https://github.com/argo-cd/test-repo2", createPath: true, addEmptyCommit: false})
3017+
3018+
repoPath = path.Join(dir, "repo3")
3019+
lockFile := path.Join(repoPath, ".git", "index.lock")
3020+
initGitRepo(t, newGitRepoOptions{path: repoPath, remote: "https://github.com/argo-cd/test-repo3", createPath: true, addEmptyCommit: false})
3021+
require.NoError(t, os.WriteFile(lockFile, []byte("test"), 0o644))
3022+
3023+
service = newService(t, ".")
3024+
service.rootDir = dir
3025+
3026+
_, err = os.Stat(lockFile)
3027+
require.NoError(t, err)
3028+
require.NoError(t, service.Init())
3029+
_, err = os.Stat(lockFile)
3030+
require.Error(t, err, "lock file should be removed after Init()")
3031+
require.ErrorContains(t, err, ".git/index.lock: no such file or directory")
30173032
}
30183033

30193034
// TestCheckoutRevisionCanGetNonstandardRefs shows that we can fetch a revision that points to a non-standard ref. In

0 commit comments

Comments
 (0)