@@ -68,8 +68,8 @@ func filterEnv() []string {
6868
6969func (git * gitRepo ) Poll (repo , branch string ) (* Commit , error ) {
7070 git .Reset ()
71- origin , err := git .Run ( "remote" , "get-url" , "origin" )
72- if err != nil || strings .TrimSpace (string ( origin ) ) != repo {
71+ origin , err := git .GetURL ( "origin" )
72+ if err != nil || strings .TrimSpace (origin ) != repo {
7373 // The repo is here, but it has wrong origin (e.g. repo in config has changed), re-clone.
7474 if err := git .clone (repo , branch ); err != nil {
7575 return nil , err
@@ -102,6 +102,14 @@ func (git *gitRepo) Poll(repo, branch string) (*Commit, error) {
102102 return git .Commit (HEAD )
103103}
104104
105+ func (git Git ) GetURL (branch string ) (string , error ) {
106+ url , err := git .Run ("remote" , "get-url" , branch )
107+ if err != nil {
108+ return "" , err
109+ }
110+ return string (url ), nil
111+ }
112+
105113func (git * gitRepo ) isNetworkError (output []byte ) bool {
106114 // The list is not exhaustive and is meant to be extended over time.
107115 return bytes .Contains (output , []byte ("fatal: read error: Connection reset by peer" ))
@@ -752,6 +760,24 @@ type BaseCommit struct {
752760 Branches []string
753761}
754762
763+ func (git Git ) TreeAndBranchForCommit (commit string ) (tree , branch string , err error ) {
764+ const cutOffDays = 60
765+ branchList , err := git .BranchesThatContain (commit , time .Now ().Add (- time .Hour * 24 * cutOffDays ))
766+ if err == nil {
767+ return "" , "" , fmt .Errorf ("failed to query branches: %w" , err )
768+ }
769+ for _ , branch := range branchList {
770+ if strings .Contains (branch .Branch , "/" ) {
771+ strs := strings .SplitN (branch .Branch , "/" , 2 )
772+ return strs [0 ], strs [1 ], nil
773+ }
774+ }
775+ if len (branchList ) != 0 {
776+ return branchList [0 ].Branch , "" , nil
777+ }
778+ return "" , "" , fmt .Errorf ("failed to find a branch containing commit" )
779+ }
780+
755781// BaseForDiff returns a list of commits that could have been the base commit
756782// for the specified git patch.
757783// The returned list is minimized to only contain the commits that are represented in different
@@ -830,7 +856,7 @@ func (git Git) BaseForDiff(diff []byte, tracer debugtracer.DebugTracer) ([]*Base
830856 }
831857 // Only focus on branches that are still alive.
832858 const cutOffDays = 60
833- list , err := git .branchesThatContain (candidate , time .Now ().Add (- time .Hour * 24 * cutOffDays ))
859+ list , err := git .BranchesThatContain (candidate , time .Now ().Add (- time .Hour * 24 * cutOffDays ))
834860 if err != nil {
835861 return nil , fmt .Errorf ("failed to query branches: %w" , err )
836862 }
@@ -924,7 +950,7 @@ type branchCommit struct {
924950 Commit string
925951}
926952
927- func (git Git ) branchesThatContain (commit string , since time.Time ) ([]branchCommit , error ) {
953+ func (git Git ) BranchesThatContain (commit string , since time.Time ) ([]branchCommit , error ) {
928954 output , err := git .Run (
929955 "branch" , "-a" ,
930956 "--contains" , commit ,
0 commit comments