Commit stores the current contents of the index in a new commit along with a log message from the user describing the changes.
(msg string, opts *CommitOptions)
| 28 | // Commit stores the current contents of the index in a new commit along with |
| 29 | // a log message from the user describing the changes. |
| 30 | func (w *Worktree) Commit(msg string, opts *CommitOptions) (plumbing.Hash, error) { |
| 31 | if err := opts.Validate(w.r); err != nil { |
| 32 | return plumbing.ZeroHash, err |
| 33 | } |
| 34 | |
| 35 | if opts.All { |
| 36 | if err := w.autoAddModifiedAndDeleted(); err != nil { |
| 37 | return plumbing.ZeroHash, err |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | var treeHash plumbing.Hash |
| 42 | |
| 43 | if opts.Amend { |
| 44 | head, err := w.r.Head() |
| 45 | if err != nil { |
| 46 | return plumbing.ZeroHash, err |
| 47 | } |
| 48 | headCommit, err := w.r.CommitObject(head.Hash()) |
| 49 | if err != nil { |
| 50 | return plumbing.ZeroHash, err |
| 51 | } |
| 52 | |
| 53 | opts.Parents = nil |
| 54 | if len(headCommit.ParentHashes) != 0 { |
| 55 | opts.Parents = []plumbing.Hash{headCommit.ParentHashes[0]} |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | idx, err := w.r.Storer.Index() |
| 60 | if err != nil { |
| 61 | return plumbing.ZeroHash, err |
| 62 | } |
| 63 | |
| 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 |
| 72 | } |
| 73 | |
| 74 | commit, err := w.buildCommitObject(msg, opts, treeHash) |
| 75 | if err != nil { |
| 76 | return plumbing.ZeroHash, err |
| 77 | } |
| 78 | |
| 79 | return commit, w.updateHEAD(commit) |
| 80 | } |
| 81 | |
| 82 | func (w *Worktree) autoAddModifiedAndDeleted() error { |
| 83 | s, err := w.Status() |