Skip to content

Commit 482570b

Browse files
committed
deletion of HEAD.lock file upon finish of RunCommandExt
Signed-off-by: reggie-k <regina.voloshin@codefresh.io>
1 parent 7023f59 commit 482570b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

util/exec/exec.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,21 @@ func RunCommandExt(cmd *exec.Cmd, opts CmdOpts) (string, error) {
184184
args := strings.Join(cmd.Args, " ")
185185
logCtx.WithFields(logrus.Fields{"dir": cmd.Dir}).Info(redactor(args))
186186

187+
// Best-effort cleanup of a stale HEAD.lock after the command finishes.
188+
defer func() {
189+
if cmd.Dir == "" {
190+
return
191+
}
192+
lockPath := filepath.Join(cmd.Dir, ".git", "HEAD.lock")
193+
if _, err := os.Stat(lockPath); err == nil {
194+
// Log and attempt removal; ignore ENOENT races
195+
logCtx.WithFields(logrus.Fields{"headLockPath": lockPath}).Warn("HEAD.lock present post-exec, removing it")
196+
if rmErr := os.Remove(lockPath); rmErr != nil && !os.IsNotExist(rmErr) {
197+
logCtx.WithFields(logrus.Fields{"headLockPath": lockPath}).Warnf("Failed to remove HEAD.lock: %v", rmErr)
198+
}
199+
}
200+
}()
201+
187202
// Helper: debug whether HEAD.lock exists under the current working directory
188203
logHeadLockStatus := func(where string) {
189204
if cmd.Dir == "" {

0 commit comments

Comments
 (0)