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
|
package gitpb
import (
"strings"
"time"
"go.wit.com/lib/config"
"go.wit.com/log"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
// sets a flag that the repos have changed
// used later by applications on exit to test if
// the protobuf needs to be written to disk
func reposChanged(b bool) {
config.SetChanged("repos", true)
}
// returns true based on os.Stat() only checks
// seems to kinda work ok. goal is to avoid os.Exec() here for speed
// this might be the 1 place where libgit2 would be a good idea
func (repo *Repo) HasChanged() bool {
return repo.DidRepoChange()
}
// does a fast check with os.Stat()
// if the mtimes changed, does a full repo.ReloadForce()
func (repo *Repo) ReloadCheck() error {
if !repo.DidRepoChange() {
return nil
}
reposChanged(true)
err := repo.ReloadForce()
if err != nil {
return err
}
return log.Errorf("gitpb.ReloadCheck() detected a change in the repo")
}
// TODO: clean this up more, but it is working now more or less
func (repo *Repo) ReloadForce() error {
reposChanged(true)
// sometimes, on new repos, if .git/HEAD does not exist
// defective git daemons or badly configured repos, 'git clone' can fail
// if so, 'git fetch origin' can repair the state
if !repo.Exists(".git/HEAD") {
cmd := []string{"git", "fetch", "origin"}
repo.RunVerbose(cmd)
cmd = []string{"git", "checkout", "main"} // todo: figure out main
repo.RunVerbose(cmd)
}
// log.Info("in reload", repo.FullPath)
repo.Tags = new(GitTags)
repo.reloadGitTags()
repo.GoDeps = new(GoDeps)
if repo.GoInfo == nil {
repo.GoInfo = new(GoInfo)
}
repo.ParseGoSum() // also sets GoPrimitive
repo.reloadVersions()
repo.setRepoType()
// this is probably a good place & time to store these
repo.reloadMtimes()
repo.CheckDirty()
repo.setRepoState()
if repo.GitConfig == nil {
if err := repo.updateGitConfig(); err != nil {
return err
}
}
// repo.VerifyRemoteAndLocalBranches(repo.GetDevelBranchName())
// repo.VerifyRemoteAndLocalBranches(repo.GetMasterBranchName())
// LastUpdate should always be the newest time
repo.Times.LastUpdate = timestamppb.New(time.Now())
repo.ValidateUTF8()
return nil
}
// used in "normal" mode check
func (repo *Repo) VerifyRemoteAndLocalBranches(bname string) bool {
if !repo.IsBranchRemote(bname) {
return true
}
lh := repo.GetLocalHash(bname)
rh := repo.GetRemoteHash(bname)
if lh == rh {
// log.Info(r.FullPath, "local devel == remote devel", lh, rh)
return true
} else {
log.Info(lh, rh, "local != remote", repo.FullPath, bname)
}
return false
}
func (repo *Repo) SetDevelBranchName(bname string) {
repo.DevelBranchName = bname
}
func (repo *Repo) SetUserBranchName(bname string) {
repo.UserBranchName = bname
}
// updates LastTag by age
func (repo *Repo) setLastTag() {
repo.LastTag = repo.FindLastTag()
}
func (repo *Repo) setCurrentBranchName() {
repo.CurrentBranchName = ""
r, err := repo.RunQuiet([]string{"git", "branch", "--show-current"})
output := strings.Join(r.Stdout, "\n")
if err != nil {
log.Log(WARN, "GetCurrentBranchName() not in a git repo?", err, repo.GetGoPath())
log.Log(WARN, "GetCurrentBranchName() output might have worked anyway:", output)
}
repo.CurrentBranchName = strings.TrimSpace(output)
}
// always spawns 'git' and always should spawn 'git'
func (repo *Repo) setCurrentBranchVersion() {
repo.CurrentBranchVersion = ""
if repo == nil {
log.Info("repo.GetCurrentBranchVersion() repo == nil")
return
}
r, err := repo.RunQuiet([]string{"git", "describe", "--tags"})
output := strings.Join(r.Stdout, "\n")
if err != nil {
// log.Log(WARN, "GetCurrentBranchVersion() not in a git repo?", err, repo.GetGoPath())
// log.Log(WARN, "GetCurrentBranchVersion() output might have worked anyway:", output)
repo.CurrentBranchVersion = "gitpb err"
return
}
repo.CurrentBranchVersion = strings.TrimSpace(output)
}
|