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)
| 33 | // Commit stores the current contents of the index in a new commit along with |
| 34 | // a log message from the user describing the changes. |
| 35 | func (w *Worktree) Commit(msg string, opts *CommitOptions) (plumbing.Hash, error) { |
| 36 | if err := opts.Validate(w.r); err != nil { |
| 37 | return plumbing.ZeroHash, err |
| 38 | } |
| 39 | |
| 40 | if opts.All { |
| 41 | if err := w.autoAddModifiedAndDeleted(); err != nil { |
| 42 | return plumbing.ZeroHash, err |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | if opts.Amend { |
| 47 | head, err := w.r.Head() |
| 48 | if err != nil { |
| 49 | return plumbing.ZeroHash, err |
| 50 | } |
| 51 | headCommit, err := w.r.CommitObject(head.Hash()) |
| 52 | if err != nil { |
| 53 | return plumbing.ZeroHash, err |
| 54 | } |
| 55 | |
| 56 | opts.Parents = nil |
| 57 | if len(headCommit.ParentHashes) != 0 { |
| 58 | opts.Parents = []plumbing.Hash{headCommit.ParentHashes[0]} |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | idx, err := w.r.Storer.Index() |
| 63 | if err != nil { |
| 64 | return plumbing.ZeroHash, err |
| 65 | } |
| 66 | |
| 67 | // First handle the case of the first commit in the repository being empty. |
| 68 | if len(opts.Parents) == 0 && len(idx.Entries) == 0 && !opts.AllowEmptyCommits { |
| 69 | return plumbing.ZeroHash, ErrEmptyCommit |
| 70 | } |
| 71 | |
| 72 | h := &buildTreeHelper{ |
| 73 | fs: w.Filesystem, |
| 74 | s: w.r.Storer, |
| 75 | } |
| 76 | |
| 77 | treeHash, err := h.BuildTree(idx, opts) |
| 78 | if err != nil { |
| 79 | return plumbing.ZeroHash, err |
| 80 | } |
| 81 | |
| 82 | previousTree := plumbing.ZeroHash |
| 83 | if len(opts.Parents) > 0 { |
| 84 | parentCommit, err := w.r.CommitObject(opts.Parents[0]) |
| 85 | if err != nil { |
| 86 | return plumbing.ZeroHash, err |
| 87 | } |
| 88 | previousTree = parentCommit.TreeHash |
| 89 | } |
| 90 | |
| 91 | if treeHash == previousTree && !opts.AllowEmptyCommits { |
| 92 | return plumbing.ZeroHash, ErrEmptyCommit |