about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-12-20T14·17+0000
committerVincent Ambo <tazjin@google.com>2019-12-20T14·17+0000
commitba7a48af75562b086834533ce308073709d2920e (patch)
tree30091367cd9742ca50eeea9deae78b63e1759569
parent7ff4b59342fd7ce26c38a4aa22afb379e8fa771c (diff)
fix(sync-gcsr): Use local worktree & pull changes into it r/213
Just fetching the remote of a bare repository does not update local
tracking branches, which means that changes do not become visible in
cgit.
-rw-r--r--services/sync-gcsr/main.go38
1 files changed, 25 insertions, 13 deletions
diff --git a/services/sync-gcsr/main.go b/services/sync-gcsr/main.go
index 0b295a2c81..eb9df80f6d 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?
 	}
 }