From 44116522dd82aefbaf7cdb9f42dbcc1e96eedc99 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 18 Jan 2020 14:48:57 +0000 Subject: feat(ops/sync-gcsr): Skip unneccessary branch updates Checks whether branches are already up-to-date before setting references. This also makes it possible to hook additional logic on the update flow. --- ops/sync-gcsr/main.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/ops/sync-gcsr/main.go b/ops/sync-gcsr/main.go index a02f6d5527..08e2627c66 100644 --- a/ops/sync-gcsr/main.go +++ b/ops/sync-gcsr/main.go @@ -44,15 +44,23 @@ func updateBranches(auth *http.BasicAuth, repo *git.Repository) error { continue } - branch := plumbing.NewHashReference( - plumbing.NewBranchReferenceName(ref.Name().Short()), - ref.Hash(), - ) + 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()) err := repo.Storer.SetReference(branch) if err != nil { return err } + log.Println("Updated branch", ref.Name().String()) } -- cgit 1.4.1