Skip to content

Commit 5d6e1a8

Browse files
authored
Merge pull request #1047 from wakatime/bugfix/git-detection
Check split lenght when finding git branch
2 parents 42030f4 + c529ce3 commit 5d6e1a8

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

.github/workflows/on_push.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ jobs:
170170
name: Build binary
171171
env:
172172
VERSION: ${{ env.TEST_VERSION }}
173-
run: make build-darwin-amd64
173+
run: make build-darwin-arm64
174174
-
175175
name: Integration tests
176176
run: make test-integration

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/wakatime/wakatime-cli
22

3-
go 1.22.2
3+
go 1.22.4
44

55
require (
66
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358

pkg/project/git.go

+7
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,13 @@ func findGitBranch(fp string) (string, error) {
290290
}
291291

292292
if len(lines) > 0 && strings.HasPrefix(strings.TrimSpace(lines[0]), "ref: ") {
293+
parts := strings.SplitN(lines[0], "/", 3)
294+
if len(parts) < 3 {
295+
log.Warnf("invalid branch from %q: %s", fp, lines[0])
296+
297+
return "", nil
298+
}
299+
293300
return strings.TrimSpace(strings.SplitN(lines[0], "/", 3)[2]), nil
294301
}
295302

pkg/project/git_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,29 @@ func TestGit_Detect_GitConfigFile_File(t *testing.T) {
115115
}
116116
}
117117

118+
func TestGit_Detect_GitConfigFile_File_MalformedHEAD(t *testing.T) {
119+
fp := setupTestGitFile(t)
120+
121+
// overwrite HEAD file with a malformed content
122+
err := os.WriteFile(filepath.Join(fp, "wakatime-cli/.git/HEAD"), []byte("ref: refs/malformed"), os.FileMode(int(0700)))
123+
require.NoError(t, err)
124+
125+
g := project.Git{
126+
Filepath: filepath.Join(fp, "wakatime-cli/src/pkg/file.go"),
127+
}
128+
129+
result, detected, err := g.Detect()
130+
require.NoError(t, err)
131+
132+
assert.True(t, detected)
133+
assert.Contains(t, result.Folder, filepath.Join(fp, "wakatime-cli"))
134+
assert.Equal(t, project.Result{
135+
Project: "wakatime-cli",
136+
Branch: "",
137+
Folder: result.Folder,
138+
}, result)
139+
}
140+
118141
func TestGit_Detect_Worktree(t *testing.T) {
119142
fp := setupTestGitWorktree(t)
120143

0 commit comments

Comments
 (0)