Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ jobs:
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Make test
run: make test
- name: Make all
run: make all
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ all: dist hash
gofmt:
@test -z $(shell gofmt -l -s $(SOURCE_DIRS) ./ | tee /dev/stderr) || (echo "[WARN] Fix formatting issues with 'make fmt'" && exit 1)

test:
$(GOCMD) test -count=1 -v ./cmd/.

build:
$(GOCMD) build .

Expand Down
37 changes: 30 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ func init() {
} else {
ref, _ := r.Head()
refHash := ref.Hash().String()
defaultGitCommit = getShortHash(refHash, 7)
remotes, _ := r.Remotes()
remoteUsed := remotes[0].Config().URLs[0]
toOrg := remoteUsed[:strings.LastIndex(remoteUsed, "/")]
defaultGitOrg = toOrg[strings.LastIndexAny(toOrg, "/:")+1:]
repoStartPos := strings.LastIndex(remoteUsed, "/") + 1
defaultGitRepo = remoteUsed[repoStartPos : len(remoteUsed)-4]
defaultGitCommit = refHash
log.Printf("Detected the following git vars org=%s repo=%s hash=%s\n", defaultGitOrg, defaultGitRepo, defaultGitCommit)
if len(remotes) > 0 {
remoteUsed := remotes[0].Config().URLs[0]
log.Printf("Detected a total of %d remotes. Using the 1st remote url %s to retrieve git info", len(remotes), remoteUsed)
defaultGitOrg, defaultGitRepo = fromRemoteToOrgRepo(remoteUsed)
log.Printf("Detected the following git vars org=%s repo=%s hash=%s\n", defaultGitOrg, defaultGitRepo, defaultGitCommit)
}
}

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

// Abbreviate the long hash to a short hash (7 digits)
func getShortHash(hash string, ndigits int) (short string) {
if len(hash) < ndigits {
short = hash
} else {
short = hash[:ndigits]
}
return
}

func fromRemoteToOrgRepo(remoteUsed string) (defaultGitOrg string, defaultGitRepo string) {
toOrg := remoteUsed[:strings.LastIndex(remoteUsed, "/")]
defaultGitOrg = toOrg[strings.LastIndexAny(toOrg, "/:")+1:]
repoStartPos := strings.LastIndex(remoteUsed, "/") + 1
repoEndPos := strings.LastIndex(remoteUsed[repoStartPos:], ".")
if repoEndPos < 0 {
defaultGitRepo = remoteUsed[repoStartPos:]
} else {
defaultGitRepo = remoteUsed[repoStartPos : repoStartPos+repoEndPos]
}
return
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
Expand Down
56 changes: 56 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cmd

import "testing"

func Test_fromRemoteToOrgRepo(t *testing.T) {
type args struct {
remoteUsed string
}
tests := []struct {
name string
args args
wantDefaultGitOrg string
wantDefaultGitRepo string
}{
{"git", args{"[email protected]:codeperfio/pprof-exporter.git"}, "codeperfio", "pprof-exporter"},
{"git-neg", args{"[email protected]:codeperfio/pprof-exporter"}, "codeperfio", "pprof-exporter"},
{"git-neg2", args{"github.com:codeperfio/pprof-exporter"}, "codeperfio", "pprof-exporter"},
{"codeperfio/example-go", args{"[email protected]:codeperfio/example-go.git"}, "codeperfio", "example-go"},
{"codeperfio/example-go-https", args{"https://github.com/codeperfio/example-go.git"}, "codeperfio", "example-go"},
{"codeperfio/example-go-https-neg", args{"https://github.com/codeperfio/example-go"}, "codeperfio", "example-go"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotDefaultGitOrg, gotDefaultGitRepo := fromRemoteToOrgRepo(tt.args.remoteUsed)
if gotDefaultGitOrg != tt.wantDefaultGitOrg {
t.Errorf("fromRemoteToOrgRepo() gotDefaultGitOrg = %v, want %v", gotDefaultGitOrg, tt.wantDefaultGitOrg)
}
if gotDefaultGitRepo != tt.wantDefaultGitRepo {
t.Errorf("fromRemoteToOrgRepo() gotDefaultGitRepo = %v, want %v", gotDefaultGitRepo, tt.wantDefaultGitRepo)
}
})
}
}

func Test_getShortHash(t *testing.T) {
type args struct {
hash string
ndigits int
}
tests := []struct {
name string
args args
wantShort string
}{
{"small", args{"a", 10}, "a"},
{"7dig", args{"1e872b59013425b7c404a91d16119e8452b983f2", 7}, "1e872b5"},
{"4dig", args{"1e872b59013425b7c404a91d16119e8452b983f2", 4}, "1e87"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotShort := getShortHash(tt.args.hash, tt.args.ndigits); gotShort != tt.wantShort {
t.Errorf("getShortHash() = %v, want %v", gotShort, tt.wantShort)
}
})
}
}