Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Check if notes exist before requesting them
Browse files Browse the repository at this point in the history
This is an optimisation for #714. We perform a single gitCommandExec to
get hashes for all commits with notes, place them into a map and use
them to query whether a commit has a note or not. This prevents
multiple calls to gitCommandExec just to see if there is a note
attached.
  • Loading branch information
philwinder committed Aug 29, 2017
1 parent 7e70eec commit 6925904
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
10 changes: 10 additions & 0 deletions daemon/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ func (d *Daemon) doSync(logger log.Logger) {
serviceIDs.Add(r.ServiceIDs(allResources))
}

notes, err := working.NoteRevList()
if err != nil {
logger.Log("err", errors.Wrap(err, "loading notes from repo"))
return
}

// Collect any events that come from notes attached to the commits
// we just synced. While we're doing this, keep track of what
// other things this sync includes e.g., releases and
Expand All @@ -178,6 +184,10 @@ func (d *Daemon) doSync(logger log.Logger) {

// Find notes in revisions.
for i := len(commits) - 1; i >= 0; i-- {
if ok := notes[commits[i].Revision]; !ok {
includes[history.NoneOfTheAbove] = true
continue
}
n, err := working.GetNote(commits[i].Revision)
if err != nil {
logger.Log("err", errors.Wrap(err, "loading notes from repo; possibly no notes"))
Expand Down
46 changes: 26 additions & 20 deletions git/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package git

import (
"fmt"
"github.com/weaveworks/flux/cluster/kubernetes/testfiles"
"github.com/weaveworks/flux/job"
"github.com/weaveworks/flux/update"
"io/ioutil"
"os/exec"
"path"
"testing"

"github.com/weaveworks/flux/cluster/kubernetes/testfiles"
"github.com/weaveworks/flux/job"
"github.com/weaveworks/flux/update"
)

const (
Expand All @@ -23,13 +24,19 @@ func TestListNotes_2Notes(t *testing.T) {
newDir, cleanup := testfiles.TempDir(t)
defer cleanup()

err := createRepo(newDir, "")
err := createRepo(newDir, "another")
if err != nil {
t.Fatal(err)
}

idHEAD_1 := testNote(newDir, "HEAD~1")
idHEAD := testNote(newDir, "HEAD")
idHEAD_1, err := testNote(newDir, "HEAD~1")
if err != nil {
t.Fatal(err)
}
idHEAD, err := testNote(newDir, "HEAD")
if err != nil {
t.Fatal(err)
}

notes, err := noteRevList(newDir, testNoteRef)
if err != nil {
Expand Down Expand Up @@ -58,7 +65,7 @@ func TestListNotes_0Notes(t *testing.T) {
newDir, cleanup := testfiles.TempDir(t)
defer cleanup()

err := createRepo(newDir, "")
err := createRepo(newDir, "another")
if err != nil {
t.Fatal(err)
}
Expand All @@ -73,10 +80,10 @@ func TestListNotes_0Notes(t *testing.T) {
}
}

func testNote(dir, rev string) job.ID {
func testNote(dir, rev string) (job.ID, error) {
id := job.ID(fmt.Sprintf("%v", noteIdCounter))
noteIdCounter += 1
addNote(dir, rev, testNoteRef, &Note{
err := addNote(dir, rev, testNoteRef, &Note{
id,
update.Spec{
update.Auto,
Expand All @@ -88,7 +95,7 @@ func testNote(dir, rev string) job.ID {
},
update.Result{},
})
return id
return id, err
}

func TestChangedFiles_SlashPath(t *testing.T) {
Expand Down Expand Up @@ -148,28 +155,27 @@ func createRepo(dir string, nestedDir string) error {
if err = execCommand("git", "-C", dir, "init"); err != nil {
return err
}
if err := execCommand("mkdir", "-p", fullPath); err != nil {
return err
}
if err = testfiles.WriteTestFiles(fullPath); err != nil {

if err := execCommand("git", "-C", dir, "config", "user.name", "notes test"); err != nil {
return err
}
if err = execCommand("git", "-C", dir, "add", "--all"); err != nil {

if err := execCommand("git", "-C", dir, "config", "user.email", "example@example.com"); err != nil {
return err
}
if err = execCommand("git", "-C", dir, "commit", "-m", "'Initial revision'"); err != nil {
if err := execCommand("mkdir", "-p", fullPath); err != nil {
return err
}
if err := execCommand("mkdir", "-p", path.Join(fullPath, "another")); err != nil {
if err = testfiles.WriteTestFiles(fullPath); err != nil {
return err
}
if err = testfiles.WriteTestFiles(path.Join(fullPath, "another")); err != nil {
if err = execCommand("git", "-C", dir, "add", "--all"); err != nil {
return err
}
if err = execCommand("git", "-C", dir, "add", "--all"); err != nil {
if err = execCommand("git", "-C", dir, "commit", "-m", "'Initial revision'"); err != nil {
return err
}
if err = execCommand("git", "-C", dir, "commit", "-m", "'Second revision'"); err != nil {
if err = execCommand("git", "-C", dir, "commit", "--allow-empty", "-m", "'Second revision'"); err != nil {
return err
}
return nil
Expand Down
6 changes: 6 additions & 0 deletions git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,9 @@ func (c *Checkout) ChangedFiles(ref string) ([]string, error) {
}
return list, err
}

func (c *Checkout) NoteRevList() (map[string]bool, error) {
c.Lock()
defer c.Unlock()
return noteRevList(c.Dir, c.SyncTag)
}

0 comments on commit 6925904

Please sign in to comment.