Skip to content

Commit 73cec03

Browse files
committed
Some code cleanups
1 parent 8e58871 commit 73cec03

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

modules/git/command.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -276,23 +276,37 @@ func (c *Command) Run(opts *RunOpts) error {
276276
return err
277277
}
278278
// Execute the git command
279-
if err := c.run(opts); err != nil {
280-
if exitError, ok := err.(*exec.ExitError); ok {
281-
if exitError.ExitCode() > 128 {
282-
// Errors greater than 128 means that the process was killed by an OS signal (see https://unix.stackexchange.com/questions/99112/default-exit-code-when-process-is-terminated)
283-
log.Warn("It appears that the git process %s has crashed. Attempting to forcbily unlock it [repo: %s]", exitError.Pid(), opts.Dir)
284-
ForciblyUnlockRepository(c.parentContext, opts.Dir)
285-
return err
286-
}
287-
return err
288-
}
279+
if err := c.doRun(opts); err != nil {
280+
unlockUponCrashing(c.parentContext, err, opts.Dir)
289281
return err
290282
}
291283
return nil
292284
}
293285

286+
func unlockUponCrashing(ctx context.Context, originalError error, repoDir string) {
287+
if hasGitProcessCrashed(originalError) {
288+
log.Warn("The git process has crashed. Attempting to forcbily unlock the underlying repo at %s", repoDir)
289+
if err := ForciblyUnlockRepository(ctx, repoDir); err != nil {
290+
log.Error("Error while trying to unlock repository at %v", err)
291+
}
292+
}
293+
}
294+
295+
func hasGitProcessCrashed(err error) bool {
296+
if exitError, ok := err.(*exec.ExitError); ok {
297+
if runtime.GOOS == "windows" {
298+
log.Warn("Cannot realiably detected if the git process has crashed in windows. Assuming it hasn't [exitCode: %s, pid: %s]", exitError.ExitCode(), exitError.Pid())
299+
return false
300+
}
301+
return exitError.ExitCode() > 128
302+
}
303+
// This function should only be called with an ExitError
304+
log.Error("hasGitProcessCrashed should only be called with an ExitError [err: %v]. Assuming it the git process hasn't crashed", err)
305+
return false
306+
}
307+
294308
// Run runs the command with the RunOpts
295-
func (c *Command) run(opts *RunOpts) error {
309+
func (c *Command) doRun(opts *RunOpts) error {
296310
if len(c.brokenArgs) != 0 {
297311
log.Error("git command is broken: %s, broken args: %s", c.String(), strings.Join(c.brokenArgs, " "))
298312
return ErrBrokenCommand

modules/git/repo_cleanup.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,18 @@ func ForciblyUnlockRepositoryIfNeeded(ctx context.Context, repoPath string) erro
2727
func cleanLocksIfNeeded(repoPath string, threshold time.Time) error {
2828
if repoPath == "" {
2929
return nil
30-
} else {
31-
log.Trace("Checking if repository %s is locked [lock threshold is %s]", repoPath, threshold)
32-
return filepath.Walk(repoPath, func(filePath string, fileInfo os.FileInfo, err error) error {
33-
if err != nil {
34-
return err
35-
}
36-
if err := cleanLockIfNeeded(filePath, fileInfo, threshold); err != nil {
37-
log.Error("Failed to remove lock file %s: %v", filePath, err)
38-
return err
39-
}
40-
return nil
41-
})
4230
}
31+
log.Trace("Checking if repository %s is locked [lock threshold is %s]", repoPath, threshold)
32+
return filepath.Walk(repoPath, func(filePath string, fileInfo os.FileInfo, err error) error {
33+
if err != nil {
34+
return err
35+
}
36+
if err := cleanLockIfNeeded(filePath, fileInfo, threshold); err != nil {
37+
log.Error("Failed to remove lock file %s: %v", filePath, err)
38+
return err
39+
}
40+
return nil
41+
})
4342
}
4443

4544
func cleanLockIfNeeded(filePath string, fileInfo os.FileInfo, threshold time.Time) error {

0 commit comments

Comments
 (0)