8000 git: worktree_commit, Fix amend commit to apply changes. Fixes #1024 by onee-only · Pull Request #1045 · go-git/go-git · GitHub
[go: up one dir, main page]

Skip to content

git: worktree_commit, Fix amend commit to apply changes. Fixes #1024 #1045

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

Merged
merged 1 commit into from
Mar 10, 2024
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.
Loading
Jump to
8000 Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions worktree_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,30 @@ func (w *Worktree) Commit(msg string, opts *CommitOptions) (plumbing.Hash, error
if err != nil {
return plumbing.ZeroHash, err
}

t, err := w.r.getTreeFromCommitHash(head.Hash())
headCommit, err := w.r.CommitObject(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
opts.Parents = nil
if len(headCommit.ParentHashes) != 0 {
opts.Parents = []plumbing.Hash{headCommit.ParentHashes[0]}
}
}

h := &buildTreeHelper{
fs: w.Filesystem,
s: w.r.Storer,
}
idx, err := w.r.Storer.Index()
if err != nil {
return plumbing.ZeroHash, err
}

treeHash, err = h.BuildTree(idx, opts)
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, treeHash)
Expand Down
23 changes: 22 additions & 1 deletion worktree_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,37 @@ func (s *WorktreeSuite) TestCommitAmend(c *C) {
_, err = w.Commit("foo\n", &CommitOptions{Author: defaultSignature()})
c.Assert(err, IsNil)

util.WriteFile(fs, "bar", []byte("bar"), 0644)

_, err = w.Add("bar")
c.Assert(err, IsNil)

amendedHash, err := w.Commit("bar\n", &CommitOptions{Amend: true})
c.Assert(err, IsNil)

headRef, err := w.r.Head()
c.Assert(err, IsNil)

c.Assert(amendedHash, Equals, headRef.Hash())

commit, err := w.r.CommitObject(headRef.Hash())
c.Assert(err, IsNil)
c.Assert(commit.Message, Equals, "bar\n")
c.Assert(commit.NumParents(), Equals, 1)

stats, err := commit.Stats()
c.Assert(err, IsNil)
c.Assert(stats, HasLen, 2)
c.Assert(stats[0], Equals, object.FileStat{
Name: "bar",
Addition: 1,
})
c.Assert(stats[1], Equals, object.FileStat{
Name: "foo",
Addition: 1,
})

assertStorageStatus(c, s.Repository, 13, 11, 11, amendedHash)
assertStorageStatus(c, s.Repository, 14, 12, 11, amendedHash)
}

func (s *WorktreeSuite) TestAddAndCommitWithSkipStatus(c *C) {
Expand Down
Loading
0