summaryrefslogtreecommitdiff
path: root/doClean.go
blob: ca04c9e8c514e744013a1efcb46d3ea6b84da68d (plain)
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
package main

import (
	"fmt"

	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

var ErrorReposHasLocalBranches error = fmt.Errorf("repo still has local branches")

func doClean() error {
	if err := IsEverythingOnMaster(); err != nil {
		log.Info("Not all repos are on the master branch")
		// return err
	}

	all := me.forge.Repos.SortByFullPath()
	for all.Scan() {
		repo := all.Next()
		if repo.GetCurrentBranchName() != repo.GetMasterBranchName() {
			// skip this while in devel
			// continue
		}
		if err := doCleanRepo(repo); err != nil {
			badRepoExit(repo, err)
		}
	}
	log.Info("All repos on the master branch are clean")
	return nil
}

// removes all local branches
func doCleanRepo(repo *gitpb.Repo) error {
	var hasLocal bool
	if argv.Verbose {
		log.Info("Cleaning:", repo.GetGoPath())
	}
	if repo.GitConfig == nil {
		return fmt.Errorf("GitConfig == nil")
	}

	for _, l := range repo.GitConfig.Local {
		log.Info("\tlocal branch name:", l.Name)
	}

	for name, b := range repo.GitConfig.Branches {
		if b.Name == "" {
			b.Name = name
		}
		if name == repo.GetMasterBranchName() {
			// never delete the master branch
			// todo: make sure the master branch is in sync with remote master
			continue
		}
		hasLocal = true
		if name == repo.GetUserBranchName() {
			if err := doCleanUserBranch(repo, b); err != nil {
				return err
			}
			continue
		}
		if name == repo.GetDevelBranchName() {
			if err := doCleanDevelBranch(repo, b); err != nil {
				return err
			}
			continue
		}
		log.Info("\tlocal branch name unknown:", name, b.Merge, b.Remote)
	}
	if hasLocal {
		return ErrorReposHasLocalBranches
	}
	return nil
}

func verifyLocalBranchIsMerged(repo *gitpb.Repo, branch *gitpb.GitBranch) error {
	return nil
}

func doCleanDevelBranch(repo *gitpb.Repo, branch *gitpb.GitBranch) error {
	log.Printf("\tDo something %s on branch name:%s merge:%s remote:%s\n", repo.GetGoPath(), branch.Name, branch.Merge, branch.Remote)
	return nil
}

func doCleanUserBranch(repo *gitpb.Repo, branch *gitpb.GitBranch) error {
	if branch.Name != repo.GetUserBranchName() {
		return fmt.Errorf("repo %s was not user branch %s", repo.GetGoPath(), branch.Name)
	}
	log.Printf("\tDo something %s on branch name:%s merge:%s remote:%s\n", repo.GetGoPath(), branch.Name, branch.Merge, branch.Remote)
	return nil
}