summaryrefslogtreecommitdiff
path: root/doClean.go
blob: acc510ae294dd737ac474c828bb90aafa7b36755 (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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package main

import (
	"fmt"
	"path/filepath"
	"slices"

	"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 repo.IsDirty() {
			continue
		}
		if err := doCleanRepo(repo); err != nil {
			log.Info(repo.GetGoPath(), err)
			okExit("")
		}
	}
	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
		}
		if name == repo.GetUserBranchName() {
			hasLocal = true
			if err := doCleanUserBranch(repo, b); err != nil {
				log.Info("\tLOCAL BRANCH ERROR user =", name)
				return err
			}
			log.Info("\tLOCAL BRANCH user =", name)
			continue
		}
		if name == repo.GetDevelBranchName() {
			/*
				if err := doCleanDevelBranch(repo, b); err != nil {
					log.Info("\tLOCAL BRANCH ERROR devel")
					return err
				}
				log.Info("\tLOCAL BRANCH devel")
			*/
			continue
		}
		if err := verifyLocalBranchIsMerged(repo, b); err != nil {
			return err
		}
	}
	if argv.Clean.Force == nil {
		if hasLocal {
			return ErrorReposHasLocalBranches
		}
	}
	return nil
}

func verifyLocalBranchIsMerged(repo *gitpb.Repo, branch *gitpb.GitBranch) error {
	base := filepath.Base(branch.Name)
	log.Info("local branch name unknown:", base, branch.Name, branch.Merge, branch.Remote, repo.GetGoPath())

	// check if it exists in the origin
	if repo.Exists(filepath.Join(".git/refs/remotes/origin", base)) {
		err := fmt.Errorf("repo %s ERROR. branch is also remote %s", repo.GetGoPath(), branch.Name)
		log.Info(err)
		if err := isSafeToDelete(repo, base); err != nil {
			log.Info(err)
			return err
		}
		if err := requiresGitPush(repo, base); err != nil {
			log.Info(err)
			return err
		}
		err = fmt.Errorf("repo %s BRANCH AND REMOTE CAN BE DELETED %s", repo.GetGoPath(), branch.Name)
		log.Info(err)
		if argv.Clean.Force != nil {
			err = forceDeleteBranch(repo, base)
			repo.Reload()
		}
		return err
	}

	if !repo.Exists(filepath.Join(".git/refs/heads", base)) {
		log.Info("GitConfig() parse logic error. not really a local branch", base)
		me.forge.Repos.Delete(repo)
		configSave = true
		return nil
	}

	// this is only a local branch
	if err := isSafeToDelete(repo, base); err != nil {
		log.Info(err)
		return err
	}
	err := fmt.Errorf("repo %s BRANCH CAN PROBABLY BE DELETED base=%s fullname=%s", repo.GetGoPath(), base, branch.Name)
	log.Info(err)
	if argv.Clean.Force != nil {
		err = forceDeleteBranch(repo, base)
		repo.Reload()
	}
	return err
}

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)
	}
	/*
		if bran == repo.GetUserBranchName() {
			log.Info("The user branch also has a remote branch", repo.CurrentTag.Refname)
			log.Info("TODO: verify the remote branch is out of date", repo.CurrentTag.Refname)
			log.Info("TODO: delete the remote branch", repo.CurrentTag.Refname)
			return nil
		}
	*/

	if !repo.Exists(filepath.Join(".git/refs/heads", branch.Name)) {
		err := fmt.Errorf("BAD FORGE LOGIC. DELETE REPO %s", repo.GetGoPath())
		log.Warn(err)
		me.forge.Repos.Repos = slices.DeleteFunc(me.forge.Repos.Repos, func(r *gitpb.Repo) bool {
			if repo.GetGoPath() == r.GetGoPath() {
				return true
			}
			return false
		})

		// me.forge.Repos.Delete(repo)
		me.forge.ConfigSave()
		// os.Exit(0)
		return nil
	}

	if repo.Exists(filepath.Join(".git/refs/remotes/origin", branch.Name)) {
		log.Info("why is this non-localonly branch a problem?", branch.Name)
		repo.RunVerbose([]string{"ls", "-l", ".git/refs/remotes/origin"})
		repo.RunVerbose([]string{"cat", filepath.Join(".git/refs/remotes/origin", branch.Name)})
		repo.RunVerbose([]string{"cat", filepath.Join(".git/refs/heads", branch.Name)})
		if err := userToDevelRequiresGitPush(repo, branch.Name); err != nil {
			log.Info(err)
			log.Info("THIS USER BRANCH MUST BE PUSHED TO DEVEL", branch.Name)
			return err
		}
		log.Info("THIS USER BRANCH IS CLEAN TO DELETE", branch.Name)
		if argv.Clean.Force != nil {
			err := forceDeleteBranch(repo, branch.Name)
			repo.Reload()
			if err != nil {
				return err
			}
		}
	} else {
		log.Info("why is this local only branch a problem?", branch.Name)
		repo.RunVerbose([]string{"ls", "-l", ".git/refs/remotes/origin"})
		r, err := repo.RunVerbose([]string{"cat", filepath.Join(".git/refs/heads", branch.Name)})
		if err == nil {
			cmd := []string{"git", "show", "-s", "--format=\"%H %ae %as %s\"", r.Stdout[0]}
			repo.RunVerbose(cmd)
			log.Info(cmd)
		}
		if err := userToDevelRequiresGitPush(repo, branch.Name); err != nil {
			log.Info(err)
			log.Info("THIS USER BRANCH MUST BE PUSHED TO DEVEL", branch.Name)
			return err
		}
		log.Info("THIS USER BRANCH IS CLEAN TO DELETE", branch.Name)
		if argv.Clean.Force != nil {
			cmd := []string{"git", "branch", "-D", branch.Name}
			if _, err := repo.RunVerbose(cmd); err != nil {
				log.Info("THE GIT BRANCH DELETE ERROR IS:", err)
				return err
			}
			return nil
		}
	}
	// return fmt.Errorf("%s repo.CurrentTag is not local: %s. Don't proceed yet", repo.GetGoPath(), repo.CurrentTag.Refname)
	log.Printf("Do something %s on branch name:%s merge:%s remote:%s\n", repo.GetGoPath(), branch.Name, branch.Merge, branch.Remote)
	return nil
}

func userToDevelRequiresGitPush(repo *gitpb.Repo, branchName string) error {
	devel := repo.GetDevelBranchName()
	missing := countDiffObjects(repo, branchName, "origin/"+devel)
	b2 := countDiffObjects(repo, "origin/"+devel, branchName)
	log.Info("user vs devel count", missing, b2)
	if missing == 0 && b2 == 0 {
		return nil
	}
	if missing != 0 {
		log.Info("user vs devel count missing != 0, b2 ==", missing, b2)
		log.Info("THIS MEANS THE LOCAL BRANCH NEEDS GIT PUSH TO ORIGIN BRANCH ==", missing)
		// if argv.Examine.Fix != nil {
		// 	return gitPushStrict(repo, branchName)
		// }
		return fmt.Errorf("user branch not clean to delete %d %d", missing, b2)
	}
	if missing == 0 {
		// log.Info("THIS MEANS THE LOCAL BRANCH IS OK TO DELETE missing =", missing)
		log.Info("THIS IS REALLY BAD RIGHT NOW. must to git push / git merge missing =", missing, b2)
	}
	return fmt.Errorf("user branch not clean to delete. maybe it is? devel might be ahead of user branch. %d %d", missing, b2)
}

// checks against upstream master
func isSafeToDelete(repo *gitpb.Repo, old string) error {
	var head string
	head = filepath.Join("origin", repo.GetMasterBranchName())

	if !repo.Exists(filepath.Join(".git/refs/remotes/", head)) {
		head = filepath.Join("origin", "HEAD")
	}

	b1 := countDiffObjects(repo, old, head)
	b2 := countDiffObjects(repo, head, old)
	log.Info(old, "vs origin count", b1, b2)
	if b1 == 0 && b2 == 0 {
		log.Info("isSafeToDelete() SAFE TO DELETE ==", old, b1, head, b2)
		return nil
	}
	if b1 == 0 {
		log.Info("isSafeToDelete() SAFE TO DELETE ==", old, b1, head, b2)
		return nil
	}
	if b1 != 0 {
		log.Info(old, "vs", head, " count b1 != 0, b2 ==", b2, "b1 =", b1)
		log.Info("THIS MEANS THE LOCAL BRANCH NEEDS GIT PUSH TO ORIGIN BRANCH ==", b1)
		cmd := repo.ConstructGitDiffLog(old, head)
		log.Info("cmd", cmd)
		cmd = repo.ConstructGitDiffLog(head, old)
		log.Info("cmd", cmd)
		if argv.Clean.Force != nil {
			// return gitPushStrict(repo, branchName)
		}
		return fmt.Errorf("user branch not clean to delete. needs git push %d %d", b1, b2)
	}
	return fmt.Errorf("user branch not clean to delete. needs git push %d %d", b1, b2)
}

// literally ignore all errors. delete everthing with no checks for now
func forceDeleteBranch(repo *gitpb.Repo, branch string) error {
	if repo.IsDirty() {
		log.Info(repo.GetGoPath(), "is dirty")
		return nil
	}
	if repo.GetUserBranchName() != branch {
		log.Info(repo.GetGoPath(), branch, "is not the user branch", repo.GetUserBranchName())
		return nil
	}
	configSave = true

	cmd := []string{"git", "branch", "-D", branch}
	if _, err := repo.RunVerbose(cmd); err != nil {
		log.Info("THE GIT BRANCH DELETE ERROR IS:", err)
		// return err
	}
	log.Info("THIS USER REMOTE BRANCH MUST BE DELETED HERE", branch)
	// git push origin --delete jcarr
	cmd = []string{"git", "push", "origin", "--delete", branch}
	if _, err := repo.RunVerbose(cmd); err != nil {
		log.Info("THE GIT BRANCH DELETE ERROR IS:", err)
		// return err
	}
	cmd = []string{"git", "branch", "-D", "--remote", "origin/" + branch}
	if _, err := repo.RunVerbose(cmd); err != nil {
		log.Info("THE GIT BRANCH DELETE ERROR IS:", err)
		// return err
	}
	// return fmt.Errorf("one at a time %s", repo.GetGoPath())
	return nil
}