@@ -450,33 +450,14 @@ func (pipeline *Pipeline) Len() int {
450450func (pipeline * Pipeline ) Commits (firstParent bool ) ([]* object.Commit , error ) {
451451 var result []* object.Commit
452452 repository := pipeline .repository
453- head , err := repository . Head ()
453+ heads , err := pipeline . HeadCommit ()
454454 if err != nil {
455- if err == plumbing .ErrReferenceNotFound {
456- refs , errr := repository .References ()
457- if errr != nil {
458- return nil , errors .Wrap (errr , "unable to list the references" )
459- }
460- refs .ForEach (func (ref * plumbing.Reference ) error {
461- if strings .HasPrefix (ref .Name ().String (), "refs/heads/HEAD/" ) {
462- head = ref
463- return storer .ErrStop
464- }
465- return nil
466- })
467- }
468- if head == nil && err != nil {
469- return nil , errors .Wrap (err , "unable to collect the commit history" )
470- }
455+ return nil , err
471456 }
472-
457+ head := heads [ 0 ]
473458 if firstParent {
474- commit , err := repository .CommitObject (head .Hash ())
475- if err != nil {
476- panic (err )
477- }
478459 // the first parent matches the head
479- for ; err != io .EOF ; commit , err = commit .Parents ().Next () {
460+ for commit := head ; err != io .EOF ; commit , err = commit .Parents ().Next () {
480461 if err != nil {
481462 panic (err )
482463 }
@@ -488,26 +469,41 @@ func (pipeline *Pipeline) Commits(firstParent bool) ([]*object.Commit, error) {
488469 }
489470 return result , nil
490471 }
491- cit , err := repository .Log (& git.LogOptions {From : head .Hash () })
472+ cit , err := repository .Log (& git.LogOptions {From : head .Hash })
492473 if err != nil {
493474 return nil , errors .Wrap (err , "unable to collect the commit history" )
494475 }
495476 defer cit .Close ()
496- cit .ForEach (func (commit * object.Commit ) error {
477+ err = cit .ForEach (func (commit * object.Commit ) error {
497478 result = append (result , commit )
498479 return nil
499480 })
500- return result , nil
481+ return result , err
501482}
502483
503484// HeadCommit returns the latest commit in the repository (HEAD).
504485func (pipeline * Pipeline ) HeadCommit () ([]* object.Commit , error ) {
505486 repository := pipeline .repository
506- headref , err := repository .Head ()
487+ head , err := repository .Head ()
507488 if err != nil {
508- return nil , err
489+ if err == plumbing .ErrReferenceNotFound {
490+ refs , errr := repository .References ()
491+ if errr != nil {
492+ return nil , errors .Wrap (errr , "unable to list the references" )
493+ }
494+ err = refs .ForEach (func (ref * plumbing.Reference ) error {
495+ if strings .HasPrefix (ref .Name ().String (), "refs/heads/HEAD/" ) {
496+ head = ref
497+ return storer .ErrStop
498+ }
499+ return nil
500+ })
501+ }
502+ }
503+ if head == nil {
504+ return nil , errors .Wrap (err , "unable to find the head reference" )
509505 }
510- commit , err := repository .CommitObject (headref .Hash ())
506+ commit , err := repository .CommitObject (head .Hash ())
511507 if err != nil {
512508 return nil , err
513509 }
0 commit comments