Skip to content

Commit

Permalink
Fix git.Export(sha1)
Browse files Browse the repository at this point in the history
`git clone -b ...` does not accept a revision -- it must be a branch
or tag -- so we can't use that to export a repo at a revision, as we'd
like to do for the helm operator's sync loop.

Instead we'll have to clone without an argument, and checkout the
desired revision.
  • Loading branch information
squaremo committed Jul 24, 2018
1 parent 571cefb commit 125a7e0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
5 changes: 4 additions & 1 deletion git/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ func (e *Export) Clean() {

// Export creates a minimal clone of the repo, at the ref given.
func (r *Repo) Export(ctx context.Context, ref string) (*Export, error) {
dir, err := r.workingClone(ctx, ref)
dir, err := r.workingClone(ctx, "")
if err != nil {
return nil, err
}
if err = checkout(ctx, dir, ref); err != nil {
return nil, err
}
return &Export{dir}, nil
}
44 changes: 44 additions & 0 deletions git/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package git

import (
"context"
"testing"
"time"

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

func TestExportAtRevision(t *testing.T) {
newDir, cleanup := testfiles.TempDir(t)
defer cleanup()

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

err := createRepo(newDir, []string{"config"})
if err != nil {
t.Fatal(err)
}
repo := NewRepo(Remote{URL: newDir}, ReadOnly)
if err := repo.Ready(ctx); err != nil {
t.Fatal(err)
}

headMinusOne, err := repo.Revision(ctx, "HEAD^1")
if err != nil {
t.Fatal(err)
}

export, err := repo.Export(ctx, headMinusOne)
if err != nil {
t.Fatal(err)
}

exportHead, err := refRevision(ctx, export.dir, "HEAD")
if err != nil {
t.Fatal(err)
}
if headMinusOne != exportHead {
t.Errorf("exported %s, but head in export dir %s is %s", headMinusOne, export.dir, exportHead)
}
}
4 changes: 4 additions & 0 deletions git/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func mirror(ctx context.Context, workingDir, repoURL string) (path string, err e
return repoPath, nil
}

func checkout(ctx context.Context, workingDir, ref string) error {
return execGitCmd(ctx, workingDir, nil, "checkout", ref)
}

// checkPush sanity-checks that we can write to the upstream repo
// (being able to `clone` is an adequate check that we can read the
// upstream).
Expand Down

0 comments on commit 125a7e0

Please sign in to comment.