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

import (
	"strings"
	"unicode/utf8"

	"io/ioutil"
	"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:
	}
}

func (rs *RepoStatus) checkBranches() bool {
	var hashCheck string
	var perfect bool = true
	all := rs.getBranches()
	path := fullpath(rs.repopath + "/.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.Warn("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)
		_, _, output := runCmd(rs.repopath, cmd)
		// git show -s --format=%ci <hash> will give you the time
		// log.Warn(fullfile)
		if hash == hashCheck {
			log.Warn(hash, output, b)
		} else {
			log.Warn(hash, output, b, "NOT THE SAME")
			perfect = false
			parts := strings.Split(b, "/")
			log.Warn("git push", parts)
		}
	}
	return perfect
}