/
commit.go
243 lines (218 loc) · 5.7 KB
/
commit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package git
import (
"fmt"
"log"
"os"
"regexp"
"strings"
"time"
)
type CommitOptions struct {
All bool
Patch bool
ResetAuthor bool
Amend bool
Date time.Time
Author Person
Signoff bool
NoVerify bool
AllowEmpty bool
AllowEmptyMessage bool
NoPostRewrite bool
Include bool
Only bool
Quiet bool
CleanupMode string
NoEdit bool
// Should be passed to CommitTree, which needs support first:
// GPGSign GPGKeyID
// NoGpgSign bool
// Things that are used to create the commit message and need to be
// parsed by package cmd/, but not included here.
// ReuseMessage, ReeditMessage, Fixup, Squash Commitish
// File string
// Message string (-m)
// Template File (COMMIT_EDITMSG)
// Status, NoStatus bool
// Verbose bool
//
// Things that only affect the output with --dry-run.
// Note: Printing the status after --dry-run isn't implemented,
// all it does is prevent the call to UpdateRef after CommitTree.
// Most of these are a no-op.
DryRun bool
Short bool
Branch bool
Porcelain bool
Long bool
NullTerminate bool
UntrackedMode StatusUntrackedMode
// FIXME: Add all the missing options here.
}
// Commit implements the command "git commit" in the repository pointed
// to by c.
func Commit(c *Client, opts CommitOptions, message CommitMessage, files []File) (CommitID, error) {
if !opts.AllowEmptyMessage && message == "" {
return CommitID{}, fmt.Errorf("Aborting commit due to empty commit message.")
}
if opts.Patch {
return CommitID{}, fmt.Errorf("Commit --patch not implemented")
}
var idx *Index
if opts.All {
var tostage []File
if opts.Include {
tostage = files
}
if _, err := Add(c, AddOptions{Update: true, DryRun: opts.DryRun}, tostage); err != nil {
log.Println("Commit adding files:", err)
return CommitID{}, err
}
}
if !opts.All && len(files) != 0 {
var idx1 *Index
head, err := c.GetHeadCommit()
if err != nil {
idx1 = NewIndex()
} else {
idx1, err = ReadTree(c, ReadTreeOptions{DryRun: true}, head)
if err != nil {
return CommitID{}, err
}
}
idx2, err := UpdateIndex(c, idx1, UpdateIndexOptions{Add: true, Remove: true}, files)
if err != nil {
return CommitID{}, err
}
idx = idx2
} else {
idx1, err := c.GitDir.ReadIndex()
if err != nil {
return CommitID{}, err
}
idx = idx1
}
// Happy path: write the tree
treeid, err := WriteTreeFromIndex(c, idx, WriteTreeOptions{})
if err != nil {
return CommitID{}, err
}
// Write the commit object
var parents []CommitID
oldHead, err := c.GetHeadCommit()
if opts.Amend {
parents, err = oldHead.Parents(c)
if err != nil {
return CommitID{}, err
}
author, err := oldHead.GetAuthor(c)
if err != nil {
return CommitID{}, err
}
if !opts.ResetAuthor {
// Back up the environment variables that commit uses to
// communicate with commit-tree, so that nothing external
// changes to the caller of this script.
defer func(oldauthorname, oldauthoremail, oldauthordate string) {
os.Setenv("GIT_AUTHOR_NAME", oldauthorname)
os.Setenv("GIT_AUTHOR_EMAIL", oldauthoremail)
os.Setenv("GIT_AUTHOR_DATE", oldauthordate)
}(
os.Getenv("GIT_AUTHOR_NAME"),
os.Getenv("GIT_AUTHOR_EMAIL"),
os.Getenv("GIT_AUTHOR_DATE"),
)
os.Setenv("GIT_AUTHOR_NAME", author.Name)
os.Setenv("GIT_AUTHOR_EMAIL", author.Email)
date, err := oldHead.GetDate(c)
if err != nil {
return CommitID{}, err
}
os.Setenv("GIT_AUTHOR_DATE", date.Format("Mon, 02 Jan 2006 15:04:05 -0700"))
}
goto skipemptycheck
} else if err == nil || err == DetachedHead {
parents = append(parents, oldHead)
}
if !opts.AllowEmpty {
if oldtree, err := oldHead.TreeID(c); err == nil {
if oldtree == treeid {
return CommitID{}, fmt.Errorf("No changes staged for commit.")
}
}
}
skipemptycheck:
cleanMessage, err := message.Cleanup(opts.CleanupMode, !opts.NoEdit)
if err != nil {
return CommitID{}, err
}
var noConfig error
cid, err := CommitTree(c, CommitTreeOptions{}, TreeID(treeid), parents, cleanMessage)
switch err {
case nil:
// Nothing
case NoGlobalConfig:
noConfig = err
default:
return CommitID{}, err
}
// Update the reference
var refmsg string
if len(cleanMessage) < 50 {
refmsg = cleanMessage
} else {
refmsg = cleanMessage[:50]
}
refmsg = fmt.Sprintf("commit: %s (dgit)", refmsg)
if err := UpdateRef(c, UpdateRefOptions{OldValue: oldHead, CreateReflog: true}, "HEAD", cid, refmsg); err != nil {
return CommitID{}, err
}
return cid, noConfig
}
type CommitMessage string
func (cm CommitMessage) String() string {
return string(cm)
}
func (cm CommitMessage) Cleanup(mode string, edit bool) (string, error) {
switch mode {
case "strip":
return cm.strip(), nil
case "whitespace":
return cm.whitespace(), nil
case "", "default":
if edit {
return cm.strip(), nil
}
return cm.whitespace(), nil
case "scissors":
return string(cm), fmt.Errorf("Unsupported cleanup mode")
default:
return string(cm), fmt.Errorf("Invalid cleanup mode")
}
}
func (cm CommitMessage) whitespace() string {
nonewlineRE, err := regexp.Compile("([\n]+)\n")
if err != nil {
panic(err)
}
replaced := nonewlineRE.ReplaceAllString(string(cm), "\n\n")
return strings.TrimSpace(replaced) + "\n"
}
func (cm CommitMessage) strip() string {
lines := strings.Split(cm.whitespace(), "\n")
filtered := make([]string, 0, len(lines))
for _, line := range lines {
if len(line) >= 1 && line[0] == '#' {
continue
}
filtered = append(filtered, line)
}
return strings.Join(filtered, "\n")
}
func (cm CommitMessage) Subject() string {
lines := strings.SplitN(cm.whitespace(), "\n", 1)
if len(lines) > 0 {
return strings.TrimSpace(lines[0])
}
return ""
}