fork of go-git with some jj specific features
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #438 from john-cai/jc/commit-ammend

Add Amend option to CommitOptions

authored by

Paulo Gomes and committed by
GitHub
cd6170c6 4ec6b3f4

+72 -13
+11
options.go
··· 498 498 // commit will not be signed. The private key must be present and already 499 499 // decrypted. 500 500 SignKey *openpgp.Entity 501 + // Amend will create a new commit object and replace the commit that HEAD currently 502 + // points to. Cannot be used with All nor Parents. 503 + Amend bool 501 504 } 502 505 503 506 // Validate validates the fields and sets the default values. 504 507 func (o *CommitOptions) Validate(r *Repository) error { 508 + if o.All && o.Amend { 509 + return errors.New("all and amend cannot be used together") 510 + } 511 + 512 + if o.Amend && len(o.Parents) > 0 { 513 + return errors.New("parents cannot be used with amend") 514 + } 515 + 505 516 if o.Author == nil { 506 517 if err := o.loadConfigAuthorAndCommitter(r); err != nil { 507 518 return err
+30 -13
worktree_commit.go
··· 36 36 } 37 37 } 38 38 39 - idx, err := w.r.Storer.Index() 40 - if err != nil { 41 - return plumbing.ZeroHash, err 42 - } 39 + var treeHash plumbing.Hash 40 + 41 + if opts.Amend { 42 + head, err := w.r.Head() 43 + if err != nil { 44 + return plumbing.ZeroHash, err 45 + } 46 + 47 + t, err := w.r.getTreeFromCommitHash(head.Hash()) 48 + if err != nil { 49 + return plumbing.ZeroHash, err 50 + } 51 + 52 + treeHash = t.Hash 53 + opts.Parents = []plumbing.Hash{head.Hash()} 54 + } else { 55 + idx, err := w.r.Storer.Index() 56 + if err != nil { 57 + return plumbing.ZeroHash, err 58 + } 43 59 44 - h := &buildTreeHelper{ 45 - fs: w.Filesystem, 46 - s: w.r.Storer, 47 - } 60 + h := &buildTreeHelper{ 61 + fs: w.Filesystem, 62 + s: w.r.Storer, 63 + } 48 64 49 - tree, err := h.BuildTree(idx, opts) 50 - if err != nil { 51 - return plumbing.ZeroHash, err 65 + treeHash, err = h.BuildTree(idx, opts) 66 + if err != nil { 67 + return plumbing.ZeroHash, err 68 + } 52 69 } 53 70 54 - commit, err := w.buildCommitObject(msg, opts, tree) 71 + commit, err := w.buildCommitObject(msg, opts, treeHash) 55 72 if err != nil { 56 73 return plumbing.ZeroHash, err 57 74 } ··· 246 263 return hash, nil 247 264 } 248 265 return h.s.SetEncodedObject(o) 249 - } 266 + }
+31
worktree_commit_test.go
··· 113 113 assertStorageStatus(c, s.Repository, 13, 11, 10, expected) 114 114 } 115 115 116 + func (s *WorktreeSuite) TestCommitAmend(c *C) { 117 + fs := memfs.New() 118 + w := &Worktree{ 119 + r: s.Repository, 120 + Filesystem: fs, 121 + } 122 + 123 + err := w.Checkout(&CheckoutOptions{}) 124 + c.Assert(err, IsNil) 125 + 126 + util.WriteFile(fs, "foo", []byte("foo"), 0644) 127 + 128 + _, err = w.Add("foo") 129 + c.Assert(err, IsNil) 130 + 131 + _, err = w.Commit("foo\n", &CommitOptions{Author: defaultSignature()}) 132 + c.Assert(err, IsNil) 133 + 134 + 135 + amendedHash, err := w.Commit("bar\n", &CommitOptions{Amend: true}) 136 + c.Assert(err, IsNil) 137 + 138 + headRef, err := w.r.Head() 139 + c.Assert(amendedHash, Equals, headRef.Hash()) 140 + commit, err := w.r.CommitObject(headRef.Hash()) 141 + c.Assert(err, IsNil) 142 + c.Assert(commit.Message, Equals, "bar\n") 143 + 144 + assertStorageStatus(c, s.Repository, 13, 11, 11, amendedHash) 145 + } 146 + 116 147 func (s *WorktreeSuite) TestCommitAll(c *C) { 117 148 expected := plumbing.NewHash("aede6f8c9c1c7ec9ca8d287c64b8ed151276fa28") 118 149