-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
73 lines (62 loc) · 1.7 KB
/
main.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
package main
import (
"fmt"
"os"
"github.com/spf13/pflag"
git "gopkg.in/src-d/go-git.v4"
)
var flagDryRun bool
func removeBranchFromConfig(repo *git.Repository, branch string) error {
config, err := repo.Config()
if err != nil {
return fmt.Errorf("couldn't get git config: %s", err)
}
newRawConfig := config.Raw.RemoveSubsection("branch", branch)
config.Raw = newRawConfig
return repo.Storer.SetConfig(config)
}
func openCurPathRepo() (*git.Repository, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("could not get current working directory: %s", err)
}
repo, err := git.PlainOpenWithOptions(cwd, &git.PlainOpenOptions{
DetectDotGit: true,
})
if err != nil {
return nil, fmt.Errorf("could not open git repository: %s", err)
}
return repo, nil
}
func errExit(err error) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
func main() {
pflag.BoolVarP(&flagDryRun, "dry-run", "d", false, "does not actually delete branches")
pflag.Parse()
repo, err := openCurPathRepo()
if err != nil {
errExit(err)
}
gerritC := NewGerritCleaner(repo)
gerritBranches, err := gerritC.MergedBranches()
if err != nil {
errExit(fmt.Errorf("getting merged gerrit branches failed: %s\n", err))
}
gitC := NewGitCleaner(repo)
gitBranches, err := gitC.MergedBranches()
if err != nil {
errExit(fmt.Errorf("getting merged git branches failed: %s\n", err))
}
branches := append(gerritBranches, gitBranches...)
for _, branchRef := range branches {
if flagDryRun {
fmt.Printf("would delete %q\n", branchRef.Name())
} else {
repo.Storer.RemoveReference(branchRef.Name())
removeBranchFromConfig(repo, branchRef.Name().Short())
fmt.Printf("deleted %q\n", branchRef.Name())
}
}
}