Skip to content

Commit c8ce9f5

Browse files
authored
When in detached HEAD, send current commit (#254)
1 parent 448826e commit c8ce9f5

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

internal/git/client.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ func (c *Client) GetBranch() string {
2626
}
2727

2828
func (c *Client) GetCommit() string {
29+
if c.GetBranch() == "" {
30+
cmd := exec.Command(c.Binary, "rev-parse", "HEAD")
31+
cmd.Dir = c.Dir
32+
33+
out, err := cmd.Output()
34+
if err != nil {
35+
return ""
36+
}
37+
38+
return strings.TrimSpace(string(out))
39+
}
40+
2941
// Map known commits to their remote ref
3042
cmd := exec.Command(c.Binary, "for-each-ref", "--format=%(objectname) %(refname)", "refs/remotes/origin")
3143
cmd.Dir = c.Dir

internal/git/client_test.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,12 @@ func TestGetCommit(t *testing.T) {
112112
require.Equal(t, "", commit)
113113
})
114114

115-
t.Run("when we're in detatched HEAD state", func(t *testing.T) {
116-
t.Run("returns the common ancestor if we have the same HEAD", func(t *testing.T) {
117-
repo, expected := repoFixture(t, "testdata/GetCommit-detached-head")
118-
119-
client := &git.Client{Binary: "git", Dir: filepath.Join(repo, "repo")}
120-
commit := client.GetCommit()
121-
require.Equal(t, expected, commit)
122-
})
123-
124-
t.Run("returns the common ancestor if we've diverged", func(t *testing.T) {
125-
repo, expected := repoFixture(t, "testdata/GetCommit-detached-head-diverged")
115+
t.Run("returns HEAD when in detached HEAD state", func(t *testing.T) {
116+
repo, expected := repoFixture(t, "testdata/GetCommit-detached-head-diverged")
126117

127-
client := &git.Client{Binary: "git", Dir: filepath.Join(repo, "repo")}
128-
commit := client.GetCommit()
129-
require.Equal(t, expected, commit)
130-
})
118+
client := &git.Client{Binary: "git", Dir: filepath.Join(repo, "repo")}
119+
commit := client.GetCommit()
120+
require.Equal(t, expected, commit)
131121
})
132122

133123
t.Run("when we have a branch checked out", func(t *testing.T) {

internal/git/testdata/GetCommit-detached-head-diverged

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ mkdir origin
55
pushd origin > /dev/null
66
git init > /dev/null
77
git commit --allow-empty -m "Initial commit" > /dev/null
8-
9-
# Expected common ancestor:
10-
git rev-parse HEAD
11-
128
popd > /dev/null
139

1410
git clone origin repo > /dev/null 2>&1
1511
pushd repo > /dev/null
1612
git commit --allow-empty -m "Diverge" > /dev/null
1713
git checkout $(git rev-parse HEAD) > /dev/null 2>&1
14+
15+
git rev-parse HEAD

0 commit comments

Comments
 (0)