summaryrefslogtreecommitdiff
path: root/git.go
blob: abf58c0d0a85f69b25525ec35ad6bd075bf732d3 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
package repostatus

import (
	"errors"
	"fmt"
	"os/user"
	"strings"
	"time"
	"unicode/utf8"

	"io/ioutil"

	"go.wit.com/log"
)

func (rs *RepoStatus) GetCurrentBranchName() string {
	return rs.currentBranch.String()
}

func (rs *RepoStatus) GetCurrentBranchVersion() string {
	return rs.currentVersion.String()
}

func (rs *RepoStatus) LastGitPull() (time.Time, error) {
	return rs.mtime(".git/FETCH_HEAD")
}

func (rs *RepoStatus) Age() time.Duration {
	var t *Tag
	t = rs.NewestTag()

	if t != nil {
		log.Log(REPO, "newest tag:", t.date.String(), t.tag.String(), t.Name())
		return t.Age()
	}

	const gitLayout = "Mon Jan 2 15:04:05 2006 -0700"
	const madeuptime = "Mon Jun 3 15:04:05 2013 -0700"
	tagTime, _ := time.Parse(gitLayout, madeuptime)
	return time.Since(tagTime)
}

var ErrorMissingGitConfig error = errors.New("missing .git/config")
var ErrorGitPullOnLocal error = errors.New("git pull on local only branch")

func (rs *RepoStatus) GitPull() (string, error) {
	currentName := rs.GetCurrentBranchName()
	if rs.IsOnlyLocalTag(currentName) {
		return "", ErrorGitPullOnLocal
	}
	var cmd []string
	cmd = append(cmd, "git", "pull")
	err, output := rs.RunCmd(cmd)
	if err != nil {
		output = "git error_,,,_a_,,,_b_,,,c"
	}
	if err == nil {
		log.Log(REPOWARN, "git pull ran", rs.Path())
		log.Log(REPOWARN, "git pull output", output)
	} else {
		log.Log(REPOWARN, "git pull error", rs.Path(), err)
	}
	return output, err
}

/*
// this isn't right
func (rs *RepoStatus) LastTagAge() (time.Time, string) {
	return time.Now(), rs.lasttag.String()
}
*/

func (rs *RepoStatus) GetLastTagVersion() string {
	return rs.lasttag.String()
}

// stores the current branch name
func (rs *RepoStatus) checkCurrentBranchName() string {
	currentname := rs.currentBranch.String()
	out := run(rs.realPath.String(), "git", "branch --show-current")
	if currentname == out {
		// nothing changed
		return currentname
	}
	rs.currentBranch.SetValue(out)
	if currentname == "" {
		return out // don't note if there was nothing before
	}
	rs.NoteChange("current branch has changed from " + currentname + " to " + out)
	return out
}

func (rs *RepoStatus) gitDescribeByHash(hash string) (string, error) {
	if hash == "" {
		return "", errors.New("hash was blank")
	}
	err, out := rs.RunCmd([]string{"git", "describe", "--tags", "--always", hash})
	if err != nil {
		log.Warn("not in a git repo or bad hash?", err, rs.Path())
		return "", err
	}
	out = strings.TrimSpace(out)
	return out, err
}

func (rs *RepoStatus) gitDescribeByName(name string) (string, error) {
	name = strings.TrimSpace(name)

	if name == "" {
		// git will return the current tag
		err, out := rs.RunCmd([]string{"git", "describe", "--tags", "--always"})
		if err != nil {
			log.Warn("not in a git repo?", err, rs.Path())
			return "", err
		}
		out = strings.TrimSpace(out)
		return out, err
	}
	if !rs.LocalTagExists(name) {
		// tag does not exist
		return "", errors.New("git fatal: Not a valid object name")
	}
	cmd := []string{"git", "describe", "--tags", "--always", name}
	err, out := rs.RunCmd(cmd)
	if err != nil {
		log.Warn("cmd =", cmd)
		log.Warn("err =", err)
		log.Warn("not in a git repo or bad tag?", rs.Path())
		return "", err
	}
	out = strings.TrimSpace(out)
	return out, err
}

// todo: don't run git every time?
func (rs *RepoStatus) checkCurrentBranchVersion() string {
	out, _ := rs.gitDescribeByName("")
	log.Log(REPO, "checkCurrentBranchVersion()", out)
	rs.currentVersion.SetValue(out)
	return out
}

// this should get the most recent tag
func (rs *RepoStatus) setLastTagVersion() {
	hash := run(rs.realPath.String(), "git", "rev-list --tags --max-count=1")
	log.Log(REPO, "getLastTagVersion()", hash)

	name, _ := rs.gitDescribeByHash(hash)
	rs.lasttag.SetText(name)
	return
}

func (rs *RepoStatus) populateTags() {
	tmp := rs.realPath.String() + "/.git/refs/tags"
	log.Log(REPO, "populateTags() path =", tmp)
	for _, tag := range listFiles(tmp) {
		if rs.tags[tag] == "" {
			log.Log(REPO, "populateTags() Adding new tag", tag)
			// rs.tagsDrop.AddText(tag)
			rs.tags[tag] = "origin"
		}
	}
	// rs.tagsDrop.SetText(rs.lasttagrev)
}

func (rs *RepoStatus) getBranches() []string {
	var all []string
	var heads []string
	var remotes []string
	heads = listFiles(rs.realPath.String() + "/.git/refs/heads")
	remotes = listFiles(rs.realPath.String() + "/.git/refs/remotes")

	all = heads

	all = append(all, remotes...)

	for _, branch := range all {
		log.Log(REPO, "getBranches()", branch)
	}
	return all
}

// returns quickly based on the last time it was checked
func (rs *RepoStatus) IsDirty() bool {
	if rs.dirtyLabel.String() == "no" {
		return false
	}
	return true
}

func (rs *RepoStatus) CheckDirty() bool {
	var start string = rs.dirtyLabel.String()
	cmd := []string{"git", "status"}
	err, out := rs.RunCmd(cmd)
	if err != nil {
		log.Warn("CheckDirty() status cmd =", cmd)
		log.Warn("CheckDirty() status out =", out)
		log.Warn("CheckDirty() status err =", err)
		log.Error(err, "CheckDirty() git status error")
		rs.dirtyLabel.SetValue("error")
		if start != "error" {
			rs.NoteChange("git status is in error " + fmt.Sprint(err))
		}
		return true
	}

	last := out[strings.LastIndex(out, "\n")+1:]

	if last == "nothing to commit, working tree clean" {
		log.Log(REPO, "CheckDirty() no", rs.realPath.String())
		rs.dirtyLabel.SetValue("no")
		if start != "no" {
			log.Log(REPOWARN, "is no longer dirty")
			rs.NoteChange("is no longer dirty")
		}
		return false
	}

	rs.dirtyLabel.SetValue("dirty")
	if start != "dirty" {
		log.Log(REPOWARN, "is now dirty")
		rs.NoteChange("is now dirty")
	}
	return true

}
func (rs *RepoStatus) CheckoutBranch(bname string) bool {
	if rs.CheckDirty() {
		log.Log(REPO, rs.realPath.String(), "is dirty")
		log.Info(bname, "is dirty", rs.Path())
		return false
	}
	if !rs.TagExists(bname) {
		// tag does not exist
		log.Log(REPO, "repo does not have branch", bname, rs.Path())
		return false
	}
	cName := rs.GetCurrentBranchName()
	if cName == bname {
		// already on branch
		return true
	}
	cmd := []string{"git", "checkout", bname}
	err, b, output := RunCmd(rs.realPath.String(), cmd)
	if err != nil {
		log.Log(REPO, err, b, output)
		return false
	}
	rs.checkCurrentBranchName()
	rs.checkCurrentBranchVersion()
	return true
}

func (rs *RepoStatus) CheckoutMaster() bool {
	if rs.CheckDirty() {
		log.Log(REPO, rs.realPath.String(), "is dirty")
		return false
	}
	mName := rs.GetMasterBranchName()
	if rs.CheckoutBranch(mName) {
		return true
	}
	return true
}

func (rs *RepoStatus) CheckoutDevel() bool {
	devel := rs.develWorkingName.String()
	// user := rs.userWorkingName.String()
	if devel == "" {
		return false
	}
	if rs.CheckDirty() {
		log.Log(REPO, rs.realPath.String(), "is dirty")
		return false
	}

	log.Log(REPO, "checkoutBranch", devel)
	rs.checkoutBranch("devel", devel)
	// log.Log(REPO, "checkoutBranch", user)
	// rs.checkoutBranch("user", user)
	return true
}

func (rs *RepoStatus) CheckoutUser() bool {
	bName := rs.GetUserBranchName()
	if bName == "" {
		return false
	}
	if rs.CheckDirty() {
		log.Log(REPO, rs.realPath.String(), "is dirty")
		return false
	}
	cmd := []string{"git", "checkout", bName}
	err, b, output := RunCmd(rs.realPath.String(), cmd)
	if err != nil {
		log.Log(REPO, err, b, output)
	}

	realname := rs.GetCurrentBranchName()
	realversion := rs.GetCurrentBranchVersion()
	log.Log(REPO, rs.realPath.String(), "realname =", realname, "realversion =", realversion)

	if realname != bName {
		log.Log(REPO, "git checkout failed", rs.realPath.String(), bName, "!=", realname)
		return false
	}
	rs.userBranchVersion.SetValue(realversion)
	return true
}

func (rs *RepoStatus) checkoutBranch(level string, branch string) {
	if rs.CheckDirty() {
		log.Log(REPO, "checkoutBranch() checkDirty() == true for repo", rs.realPath.String(), "looking for branch:", branch)
		return
	}
	out := run(rs.realPath.String(), "git", "checkout "+branch)
	log.Log(REPO, rs.realPath.String(), "git checkout "+branch, "returned", out)

	realname := rs.GetCurrentBranchName()
	realversion := rs.GetCurrentBranchVersion()
	log.Log(REPO, rs.realPath.String(), "realname =", realname, "realversion =", realversion)

	switch level {
	case "master":
		rs.mainBranchVersion.SetValue(realversion)
	case "devel":
		rs.develBranchVersion.SetValue(realversion)
	case "user":
		rs.userBranchVersion.SetValue(realversion)
	default:
	}
}

// attempt's to guess at what master is.
// TODO: fix this properly
func (rs *RepoStatus) guessMainWorkingName() {
	if !rs.Ready() {
		return
	}
	if rs.TagExists("guimaster") {
		rs.mainWorkingName.SetText("guimaster")
		rs.mainBranchVersion.SetLabel("guimaster")
		return
	}
	if rs.TagExists("master") {
		rs.mainWorkingName.SetText("master")
		rs.mainBranchVersion.SetLabel("master")
		return
	}
	if rs.TagExists("main") {
		rs.mainWorkingName.SetText("main")
		rs.mainBranchVersion.SetLabel("main")
		return
	}

	// figure out what to do here
	rs.mainWorkingName.SetText("FIXME")
	rs.mainBranchVersion.SetLabel("FIXME")
}

func (rs *RepoStatus) guessDevelWorkingName() {
	if rs.TagExists("guidevel") {
		rs.develWorkingName.SetValue("guidevel")
		rs.develBranchVersion.SetLabel("guidevel")
		return
	}
	if rs.TagExists("devel") {
		rs.develWorkingName.SetValue("devel")
		rs.develBranchVersion.SetLabel("devel")
		return
	}

	// figure out what to do here
	rs.develWorkingName.SetValue("develFIXME")
	rs.develBranchVersion.SetLabel("develFIXME")
}

func (rs *RepoStatus) setUserWorkingName() {
	usr, _ := user.Current()
	uname := usr.Username
	if rs.TagExists(uname) {
		rs.userWorkingName.SetValue(uname)
		rs.userBranchVersion.SetLabel(uname)
		return
	}
	rs.userWorkingName.SetValue("need to create " + uname)
	rs.userBranchVersion.SetLabel("need to create " + uname)
}

// returns "master", "devel", os.Username, etc
func (rs *RepoStatus) GetMasterBranchName() string {
	name := rs.mainWorkingName.String()
	return name
}
func (rs *RepoStatus) GetDevelBranchName() string {
	name := rs.develWorkingName.String()
	return name
}

func (rs *RepoStatus) GetUserBranchName() string {
	name := rs.userWorkingName.String()
	return name
}

// returns the git versions like "1.3-2-laksdjf" or whatever
func (rs *RepoStatus) GetMasterVersion() string {
	name := rs.mainBranchVersion.String()
	return name
}
func (rs *RepoStatus) GetDevelVersion() string {
	name := rs.develBranchVersion.String()
	return name
}
func (rs *RepoStatus) GetUserVersion() string {
	name := rs.userBranchVersion.String()
	return name
}

func (rs *RepoStatus) setMasterVersion(s string) {
	old := rs.GetMasterVersion()
	if old == s {
		return
	}
	rs.mainBranchVersion.SetValue(s)
	if old == "" {
		return // don't note if there was nothing before
	}
	rs.NoteChange("master branch has been changed from " + old + " to " + s)
}

func (rs *RepoStatus) setDevelVersion(s string) {
	old := rs.GetDevelVersion()
	if old == s {
		return
	}
	if old == "" {
		// don't note nothing
	} else {
		rs.NoteChange("devel branch has been changed from " + old + " to " + s)
	}
	rs.develBranchVersion.SetValue(s)
}

func (rs *RepoStatus) setUserVersion(s string) {
	old := rs.GetUserVersion()
	if old == s {
		return
	}
	if old == "" {
		// don't note nothing
	} else {
		rs.NoteChange("user branch has been changed from " + old + " to " + s)
	}
	rs.userBranchVersion.SetValue(s)
}

func (rs *RepoStatus) GitState() string {
	return rs.gitState.String()
}

func (rs *RepoStatus) CheckGitState() string {
	rs.setState()
	return rs.gitState.String()
}

func (rs *RepoStatus) GetStatus() string {
	return rs.gitState.String()
}

func (rs *RepoStatus) setState() {
	rs.changed = false
	if rs.CheckDirty() {
		log.Log(REPO, "CheckDirty() true")
		rs.gitState.SetText("dirty")
		return
	}
	if rs.GetUserVersion() != rs.GetDevelVersion() {
		rs.gitState.SetText("merge to devel")
		return
	}
	if rs.GetDevelVersion() != rs.GetMasterVersion() {
		rs.gitState.SetText("merge to main")
		return
	}
	if rs.lasttag.String() != rs.GetMasterVersion() {
		rs.gitState.SetText("unchanged")
		return
	}

	if rs.CheckBranches() {
		log.Log(REPO, "Branches are Perfect")
		rs.gitState.SetText("PERFECT")
		return
	}
	log.Log(REPO, rs.String(), "Branches are not Perfect")
	rs.gitState.SetText("unknown branches")
}

// TODO: make this report the error somewhere
// This is supposed to check all the branches to make sure
// the are the same. that was originally what this was for
// now I think it's jsut probably dumb old code that doesn't
// need to be here

// actually, this is to attempt to verify absolutely everything
// is pushed upstream before doing a rm -rf ~/go/src
// TODO: revisit this code in the autotypist later
func (rs *RepoStatus) CheckBranches() bool {
	var hashCheck string
	var perfect bool = true
	all := rs.getBranches()
	path := rs.realPath.String() + "/.git/refs/"
	for _, b := range all {
		parts := strings.Split(b, "/")
		rdir := "heads"
		if len(parts) == 2 {
			rdir = "remotes"
		}
		fullfile := path + "/" + rdir + "/" + b

		// check if the ref name is "HEAD". if so, skip
		runeCount := utf8.RuneCountInString(fullfile)
		// Convert the string to a slice of runes
		runes := []rune(fullfile)
		// Slice the last 4 runes
		lastFour := runes[runeCount-4:]
		if string(lastFour) == "HEAD" {
			log.Log(REPO, "skip HEAD fullfile", fullfile)
			continue
		}

		content, _ := ioutil.ReadFile(fullfile)
		hash := strings.TrimSpace(string(content))
		if hashCheck == "" {
			hashCheck = hash
		}
		var cmd []string
		cmd = append(cmd, "git", "show", "-s", "--format=%ci", hash)
		err, output := rs.RunCmd(cmd)
		if err != nil {
			// log.Log(WARN, "cmd failed", cmd, "err =", err, "in", rs.String())
		}
		// git show -s --format=%ci <hash> will give you the time
		// log.Log(REPO, fullfile)
		if hash == hashCheck {
			log.Log(REPO, hash, output, b)
		} else {
			// log.Log(WARN, rs.String(), hash, output, b)
			// log.Log(WARN, "UNKNOWN BRANCHES IN THIS REPO", cmd)
			rs.versionCmdOutput.SetText("UNKNOWN BRANCHES")
			perfect = false
			// parts := strings.Split(b, "/")
			// log.Warn("git push", parts)
		}
	}
	return perfect
}