This repository was archived by the owner on Nov 23, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathsquash_wip.go
More file actions
161 lines (136 loc) · 4.96 KB
/
Copy pathsquash_wip.go
File metadata and controls
161 lines (136 loc) · 4.96 KB
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
package main
import (
"io"
"io/ioutil"
"os"
"strconv"
"strings"
)
type Replacer func(string) string
func squashWip(configuration Configuration) {
if !isMobProgramming(configuration) {
sayTodo("to start working together, use", configuration.mob("start"))
return
}
if endsWithWipCommit(configuration) {
sayError(`failed to squash wip commits
last commit must be a manual commit`)
sayEmptyLine()
sayTodo("create a manual commit with a commit message to fix this:", "git commit --allow-empty -m \"your message goes here\"")
return
}
currentBaseBranch, currentWipBranch := determineBranches(gitCurrentBranch(), gitBranches(), configuration)
mergeBase := silentgit("merge-base", currentWipBranch.String(), currentBaseBranch.String())
originalGitEditor, originalGitSequenceEditor := getEnvGitEditor()
setEnvGitEditor(
mobExecutable()+" squash-wip --git-editor",
mobExecutable()+" squash-wip --git-sequence-editor",
)
silentgit("rebase", "-i", "--keep-empty", mergeBase)
setEnvGitEditor(originalGitEditor, originalGitSequenceEditor)
sayInfo("the history of your '" + currentWipBranch.String() + "' branch has been rewritten to combine all wip commits with their following manual commits:")
sayEmptyLine()
sayLastCommitsWithMessage(currentBaseBranch.String(), currentWipBranch.String())
sayEmptyLine()
sayTodo("to finally put the changes into the base branch preserving the resulting commits, call:", configuration.mob("done --no-squash"))
}
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 {
sayInfo("wip branch '" + currentWipBranch + "' contains " + strconv.Itoa(len(lines)) + " commits. The last 10 were:")
lines = lines[:10]
}
output := strings.Join(lines, "\n")
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()
return "go run $(ls -1 " + wd + "/*.go | grep -v _test.go)"
} else {
return "mob"
}
}
func isTestEnvironment() bool {
return strings.HasSuffix(os.Args[0], ".test") ||
strings.HasSuffix(os.Args[0], "_test") ||
os.Args[1] == "-test.v"
}
// used for non-interactive fixing of commit messages of squashed commits
func squashWipGitEditor(fileName string, configuration 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 Configuration) {
replaceFileContents(fileName, func(input string) string {
return markPostWipCommitsForSquashing(input, configuration)
})
}
func replaceFileContents(fileName string, replacer Replacer) {
file, _ := os.OpenFile(fileName, os.O_RDWR, 0666)
input, err := ioutil.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 Configuration) string {
var result []string
for _, line := range strings.Split(input, "\n") {
if configuration.isWipCommitMessage(line) {
result = append(result, "# "+line)
} else {
result = append(result, line)
}
}
return strings.Join(result, "\n")
}
func endsWithWipCommit(configuration Configuration) bool {
return configuration.isWipCommitMessage(commitsOnCurrentBranch(configuration)[0])
}
func commitsOnCurrentBranch(configuration Configuration) []string {
currentBaseBranch, currentWipBranch := determineBranches(gitCurrentBranch(), gitBranches(), configuration)
commitsBaseWipBranch := currentBaseBranch.String() + ".." + currentWipBranch.String()
log := silentgit("--no-pager", "log", commitsBaseWipBranch, "--pretty=format:%s")
lines := strings.Split(log, "\n")
return lines
}
func markPostWipCommitsForSquashing(input string, configuration Configuration) string {
var result []string
var squashNext = false
for _, line := range strings.Split(input, "\n") {
if squashNext && isPick(line) {
result = append(result, markSquash(line))
} else {
result = append(result, line)
}
squashNext = isRebaseWipCommitLine(line, configuration)
}
return strings.Join(result, "\n")
}
func markSquash(line string) string {
return strings.Replace(line, "pick ", "squash ", 1)
}
func isRebaseWipCommitLine(line string, configuration Configuration) bool {
return isPick(line) && strings.HasSuffix(line, configuration.WipCommitMessage)
}
func isPick(line string) bool {
return strings.HasPrefix(line, "pick ")
}