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"
|
2019-12-30 05:06:46 +01:00
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
2019-12-20 14:16:57 +01:00
|
|
|
"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-30 05:06:46 +01:00
|
|
|
// ensure that all remote branches exist locally & are up to date.
|
|
|
|
func updateBranches(auth *http.BasicAuth, repo *git.Repository) error {
|
|
|
|
origin, err := repo.Remote("origin")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
refs, err := origin.List(&git.ListOptions{
|
|
|
|
Auth: auth,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ref := range refs {
|
|
|
|
if !ref.Name().IsBranch() || ref.Type() != plumbing.HashReference {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-01-18 15:48:57 +01:00
|
|
|
name := plumbing.NewBranchReferenceName(ref.Name().Short())
|
|
|
|
|
|
|
|
if current, err := repo.Storer.Reference(name); err == nil {
|
|
|
|
// Determine whether the reference has changed to skip
|
|
|
|
// unnecessary modifications.
|
|
|
|
if current.Hash() == ref.Hash() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
branch := plumbing.NewHashReference(name, ref.Hash())
|
2019-12-30 05:06:46 +01:00
|
|
|
|
|
|
|
err := repo.Storer.SetReference(branch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-18 15:48:57 +01:00
|
|
|
|
2019-12-30 05:06:46 +01:00
|
|
|
log.Println("Updated branch", ref.Name().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateRepo(auth *http.BasicAuth, repo *git.Repository, opts *git.FetchOptions) error {
|
|
|
|
err := repo.Fetch(opts)
|
|
|
|
|
2019-12-20 15:17:06 +01:00
|
|
|
if err == git.NoErrAlreadyUpToDate {
|
|
|
|
// nothing to do ...
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-30 05:06:46 +01:00
|
|
|
log.Println("Fetched updates from remote, updating local branches")
|
|
|
|
return updateBranches(auth, repo)
|
2019-12-20 15:17:06 +01:00
|
|
|
}
|
|
|
|
|
2019-12-29 04:50:31 +01:00
|
|
|
func cloneRepo(dest, project, repo string, auth *http.BasicAuth) (*git.Repository, error) {
|
|
|
|
var cloneOpts = git.CloneOptions{
|
|
|
|
Auth: auth,
|
|
|
|
URL: fmt.Sprintf("https://source.developers.google.com/p/%s/r/%s", project, repo),
|
|
|
|
}
|
2019-12-20 14:16:57 +01:00
|
|
|
|
2019-12-30 05:06:46 +01:00
|
|
|
handle, err := git.PlainClone(dest, true, &cloneOpts)
|
2019-12-20 14:16:57 +01:00
|
|
|
|
2019-12-29 04:50:31 +01:00
|
|
|
if err == git.ErrRepositoryAlreadyExists {
|
|
|
|
handle, err = git.PlainOpen(dest)
|
2019-12-20 14:16:57 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 05:06:46 +01:00
|
|
|
return handle, updateBranches(auth, handle)
|
2019-12-29 04:50:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
dest := EnvOr("SYNC_DEST", "/git/depot")
|
|
|
|
project := EnvOr("SYNC_PROJECT", "tazjins-infrastructure")
|
|
|
|
repo := EnvOr("SYNC_REPO", "depot")
|
|
|
|
user := os.Getenv("SYNC_USER")
|
|
|
|
pass := os.Getenv("SYNC_PASS")
|
|
|
|
|
|
|
|
log.Printf("Syncing repository '%s/%s' to destination '%s'", project, repo, dest)
|
|
|
|
|
|
|
|
var auth *http.BasicAuth
|
2019-12-20 14:16:57 +01:00
|
|
|
if user != "" && pass != "" {
|
2019-12-29 04:50:31 +01:00
|
|
|
auth = &http.BasicAuth{
|
2019-12-20 14:16:57 +01:00
|
|
|
Username: user,
|
|
|
|
Password: pass,
|
|
|
|
}
|
|
|
|
log.Println("Enabling basic authentication as user", user)
|
|
|
|
}
|
|
|
|
|
2019-12-29 04:50:31 +01:00
|
|
|
handle, err := cloneRepo(dest, project, repo, auth)
|
2019-12-20 14:16:57 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2019-12-29 04:50:31 +01:00
|
|
|
log.Fatalf("Failed to clone repository: %s", err)
|
2019-12-20 14:16:57 +01:00
|
|
|
} else {
|
|
|
|
log.Println("Initiating update loop")
|
|
|
|
}
|
|
|
|
|
2019-12-30 05:06:46 +01:00
|
|
|
fetchOpts := git.FetchOptions{
|
2019-12-29 04:50:31 +01:00
|
|
|
Auth: auth,
|
2019-12-20 15:17:06 +01:00
|
|
|
Force: true,
|
2019-12-20 14:16:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2019-12-30 05:06:46 +01:00
|
|
|
if err = updateRepo(auth, handle, &fetchOpts); err != nil {
|
2019-12-20 15:17:06 +01:00
|
|
|
log.Fatalf("Failed to pull updated repository: %s", err)
|
2019-12-20 14:16:57 +01:00
|
|
|
}
|
2019-12-30 05:06:46 +01:00
|
|
|
|
|
|
|
time.Sleep(10 * time.Second)
|
2019-12-20 14:16:57 +01:00
|
|
|
}
|
|
|
|
}
|