From ba7a48af75562b086834533ce308073709d2920e Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 20 Dec 2019 14:17:06 +0000 Subject: fix(sync-gcsr): Use local worktree & pull changes into it Just fetching the remote of a bare repository does not update local tracking branches, which means that changes do not become visible in cgit. --- services/sync-gcsr/main.go | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/services/sync-gcsr/main.go b/services/sync-gcsr/main.go index 0b295a2c817e..eb9df80f6d55 100644 --- a/services/sync-gcsr/main.go +++ b/services/sync-gcsr/main.go @@ -24,6 +24,19 @@ func EnvOr(key, def string) string { return v } +func updateRepo(repo *git.Repository, tree *git.Worktree, opts *git.PullOptions) error { + err := tree.Pull(opts) + if err == git.NoErrAlreadyUpToDate { + // nothing to do ... + return nil + } else if err != nil { + return err + } + + log.Println("Updated local repository mirror") + return nil +} + func main() { var dest = EnvOr("SYNC_DEST", "/git/depot") var project = EnvOr("SYNC_PROJECT", "tazjins-infrastructure") @@ -46,7 +59,7 @@ func main() { } action := "clone" - handle, err := git.PlainClone(dest, true, &cloneOpts) + handle, err := git.PlainClone(dest, false, &cloneOpts) if err == git.ErrRepositoryAlreadyExists { log.Println("Repository has already been cloned!") @@ -60,21 +73,20 @@ func main() { log.Println("Initiating update loop") } - fetchOpts := git.FetchOptions{ - Auth: cloneOpts.Auth, + tree, err := handle.Worktree() + if err != nil { + log.Fatalln("Failed to open repository worktree:", err) + } + + pullOpts := git.PullOptions{ + Auth: cloneOpts.Auth, + Force: true, } for { - time.Sleep(30 * time.Second) // TODO(tazjin): Config option for fetch interval? - err = handle.Fetch(&fetchOpts) - - if err == git.NoErrAlreadyUpToDate { - // no-op ... - err = nil - } else if err != nil { - log.Fatalf("Failed to fetch updated repository: %s", err) - } else { - log.Println("Fetched new updates from remote repository") + if err = updateRepo(handle, tree, &pullOpts); err != nil { + log.Fatalf("Failed to pull updated repository: %s", err) } + time.Sleep(30 * time.Second) // TODO(tazjin): Config option for pull interval? } } -- cgit 1.4.1