Skip to content

Commit

Permalink
Add support for resolving refs, when linked.
Browse files Browse the repository at this point in the history
With ko-build/ko#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: <ref>` entry, it attempts to read that
file from `KO_DATA_PATH` next, which lets the changeset work much more effectively.
  • Loading branch information
mattmoor committed Aug 15, 2019
1 parent fd4659e commit af0b9a0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
15 changes: 11 additions & 4 deletions changeset/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions changeset/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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: "",
Expand Down
1 change: 1 addition & 0 deletions changeset/testdata/garbage/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
garbage contents
1 change: 1 addition & 0 deletions changeset/testdata/with-refs/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/branch-name
1 change: 1 addition & 0 deletions changeset/testdata/with-refs/refs/heads/branch-name
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a2d1bdfe929516d7da141aef68631a7ee6941b2d

0 comments on commit af0b9a0

Please sign in to comment.