summaryrefslogtreecommitdiff
path: root/repoNew.go
blob: b87ee4cfab1e5fd7b11e0ee81a6714955a018ee1 (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
93
94
95
96
97
98
99
100
package forgepb

import (
	"os/user"

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

func (f *Forge) NewGoPathRepo(gopath string) (*gitpb.Repo, error) {
	repo, err := f.Repos.NewGoPath(f.GetGoSrc(), gopath, "")
	if err != nil {
		return nil, err
	}
	f.VerifyBranchNames(repo)
	repo.ParseGoSum()
	return repo, nil
}

func (f *Forge) VerifyBranchNames(newr *gitpb.Repo) {
	// log.Info("init worked for", newr.GoPath)

	if newr.GetMasterBranchName() == "" {
		// try to guess what the 'master' branch is
		if newr.IsBranch("guimaster") {
			newr.SetMasterBranchName("guimaster")
		} else if newr.IsBranch("master") {
			newr.SetMasterBranchName("master")
		} else if newr.IsBranch("main") {
			newr.SetMasterBranchName("main")
		} else {
			// todo, figure out the name from git
			newr.SetMasterBranchName("master")
			if newr.CheckoutMaster() {
			} else {
				cmd := []string{"git", "branch", "master"}
				newr.Run(cmd)
			}
		}
	}

	if f.Config.IsReadOnly(newr.GoPath) {
		return
	}

	if newr.GetDevelBranchName() == "" {
		if newr.IsBranch("guidevel") {
			newr.SetDevelBranchName("guidevel")
		} else if newr.IsBranch("devel") {
			newr.SetDevelBranchName("devel")
		} else {
			// forcing for now. todo: warn users
			newr.SetDevelBranchName("devel")
			if newr.CheckoutDevel() {
			} else {
				cmd := []string{"git", "branch", "devel"}
				newr.Run(cmd)
			}
		}
	}

	if newr.GetUserBranchName() == "" {
		usr, _ := user.Current()
		uname := usr.Username
		if newr.IsBranch(uname) {
			newr.SetUserBranchName(uname)
		} else {
			// forcing for now. todo: warn users
			newr.SetUserBranchName(uname)
			if newr.CheckoutUser() {
			} else {
				cmd := []string{"git", "branch", uname}
				newr.Run(cmd)
			}
		}
	}
}

// todo: check the forge config
func (f *Forge) configUserBranchName(repo *gitpb.Repo) string {
	if repo.GetUserBranchName() != "" {
		return repo.GetUserBranchName()
	}
	usr, _ := user.Current()
	uname := usr.Username
	return uname
}

// todo: check the forge config
func (f *Forge) configDevelBranchName(repo *gitpb.Repo) string {
	if repo.GetDevelBranchName() != "" {
		return repo.GetDevelBranchName()
	}
	if repo.IsBranch("guidevel") {
		return "guidevel"
	}
	if repo.IsBranch("devel") {
		return "devel"
	}
	return "devel"
}