Move prune logic for cohesion

This commit is contained in:
Tim Hockin 2019-10-27 21:19:42 -07:00
parent 147a5f5fff
commit d980b6d9d6
1 changed files with 24 additions and 24 deletions

View File

@ -380,50 +380,36 @@ func sleepForever() {
os.Exit(0) os.Exit(0)
} }
// updateSymlink atomically swaps the symlink to point at the specified directory and cleans up the previous worktree. // updateSymlink atomically swaps the symlink to point at the specified
func updateSymlink(ctx context.Context, gitRoot, link, newDir string) error { // directory and cleans up the previous worktree. If there was a previous
// worktree, this returns the path to it.
func updateSymlink(ctx context.Context, gitRoot, link, newDir string) (string, error) {
// Get currently-linked repo directory (to be removed), unless it doesn't exist // Get currently-linked repo directory (to be removed), unless it doesn't exist
fullpath := path.Join(gitRoot, link) fullpath := path.Join(gitRoot, link)
currentDir, err := filepath.EvalSymlinks(fullpath) currentDir, err := filepath.EvalSymlinks(fullpath)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("error accessing symlink: %v", err) return "", fmt.Errorf("error accessing symlink: %v", err)
} }
// newDir is /git/rev-..., we need to change it to relative path. // newDir is /git/rev-..., we need to change it to relative path.
// Volume in other container may not be mounted at /git, so the symlink can't point to /git. // Volume in other container may not be mounted at /git, so the symlink can't point to /git.
newDirRelative, err := filepath.Rel(gitRoot, newDir) newDirRelative, err := filepath.Rel(gitRoot, newDir)
if err != nil { if err != nil {
return fmt.Errorf("error converting to relative path: %v", err) return "", fmt.Errorf("error converting to relative path: %v", err)
} }
const tmplink = "tmp-link" const tmplink = "tmp-link"
if _, err := runCommand(ctx, gitRoot, "ln", "-snf", newDirRelative, tmplink); err != nil { if _, err := runCommand(ctx, gitRoot, "ln", "-snf", newDirRelative, tmplink); err != nil {
return fmt.Errorf("error creating symlink: %v", err) return "", fmt.Errorf("error creating symlink: %v", err)
} }
log.V(1).Info("created tmp symlink", "root", gitRoot, "dst", newDirRelative, "src", tmplink) log.V(1).Info("created tmp symlink", "root", gitRoot, "dst", newDirRelative, "src", tmplink)
if _, err := runCommand(ctx, gitRoot, "mv", "-T", tmplink, link); err != nil { if _, err := runCommand(ctx, gitRoot, "mv", "-T", tmplink, link); err != nil {
return fmt.Errorf("error replacing symlink: %v", err) return "", fmt.Errorf("error replacing symlink: %v", err)
} }
log.V(1).Info("renamed symlink", "root", gitRoot, "old_name", tmplink, "new_name", link) log.V(1).Info("renamed symlink", "root", gitRoot, "old_name", tmplink, "new_name", link)
// Clean up previous worktree return currentDir, nil
if len(currentDir) > 0 {
if err = os.RemoveAll(currentDir); err != nil {
return fmt.Errorf("error removing directory: %v", err)
}
log.V(1).Info("removed previous worktree", "path", currentDir)
_, err := runCommand(ctx, gitRoot, *flGitCmd, "worktree", "prune")
if err != nil {
return err
}
log.V(1).Info("pruned old worktrees")
}
return nil
} }
// addWorktreeAndSwap creates a new worktree and calls updateSymlink to swap the symlink to point to the new worktree // addWorktreeAndSwap creates a new worktree and calls updateSymlink to swap the symlink to point to the new worktree
@ -494,7 +480,21 @@ func addWorktreeAndSwap(ctx context.Context, gitRoot, dest, branch, rev string,
} }
} }
return updateSymlink(ctx, gitRoot, dest, worktreePath) // Flip the symlink.
if oldWorktree, err := updateSymlink(ctx, gitRoot, dest, worktreePath); err != nil {
return err
} else if oldWorktree != "" {
// Clean up previous worktree
if err := os.RemoveAll(oldWorktree); err != nil {
return fmt.Errorf("error removing directory: %v", err)
}
if _, err := runCommand(ctx, gitRoot, *flGitCmd, "worktree", "prune"); err != nil {
return err
}
log.V(1).Info("pruned old worktrees")
}
return nil
} }
func cloneRepo(ctx context.Context, repo, branch, rev string, depth int, gitRoot string) error { func cloneRepo(ctx context.Context, repo, branch, rev string, depth int, gitRoot string) error {