@@ -272,6 +272,9 @@ type Pipeline struct {
272272 // PrintActions indicates whether to print the taken actions during the execution.
273273 PrintActions bool
274274
275+ // Branch used for pipeline.HeadCommit. Leave blank to use HEAD.
276+ Branch string
277+
275278 // Repository points to the analysed Git repository struct from go-git.
276279 repository * git.Repository
277280
@@ -484,34 +487,59 @@ func (pipeline *Pipeline) Commits(firstParent bool) ([]*object.Commit, error) {
484487// HeadCommit returns the latest commit in the repository (HEAD).
485488func (pipeline * Pipeline ) HeadCommit () ([]* object.Commit , error ) {
486489 repository := pipeline .repository
487- head , err := repository .Head ()
488- if err == plumbing .ErrReferenceNotFound {
489- refs , errr := repository .References ()
490- if errr != nil {
491- return nil , errors .Wrap (errr , "unable to list the references" )
492- }
493- var refnames []string
494- refByName := map [string ]* plumbing.Reference {}
495- err = refs .ForEach (func (ref * plumbing.Reference ) error {
496- refname := ref .Name ().String ()
497- refnames = append (refnames , refname )
498- refByName [refname ] = ref
499- if strings .HasPrefix (refname , "refs/heads/HEAD/" ) {
490+
491+ var head * plumbing.Reference
492+ if pipeline .Branch != "" {
493+ pipeline .l .Infof ("querying for head of branch %s" , pipeline .Branch )
494+ branch := plumbing .NewBranchReferenceName (pipeline .Branch )
495+ iter , err := repository .Branches ()
496+ if err != nil {
497+ return nil , errors .Wrap (err , "unable to list branches" )
498+ }
499+ if err := iter .ForEach (func (ref * plumbing.Reference ) error {
500+ pipeline .l .Info (ref .Name ())
501+ if ref .Name () == branch {
500502 head = ref
501503 return storer .ErrStop
502504 }
503505 return nil
504- })
505- if head == nil {
506- sort .Strings (refnames )
507- headName := refnames [len (refnames )- 1 ]
508- pipeline .l .Warnf ("could not determine the HEAD, falling back to %s" , headName )
509- head = refByName [headName ]
506+ }); err != nil {
507+ return nil , errors .Wrap (err , "unable to find branch head" )
508+ }
509+ } else {
510+ var err error
511+ head , err = repository .Head ()
512+ if err == plumbing .ErrReferenceNotFound {
513+ refs , errr := repository .References ()
514+ if errr != nil {
515+ return nil , errors .Wrap (errr , "unable to list the references" )
516+ }
517+ var refnames []string
518+ refByName := map [string ]* plumbing.Reference {}
519+ err = refs .ForEach (func (ref * plumbing.Reference ) error {
520+ refname := ref .Name ().String ()
521+ refnames = append (refnames , refname )
522+ refByName [refname ] = ref
523+ if strings .HasPrefix (refname , "refs/heads/HEAD/" ) {
524+ head = ref
525+ return storer .ErrStop
526+ }
527+ return nil
528+ })
529+ if head == nil {
530+ sort .Strings (refnames )
531+ headName := refnames [len (refnames )- 1 ]
532+ pipeline .l .Warnf ("could not determine the HEAD, falling back to %s" , headName )
533+ head = refByName [headName ]
534+ }
535+ } else if err != nil {
536+ return nil , errors .Wrap (err , "unable to find the head reference" )
510537 }
511538 }
512539 if head == nil {
513- return nil , errors .Wrap ( err , "unable to find the head reference" )
540+ return nil , errors .New ( "unable to find the head reference" )
514541 }
542+
515543 commit , err := repository .CommitObject (head .Hash ())
516544 if err != nil {
517545 return nil , err
0 commit comments