1 // This file is a part of 'git-purged' tool, https://thekondor.net
15 branches git_purged.Branches
19 func NewApp(git *git_purged.Git, log *logger.Logger) App {
22 branches : git_purged.NewBranches(*git),
27 func (self App) PrintPurgedBranches(pruneBefore bool) {
29 self.pruneRemoteOrigin()
32 goneBranchNames, err := self.branches.ListGone()
34 self.log.Fatalf("Failed to list purged branches: %v\n", err)
37 self.list(goneBranchNames)
40 func (self App) PrintAliveBranches(pruneBefore bool) {
42 self.pruneRemoteOrigin()
45 nonGoneBranchNames, err := self.branches.ListNonGone()
47 self.log.Fatalf("Failed to list non-purged branches: %v\n", err)
50 self.list(nonGoneBranchNames)
53 func (self App) list(branchNames []string) {
54 if 0 == len(branchNames) {
55 self.log.Printf("No branches to show. Didn't you forget to call 'git fetch' before?")
59 for _, name := range branchNames {
64 func (self App) pruneRemoteOrigin() {
65 if err := self.git.PruneRemoteOrigin(); nil != err {
66 self.log.Fatalf("Failed to prune remote origin: %v\n", err)
70 func NewGit(log *logger.Logger) *git_purged.Git{
71 git, err := git_purged.NewGit(git_purged.NewGitExternalCommand())
76 if git_purged.GitNotAvailableErr == err {
77 log.Fatal("Error: 'git' command is not available or misconfigured");
79 log.Fatalf("Error: 'git' command cannot be used (%v)\n", err)
86 log := logger.New(os.Stdout, "[git-purged] ", 0)
88 inverseFlag := flag.Bool("inverse", false, "Inverse output by printing alive branches only. Optional.")
89 skipPruneFlag := flag.Bool("skip-prune", false, "Skip prunning of remote's origin to calculate purged branches before listing. Optional.")
90 helpFlag := flag.Bool("help", false, "Show this help.")
94 fmt.Printf("git-purged - subcommand to list purged (already removed on a remote master) branches.\n")
95 fmt.Printf(" build: %s#%s (%s)\n\n", BuildDate, BuildGitCommit, BuildGitOrigin)
98 log.Fatalf("No action requested")
102 if !git.IsRepository() {
103 log.Fatalf("Current directory is not a valid working git tree")
106 app := NewApp(git, log)
108 app.PrintAliveBranches(*skipPruneFlag)
110 app.PrintPurgedBranches(*skipPruneFlag)