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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
package gitpb
import (
"fmt"
"os/user"
"path/filepath"
"go.wit.com/log"
)
func (repo *Repo) CheckoutMasterError() error {
bName := repo.GetMasterBranchName()
if err := repo.checkoutBranchError(bName); err != nil {
return err
}
return nil
}
// deprecate this
func (repo *Repo) CheckoutMaster() bool {
bName := repo.GetMasterBranchName()
if repo.checkoutBranch(bName) {
return true
}
return false
}
func (repo *Repo) CheckoutDevelError() error {
bName := repo.GetDevelBranchName()
if err := repo.checkoutBranchError(bName); err != nil {
return err
// switch ok
}
repo.DevelBranchName = bName
return nil
}
// deprecate this
func (repo *Repo) CheckoutDevel() bool {
bName := repo.GetDevelBranchName()
if repo.checkoutBranch(bName) {
repo.DevelBranchName = bName
return true
// switch ok
}
return false
}
func (repo *Repo) CheckoutUser() error {
bName := repo.GetUserBranchName()
if bName == "uerr" {
usr, _ := user.Current()
repo.SetUserBranchName(usr.Username)
bName = usr.Username
log.Info("gitpb CheckoutUser() somehow got user 'uerr'")
}
return repo.createUserBranch(bName)
/*
if err != nil {
log.Info("attempting checkout user error", repo.GetGoPath(), bName, err)
}
return err
*/
}
func (repo *Repo) BranchExists(bName string) bool {
// fixme after move to protobuf
return true
}
func (repo *Repo) checkoutBranchError(bName string) error {
if !repo.BranchExists(bName) {
return fmt.Errorf("no branch")
}
if bName == "" {
return fmt.Errorf("branch name was blank")
}
if repo.CheckDirty() {
log.Log(INFO, repo.GetFullPath(), "is dirty")
return fmt.Errorf("repo is dirty")
}
cmd := []string{"git", "checkout", bName}
r := repo.Run(cmd)
if r.Error != nil {
log.Log(INFO, "git checkout error:", r.Error)
return r.Error
}
// remove this if everything works
realname := repo.GetCurrentBranchName()
realversion := repo.GetCurrentBranchVersion()
log.Info("checkoutranchError()", repo.GetFullPath(), "realname =", realname, "realversion =", realversion)
/* this is deprecated now probably
if realname != bName {
log.Log(INFO, "git checkout failed", repo.GetFullPath(), bName, "!=", realname)
return fmt.Errorf("repo Reload() name match failure")
}
*/
return nil
}
func (repo *Repo) checkoutBranch(bName string) bool {
if !repo.BranchExists(bName) {
return false
}
if bName == "" {
return false
}
if repo.CheckDirty() {
log.Log(INFO, repo.GetFullPath(), "is dirty")
return false
}
cmd := []string{"git", "checkout", bName}
r := repo.Run(cmd)
if r.Error != nil {
log.Log(INFO, "git checkout error:", r.Error)
}
realname := repo.GetCurrentBranchName()
realversion := repo.GetCurrentBranchVersion()
log.Log(INFO, repo.GetFullPath(), "realname =", realname, "realversion =", realversion)
if realname != bName {
log.Log(INFO, "git checkout failed", repo.GetFullPath(), bName, "!=", realname)
return false
}
return true
}
// actually creates a local user branch
func (repo *Repo) createUserBranch(branch string) error {
if branch == "" {
// get the username here?
usr, err := user.Current()
if err != nil {
return err
}
branch = usr.Username
}
if repo.IsDirty() {
// never change repos on dirty branches
return fmt.Errorf("repo is dirty")
}
if repo.Exists(filepath.Join(".git/refs/heads", branch)) {
var err error
// there is already a local user branch
cmd := []string{"git", "checkout", branch}
if _, err = repo.RunVerboseOnError(cmd); err == nil {
return nil
}
log.Log(INFO, "git checkout error:", err)
}
if repo.Exists(filepath.Join(".git/refs/remote/origin", branch)) {
var err error
// there is a remote user branch
// todo: check other remotes
cmd := []string{"git", "checkout", branch}
if _, err = repo.RunVerboseOnError(cmd); err == nil {
return nil
}
log.Log(INFO, "git checkout error:", err)
}
if repo.GetCurrentBranchName() != repo.GetDevelBranchName() {
repo.CheckoutDevel()
}
repo.ReloadCheck()
if repo.GetCurrentBranchName() != repo.GetDevelBranchName() {
log.Info("create user branch will probably fail", repo.GetGoPath())
// TODO: FIX THIS
// return fmt.Errorf("repo must be on devel branch %s", repo.GetGoPath())
}
// create the branch from devel
cmd := []string{"git", "branch", branch}
if _, err := repo.RunVerboseOnError(cmd); err != nil {
return err
}
cmd = []string{"git", "checkout", branch}
if _, err := repo.RunVerboseOnError(cmd); err != nil {
return err
}
return nil
}
|