summaryrefslogtreecommitdiff
path: root/git.go
blob: f51efe96dc985e5e7cf7ffe72f181aecc9553d66 (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
package repostatus

import (
	"go.wit.com/log"
)

func (rs *RepoStatus) getCurrentBranchName() string {
	out := run(rs.repopath, "git", "branch --show-current")
	log.Warn("getCurrentBranchName() =", out)
	rs.currentBranch.Set(out)
	return out
}

func (rs *RepoStatus) getCurrentBranchVersion() string {
	out := run(rs.repopath, "git", "describe --tags")
	log.Warn("getCurrentBranchVersion()", out)
	rs.currentVersion.Set(out)
	return out
}

func (rs *RepoStatus) getLastTagVersion() string {
	out := run(rs.repopath, "git", "rev-list --tags --max-count=1")
	log.Warn("getLastTagVersion()", out)
	rs.lasttagrev = out

	lastreal := "describe --tags " + out
	// out = run(r.path, "git", "describe --tags c871d5ecf051a7dc4e3a77157cdbc0a457eb9ae1")
	out = run(rs.repopath, "git", lastreal)
	rs.lasttag.Set(out)
	rs.tagsDrop.Set(out)
	// rs.lastLabel.Set(out)
	return out
}

func (rs *RepoStatus) populateTags() {
	tmp := fullpath(rs.repopath + "/.git/refs/tags")
	log.Warn("populateTags() path =", tmp)
	for _, tag := range listFiles(tmp) {
		if rs.tags[tag] == "" {
			log.Warn("populateTags() Adding new tag", tag)
			rs.tagsDrop.Add(tag)
			rs.tags[tag] = "origin"
		}
	}
	// rs.tagsDrop.Set(rs.lasttagrev)
}

/*
.git/refs/remotes
.git/refs/remotes/github
.git/refs/remotes/github/devel
.git/refs/remotes/github/main
.git/refs/remotes/origin
.git/refs/remotes/origin/devel
.git/refs/remotes/origin/main
.git/refs/remotes/origin/jcarr
.git/refs/heads
*/

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

	all = heads

	all = append(all, remotes...)

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

func (rs *RepoStatus) checkDirty() bool {
	out := run(rs.repopath, "git", "diff-index HEAD")
	if out == "" {
		log.Warn("checkDirty() no", rs.repopath)
		rs.dirtyLabel.Set("no")
		return false
	} else {
		log.Warn("checkDirty() true", rs.repopath)
		rs.dirtyLabel.Set("dirty")
		return true
	}

}

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

	realname := rs.getCurrentBranchName()
	realversion := rs.getCurrentBranchVersion()
	log.Warn(rs.repopath, "realname =", realname, "realversion =", realversion)

	switch level {
	case "master":
		rs.masterBranch.Set(realversion)
	case "devel":
		rs.develBranch.Set(realversion)
	case "user":
		rs.jcarrBranch.Set(realversion)
	default:
	}
}