Skip to content
This repository was archived by the owner on May 24, 2024. It is now read-only.

Commit f5fb06c

Browse files
Add git branches data
Signed-off-by: Łukasz Gryglicki <[email protected]>
1 parent 5026dff commit f5fb06c

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

cmd/git/git.go

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ type DSGit struct {
549549
OrphanedCommits []string // orphaned commits SHAs
550550
OrphanedMap map[string]struct{} // orphaned commits SHAs
551551
DefaultBranch string // default branch name, example: master, main
552+
Branches map[string]struct{} // all branches
552553
// PairProgramming mode
553554
PairProgramming bool
554555
// CommitsHash is a map of commit hashes for each repo
@@ -871,6 +872,11 @@ func (j *DSGit) EnrichItem(ctx *shared.Ctx, item map[string]interface{}) (rich m
871872
rich["message_analyzed"] = nil
872873
rich["message"] = nil
873874
}
875+
iBranch, _ := commit["branch"]
876+
branch, _ := iBranch.(string)
877+
rich["branch"] = branch
878+
rich["default_branch"] = j.DefaultBranch
879+
rich["is_default_branch"] = j.DefaultBranch == branch
874880
comm, ok := shared.Dig(commit, []string{"commit"}, false, true)
875881
var hsh string
876882
if ok {
@@ -1078,6 +1084,8 @@ func (j *DSGit) GetModelData(ctx *shared.Ctx, docs []interface{}) []insights.Com
10781084
doc, _ := iDoc.(map[string]interface{})
10791085
commit.URL, _ = doc["commit_url"].(string)
10801086
commit.SHA, _ = doc["hash"].(string)
1087+
commit.Branch, _ = doc["branch"].(string)
1088+
commit.DefaultBranch, _ = doc["is_default_branch"].(bool)
10811089
commit.ShortHash, _ = doc["hash_short"].(string)
10821090
commit.Source, _ = doc["commit_repo_type"].(string)
10831091
commit.Message, _ = doc["message"].(string)
@@ -1413,6 +1421,7 @@ func (j *DSGit) GetGitBranches(ctx *shared.Ctx) (err error) {
14131421
shared.Printf("git branch data for %s: %s\n", j.URL, sout)
14141422
}
14151423
ary := strings.Split(sout, "\n")
1424+
j.Branches = make(map[string]struct{})
14161425
for _, branch := range ary {
14171426
branch := strings.TrimSpace(branch)
14181427
if branch == "" {
@@ -1421,13 +1430,17 @@ func (j *DSGit) GetGitBranches(ctx *shared.Ctx) (err error) {
14211430
if ctx.Debug > 1 {
14221431
shared.Printf("branch: '%s'\n", branch)
14231432
}
1424-
if branch[:1] == "*" {
1433+
if strings.HasPrefix(branch, "* ") {
14251434
branch = branch[2:]
14261435
if ctx.Debug > 0 {
14271436
shared.Printf("Default branch: '%s'\n", branch)
14281437
}
14291438
j.DefaultBranch = branch
14301439
}
1440+
j.Branches[branch] = struct{}{}
1441+
}
1442+
if ctx.Debug > 0 {
1443+
shared.Printf("Branches: %v\n", j.Branches)
14311444
}
14321445
return
14331446
}
@@ -1437,6 +1450,8 @@ func (j *DSGit) ParseGitLog(ctx *shared.Ctx) (cmd *exec.Cmd, err error) {
14371450
if ctx.Debug > 0 {
14381451
shared.Printf("parsing logs from %s\n", j.GitPath)
14391452
}
1453+
// Example full command line:
1454+
// LANG=C PAGER='' git log --reverse --topo-order --branches --tags --remotes=origin --no-color --decorate --raw --numstat --pretty=fuller --decorate=full --parents -M -C -c
14401455
cmdLine := []string{"git", "log", "--reverse", "--topo-order", "--branches", "--tags", "--remotes=origin"}
14411456
cmdLine = append(cmdLine, GitLogOptions...)
14421457
if ctx.DateFrom != nil {
@@ -1722,12 +1737,44 @@ func (j *DSGit) GitEnrichItems(ctx *shared.Ctx, thrN int, items []interface{}, d
17221737
}
17231738

17241739
// GetCommitBranch - get commit branch from refs
1725-
func (j *DSGit) GetCommitBranch(refs []string) string {
1740+
func (j *DSGit) GetCommitBranch(ctx *shared.Ctx, refs []string) (branch string) {
17261741
// ref can be:
17271742
// tag: refs/tags/0.9.0
17281743
// refs/heads/DA-2371-prod
17291744
// HEAD -> unicron-add-branches, origin/main, origin/HEAD, main
1730-
return refs[0]
1745+
// origin/main, origin/HEAD, main
1746+
tag := ""
1747+
for _, ref := range refs {
1748+
isTag := false
1749+
if strings.HasPrefix(ref, "tag: ") {
1750+
isTag = true
1751+
ref = ref[5:]
1752+
}
1753+
ary := strings.Split(ref, " -> ")
1754+
if len(ary) > 0 {
1755+
ref = ary[len(ary)-1]
1756+
}
1757+
ref = strings.Replace(ref, "origin/", "", 1)
1758+
ref = strings.Replace(ref, "refs/heads/", "", 1)
1759+
if isTag == true {
1760+
tag = ref
1761+
continue
1762+
}
1763+
if ref == j.DefaultBranch {
1764+
continue
1765+
}
1766+
branch = ref
1767+
}
1768+
if branch == "" && tag != "" {
1769+
branch = tag
1770+
}
1771+
if branch == "" {
1772+
branch = j.DefaultBranch
1773+
}
1774+
if ctx.Debug > 1 {
1775+
shared.Printf("Branch: %+v -> %s\n", refs, branch)
1776+
}
1777+
return
17311778
}
17321779

17331780
// ParseCommit - parse commit
@@ -1758,7 +1805,7 @@ func (j *DSGit) ParseCommit(ctx *shared.Ctx, line string) (parsed bool, err erro
17581805
j.Commit["parents"] = parentsAry
17591806
j.Commit["refs"] = refsAry
17601807
if len(refsAry) > 0 {
1761-
j.Commit["branch"] = j.GetCommitBranch(refsAry)
1808+
j.Commit["branch"] = j.GetCommitBranch(ctx, refsAry)
17621809
} else {
17631810
j.Commit["branch"] = j.DefaultBranch
17641811
}

scripts/example_run.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
if [ ! -z "$CONSOLE" ]
33
then
4-
GIT_TAGS="c,d,e" ./scripts/git.sh --git-url='https://github.com/LF-engineering/dev-analytics-api' --git-date-from "2015-01" --git-date-to "2022-01" --git-pack-size=100 --git-tags="a,b,c" --git-project=Kubernetes --git-skip-cache-cleanup=1 --git-stream='' $*
4+
GIT_TAGS="c,d,e" ./scripts/git.sh --git-url='https://github.com/LF-engineering/insights-datasource-git' --git-date-from "2015-01" --git-date-to "2023-01" --git-pack-size=100 --git-tags="a,b,c" --git-project=Kubernetes --git-skip-cache-cleanup=1 --git-stream='' $*
55
else
6-
GIT_TAGS="c,d,e" ./scripts/git.sh --git-url='https://github.com/LF-engineering/dev-analytics-affiliation' --git-date-from "2015-01" --git-date-to "2022-01" --git-pack-size=100 --git-tags="a,b,c" --git-project=Kubernetes --git-skip-cache-cleanup=1 $*
6+
GIT_TAGS="c,d,e" ./scripts/git.sh --git-url='https://github.com/LF-engineering/insights-datasource-git' --git-date-from "2015-01" --git-date-to "2023-01" --git-pack-size=100 --git-tags="a,b,c" --git-project=Kubernetes --git-skip-cache-cleanup=1 $*
77
fi

0 commit comments

Comments
 (0)