@@ -7,6 +7,7 @@ package git
77import (
88 "bytes"
99 "io"
10+ "os"
1011 "strconv"
1112 "strings"
1213
@@ -414,7 +415,7 @@ func (repo *Repository) commitsBefore(id ObjectID, limit int) ([]*Commit, error)
414415
415416 commits := make ([]* Commit , 0 , len (formattedLog ))
416417 for _ , commit := range formattedLog {
417- branches , err := repo .getBranches (commit , 2 )
418+ branches , err := repo .getBranches (os . Environ (), commit . ID . String () , 2 )
418419 if err != nil {
419420 return nil , err
420421 }
@@ -437,12 +438,15 @@ func (repo *Repository) getCommitsBeforeLimit(id ObjectID, num int) ([]*Commit,
437438 return repo .commitsBefore (id , num )
438439}
439440
440- func (repo * Repository ) getBranches (commit * Commit , limit int ) ([]string , error ) {
441+ func (repo * Repository ) getBranches (env [] string , commitID string , limit int ) ([]string , error ) {
441442 if DefaultFeatures ().CheckVersionAtLeast ("2.7.0" ) {
442443 stdout , _ , err := NewCommand (repo .Ctx , "for-each-ref" , "--format=%(refname:strip=2)" ).
443444 AddOptionFormat ("--count=%d" , limit ).
444- AddOptionValues ("--contains" , commit .ID .String (), BranchPrefix ).
445- RunStdString (& RunOpts {Dir : repo .Path })
445+ AddOptionValues ("--contains" , commitID , BranchPrefix ).
446+ RunStdString (& RunOpts {
447+ Dir : repo .Path ,
448+ Env : env ,
449+ })
446450 if err != nil {
447451 return nil , err
448452 }
@@ -451,7 +455,10 @@ func (repo *Repository) getBranches(commit *Commit, limit int) ([]string, error)
451455 return branches , nil
452456 }
453457
454- stdout , _ , err := NewCommand (repo .Ctx , "branch" ).AddOptionValues ("--contains" , commit .ID .String ()).RunStdString (& RunOpts {Dir : repo .Path })
458+ stdout , _ , err := NewCommand (repo .Ctx , "branch" ).AddOptionValues ("--contains" , commitID ).RunStdString (& RunOpts {
459+ Dir : repo .Path ,
460+ Env : env ,
461+ })
455462 if err != nil {
456463 return nil , err
457464 }
@@ -513,3 +520,35 @@ func (repo *Repository) AddLastCommitCache(cacheKey, fullName, sha string) error
513520 }
514521 return nil
515522}
523+
524+ func (repo * Repository ) GetCommitBranchStart (env []string , branch , endCommitID string ) (string , error ) {
525+ cmd := NewCommand (repo .Ctx , "log" , prettyLogFormat )
526+ cmd .AddDynamicArguments (endCommitID )
527+
528+ stdout , _ , runErr := cmd .RunStdBytes (& RunOpts {
529+ Dir : repo .Path ,
530+ Env : env ,
531+ })
532+ if runErr != nil {
533+ return "" , runErr
534+ }
535+
536+ parts := bytes .Split (bytes .TrimSpace (stdout ), []byte {'\n' })
537+
538+ var startCommitID string
539+ for _ , commitID := range parts {
540+ branches , err := repo .getBranches (env , string (commitID ), 2 )
541+ if err != nil {
542+ return "" , err
543+ }
544+ for _ , b := range branches {
545+ if b != branch {
546+ return startCommitID , nil
547+ }
548+ }
549+
550+ startCommitID = string (commitID )
551+ }
552+
553+ return "" , nil
554+ }
0 commit comments