/
prune_branches.go
67 lines (58 loc) · 2.04 KB
/
prune_branches.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
package cmd
import (
"github.com/Originate/git-town/src/git"
"github.com/Originate/git-town/src/script"
"github.com/Originate/git-town/src/steps"
"github.com/Originate/git-town/src/util"
"github.com/spf13/cobra"
)
var pruneBranchesCommand = &cobra.Command{
Use: "prune-branches",
Short: "Deletes local branches whose tracking branch no longer exists",
Long: `Deletes local branches whose tracking branch no longer exists
Deletes branches whose tracking branch no longer exists from the local repository.
This usually means the branch was shipped or killed on another machine.`,
Run: func(cmd *cobra.Command, args []string) {
checkPruneBranchesPreconditions()
stepList := getPruneBranchesStepList()
runState := steps.NewRunState("prune-branches", stepList)
steps.Run(runState)
},
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
return util.FirstError(
git.ValidateIsRepository,
validateIsConfigured,
git.Config().ValidateIsOnline,
)
},
}
func checkPruneBranchesPreconditions() {
if git.HasRemote("origin") {
script.Fetch()
}
}
func getPruneBranchesStepList() (result steps.StepList) {
initialBranchName := git.GetCurrentBranchName()
for _, branchName := range git.GetLocalBranchesWithDeletedTrackingBranches() {
if initialBranchName == branchName {
result.Append(&steps.CheckoutBranchStep{BranchName: git.Config().GetMainBranch()})
}
parent := git.Config().GetParentBranch(branchName)
if parent != "" {
for _, child := range git.Config().GetChildBranches(branchName) {
result.Append(&steps.SetParentBranchStep{BranchName: child, ParentBranchName: parent})
}
result.Append(&steps.DeleteParentBranchStep{BranchName: branchName})
}
if git.Config().IsPerennialBranch(branchName) {
result.Append(&steps.RemoveFromPerennialBranches{BranchName: branchName})
}
result.Append(&steps.DeleteLocalBranchStep{BranchName: branchName})
}
result.Wrap(steps.WrapOptions{RunInGitRoot: false, StashOpenChanges: false})
return
}
func init() {
RootCmd.AddCommand(pruneBranchesCommand)
}