Skip to content

Commit

Permalink
ci_runner: cleanup previously setup repo
Browse files Browse the repository at this point in the history
Clean up well known files that could cause subsequent git operations to
fail.
  • Loading branch information
sluongng committed Jun 18, 2024
1 parent 9a9a91b commit 3da4637
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions enterprise/server/cmd/ci_runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,9 @@ func (ws *workspace) setup(ctx context.Context) error {
if err := os.Chdir(repoDirName); err != nil {
return status.WrapError(err, "cd")
}
if err := ws.cleanup(ctx); err != nil {
return err
}
err := ws.sync(ctx)
if err == nil {
return nil
Expand Down Expand Up @@ -1690,6 +1693,27 @@ func (ws *workspace) applyPatch(ctx context.Context, bsClient bspb.ByteStreamCli
return nil
}

var gitTroubles = []string{
"index.lock",
"shallow.lock",
"HEAD.lock",
"hooks/post-checkout",
"config.lock",
}

func (ws *workspace) cleanup(ctx context.Context) error {
for _, f := range gitTroubles {
if err := os.Remove(filepath.Join(".git", f)); err != nil {
if strings.Contains(err.Error(), "no such file or directory") {
// It's good that the file does not exist
continue
}
return fmt.Errorf("failed to cleanup %s: %v", f, err)
}
}
return nil
}

func (ws *workspace) sync(ctx context.Context) error {
if *pushedBranch == "" && *commitSHA == "" {
return status.InvalidArgumentError("expected at least one of `pushed_branch` or `commit_sha` to be set")
Expand Down

0 comments on commit 3da4637

Please sign in to comment.