Skip to content

Commit f3113d2

Browse files
authored
Merge pull request go-git#1045 from onee-only/fix-amend-with-changes
git: worktree_commit, Fix amend commit to apply changes. Fixes go-git#1024
2 parents d9497ba + 74febd2 commit f3113d2

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

worktree_commit.go

+17-16
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,30 @@ func (w *Worktree) Commit(msg string, opts *CommitOptions) (plumbing.Hash, error
4545
if err != nil {
4646
return plumbing.ZeroHash, err
4747
}
48-
49-
t, err := w.r.getTreeFromCommitHash(head.Hash())
48+
headCommit, err := w.r.CommitObject(head.Hash())
5049
if err != nil {
5150
return plumbing.ZeroHash, err
5251
}
5352

54-
treeHash = t.Hash
55-
opts.Parents = []plumbing.Hash{head.Hash()}
56-
} else {
57-
idx, err := w.r.Storer.Index()
58-
if err != nil {
59-
return plumbing.ZeroHash, err
53+
opts.Parents = nil
54+
if len(headCommit.ParentHashes) != 0 {
55+
opts.Parents = []plumbing.Hash{headCommit.ParentHashes[0]}
6056
}
57+
}
6158

62-
h := &buildTreeHelper{
63-
fs: w.Filesystem,
64-
s: w.r.Storer,
65-
}
59+
idx, err := w.r.Storer.Index()
60+
if err != nil {
61+
return plumbing.ZeroHash, err
62+
}
6663

67-
treeHash, err = h.BuildTree(idx, opts)
68-
if err != nil {
69-
return plumbing.ZeroHash, err
70-
}
64+
h := &buildTreeHelper{
65+
fs: w.Filesystem,
66+
s: w.r.Storer,
67+
}
68+
69+
treeHash, err = h.BuildTree(idx, opts)
70+
if err != nil {
71+
return plumbing.ZeroHash, err
7172
}
7273

7374
commit, err := w.buildCommitObject(msg, opts, treeHash)

worktree_commit_test.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,37 @@ func (s *WorktreeSuite) TestCommitAmend(c *C) {
131131
_, err = w.Commit("foo\n", &CommitOptions{Author: defaultSignature()})
132132
c.Assert(err, IsNil)
133133

134+
util.WriteFile(fs, "bar", []byte("bar"), 0644)
135+
136+
_, err = w.Add("bar")
137+
c.Assert(err, IsNil)
138+
134139
amendedHash, err := w.Commit("bar\n", &CommitOptions{Amend: true})
135140
c.Assert(err, IsNil)
136141

137142
headRef, err := w.r.Head()
143+
c.Assert(err, IsNil)
144+
138145
c.Assert(amendedHash, Equals, headRef.Hash())
146+
139147
commit, err := w.r.CommitObject(headRef.Hash())
140148
c.Assert(err, IsNil)
141149
c.Assert(commit.Message, Equals, "bar\n")
150+
c.Assert(commit.NumParents(), Equals, 1)
151+
152+
stats, err := commit.Stats()
153+
c.Assert(err, IsNil)
154+
c.Assert(stats, HasLen, 2)
155+
c.Assert(stats[0], Equals, object.FileStat{
156+
Name: "bar",
157+
Addition: 1,
158+
})
159+
c.Assert(stats[1], Equals, object.FileStat{
160+
Name: "foo",
161+
Addition: 1,
162+
})
142163

143-
assertStorageStatus(c, s.Repository, 13, 11, 11, amendedHash)
164+
assertStorageStatus(c, s.Repository, 14, 12, 11, amendedHash)
144165
}
145166

146167
func (s *WorktreeSuite) TestAddAndCommitWithSkipStatus(c *C) {

0 commit comments

Comments
 (0)