aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--options.go11
-rw-r--r--worktree_commit.go41
-rw-r--r--worktree_commit_test.go31
3 files changed, 71 insertions, 12 deletions
diff --git a/options.go b/options.go
index d6f0e9f..a2d62bd 100644
--- a/options.go
+++ b/options.go
@@ -471,10 +471,21 @@ type CommitOptions struct {
// commit will not be signed. The private key must be present and already
// decrypted.
SignKey *openpgp.Entity
+ // Amend will create a new commit object and replace the commit that HEAD currently
+ // points to. Cannot be used with All nor Parents.
+ Amend bool
}
// Validate validates the fields and sets the default values.
func (o *CommitOptions) Validate(r *Repository) error {
+ if o.All && o.Amend {
+ return errors.New("all and amend cannot be used together")
+ }
+
+ if o.Amend && len(o.Parents) > 0 {
+ return errors.New("parents cannot be used with amend")
+ }
+
if o.Author == nil {
if err := o.loadConfigAuthorAndCommitter(r); err != nil {
return err
diff --git a/worktree_commit.go b/worktree_commit.go
index dc79569..86320d8 100644
--- a/worktree_commit.go
+++ b/worktree_commit.go
@@ -29,22 +29,39 @@ func (w *Worktree) Commit(msg string, opts *CommitOptions) (plumbing.Hash, error
}
}
- idx, err := w.r.Storer.Index()
- if err != nil {
- return plumbing.ZeroHash, err
- }
+ var treeHash plumbing.Hash
- h := &buildTreeHelper{
- fs: w.Filesystem,
- s: w.r.Storer,
- }
+ if opts.Amend {
+ head, err := w.r.Head()
+ if err != nil {
+ return plumbing.ZeroHash, err
+ }
- tree, err := h.BuildTree(idx)
- if err != nil {
- return plumbing.ZeroHash, err
+ t, err := w.getTreeFromCommitHash(head.Hash())
+ if err != nil {
+ return plumbing.ZeroHash, err
+ }
+
+ treeHash = t.Hash
+ opts.Parents = []plumbing.Hash{head.Hash()}
+ }else {
+ idx, err := w.r.Storer.Index()
+ if err != nil {
+ return plumbing.ZeroHash, err
+ }
+
+ h := &buildTreeHelper{
+ fs: w.Filesystem,
+ s: w.r.Storer,
+ }
+
+ treeHash, err = h.BuildTree(idx)
+ if err != nil {
+ return plumbing.ZeroHash, err
+ }
}
- commit, err := w.buildCommitObject(msg, opts, tree)
+ commit, err := w.buildCommitObject(msg, opts, treeHash)
if err != nil {
return plumbing.ZeroHash, err
}
diff --git a/worktree_commit_test.go b/worktree_commit_test.go
index 097c6e5..547c820 100644
--- a/worktree_commit_test.go
+++ b/worktree_commit_test.go
@@ -89,6 +89,37 @@ func (s *WorktreeSuite) TestCommitParent(c *C) {
assertStorageStatus(c, s.Repository, 13, 11, 10, expected)
}
+func (s *WorktreeSuite) TestCommitAmend(c *C) {
+ fs := memfs.New()
+ w := &Worktree{
+ r: s.Repository,
+ Filesystem: fs,
+ }
+
+ err := w.Checkout(&CheckoutOptions{})
+ c.Assert(err, IsNil)
+
+ util.WriteFile(fs, "foo", []byte("foo"), 0644)
+
+ _, err = w.Add("foo")
+ c.Assert(err, IsNil)
+
+ _, err = w.Commit("foo\n", &CommitOptions{Author: defaultSignature()})
+ c.Assert(err, IsNil)
+
+
+ amendedHash, err := w.Commit("bar\n", &CommitOptions{Amend: true})
+ c.Assert(err, IsNil)
+
+ headRef, err := w.r.Head()
+ c.Assert(amendedHash, Equals, headRef.Hash())
+ commit, err := w.r.CommitObject(headRef.Hash())
+ c.Assert(err, IsNil)
+ c.Assert(commit.Message, Equals, "bar\n")
+
+ assertStorageStatus(c, s.Repository, 13, 11, 11, amendedHash)
+}
+
func (s *WorktreeSuite) TestCommitAll(c *C) {
expected := plumbing.NewHash("aede6f8c9c1c7ec9ca8d287c64b8ed151276fa28")