@@ -548,6 +548,8 @@ 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
552+ Branches map [string ]struct {} // all branches
551553 // PairProgramming mode
552554 PairProgramming bool
553555 // CommitsHash is a map of commit hashes for each repo
@@ -870,6 +872,11 @@ func (j *DSGit) EnrichItem(ctx *shared.Ctx, item map[string]interface{}) (rich m
870872 rich ["message_analyzed" ] = nil
871873 rich ["message" ] = nil
872874 }
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
873880 comm , ok := shared .Dig (commit , []string {"commit" }, false , true )
874881 var hsh string
875882 if ok {
@@ -1077,6 +1084,8 @@ func (j *DSGit) GetModelData(ctx *shared.Ctx, docs []interface{}) []insights.Com
10771084 doc , _ := iDoc .(map [string ]interface {})
10781085 commit .URL , _ = doc ["commit_url" ].(string )
10791086 commit .SHA , _ = doc ["hash" ].(string )
1087+ commit .Branch , _ = doc ["branch" ].(string )
1088+ commit .DefaultBranch , _ = doc ["is_default_branch" ].(bool )
10801089 commit .ShortHash , _ = doc ["hash_short" ].(string )
10811090 commit .Source , _ = doc ["commit_repo_type" ].(string )
10821091 commit .Message , _ = doc ["message" ].(string )
@@ -1396,11 +1405,53 @@ func (j *DSGit) GetOrphanedCommits(ctx *shared.Ctx, thrN int) (ch chan error, er
13961405 return ch , nil
13971406}
13981407
1408+ // GetGitBranches - get default git branch name
1409+ func (j * DSGit ) GetGitBranches (ctx * shared.Ctx ) (err error ) {
1410+ if ctx .Debug > 0 {
1411+ shared .Printf ("get git branch data from %s\n " , j .GitPath )
1412+ }
1413+ cmdLine := []string {"git" , "branch" , "-a" }
1414+ var sout , serr string
1415+ sout , serr , err = shared .ExecCommand (ctx , cmdLine , j .GitPath , GitDefaultEnv )
1416+ if err != nil {
1417+ shared .Printf ("error executing %v: %v\n %s\n %s\n " , cmdLine , err , sout , serr )
1418+ return
1419+ }
1420+ if ctx .Debug > 0 {
1421+ shared .Printf ("git branch data for %s: %s\n " , j .URL , sout )
1422+ }
1423+ ary := strings .Split (sout , "\n " )
1424+ j .Branches = make (map [string ]struct {})
1425+ for _ , branch := range ary {
1426+ branch := strings .TrimSpace (branch )
1427+ if branch == "" {
1428+ continue
1429+ }
1430+ if ctx .Debug > 1 {
1431+ shared .Printf ("branch: '%s'\n " , branch )
1432+ }
1433+ if strings .HasPrefix (branch , "* " ) {
1434+ branch = branch [2 :]
1435+ if ctx .Debug > 0 {
1436+ shared .Printf ("Default branch: '%s'\n " , branch )
1437+ }
1438+ j .DefaultBranch = branch
1439+ }
1440+ j .Branches [branch ] = struct {}{}
1441+ }
1442+ if ctx .Debug > 0 {
1443+ shared .Printf ("Branches: %v\n " , j .Branches )
1444+ }
1445+ return
1446+ }
1447+
13991448// ParseGitLog - update git repo
14001449func (j * DSGit ) ParseGitLog (ctx * shared.Ctx ) (cmd * exec.Cmd , err error ) {
14011450 if ctx .Debug > 0 {
14021451 shared .Printf ("parsing logs from %s\n " , j .GitPath )
14031452 }
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
14041455 cmdLine := []string {"git" , "log" , "--reverse" , "--topo-order" , "--branches" , "--tags" , "--remotes=origin" }
14051456 cmdLine = append (cmdLine , GitLogOptions ... )
14061457 if ctx .DateFrom != nil {
@@ -1685,6 +1736,47 @@ func (j *DSGit) GitEnrichItems(ctx *shared.Ctx, thrN int, items []interface{}, d
16851736 return
16861737}
16871738
1739+ // GetCommitBranch - get commit branch from refs
1740+ func (j * DSGit ) GetCommitBranch (ctx * shared.Ctx , refs []string ) (branch string ) {
1741+ // ref can be:
1742+ // tag: refs/tags/0.9.0
1743+ // refs/heads/DA-2371-prod
1744+ // HEAD -> unicron-add-branches, origin/main, origin/HEAD, main
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
1778+ }
1779+
16881780// ParseCommit - parse commit
16891781func (j * DSGit ) ParseCommit (ctx * shared.Ctx , line string ) (parsed bool , err error ) {
16901782 m := shared .MatchGroups (GitCommitPattern , line )
@@ -1712,6 +1804,11 @@ func (j *DSGit) ParseCommit(ctx *shared.Ctx, line string) (parsed bool, err erro
17121804 j .Commit ["commit" ] = m ["commit" ]
17131805 j .Commit ["parents" ] = parentsAry
17141806 j .Commit ["refs" ] = refsAry
1807+ if len (refsAry ) > 0 {
1808+ j .Commit ["branch" ] = j .GetCommitBranch (ctx , refsAry )
1809+ } else {
1810+ j .Commit ["branch" ] = j .DefaultBranch
1811+ }
17151812 j .CommitFiles = make (map [string ]map [string ]interface {})
17161813 j .ParseState = GitParseStateHeader
17171814 parsed = true
@@ -2107,8 +2204,15 @@ func (j *DSGit) Sync(ctx *shared.Ctx) (err error) {
21072204 return
21082205 }
21092206 }
2207+ err = j .GetGitBranches (ctx )
2208+ if err != nil {
2209+ return
2210+ }
21102211 var cmd * exec.Cmd
21112212 cmd , err = j .ParseGitLog (ctx )
2213+ if err != nil {
2214+ return
2215+ }
21122216 // Continue with operations that need git ops
21132217 nThreads := 0
21142218 locFinished := false
0 commit comments