@@ -82,7 +82,7 @@ func (s *RepositorySuite) TestInitWithInvalidDefaultBranch(c *C) {
8282 c .Assert (err , NotNil )
8383}
8484
85- func createCommit (c * C , r * Repository ) {
85+ func createCommit (c * C , r * Repository ) plumbing. Hash {
8686 // Create a commit so there is a HEAD to check
8787 wt , err := r .Worktree ()
8888 c .Assert (err , IsNil )
@@ -101,13 +101,14 @@ func createCommit(c *C, r *Repository) {
101101 Email : "go-git@fake.local" ,
102102 When : time .Now (),
103103 }
104- _ , err = wt .Commit ("test commit message" , & CommitOptions {
104+
105+ h , err := wt .Commit ("test commit message" , & CommitOptions {
105106 All : true ,
106107 Author : & author ,
107108 Committer : & author ,
108109 })
109110 c .Assert (err , IsNil )
110-
111+ return h
111112}
112113
113114func (s * RepositorySuite ) TestInitNonStandardDotGit (c * C ) {
@@ -439,6 +440,112 @@ func (s *RepositorySuite) TestCreateBranchAndBranch(c *C) {
439440 c .Assert (branch .Merge , Equals , testBranch .Merge )
440441}
441442
443+ func (s * RepositorySuite ) TestMergeFF (c * C ) {
444+ r , err := Init (memory .NewStorage (), memfs .New ())
445+ c .Assert (err , IsNil )
446+ c .Assert (r , NotNil )
447+
448+ createCommit (c , r )
449+ createCommit (c , r )
450+ createCommit (c , r )
451+ lastCommit := createCommit (c , r )
452+
453+ wt , err := r .Worktree ()
454+ c .Assert (err , IsNil )
455+
456+ targetBranch := plumbing .NewBranchReferenceName ("foo" )
457+ err = wt .Checkout (& CheckoutOptions {
458+ Hash : lastCommit ,
459+ Create : true ,
460+ Branch : targetBranch ,
461+ })
462+ c .Assert (err , IsNil )
463+
464+ createCommit (c , r )
465+ fooHash := createCommit (c , r )
466+
467+ // Checkout the master branch so that we can try to merge foo into it.
468+ err = wt .Checkout (& CheckoutOptions {
469+ Branch : plumbing .Master ,
470+ })
471+ c .Assert (err , IsNil )
472+
473+ head , err := r .Head ()
474+ c .Assert (err , IsNil )
475+ c .Assert (head .Hash (), Equals , lastCommit )
476+
477+ targetRef := plumbing .NewHashReference (targetBranch , fooHash )
478+ c .Assert (targetRef , NotNil )
479+
480+ err = r .Merge (* targetRef , MergeOptions {
481+ Strategy : FastForwardMerge ,
482+ })
483+ c .Assert (err , IsNil )
484+
485+ head , err = r .Head ()
486+ c .Assert (err , IsNil )
487+ c .Assert (head .Hash (), Equals , fooHash )
488+ }
489+
490+ func (s * RepositorySuite ) TestMergeFF_Invalid (c * C ) {
491+ r , err := Init (memory .NewStorage (), memfs .New ())
492+ c .Assert (err , IsNil )
493+ c .Assert (r , NotNil )
494+
495+ // Keep track of the first commit, which will be the
496+ // reference to create the target branch so that we
497+ // can simulate a non-ff merge.
498+ firstCommit := createCommit (c , r )
499+ createCommit (c , r )
500+ createCommit (c , r )
501+ lastCommit := createCommit (c , r )
502+
503+ wt , err := r .Worktree ()
504+ c .Assert (err , IsNil )
505+
506+ targetBranch := plumbing .NewBranchReferenceName ("foo" )
507+ err = wt .Checkout (& CheckoutOptions {
508+ Hash : firstCommit ,
509+ Create : true ,
510+ Branch : targetBranch ,
511+ })
512+
513+ c .Assert (err , IsNil )
514+
515+ createCommit (c , r )
516+ h := createCommit (c , r )
517+
518+ // Checkout the master branch so that we can try to merge foo into it.
519+ err = wt .Checkout (& CheckoutOptions {
520+ Branch : plumbing .Master ,
521+ })
522+ c .Assert (err , IsNil )
523+
524+ head , err := r .Head ()
525+ c .Assert (err , IsNil )
526+ c .Assert (head .Hash (), Equals , lastCommit )
527+
528+ targetRef := plumbing .NewHashReference (targetBranch , h )
529+ c .Assert (targetRef , NotNil )
530+
531+ err = r .Merge (* targetRef , MergeOptions {
532+ Strategy : MergeStrategy (10 ),
533+ })
534+ c .Assert (err , Equals , ErrUnsupportedMergeStrategy )
535+
536+ // Failed merge operations must not change HEAD.
537+ head , err = r .Head ()
538+ c .Assert (err , IsNil )
539+ c .Assert (head .Hash (), Equals , lastCommit )
540+
541+ err = r .Merge (* targetRef , MergeOptions {})
542+ c .Assert (err , Equals , ErrFastForwardMergeNotPossible )
543+
544+ head , err = r .Head ()
545+ c .Assert (err , IsNil )
546+ c .Assert (head .Hash (), Equals , lastCommit )
547+ }
548+
442549func (s * RepositorySuite ) TestCreateBranchUnmarshal (c * C ) {
443550 r , _ := Init (memory .NewStorage (), nil )
444551
0 commit comments