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

import (
	"bufio"
	"os"
	"path/filepath"
	"strings"

	"go.wit.com/log"
)

// GitConfig represents the parsed .git/config data
// type GitConfig map[string]map[string]string

type remote struct {
	url   string
	fetch string
}

type branch struct {
	remote string
	merge  string
}

type GitConfig struct {
	core     map[string]string  // map[origin] = "https:/git.wit.org/gui/gadgets"
	remotes  map[string]*remote // map[origin] = "https:/git.wit.org/gui/gadgets"
	branches map[string]*branch // map[guimaster] = origin guimaster
}

type GoConfig map[string]string

func listGitDirectories() []string {
	var all []string
	homeDir, err := os.UserHomeDir()
	if err != nil {
		log.Log(WARN, "Error getting home directory:", err)
		return nil
	}

	srcDir := filepath.Join(homeDir, "go/src")

	err = filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			log.Log(WARN, "Error accessing path:", path, err)
			return nil
		}

		// Check if the current path is a directory and has a .git subdirectory
		if info.IsDir() && isGitDir(path) {
			all = append(all, path)
			// fmt.Println(path)
		}

		return nil
	})

	if err != nil {
		log.Log(WARN, "Error walking the path:", srcDir, err)
	}

	return all
}

// isGitDir checks if a .git directory exists inside the given directory
func isGitDir(dir string) bool {
	gitDir := filepath.Join(dir, ".git")
	info, err := os.Stat(gitDir)
	if os.IsNotExist(err) {
		return false
	}
	return info.IsDir()
}

// readGitConfig reads and parses the .git/config file
func readGitConfig(filePath string) (*GitConfig, error) {
	file, err := os.Open(filePath)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	var currentSection string = ""
	var currentName string = ""

	config := new(GitConfig)
	config.core = make(map[string]string)
	config.remotes = make(map[string]*remote)
	config.branches = make(map[string]*branch)

	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":
			config.core[key] = value
		case "remote":
			test, ok := config.remotes[currentName]
			if !ok {
				test = new(remote)
				config.remotes[currentName] = test
			}
			log.Log(INFO, "switch currentSection", currentSection, currentName)
			switch key {
			case "url":
				if test.url == value {
					continue
				}
				if test.url == "" {
					test.url = value
					continue
				}
				log.Log(WARN, "error url mismatch", test.url, value)
			case "fetch":
				if test.fetch == value {
					continue
				}
				if test.fetch == "" {
					test.fetch = value
					continue
				}
				log.Log(WARN, "error fetch mismatch", test.fetch, value)
			default:
				log.Log(WARN, "error unknown remote:", currentSection, currentName, "key", key, "value", value)
			}
		case "branch":
			test, ok := config.branches[currentName]
			if !ok {
				test = new(branch)
				config.branches[currentName] = test
			}
			switch key {
			case "remote":
				config.branches[currentName].remote = value
			case "merge":
				config.branches[currentName].merge = value
			default:
				log.Log(WARN, "error unknown remote:", currentSection, currentName, key, value)
			}
		default:
			log.Log(WARN, "error unknown currentSection", currentSection, "line:", line)
		}
	}

	if err := scanner.Err(); err != nil {
		return nil, err
	}

	return config, nil
}

// readGoMod reads and parses the go.sum file (TODO: do the go.mod file)
func (rs *RepoStatus) ReadGoMod() bool {
	tmp := filepath.Join(rs.realPath.String(), "go.sum")
	gomod, err := os.Open(tmp)
	if err != nil {
		log.Log(WARN, "missing go.mod", rs.realPath.String())
		rs.goConfig = nil
		return false
	}
	defer gomod.Close()

	tmp = filepath.Join(rs.realPath.String(), "go.sum")
	gosum, err := os.Open(tmp)
	if err != nil {
		log.Log(WARN, "missing go.sum", rs.realPath.String())
		rs.goConfig = nil
		return false
	}
	defer gosum.Close()

	var deps GoConfig
	deps = make(GoConfig)

	scanner := bufio.NewScanner(gosum)
	log.Log(INFO, "gosum:", tmp)
	for scanner.Scan() {
		line := strings.TrimSpace(scanner.Text())

		parts := strings.Split(line, " ")
		if len(parts) == 3 {
			godep := strings.TrimSpace(parts[0])
			version := strings.TrimSpace(parts[1])
			if strings.HasSuffix(version, "/go.mod") {
				version = strings.TrimSuffix(version, "/go.mod")
			}
			currentversion, ok := deps[godep]
			if ok {
				if currentversion != version {
					log.Log(WARN, "versions do not match!!!", deps[godep], version, currentversion)
				}
			} else {
				deps[godep] = version
				log.Log(INFO, "\t", godep, "=", version)
			}
		} else {
			log.Log(WARN, "\t INVALID:", parts)
		}
	}

	if err := scanner.Err(); err != nil {
		rs.goConfig = nil
		return false
	}

	rs.goConfig = deps
	return true
}

func ScanGoSrc() {
	log.Log(WARN, "Scanning all go.sum files")
	for path, rs := range windowMap {
		if rs.ReadGoMod() {
			// everything is ok
		} else {
			log.Log(WARN, "failed reading go.sum repo:", path)
		}
	}
}

func ScanGitConfig() {
	for i, path := range listGitDirectories() {
		filename := filepath.Join(path, ".git/config")
		_, err := readGitConfig(filename)
		if err != nil {
			log.Log(WARN, "repo =", i, path)
			log.Log(WARN, "Error reading .git/config:", err)
		}
	}
}