diff options
Diffstat (limited to 'worktree_commit.go')
-rw-r--r-- | worktree_commit.go | 41 |
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 } |