@ -7,6 +7,7 @@ package git
import (
"bytes"
"io"
"os"
"strconv"
"strings"
@ -414,7 +415,7 @@ func (repo *Repository) commitsBefore(id ObjectID, limit int) ([]*Commit, error)
commits := make ( [ ] * Commit , 0 , len ( formattedLog ) )
for _ , commit := range formattedLog {
branches , err := repo . getBranches ( commit , 2 )
branches , err := repo . getBranches ( os . Environ ( ) , commit . ID . String ( ) , 2 )
if err != nil {
return nil , err
}
@ -437,12 +438,15 @@ func (repo *Repository) getCommitsBeforeLimit(id ObjectID, num int) ([]*Commit,
return repo . commitsBefore ( id , num )
}
func ( repo * Repository ) getBranches ( commit * Commit , limit int ) ( [ ] string , error ) {
func ( repo * Repository ) getBranches ( env [ ] string , commitID string , limit int ) ( [ ] string , error ) {
if DefaultFeatures ( ) . CheckVersionAtLeast ( "2.7.0" ) {
stdout , _ , err := NewCommand ( repo . Ctx , "for-each-ref" , "--format=%(refname:strip=2)" ) .
AddOptionFormat ( "--count=%d" , limit ) .
AddOptionValues ( "--contains" , commit . ID . String ( ) , BranchPrefix ) .
RunStdString ( & RunOpts { Dir : repo . Path } )
AddOptionValues ( "--contains" , commitID , BranchPrefix ) .
RunStdString ( & RunOpts {
Dir : repo . Path ,
Env : env ,
} )
if err != nil {
return nil , err
}
@ -451,7 +455,10 @@ func (repo *Repository) getBranches(commit *Commit, limit int) ([]string, error)
return branches , nil
}
stdout , _ , err := NewCommand ( repo . Ctx , "branch" ) . AddOptionValues ( "--contains" , commit . ID . String ( ) ) . RunStdString ( & RunOpts { Dir : repo . Path } )
stdout , _ , err := NewCommand ( repo . Ctx , "branch" ) . AddOptionValues ( "--contains" , commitID ) . RunStdString ( & RunOpts {
Dir : repo . Path ,
Env : env ,
} )
if err != nil {
return nil , err
}
@ -513,3 +520,35 @@ func (repo *Repository) AddLastCommitCache(cacheKey, fullName, sha string) error
}
return nil
}
func ( repo * Repository ) GetCommitBranchStart ( env [ ] string , branch , endCommitID string ) ( string , error ) {
cmd := NewCommand ( repo . Ctx , "log" , prettyLogFormat )
cmd . AddDynamicArguments ( endCommitID )
stdout , _ , runErr := cmd . RunStdBytes ( & RunOpts {
Dir : repo . Path ,
Env : env ,
} )
if runErr != nil {
return "" , runErr
}
parts := bytes . Split ( bytes . TrimSpace ( stdout ) , [ ] byte { '\n' } )
var startCommitID string
for _ , commitID := range parts {
branches , err := repo . getBranches ( env , string ( commitID ) , 2 )
if err != nil {
return "" , err
}
for _ , b := range branches {
if b != branch {
return startCommitID , nil
}
}
startCommitID = string ( commitID )
}
return "" , nil
}