Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Amend option to CommitOptions #438

Merged
merged 2 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 30 additions & 13 deletions worktree_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -246,4 +263,4 @@ func (h *buildTreeHelper) copyTreeToStorageRecursive(parent string, t *object.Tr
return hash, nil
}
return h.s.SetEncodedObject(o)
}
}
31 changes: 31 additions & 0 deletions worktree_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down