2019-12-20 14:16:57 +01:00
|
|
|
// Copyright 2019 Google LLC.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
// sync-gcsr implements a small utility that periodically mirrors a
|
|
|
|
// remote Google Cloud Source Repository to a local file path.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
git "gopkg.in/src-d/go-git.v4"
|
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func EnvOr(key, def string) string {
|
|
|
|
v := os.Getenv(key)
|
|
|
|
if v == "" {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2019-12-20 15:17:06 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-20 14:16:57 +01:00
|
|
|
func main() {
|
|
|
|
var dest = EnvOr("SYNC_DEST", "/git/depot")
|
|
|
|
var project = EnvOr("SYNC_PROJECT", "tazjins-infrastructure")
|
|
|
|
var repo = EnvOr("SYNC_REPO", "depot")
|
|
|
|
var user = os.Getenv("SYNC_USER")
|
|
|
|
var pass = os.Getenv("SYNC_PASS")
|
|
|
|
|
|
|
|
log.Printf("Syncing repository '%s/%s' to destination '%s'", project, repo, dest)
|
|
|
|
|
|
|
|
var cloneOpts = git.CloneOptions{
|
|
|
|
URL: fmt.Sprintf("https://source.developers.google.com/p/%s/r/%s", project, repo),
|
|
|
|
}
|
|
|
|
|
|
|
|
if user != "" && pass != "" {
|
|
|
|
cloneOpts.Auth = &http.BasicAuth{
|
|
|
|
Username: user,
|
|
|
|
Password: pass,
|
|
|
|
}
|
|
|
|
log.Println("Enabling basic authentication as user", user)
|
|
|
|
}
|
|
|
|
|
|
|
|
action := "clone"
|
2019-12-20 15:17:06 +01:00
|
|
|
handle, err := git.PlainClone(dest, false, &cloneOpts)
|
2019-12-20 14:16:57 +01:00
|
|
|
|
|
|
|
if err == git.ErrRepositoryAlreadyExists {
|
|
|
|
log.Println("Repository has already been cloned!")
|
|
|
|
handle, err = git.PlainOpen(dest)
|
|
|
|
action = "open"
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2019-12-20 15:40:21 +01:00
|
|
|
log.Fatalf("Failed to %s repository: %s", action, err)
|
2019-12-20 14:16:57 +01:00
|
|
|
} else {
|
|
|
|
log.Println("Initiating update loop")
|
|
|
|
}
|
|
|
|
|
2019-12-20 15:17:06 +01:00
|
|
|
tree, err := handle.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Failed to open repository worktree:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pullOpts := git.PullOptions{
|
|
|
|
Auth: cloneOpts.Auth,
|
|
|
|
Force: true,
|
2019-12-20 14:16:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2019-12-20 15:17:06 +01:00
|
|
|
if err = updateRepo(handle, tree, &pullOpts); err != nil {
|
|
|
|
log.Fatalf("Failed to pull updated repository: %s", err)
|
2019-12-20 14:16:57 +01:00
|
|
|
}
|
2019-12-20 15:17:06 +01:00
|
|
|
time.Sleep(30 * time.Second) // TODO(tazjin): Config option for pull interval?
|
2019-12-20 14:16:57 +01:00
|
|
|
}
|
|
|
|
}
|