aboutsummaryrefslogtreecommitdiffstats
path: root/worktree_commit.go
diff options
context:
space:
mode:
authorJohn Cai <jcai@gitlab.com>2022-01-03 15:40:28 -0500
committerJohn Cai <jcai@gitlab.com>2022-01-03 15:49:31 -0500
commit5882d60fb7ccd4cfc0fe69286aa96e198c9d1eb0 (patch)
treea310e673d288758403d0e99086cec6f801480d3b /worktree_commit.go
parentf0b111ab70e4e90013658b0835929b2083902017 (diff)
downloadgo-git-5882d60fb7ccd4cfc0fe69286aa96e198c9d1eb0.tar.gz
Add Amend option to CommitOptions
Adds an Amend option to CommitOptions that behaves like git --amend. This change includes modifications to the Validate function to disallow a Commit call with both Amend and either Parents or All enabled.
Diffstat (limited to 'worktree_commit.go')
-rw-r--r--worktree_commit.go41
1 files changed, 29 insertions, 12 deletions
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
}