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
|
package gitpb
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"go.wit.com/log"
)
// does processing on the go.mod and go.sum files
func (repo *Repo) updateConfig() error {
if repo == nil {
return fmt.Errorf("gitpb.updateConfig() repo == nil")
}
if repo.Config == nil {
repo.Config = new(GitConfig)
}
repo.Config.Core = make(map[string]string)
repo.Config.Remotes = make(map[string]*GitRemote)
repo.Config.Branches = make(map[string]*GitBranch)
repo.Config.Submodules = make(map[string]string)
repo.Config.Versions = make(map[string]string)
repo.Config.Hashes = make(map[string]string)
url, err := repo.readConfig()
if repo.URL == "" {
repo.URL = url
}
if repo.URL != url {
log.Info("gitpb: updated URL:", url, repo.URL)
repo.URL = url
}
if url == "" {
log.Info(repo.FullPath, "url was blank. warn user this repo is only on the local disk")
}
return err
}
// readConfig reads and parses the .git/config file
func (repo *Repo) readConfig() (string, error) {
var foundURL string
filename := filepath.Join(repo.GetFullPath(), ".git/config")
file, err := os.Open(filename)
defer file.Close()
if err != nil {
return "", err
}
var currentSection string = ""
var currentName string = ""
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
// Skip empty lines and comments
if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, ";") {
continue
}
// Check for section headers
if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
line = strings.Trim(line, "[]")
parts := strings.Split(line, " ")
currentSection = parts[0]
if len(parts) == 2 {
line = strings.Trim(line, "[]")
currentName = strings.Trim(parts[1], "\"")
}
continue
}
partsNew := strings.SplitN(line, "=", 2)
if len(partsNew) != 2 {
log.Log(WARN, "error on config section:", currentSection, "line:", line)
}
key := strings.TrimSpace(partsNew[0])
key = strings.TrimSuffix(key, "\"")
value := strings.TrimSpace(partsNew[1])
value = strings.TrimSuffix(value, "\"")
switch currentSection {
case "core":
repo.Config.Core[key] = value
case "gui":
// don't really need gui stuff right now
case "pull":
// don't store git config pull settings here
// git config probably has 'rebase = false'
case "remote":
test, ok := repo.Config.Remotes[currentName]
if !ok {
test = new(GitRemote)
repo.Config.Remotes[currentName] = test
}
log.Log(INFO, "switch currentSection", currentSection, currentName)
switch key {
case "url":
if foundURL == "" {
foundURL = value
} else {
if foundURL != value {
log.Info("TODO: gitpb: handle multiple remotes in the parser", foundURL, value)
}
}
if test.Url == value {
continue
}
if test.Url == "" {
test.Url = value
continue
}
log.Log(INFO, "error url mismatch", test.Url, value)
case "fetch":
if test.Fetch == value {
continue
}
if test.Fetch == "" {
test.Fetch = value
continue
}
log.Log(INFO, "error fetch mismatch", test.Fetch, value)
default:
log.Log(INFO, "unknown remote:", line)
}
case "branch":
test, ok := repo.Config.Branches[currentName]
if !ok {
test = new(GitBranch)
repo.Config.Branches[currentName] = test
repo.processBranch(currentName)
}
switch key {
case "remote":
repo.Config.Branches[currentName].Remote = value
case "merge":
repo.Config.Branches[currentName].Merge = value
default:
log.Log(INFO, "error unknown remote:", currentSection, currentName, key, value)
log.Info("git .config unknown branch:", line)
}
case "submodule":
// test, ok := rs.gitConfig.submodules[currentName]
switch key {
case "active":
// probably 'true' or 'false'
case "url":
repo.Config.Submodules[currentName] = value
default:
log.Log(WARN, "unknown submodule line:", line)
}
case "diff":
// test, ok := rs.gitConfig.submodules[currentName]
switch key {
case "renameLimit":
log.Log(INFO, "name:", line)
default:
log.Log(WARN, "unknown name line:", filename, line)
}
case "user":
// test, ok := rs.gitConfig.submodules[currentName]
switch key {
case "name":
log.Log(INFO, "name:", line)
case "email":
log.Log(INFO, "email:", line)
default:
log.Log(WARN, "unknown name line:", filename, line)
}
case "git-bug":
log.Log(WARN, "repo uses git-bug!", filename)
default:
log.Log(WARN, filename, "unknown line:", line)
}
}
if err := scanner.Err(); err != nil {
return "", err
}
return foundURL, nil
}
func (repo *Repo) processBranch(branch string) {
log.Log(INFO, " ", branch)
hash, ok := repo.Config.Hashes[branch]
filename := filepath.Join(repo.GetFullPath() + "/.git/refs/heads/" + branch)
log.Log(INFO, " hash: need to open", filename)
data, err := ioutil.ReadFile(filename)
if err != nil {
// need to get the hash some other way.
// log.Log(WARN, "hash: read failed", filename)
return
}
newhash := strings.TrimSpace(string(data))
log.Log(INFO, " hash:", newhash)
repo.Config.Hashes[branch] = newhash
if ok {
if hash != newhash {
log.Log(WARN, "hash changed", hash)
}
}
name, _ := repo.gitDescribeByHash(newhash)
repo.Config.Versions[newhash] = name
log.Log(INFO, " hash: version", name)
}
|