From 57ba0d2e46525847861d8f32e2681a8936716211 Mon Sep 17 00:00:00 2001 From: Simon Marsh Date: Sat, 1 Jun 2019 19:46:23 +0100 Subject: [PATCH] Tidy updating of th registry to fix use on non-master branches --- dn42regsrv.go | 4 ++-- registry.go | 33 +++++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/dn42regsrv.go b/dn42regsrv.go index c651582..f5edcfb 100644 --- a/dn42regsrv.go +++ b/dn42regsrv.go @@ -122,7 +122,7 @@ func main() { refreshInterval = flag.StringP("Refresh", "i", "60m", "Refresh interval") gitPath = flag.StringP("GitPath", "g", "/usr/bin/git", "Path to git executable") autoPull = flag.BoolP("AutoPull", "a", true, "Automatically pull the registry") - pullURL = flag.StringP("PullURL", "p", "origin", "URL to auto pull") + branch = flag.StringP("Branch", "p", "master", "git branch to pull") ) flag.Parse() @@ -139,7 +139,7 @@ func main() { } InitialiseRegistryData(*regDir, interval, - *gitPath, *autoPull, *pullURL) + *gitPath, *autoPull, *branch) // initialise router router := mux.NewRouter() diff --git a/registry.go b/registry.go index 1c5836f..7c56622 100644 --- a/registry.go +++ b/registry.go @@ -600,30 +600,43 @@ func getCommitHash(regDir string, gitPath string) string { ////////////////////////////////////////////////////////////////////////// // refresh the registry -func refreshRegistry(regDir string, gitPath string, pullURL string) { +func refreshRegistry(regDir string, gitPath string, branch string) { - // run git to get the latest commit hash - cmd := exec.Command(gitPath, "pull", pullURL) + // run git fetch to get the current commits from the master + cmd := exec.Command(gitPath, "fetch") cmd.Dir = regDir // execute - out, err := cmd.Output() - if err != nil { + if out, err := cmd.Output(); err != nil { log.WithFields(log.Fields{ "error": err, "gitPath": gitPath, "regDir": regDir, - "pullURL": pullURL, - }).Error("Failed to execute git log") + }).Error("Failed to execute git fetch") + } else { + fmt.Printf("Git Fetch: %s", string(out)) } - fmt.Println(string(out)) + // then reset hard to match the master + cmd = exec.Command(gitPath, "reset", "--hard", "origin/"+branch) + cmd.Dir = regDir + // execute + if out, err := cmd.Output(); err != nil { + log.WithFields(log.Fields{ + "error": err, + "gitPath": gitPath, + "regDir": regDir, + "branch": branch, + }).Error("Failed to execute git reset") + } else { + fmt.Printf("Git Reset: %s", string(out)) + } } ////////////////////////////////////////////////////////////////////////// // called from main to initialse the registry data and syncing func InitialiseRegistryData(regDir string, refresh time.Duration, - gitPath string, autoPull bool, pullURL string) { + gitPath string, autoPull bool, branch string) { // validate that the regDir/data path exists dataPath := regDir + "/data" @@ -675,7 +688,7 @@ func InitialiseRegistryData(regDir string, refresh time.Duration, // automatically try to refresh the registry ? if autoPull { - refreshRegistry(regDir, gitPath, pullURL) + refreshRegistry(regDir, gitPath, branch) } // get the latest hash