Skip to content

Commit 7984f52

Browse files
authored
Merge pull request #12 from thockin/master
Fix when a tag/rev moves
2 parents 15ba743 + 0adea90 commit 7984f52

File tree

2 files changed

+39
-42
lines changed

2 files changed

+39
-42
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ REGISTRY ?= gcr.io/google_containers
1818
IMAGE = $(REGISTRY)/git-sync-$(ARCH)
1919
LEGACY_AMD64_IMAGE = $(REGISTRY)/git-sync
2020

21-
TAG = v2.0.1
21+
TAG = v2.0.2
2222

2323
# Architectures supported: amd64, arm, arm64 and ppc64le
2424
ARCH ?= amd64

main.go

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func main() {
178178
}
179179

180180
failCount = 0
181-
log.V(0).Infof("next sync in %d seconds", *flWait)
181+
log.V(1).Infof("next sync in %d seconds", *flWait)
182182
time.Sleep(time.Duration(*flWait) * time.Second)
183183
}
184184
}
@@ -247,27 +247,12 @@ func updateSymlink(gitRoot, link, newDir string) error {
247247
}
248248

249249
// addWorktreeAndSwap creates a new worktree and calls updateSymlink to swap the symlink to point to the new worktree
250-
func addWorktreeAndSwap(gitRoot, dest, branch, rev string) error {
251-
// Fetch the branch. This is required for the worktree to be based on the
252-
// current rev.
253-
_, err := runCommand(gitRoot, "git", "fetch", "--tags", "origin", branch)
254-
if err != nil {
255-
return err
256-
}
257-
258-
// Find the real commit for the rev.
259-
output, err := runCommand(gitRoot, "git", "rev-list", "-n1", rev)
260-
if err != nil {
261-
return err
262-
}
263-
revhash := strings.Trim(string(output), "\n")
264-
if revhash != rev {
265-
log.V(0).Infof("rev %s resolves to %s", rev, revhash)
266-
}
250+
func addWorktreeAndSwap(gitRoot, dest, branch, rev, hash string) error {
251+
log.V(0).Infof("syncing to %s (%s)", rev, hash)
267252

268253
// Make a worktree for this exact git hash.
269-
worktreePath := path.Join(gitRoot, "rev-"+revhash)
270-
_, err = runCommand(gitRoot, "git", "worktree", "add", worktreePath, "origin/"+branch)
254+
worktreePath := path.Join(gitRoot, "rev-"+hash)
255+
_, err := runCommand(gitRoot, "git", "worktree", "add", worktreePath, "origin/"+branch)
271256
if err != nil {
272257
return err
273258
}
@@ -287,11 +272,11 @@ func addWorktreeAndSwap(gitRoot, dest, branch, rev string) error {
287272
}
288273

289274
// Reset the worktree's working copy to the specific rev.
290-
_, err = runCommand(worktreePath, "git", "reset", "--hard", rev)
275+
_, err = runCommand(worktreePath, "git", "reset", "--hard", hash)
291276
if err != nil {
292277
return err
293278
}
294-
log.V(0).Infof("reset worktree %s to %s", worktreePath, rev)
279+
log.V(0).Infof("reset worktree %s to %s", worktreePath, hash)
295280

296281
if *flChmod != 0 {
297282
// set file permissions
@@ -319,12 +304,20 @@ func cloneRepo(repo, branch, rev string, depth int, gitRoot string) error {
319304
return nil
320305
}
321306

307+
func hashForRev(rev, gitRoot string) (string, error) {
308+
output, err := runCommand(gitRoot, "git", "rev-list", "-n1", rev)
309+
if err != nil {
310+
return "", err
311+
}
312+
return strings.Trim(string(output), "\n"), nil
313+
}
314+
322315
func revIsHash(rev, gitRoot string) (bool, error) {
323316
// If a rev is a tag name or HEAD, rev-list will produce the git hash. If
324317
// it is already a git hash, the output will be the same hash. Of course, a
325318
// user could specify "abc" and match "abcdef12345678", so we just do a
326319
// prefix match.
327-
output, err := runCommand(gitRoot, "git", "rev-list", "-n1", rev)
320+
output, err := hashForRev(rev, gitRoot)
328321
if err != nil {
329322
return false, err
330323
}
@@ -335,56 +328,60 @@ func revIsHash(rev, gitRoot string) (bool, error) {
335328
func syncRepo(repo, branch, rev string, depth int, gitRoot, dest string) error {
336329
target := path.Join(gitRoot, dest)
337330
gitRepoPath := path.Join(target, ".git")
331+
hash := rev
338332
_, err := os.Stat(gitRepoPath)
339333
switch {
340334
case os.IsNotExist(err):
341335
err = cloneRepo(repo, branch, rev, depth, gitRoot)
342336
if err != nil {
343337
return err
344338
}
339+
hash, err = hashForRev(rev, gitRoot)
340+
if err != nil {
341+
return err
342+
}
345343
case err != nil:
346-
return fmt.Errorf("error checking if repo exist %q: %v", gitRepoPath, err)
344+
return fmt.Errorf("error checking if repo exists %q: %v", gitRepoPath, err)
347345
default:
348-
needed, err := needResync(target, rev)
346+
local, remote, err := getRevs(target, rev)
349347
if err != nil {
350348
return err
351349
}
352-
if needed {
350+
log.V(2).Infof("local hash: %s", local)
351+
log.V(2).Infof("remote hash: %s", remote)
352+
if local != remote {
353353
log.V(0).Infof("update required")
354+
hash = remote
354355
} else {
355-
log.V(0).Infof("no update required")
356+
log.V(1).Infof("no update required")
356357
return nil
357358
}
358359
}
359360

360-
return addWorktreeAndSwap(gitRoot, dest, branch, rev)
361+
return addWorktreeAndSwap(gitRoot, dest, branch, rev, hash)
361362
}
362363

363-
// needResync returns true if the upstream hash for rev is different from the local one.
364-
func needResync(localDir, rev string) (bool, error) {
364+
// getRevs returns the local and upstream hashes for rev.
365+
func getRevs(localDir, rev string) (string, string, error) {
365366
// Ask git what the exact hash is for rev.
366-
local, err := runCommand(localDir, "git", "rev-list", "-n1", rev)
367+
local, err := hashForRev(rev, localDir)
367368
if err != nil {
368-
return false, err
369+
return "", "", err
369370
}
370-
local = strings.Trim(local, "\n")
371371

372372
// Fetch rev from upstream.
373-
_, err = runCommand(localDir, "git", "fetch", "origin", rev)
373+
_, err = runCommand(localDir, "git", "fetch", "--tags", "origin", rev)
374374
if err != nil {
375-
return false, err
375+
return "", "", err
376376
}
377377

378378
// Ask git what the exact hash is for upstream rev.
379-
remote, err := runCommand(localDir, "git", "rev-list", "-n1", "FETCH_HEAD")
379+
remote, err := hashForRev("FETCH_HEAD", localDir)
380380
if err != nil {
381-
return false, err
381+
return "", "", err
382382
}
383-
remote = strings.Trim(remote, "\n")
384383

385-
log.V(2).Infof("local hash: %s", local)
386-
log.V(2).Infof("remote hash: %s", remote)
387-
return (local != remote), nil
384+
return local, remote, nil
388385
}
389386

390387
func cmdForLog(command string, args ...string) string {

0 commit comments

Comments
 (0)