Skip to content

Commit f3856cd

Browse files
Merge pull request #2 from codeperfio/git.fix
[fix] Fixed git var extraction. [add] using short hash for commit reference
2 parents 843a7e0 + 9100016 commit f3856cd

File tree

4 files changed

+91
-7
lines changed

4 files changed

+91
-7
lines changed

.github/workflows/build.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ jobs:
2323
uses: actions/setup-go@v2
2424
with:
2525
go-version: ${{ matrix.go-version }}
26+
- name: Make test
27+
run: make test
2628
- name: Make all
2729
run: make all

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ all: dist hash
2121
gofmt:
2222
@test -z $(shell gofmt -l -s $(SOURCE_DIRS) ./ | tee /dev/stderr) || (echo "[WARN] Fix formatting issues with 'make fmt'" && exit 1)
2323

24+
test:
25+
$(GOCMD) test -count=1 -v ./cmd/.
26+
2427
build:
2528
$(GOCMD) build .
2629

cmd/root.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ func init() {
7171
} else {
7272
ref, _ := r.Head()
7373
refHash := ref.Hash().String()
74+
defaultGitCommit = getShortHash(refHash, 7)
7475
remotes, _ := r.Remotes()
75-
remoteUsed := remotes[0].Config().URLs[0]
76-
toOrg := remoteUsed[:strings.LastIndex(remoteUsed, "/")]
77-
defaultGitOrg = toOrg[strings.LastIndexAny(toOrg, "/:")+1:]
78-
repoStartPos := strings.LastIndex(remoteUsed, "/") + 1
79-
defaultGitRepo = remoteUsed[repoStartPos : len(remoteUsed)-4]
80-
defaultGitCommit = refHash
81-
log.Printf("Detected the following git vars org=%s repo=%s hash=%s\n", defaultGitOrg, defaultGitRepo, defaultGitCommit)
76+
if len(remotes) > 0 {
77+
remoteUsed := remotes[0].Config().URLs[0]
78+
log.Printf("Detected a total of %d remotes. Using the 1st remote url %s to retrieve git info", len(remotes), remoteUsed)
79+
defaultGitOrg, defaultGitRepo = fromRemoteToOrgRepo(remoteUsed)
80+
log.Printf("Detected the following git vars org=%s repo=%s hash=%s\n", defaultGitOrg, defaultGitRepo, defaultGitCommit)
81+
}
8282
}
8383

8484
// Here you will define your flags and configuration settings.
@@ -97,6 +97,29 @@ func init() {
9797
rootCmd.MarkPersistentFlagRequired("bench")
9898
}
9999

100+
// Abbreviate the long hash to a short hash (7 digits)
101+
func getShortHash(hash string, ndigits int) (short string) {
102+
if len(hash) < ndigits {
103+
short = hash
104+
} else {
105+
short = hash[:ndigits]
106+
}
107+
return
108+
}
109+
110+
func fromRemoteToOrgRepo(remoteUsed string) (defaultGitOrg string, defaultGitRepo string) {
111+
toOrg := remoteUsed[:strings.LastIndex(remoteUsed, "/")]
112+
defaultGitOrg = toOrg[strings.LastIndexAny(toOrg, "/:")+1:]
113+
repoStartPos := strings.LastIndex(remoteUsed, "/") + 1
114+
repoEndPos := strings.LastIndex(remoteUsed[repoStartPos:], ".")
115+
if repoEndPos < 0 {
116+
defaultGitRepo = remoteUsed[repoStartPos:]
117+
} else {
118+
defaultGitRepo = remoteUsed[repoStartPos : repoStartPos+repoEndPos]
119+
}
120+
return
121+
}
122+
100123
// initConfig reads in config file and ENV variables if set.
101124
func initConfig() {
102125
if cfgFile != "" {

cmd/root_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cmd
2+
3+
import "testing"
4+
5+
func Test_fromRemoteToOrgRepo(t *testing.T) {
6+
type args struct {
7+
remoteUsed string
8+
}
9+
tests := []struct {
10+
name string
11+
args args
12+
wantDefaultGitOrg string
13+
wantDefaultGitRepo string
14+
}{
15+
{"git", args{"git@github.com:codeperfio/pprof-exporter.git"}, "codeperfio", "pprof-exporter"},
16+
{"git-neg", args{"git@github.com:codeperfio/pprof-exporter"}, "codeperfio", "pprof-exporter"},
17+
{"git-neg2", args{"github.com:codeperfio/pprof-exporter"}, "codeperfio", "pprof-exporter"},
18+
{"codeperfio/example-go", args{"git@github.com:codeperfio/example-go.git"}, "codeperfio", "example-go"},
19+
{"codeperfio/example-go-https", args{"https://github.com/codeperfio/example-go.git"}, "codeperfio", "example-go"},
20+
{"codeperfio/example-go-https-neg", args{"https://github.com/codeperfio/example-go"}, "codeperfio", "example-go"},
21+
}
22+
for _, tt := range tests {
23+
t.Run(tt.name, func(t *testing.T) {
24+
gotDefaultGitOrg, gotDefaultGitRepo := fromRemoteToOrgRepo(tt.args.remoteUsed)
25+
if gotDefaultGitOrg != tt.wantDefaultGitOrg {
26+
t.Errorf("fromRemoteToOrgRepo() gotDefaultGitOrg = %v, want %v", gotDefaultGitOrg, tt.wantDefaultGitOrg)
27+
}
28+
if gotDefaultGitRepo != tt.wantDefaultGitRepo {
29+
t.Errorf("fromRemoteToOrgRepo() gotDefaultGitRepo = %v, want %v", gotDefaultGitRepo, tt.wantDefaultGitRepo)
30+
}
31+
})
32+
}
33+
}
34+
35+
func Test_getShortHash(t *testing.T) {
36+
type args struct {
37+
hash string
38+
ndigits int
39+
}
40+
tests := []struct {
41+
name string
42+
args args
43+
wantShort string
44+
}{
45+
{"small", args{"a", 10}, "a"},
46+
{"7dig", args{"1e872b59013425b7c404a91d16119e8452b983f2", 7}, "1e872b5"},
47+
{"4dig", args{"1e872b59013425b7c404a91d16119e8452b983f2", 4}, "1e87"},
48+
}
49+
for _, tt := range tests {
50+
t.Run(tt.name, func(t *testing.T) {
51+
if gotShort := getShortHash(tt.args.hash, tt.args.ndigits); gotShort != tt.wantShort {
52+
t.Errorf("getShortHash() = %v, want %v", gotShort, tt.wantShort)
53+
}
54+
})
55+
}
56+
}

0 commit comments

Comments
 (0)