From 415303f9009835c305e87b39b2c002e5cab9478e Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Sun, 27 Oct 2019 17:40:14 -0700 Subject: [PATCH 1/3] Rename for clarity --- cmd/git-sync/main.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/git-sync/main.go b/cmd/git-sync/main.go index 060b4e8..f836241 100644 --- a/cmd/git-sync/main.go +++ b/cmd/git-sync/main.go @@ -525,7 +525,8 @@ func cloneRepo(ctx context.Context, repo, branch, rev string, depth int, gitRoot return nil } -func hashForRev(ctx context.Context, rev, gitRoot string) (string, error) { +// localHashForRev returns the locally known hash for a given rev. +func localHashForRev(ctx context.Context, rev, gitRoot string) (string, error) { output, err := runCommand(ctx, gitRoot, *flGitCmd, "rev-parse", rev) if err != nil { return "", err @@ -558,7 +559,7 @@ func revIsHash(ctx context.Context, rev, gitRoot string) (bool, error) { // hash, the output will be the same hash as the input. Of course, a user // could specify "abc" and match "abcdef12345678", so we just do a prefix // match. - output, err = hashForRev(ctx, rev, gitRoot) + output, err = localHashForRev(ctx, rev, gitRoot) if err != nil { return false, err } @@ -578,7 +579,7 @@ func syncRepo(ctx context.Context, repo, branch, rev string, depth int, gitRoot, if err != nil { return false, "", err } - hash, err = hashForRev(ctx, rev, gitRoot) + hash, err = localHashForRev(ctx, rev, gitRoot) if err != nil { return false, "", err } @@ -604,7 +605,7 @@ func syncRepo(ctx context.Context, repo, branch, rev string, depth int, gitRoot, // getRevs returns the local and upstream hashes for rev. func getRevs(ctx context.Context, localDir, branch, rev string) (string, string, error) { // Ask git what the exact hash is for rev. - local, err := hashForRev(ctx, rev, localDir) + local, err := localHashForRev(ctx, rev, localDir) if err != nil { return "", "", err } From 287a0e54644f17ea7fc191f4d8b4fa3304e18dbb Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Sun, 27 Oct 2019 21:30:56 -0700 Subject: [PATCH 2/3] var renames for readability --- cmd/git-sync/main.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/git-sync/main.go b/cmd/git-sync/main.go index f836241..466461a 100644 --- a/cmd/git-sync/main.go +++ b/cmd/git-sync/main.go @@ -385,14 +385,14 @@ func sleepForever() { // 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 - fullpath := path.Join(gitRoot, link) - currentDir, err := filepath.EvalSymlinks(fullpath) + linkPath := path.Join(gitRoot, link) + oldWorktreePath, err := filepath.EvalSymlinks(linkPath) if err != nil && !os.IsNotExist(err) { - return "", fmt.Errorf("error accessing symlink: %v", err) + return "", fmt.Errorf("error accessing current worktree: %v", err) } - // 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. + // newDir is absolute, so we need to change it to a relative path. This is + // so it can be volume-mounted at another path and the symlink still works. newDirRelative, err := filepath.Rel(gitRoot, newDir) if err != nil { return "", fmt.Errorf("error converting to relative path: %v", err) @@ -409,7 +409,7 @@ func updateSymlink(ctx context.Context, gitRoot, link, newDir string) (string, e } log.V(1).Info("renamed symlink", "root", gitRoot, "old_name", tmplink, "new_name", link) - return currentDir, nil + return oldWorktreePath, nil } // addWorktreeAndSwap creates a new worktree and calls updateSymlink to swap the symlink to point to the new worktree From e97eff010f19f8cbe8f0a6149a874a695b405623 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Sun, 27 Oct 2019 17:51:55 -0700 Subject: [PATCH 3/3] trivial comments --- cmd/git-sync/main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/git-sync/main.go b/cmd/git-sync/main.go index 466461a..36d20af 100644 --- a/cmd/git-sync/main.go +++ b/cmd/git-sync/main.go @@ -575,6 +575,7 @@ func syncRepo(ctx context.Context, repo, branch, rev string, depth int, gitRoot, _, err := os.Stat(gitRepoPath) switch { case os.IsNotExist(err): + // First time. Just clone it and get the hash. err = cloneRepo(ctx, repo, branch, rev, depth, gitRoot) if err != nil { return false, "", err @@ -586,6 +587,7 @@ func syncRepo(ctx context.Context, repo, branch, rev string, depth int, gitRoot, case err != nil: return false, "", fmt.Errorf("error checking if repo exists %q: %v", gitRepoPath, err) default: + // Not the first time. Figure out if the ref has changed. local, remote, err := getRevs(ctx, target, branch, rev) if err != nil { return false, "", err