-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
squash_wip.go
235 lines (196 loc) · 7.05 KB
/
squash_wip.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
package main
import (
config "github.com/remotemobprogramming/mob/v5/configuration"
"github.com/remotemobprogramming/mob/v5/say"
"io"
"os"
"path/filepath"
"strconv"
"strings"
)
type Replacer func(string) string
func squashWip(configuration config.Configuration) {
if hasUncommittedChanges() {
makeWipCommit(configuration)
}
currentBaseBranch, currentWipBranch := determineBranches(gitCurrentBranch(), gitBranches(), configuration)
mergeBase := silentgit("merge-base", currentWipBranch.String(), currentBaseBranch.remote(configuration).String())
originalGitEditor, originalGitSequenceEditor := getEnvGitEditor()
setEnvGitEditor(
mobExecutable()+" squash-wip --git-editor",
mobExecutable()+" squash-wip --git-sequence-editor",
)
say.Info("rewriting history of '" + currentWipBranch.String() + "': squashing wip commits while keeping manual commits.")
git("rebase", "--interactive", "--keep-empty", mergeBase)
setEnvGitEditor(originalGitEditor, originalGitSequenceEditor)
say.Info("resulting history is:")
sayLastCommitsWithMessage(currentBaseBranch.remote(configuration).String(), currentWipBranch.String())
if lastCommitIsWipCommit(configuration) { // last commit is wip commit
say.Info("undoing the final wip commit and staging its changes:")
git("reset", "--soft", "HEAD^")
}
git("push", "--force", gitHooksOption(configuration))
}
func lastCommitIsWipCommit(configuration config.Configuration) bool {
return strings.HasPrefix(lastCommitMessage(), configuration.WipCommitMessage)
}
func lastCommitMessage() string {
return silentgit("log", "-1", "--pretty=format:%B")
}
func sayLastCommitsWithMessage(currentBaseBranch string, currentWipBranch string) {
commitsBaseWipBranch := currentBaseBranch + ".." + currentWipBranch
log := silentgit("--no-pager", "log", commitsBaseWipBranch, "--pretty=oneline", "--abbrev-commit")
lines := strings.Split(log, "\n")
if len(lines) > 10 {
say.Info("wip branch '" + currentWipBranch + "' contains " + strconv.Itoa(len(lines)) + " commits. The last 10 were:")
lines = lines[:10]
}
output := strings.Join(lines, "\n")
say.Say(output)
}
func setEnvGitEditor(gitEditor string, gitSequenceEditor string) {
os.Setenv("GIT_EDITOR", gitEditor)
os.Setenv("GIT_SEQUENCE_EDITOR", gitSequenceEditor)
}
func getEnvGitEditor() (gitEditor string, gitSequenceEditor string) {
gitEditor = os.Getenv("GIT_EDITOR")
gitSequenceEditor = os.Getenv("GIT_SEQUENCE_EDITOR")
return
}
func mobExecutable() string {
if isTestEnvironment() {
wd, _ := os.Getwd()
// Convert Windows path separators to /, so they work in git bash. No-op for non-Windows paths.
wd = filepath.ToSlash(wd)
return "cd " + wd + " && go run $(ls -1 ./*.go | grep -v _test.go)"
} else {
return "mob"
}
}
func isTestEnvironment() bool {
cliName := currentCliName(os.Args[0])
return strings.HasSuffix(cliName, ".test") ||
strings.HasSuffix(cliName, "_test") ||
os.Args[1] == "-test.v"
}
// used for non-interactive fixing of commit messages of squashed commits
func squashWipGitEditor(fileName string, configuration config.Configuration) {
replaceFileContents(fileName, func(input string) string {
return commentWipCommits(input, configuration)
})
}
// used for non-interactive rebase to squash post-wip-commits
func squashWipGitSequenceEditor(fileName string, configuration config.Configuration) {
replaceFileContents(fileName, func(input string) string {
result := markPostWipCommitsForSquashing(input, configuration)
return markStartCommitForDropping(result, configuration)
})
}
func replaceFileContents(fileName string, replacer Replacer) {
file, _ := os.OpenFile(fileName, os.O_RDWR, 0666)
input, err := io.ReadAll(file)
if err != nil {
panic(err)
}
result := replacer(string(input))
file.Seek(0, io.SeekStart)
file.Truncate(0)
file.WriteString(result)
file.Close()
}
func commentWipCommits(input string, configuration config.Configuration) string {
var result []string
ignoreBlock := false
lines := strings.Split(input, "\n")
for idx, line := range lines {
if configuration.IsWipCommitMessage(line) {
ignoreBlock = true
} else if line == "" && isNextLineComment(lines, idx) {
ignoreBlock = false
}
if ignoreBlock {
result = append(result, "# "+line)
} else {
result = append(result, line)
}
}
return strings.Join(result, "\n")
}
func isNextLineComment(lines []string, currentLineIndex int) bool {
return len(lines) > currentLineIndex+1 && strings.HasPrefix(lines[currentLineIndex+1], "#")
}
func markPostWipCommitsForSquashing(input string, configuration config.Configuration) string {
var result []string
inputLines := strings.Split(input, "\n")
for index := range inputLines {
markedLine := markLine(inputLines, index, configuration)
result = append(result, markedLine)
}
return strings.Join(result, "\n")
}
func markStartCommitForDropping(input string, configuration config.Configuration) string {
inputLines := strings.Split(input, "\n")
result := inputLines
firstLine := inputLines[0]
if isStartCISkipCommitLine(firstLine, configuration) {
markedLine := markDrop(firstLine)
result[0] = markedLine
}
return strings.Join(result, "\n")
}
func markLine(inputLines []string, i int, configuration config.Configuration) string {
var resultLine = inputLines[i]
previousLine := previousLine(inputLines, i)
if isWipCommitLine(previousLine, configuration) {
forthComingLines := inputLines[i:]
if hasOnlyWipCommits(forthComingLines, configuration) {
resultLine = markFixup(inputLines[i])
} else {
resultLine = markSquash(inputLines[i])
}
}
return resultLine
}
func previousLine(inputLines []string, currentIndex int) string {
var previousLine = ""
if currentIndex > 0 {
previousLine = inputLines[currentIndex-1]
}
return previousLine
}
func hasOnlyWipCommits(forthComingLines []string, configuration config.Configuration) bool {
var onlyWipCommits = true
for _, forthComingLine := range forthComingLines {
if isPick(forthComingLine) && isManualCommit(forthComingLine, configuration) {
onlyWipCommits = false
}
}
return onlyWipCommits
}
func markSquash(line string) string {
return strings.Replace(line, "pick ", "squash ", 1)
}
func markFixup(line string) string {
return strings.Replace(line, "pick ", "fixup ", 1)
}
func markDrop(line string) string {
return strings.Replace(line, "pick ", "drop ", 1)
}
func isWipCommitLine(line string, configuration config.Configuration) bool {
return isPick(line) && isWipCommit(line, configuration)
}
func isStartCISkipCommitLine(line string, configuration config.Configuration) bool {
return isPick(line) && isStartCISkipCommit(line, configuration)
}
func isManualCommit(line string, configuration config.Configuration) bool {
return !isWipCommit(line, configuration) && !isStartCISkipCommit(line, configuration)
}
func isWipCommit(line string, configuration config.Configuration) bool {
return strings.Contains(line, configuration.WipCommitMessage)
}
func isStartCISkipCommit(line string, configuration config.Configuration) bool {
return strings.Contains(line, configuration.StartCommitMessage)
}
func isPick(line string) bool {
return strings.HasPrefix(line, "pick ")
}