aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaulo Gomes <pjbgf@linux.com>2023-08-05 10:30:24 +0100
committerGitHub <noreply@github.com>2023-08-05 10:30:24 +0100
commitcd6170c6f808453f58dcfd15c6c59345e3df402b (patch)
tree15c5e333b93641f9eadcb4bf4b34c338135f7a23
parent4ec6b3f4fa9cdfe8f10d0953ac7d398d01a90f17 (diff)
parente6f68d2e4cd1bc4447126816c7c27e1fc2098e30 (diff)
downloadgo-git-cd6170c6f808453f58dcfd15c6c59345e3df402b.tar.gz
Merge pull request #438 from john-cai/jc/commit-ammend
Add Amend option to CommitOptions
-rw-r--r--options.go11
-rw-r--r--worktree_commit.go43
-rw-r--r--worktree_commit_test.go31
3 files changed, 72 insertions, 13 deletions
diff --git a/options.go b/options.go
index 6d802d1..757bdc8 100644
--- a/options.go
+++ b/options.go
@@ -498,10 +498,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 e927721..eaa21c3 100644
--- a/worktree_commit.go
+++ b/worktree_commit.go
@@ -36,22 +36,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, opts)
- if err != nil {
- return plumbing.ZeroHash, err
+ t, err := w.r.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, opts)
+ 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
}
@@ -246,4 +263,4 @@ func (h *buildTreeHelper) copyTreeToStorageRecursive(parent string, t *object.Tr
return hash, nil
}
return h.s.SetEncodedObject(o)
-}
+} \ No newline at end of file
diff --git a/worktree_commit_test.go b/worktree_commit_test.go
index bfeb81d..1ac1990 100644
--- a/worktree_commit_test.go
+++ b/worktree_commit_test.go
@@ -113,6 +113,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")