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

Commit 5026dff

Browse files
Git branches wip
Signed-off-by: Łukasz Gryglicki <[email protected]>
1 parent cae5278 commit 5026dff

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

cmd/git/git.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ type DSGit struct {
548548
RecentLines []string // recent commit lines
549549
OrphanedCommits []string // orphaned commits SHAs
550550
OrphanedMap map[string]struct{} // orphaned commits SHAs
551+
DefaultBranch string // default branch name, example: master, main
551552
// PairProgramming mode
552553
PairProgramming bool
553554
// CommitsHash is a map of commit hashes for each repo
@@ -1396,6 +1397,41 @@ func (j *DSGit) GetOrphanedCommits(ctx *shared.Ctx, thrN int) (ch chan error, er
13961397
return ch, nil
13971398
}
13981399

1400+
// GetGitBranches - get default git branch name
1401+
func (j *DSGit) GetGitBranches(ctx *shared.Ctx) (err error) {
1402+
if ctx.Debug > 0 {
1403+
shared.Printf("get git branch data from %s\n", j.GitPath)
1404+
}
1405+
cmdLine := []string{"git", "branch", "-a"}
1406+
var sout, serr string
1407+
sout, serr, err = shared.ExecCommand(ctx, cmdLine, j.GitPath, GitDefaultEnv)
1408+
if err != nil {
1409+
shared.Printf("error executing %v: %v\n%s\n%s\n", cmdLine, err, sout, serr)
1410+
return
1411+
}
1412+
if ctx.Debug > 0 {
1413+
shared.Printf("git branch data for %s: %s\n", j.URL, sout)
1414+
}
1415+
ary := strings.Split(sout, "\n")
1416+
for _, branch := range ary {
1417+
branch := strings.TrimSpace(branch)
1418+
if branch == "" {
1419+
continue
1420+
}
1421+
if ctx.Debug > 1 {
1422+
shared.Printf("branch: '%s'\n", branch)
1423+
}
1424+
if branch[:1] == "*" {
1425+
branch = branch[2:]
1426+
if ctx.Debug > 0 {
1427+
shared.Printf("Default branch: '%s'\n", branch)
1428+
}
1429+
j.DefaultBranch = branch
1430+
}
1431+
}
1432+
return
1433+
}
1434+
13991435
// ParseGitLog - update git repo
14001436
func (j *DSGit) ParseGitLog(ctx *shared.Ctx) (cmd *exec.Cmd, err error) {
14011437
if ctx.Debug > 0 {
@@ -1685,6 +1721,15 @@ func (j *DSGit) GitEnrichItems(ctx *shared.Ctx, thrN int, items []interface{}, d
16851721
return
16861722
}
16871723

1724+
// GetCommitBranch - get commit branch from refs
1725+
func (j *DSGit) GetCommitBranch(refs []string) string {
1726+
// ref can be:
1727+
// tag: refs/tags/0.9.0
1728+
// refs/heads/DA-2371-prod
1729+
// HEAD -> unicron-add-branches, origin/main, origin/HEAD, main
1730+
return refs[0]
1731+
}
1732+
16881733
// ParseCommit - parse commit
16891734
func (j *DSGit) ParseCommit(ctx *shared.Ctx, line string) (parsed bool, err error) {
16901735
m := shared.MatchGroups(GitCommitPattern, line)
@@ -1712,6 +1757,11 @@ func (j *DSGit) ParseCommit(ctx *shared.Ctx, line string) (parsed bool, err erro
17121757
j.Commit["commit"] = m["commit"]
17131758
j.Commit["parents"] = parentsAry
17141759
j.Commit["refs"] = refsAry
1760+
if len(refsAry) > 0 {
1761+
j.Commit["branch"] = j.GetCommitBranch(refsAry)
1762+
} else {
1763+
j.Commit["branch"] = j.DefaultBranch
1764+
}
17151765
j.CommitFiles = make(map[string]map[string]interface{})
17161766
j.ParseState = GitParseStateHeader
17171767
parsed = true
@@ -2107,8 +2157,15 @@ func (j *DSGit) Sync(ctx *shared.Ctx) (err error) {
21072157
return
21082158
}
21092159
}
2160+
err = j.GetGitBranches(ctx)
2161+
if err != nil {
2162+
return
2163+
}
21102164
var cmd *exec.Cmd
21112165
cmd, err = j.ParseGitLog(ctx)
2166+
if err != nil {
2167+
return
2168+
}
21122169
// Continue with operations that need git ops
21132170
nThreads := 0
21142171
locFinished := false

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/lukaszgryglicki/trailers-test' --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/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='' $*
55
else
6-
GIT_TAGS="c,d,e" ./scripts/git.sh --git-url='https://github.com/lukaszgryglicki/trailers-test' --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/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 $*
77
fi

0 commit comments

Comments
 (0)