aboutsummaryrefslogtreecommitdiffstats
path: root/worktree_commit.go
diff options
context:
space:
mode:
Diffstat (limited to 'worktree_commit.go')
-rw-r--r--worktree_commit.go43
1 files changed, 30 insertions, 13 deletions
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