-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcleanup.go
262 lines (217 loc) · 6.44 KB
/
cleanup.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Package main provides the cleanup executable and its implementation.
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os/exec"
"strings"
)
const (
gitDir string = ".git"
searchExpr string = ": gone]"
)
// RepositoryPath describes the filesystem path for a repository.
type RepositoryPath string
// BranchesOptions are user-defined options for the `branches` command
// which will be instantiated and populated by the CLI.
type BranchesOptions struct {
HasMultipleRepos bool
Force bool
DryRun bool
Exclude string
Where string
AndWhere string
}
// VersionOptions are user-defined options for the `version` command
// which will be instantiated and populated by the CLI.
type VersionOptions struct {
Quiet bool
}
// RunBranches is the entry point for the `branch` command and deletes
// all gone Git branches under a specified path. This path either has
// to be a repository or a root directory that contains multiple repos.
//
// Returns an error if no Git repository can be found in the path.
func RunBranches(path string, options *BranchesOptions, w io.Writer) error {
repositories, err := repositoryPaths(path, options.HasMultipleRepos)
if err != nil {
return err
}
if len(repositories) == 0 {
return errors.New("no Git repository found")
}
exclude := strings.Split(options.Exclude, ",")
exclude = append(exclude, "master")
for _, repo := range repositories {
deleted, err := deleteBranches(repo, options, exclude)
if err != nil {
output := fmt.Sprintf("Error in repository %s: %s\n", repo, err.Error())
_, _ = w.Write([]byte(output))
continue
}
if len(deleted) == 0 {
output := fmt.Sprintf("No gone branches found in repository %s.\n", repo)
_, _ = w.Write([]byte(output))
continue
}
output := fmt.Sprintf("Found gone branches in repository %s:\n", repo)
_, _ = w.Write([]byte(output))
for branch, err := range deleted {
var output string
switch {
case options.DryRun:
output = fmt.Sprintf("\t- Will delete branch %s\n", branch)
case err != nil:
output = fmt.Sprintf("\t- Failed to delete branch %s: %s\n", branch, err.Error())
default:
output = fmt.Sprintf("\t- Deleted branch %s\n", branch)
}
_, _ = w.Write([]byte(output))
}
}
return nil
}
// Version displays version information for cleanup.
func Version(options *VersionOptions, w io.Writer) error {
var output string
switch {
case options.Quiet:
output = fmt.Sprintf("%s\n", version)
default:
output = fmt.Sprintf("cleanup version %s (%s)\n", version, repoURL)
}
_, _ = w.Write([]byte(output))
return nil
}
// deleteBranches deletes branches in a repository that are considered
// gone. For determining these branches, `git branch -vv` will be used.
//
// Returns a map of deleted branch names mapped against an error value.
// If the error value is not nil, the corresponding branch couldn't be
// deleted successfully. The second return value indicates if an error
// occurred when running the `git branch -vv` command.
func deleteBranches(path RepositoryPath, options *BranchesOptions, exclude []string) (map[string]error, error) {
cmd := exec.Command("git", "branch", "-vv")
cmd.Dir = string(path)
out, err := cmd.Output()
if err != nil {
return nil, err
}
branches := readBranchNames(out, func(line string) bool {
if options.Where != "" {
return strings.Contains(line, options.Where)
}
if options.AndWhere != "" {
return strings.Contains(line, searchExpr) && strings.Contains(line, options.AndWhere)
}
return strings.Contains(line, searchExpr)
})
deleted := make(map[string]error)
for _, branch := range branches {
if isExcluded(branch, exclude) {
continue
}
if !options.DryRun {
cmd := exec.Command("git", "branch", "-d", branch)
cmd.Dir = string(path)
_, err := cmd.Output()
deleted[branch] = err
} else {
deleted[branch] = nil
}
}
return deleted, nil
}
// readBranchNames reads Git branch names contained in a byte slice,
// which is expected to be the output of `git branch -vv`. Each line
// is tested against a filter function and will only be processed if
// it passes that filter.
//
// The Git output is expected to look like this:
//
// * master 34a234a [origin/master] Merged some features
// feature/1 34a234a [origin/feature/1: gone] Implemented endpoints
// feature/2 3fc2e37 [origin/feature/2: behind 71] Added CLI command
//
// Returns a list of branch names that appeared in the byte sequence.
func readBranchNames(buf []byte, filter func(string) bool) []string {
branches := make([]string, 0)
scanner := bufio.NewScanner(bytes.NewBuffer(buf))
for scanner.Scan() {
line := scanner.Text()
if !filter(line) {
continue
}
for i, c := range line {
if i > 1 && string(c) == " " {
branches = append(branches, line[2:i])
break
}
}
}
return branches
}
// repositoryPaths returns all repository paths contained in a path.
//
// If the provided path itself is a repository, it will be included in
// the returned path slice. If the hasMultipleRepos flag is `true`, all
// direct parent directories that are Git repositories will be returned.
func repositoryPaths(path string, hasMultipleRepos bool) ([]RepositoryPath, error) {
paths := make([]RepositoryPath, 0)
isRepo, err := isRepository(RepositoryPath(path))
if err != nil {
return nil, err
}
if isRepo {
paths = append(paths, RepositoryPath(path))
}
if !hasMultipleRepos {
return paths, nil
}
content, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
for _, repo := range content {
if !repo.IsDir() {
continue
}
currentPath := RepositoryPath(path + "/" + repo.Name())
isRepo, err := isRepository(currentPath)
if err != nil {
return nil, err
}
if isRepo {
paths = append(paths, currentPath)
}
}
return paths, nil
}
// isRepository checks if a given path is a Git repository, meaning that
// it contains a `.git` directory.
func isRepository(path RepositoryPath) (bool, error) {
content, err := ioutil.ReadDir(string(path))
if err != nil {
return false, err
}
for _, item := range content {
if item.IsDir() && item.Name() == gitDir {
return true, nil
}
}
return false, nil
}
// isExcluded checks if a branch is contained in an slice of excluded
// branches. Whitespaces will be skipped when comparing the branches.
func isExcluded(branch string, exclude []string) bool {
for _, e := range exclude {
if branch == strings.Trim(e, " ") {
return true
}
}
return false
}