From af0b9a0412531b44b954494c3d132392f194022a Mon Sep 17 00:00:00 2001 From: Matt Moore Date: Thu, 15 Aug 2019 14:55:43 +0000 Subject: [PATCH] Add support for resolving refs, when linked. With https://github.com/google/ko/pull/73 we can link the Git ref information into images with: ``` ln -r -s .git/refs ./cmd/foo/kodata ``` If the changeset package gets a `ref: ` entry, it attempts to read that file from `KO_DATA_PATH` next, which lets the changeset work much more effectively. --- changeset/commit.go | 15 +++++++++++---- changeset/commit_test.go | 17 +++++++++++++---- changeset/testdata/garbage/HEAD | 1 + changeset/testdata/with-refs/HEAD | 1 + .../testdata/with-refs/refs/heads/branch-name | 1 + 5 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 changeset/testdata/garbage/HEAD create mode 100644 changeset/testdata/with-refs/HEAD create mode 100644 changeset/testdata/with-refs/refs/heads/branch-name diff --git a/changeset/commit.go b/changeset/commit.go index 77ed709321..5d2bf5398a 100644 --- a/changeset/commit.go +++ b/changeset/commit.go @@ -42,11 +42,18 @@ func Get() (string, error) { return "", err } commitID := strings.TrimSpace(string(data)) - if !commitIDRE.MatchString(commitID) { - err := fmt.Errorf("%q is not a valid GitHub commit ID", commitID) - return "", err + if strings.HasPrefix(commitID, "ref: ") { + refName := commitID[len("ref: "):] + data, err := readFileFromKoData(refName) + if err != nil { + return "", err + } + commitID = strings.TrimSpace(string(data)) + } + if commitIDRE.MatchString(commitID) { + return commitID[:7], nil } - return string(commitID[0:7]), nil + return "", fmt.Errorf("%q is not a valid GitHub commit ID", commitID) } // readFileFromKoData tries to read data as string from the file with given name diff --git a/changeset/commit_test.go b/changeset/commit_test.go index 85076500e0..00257834f0 100644 --- a/changeset/commit_test.go +++ b/changeset/commit_test.go @@ -24,8 +24,8 @@ import ( ) const ( - nonCommittedHeadContext = "ref: refs/heads/non_committed_branch" - testCommitID = "a2d1bdf" + nonCommittedHeadRef = "refs/heads/non_committed_branch" + testCommitID = "a2d1bdf" ) func TestReadFile(t *testing.T) { @@ -41,10 +41,19 @@ func TestReadFile(t *testing.T) { koDataPath: "testdata", want: testCommitID, }, { - name: "non committed branch", + name: "no refs link", koDataPath: "testdata/noncommitted", wantErr: true, - err: fmt.Errorf("%q is not a valid GitHub commit ID", nonCommittedHeadContext), + err: fmt.Errorf("open testdata/noncommitted/%s: no such file or directory", nonCommittedHeadRef), + }, { + name: "with refs link", + koDataPath: "testdata/with-refs", + want: testCommitID, + }, { + name: "with bad content", + koDataPath: "testdata/garbage", + wantErr: true, + err: fmt.Errorf(`"garbage contents" is not a valid GitHub commit ID`), }, { name: "KO_DATA_PATH is empty", koDataPath: "", diff --git a/changeset/testdata/garbage/HEAD b/changeset/testdata/garbage/HEAD new file mode 100644 index 0000000000..0717e55530 --- /dev/null +++ b/changeset/testdata/garbage/HEAD @@ -0,0 +1 @@ +garbage contents \ No newline at end of file diff --git a/changeset/testdata/with-refs/HEAD b/changeset/testdata/with-refs/HEAD new file mode 100644 index 0000000000..b86b48fd85 --- /dev/null +++ b/changeset/testdata/with-refs/HEAD @@ -0,0 +1 @@ +ref: refs/heads/branch-name \ No newline at end of file diff --git a/changeset/testdata/with-refs/refs/heads/branch-name b/changeset/testdata/with-refs/refs/heads/branch-name new file mode 100644 index 0000000000..58b38ebaed --- /dev/null +++ b/changeset/testdata/with-refs/refs/heads/branch-name @@ -0,0 +1 @@ +a2d1bdfe929516d7da141aef68631a7ee6941b2d