2024-01-23 10:52:17 -06:00
|
|
|
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
|
2024-11-08 06:43:33 -06:00
|
|
|
// TODO: switch to protobuf
|
2024-01-23 10:52:17 -06:00
|
|
|
|
|
|
|
type remote struct {
|
|
|
|
url string
|
|
|
|
fetch string
|
|
|
|
}
|
|
|
|
|
|
|
|
type branch struct {
|
|
|
|
remote string
|
|
|
|
merge string
|
|
|
|
}
|
|
|
|
|
|
|
|
type GitConfig struct {
|
2024-03-09 16:50:18 -06:00
|
|
|
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
|
|
|
|
submodules map[string]string
|
|
|
|
hashes map[string]string
|
|
|
|
versions map[string]string
|
2024-01-23 10:52:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type GoConfig map[string]string
|
|
|
|
|
2024-01-25 01:30:01 -06:00
|
|
|
func ListGitDirectories() []string {
|
2024-01-23 10:52:17 -06:00
|
|
|
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
|
2024-03-01 21:45:06 -06:00
|
|
|
if info.IsDir() && IsGitDir(path) {
|
2024-01-23 10:52:17 -06:00
|
|
|
all = append(all, path)
|
|
|
|
// fmt.Println(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Log(WARN, "Error walking the path:", srcDir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return all
|
|
|
|
}
|
|
|
|
|
2024-03-01 21:45:06 -06:00
|
|
|
// IsGitDir checks if a .git directory exists inside the given directory
|
|
|
|
func IsGitDir(dir string) bool {
|
2024-01-23 10:52:17 -06:00
|
|
|
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
|
2024-01-23 22:47:39 -06:00
|
|
|
func (rs *RepoStatus) readGitConfig() error {
|
|
|
|
filename := filepath.Join(rs.realPath.String(), "/.git/config")
|
|
|
|
file, err := os.Open(filename)
|
2024-01-23 10:52:17 -06:00
|
|
|
if err != nil {
|
2024-02-16 11:41:29 -06:00
|
|
|
log.Log(WARN, "readGitConfig() failed for file:", filename)
|
2024-01-25 02:23:56 -06:00
|
|
|
filename = filepath.Join(rs.realPath.String(), "../.git/config")
|
|
|
|
log.Log(WARN, "readGitConfig() trying up one directory instead", filename)
|
|
|
|
file, err = os.Open(filename)
|
|
|
|
if err != nil {
|
2024-03-09 16:50:18 -06:00
|
|
|
return err
|
2024-01-25 02:23:56 -06:00
|
|
|
}
|
2024-01-23 10:52:17 -06:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
var currentSection string = ""
|
|
|
|
var currentName string = ""
|
|
|
|
|
2024-01-23 22:47:39 -06:00
|
|
|
rs.gitConfig = new(GitConfig)
|
|
|
|
rs.gitConfig.core = make(map[string]string)
|
|
|
|
rs.gitConfig.remotes = make(map[string]*remote)
|
|
|
|
rs.gitConfig.branches = make(map[string]*branch)
|
2024-03-09 16:50:18 -06:00
|
|
|
rs.gitConfig.submodules = make(map[string]string)
|
2024-01-23 22:47:39 -06:00
|
|
|
rs.gitConfig.versions = make(map[string]string)
|
|
|
|
rs.gitConfig.hashes = make(map[string]string)
|
2024-01-23 10:52:17 -06:00
|
|
|
|
|
|
|
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, "[]")
|
2024-01-23 15:20:54 -06:00
|
|
|
currentName = strings.Trim(parts[1], "\"")
|
2024-01-23 10:52:17 -06:00
|
|
|
}
|
|
|
|
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])
|
2024-01-23 15:20:54 -06:00
|
|
|
key = strings.TrimSuffix(key, "\"")
|
|
|
|
|
2024-01-23 10:52:17 -06:00
|
|
|
value := strings.TrimSpace(partsNew[1])
|
2024-01-23 15:20:54 -06:00
|
|
|
value = strings.TrimSuffix(value, "\"")
|
2024-01-23 10:52:17 -06:00
|
|
|
|
|
|
|
switch currentSection {
|
|
|
|
case "core":
|
2024-01-23 22:47:39 -06:00
|
|
|
rs.gitConfig.core[key] = value
|
2024-02-22 15:29:22 -06:00
|
|
|
case "pull":
|
|
|
|
// don't store git config pull settings here
|
|
|
|
// probably has 'rebase = false'
|
2024-01-23 10:52:17 -06:00
|
|
|
case "remote":
|
2024-01-23 22:47:39 -06:00
|
|
|
test, ok := rs.gitConfig.remotes[currentName]
|
2024-01-23 10:52:17 -06:00
|
|
|
if !ok {
|
|
|
|
test = new(remote)
|
2024-01-23 22:47:39 -06:00
|
|
|
rs.gitConfig.remotes[currentName] = test
|
2024-01-23 10:52:17 -06:00
|
|
|
}
|
|
|
|
log.Log(INFO, "switch currentSection", currentSection, currentName)
|
|
|
|
switch key {
|
|
|
|
case "url":
|
|
|
|
if test.url == value {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if test.url == "" {
|
|
|
|
test.url = value
|
|
|
|
continue
|
|
|
|
}
|
2024-03-09 16:50:18 -06:00
|
|
|
log.Log(REPO, "error url mismatch", test.url, value)
|
2024-01-23 10:52:17 -06:00
|
|
|
case "fetch":
|
|
|
|
if test.fetch == value {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if test.fetch == "" {
|
|
|
|
test.fetch = value
|
|
|
|
continue
|
|
|
|
}
|
2024-03-09 16:50:18 -06:00
|
|
|
log.Log(REPO, "error fetch mismatch", test.fetch, value)
|
2024-01-23 10:52:17 -06:00
|
|
|
default:
|
2024-03-09 16:50:18 -06:00
|
|
|
log.Log(REPO, "unknown remote:", rs.Path(), line)
|
2024-01-23 10:52:17 -06:00
|
|
|
}
|
|
|
|
case "branch":
|
2024-01-23 22:47:39 -06:00
|
|
|
test, ok := rs.gitConfig.branches[currentName]
|
2024-01-23 10:52:17 -06:00
|
|
|
if !ok {
|
|
|
|
test = new(branch)
|
2024-01-23 22:47:39 -06:00
|
|
|
rs.gitConfig.branches[currentName] = test
|
|
|
|
rs.processBranch(currentName)
|
2024-01-23 10:52:17 -06:00
|
|
|
}
|
|
|
|
switch key {
|
|
|
|
case "remote":
|
2024-01-23 22:47:39 -06:00
|
|
|
rs.gitConfig.branches[currentName].remote = value
|
2024-01-23 10:52:17 -06:00
|
|
|
case "merge":
|
2024-01-23 22:47:39 -06:00
|
|
|
rs.gitConfig.branches[currentName].merge = value
|
2024-01-23 10:52:17 -06:00
|
|
|
default:
|
2024-03-09 16:50:18 -06:00
|
|
|
log.Log(REPO, "error unknown remote:", currentSection, currentName, key, value)
|
|
|
|
log.Log(REPO, "unknown branch:", rs.Path(), line)
|
|
|
|
}
|
|
|
|
case "submodule":
|
|
|
|
// test, ok := rs.gitConfig.submodules[currentName]
|
|
|
|
switch key {
|
|
|
|
case "active":
|
|
|
|
// probably 'true' or 'false'
|
|
|
|
case "url":
|
|
|
|
rs.gitConfig.submodules[currentName] = value
|
|
|
|
default:
|
|
|
|
log.Log(REPOWARN, "unknown submodule line:", rs.Path(), line)
|
2024-01-23 10:52:17 -06:00
|
|
|
}
|
|
|
|
default:
|
2024-03-09 16:50:18 -06:00
|
|
|
log.Log(REPOWARN, "unknown line:", rs.Path(), line)
|
2024-01-23 10:52:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
2024-01-23 22:47:39 -06:00
|
|
|
return err
|
2024-01-23 10:52:17 -06:00
|
|
|
}
|
|
|
|
|
2024-01-23 22:47:39 -06:00
|
|
|
return nil
|
2024-01-23 10:52:17 -06:00
|
|
|
}
|
|
|
|
|
2024-02-13 16:17:29 -06:00
|
|
|
func (rs *RepoStatus) GitURL() string {
|
|
|
|
origin, ok := rs.gitConfig.remotes["origin"]
|
|
|
|
if ok {
|
|
|
|
return origin.url
|
|
|
|
}
|
|
|
|
for i, s := range rs.gitConfig.remotes {
|
|
|
|
log.Log(WARN, "remote:", i, s.url)
|
|
|
|
}
|
|
|
|
log.Log(WARN, "GitURL() repo has non-standard origin or is not uploaded")
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2024-02-05 19:01:50 -06:00
|
|
|
func (rs *RepoStatus) GitLsFiles() (bool, string) {
|
2024-11-08 06:43:33 -06:00
|
|
|
r := rs.Run([]string{"git", "ls-files"})
|
|
|
|
output := strings.Join(r.Stdout, "\n")
|
|
|
|
if r.Error != nil {
|
|
|
|
log.Warn("git ls-files failed err =", r.Error)
|
2024-02-05 19:01:50 -06:00
|
|
|
log.Warn("git ls-files failed output =", output)
|
|
|
|
return false, output
|
|
|
|
}
|
|
|
|
return true, output
|
|
|
|
}
|
|
|
|
|
2024-01-25 01:30:01 -06:00
|
|
|
func (rs *RepoStatus) ReadOnly() bool {
|
|
|
|
if rs.readOnly.String() == "true" {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-23 11:00:33 -06:00
|
|
|
func (rs *RepoStatus) SetReadOnly(b bool) {
|
|
|
|
if b {
|
|
|
|
rs.readOnly.SetText("true")
|
|
|
|
} else {
|
|
|
|
rs.readOnly.SetText("false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-23 22:47:39 -06:00
|
|
|
func (rs *RepoStatus) processBranch(branch string) {
|
|
|
|
fullpath := rs.realPath.String()
|
2024-02-14 02:35:47 -06:00
|
|
|
log.Log(INFO, " ", branch)
|
2024-01-23 22:47:39 -06:00
|
|
|
hash, ok := rs.gitConfig.hashes[branch]
|
|
|
|
filename := fullpath + "/.git/refs/heads/" + branch
|
2024-02-14 02:35:47 -06:00
|
|
|
log.Log(INFO, " hash: need to open", filename)
|
2024-01-23 22:47:39 -06:00
|
|
|
newhash, err := readFileToString(filename)
|
|
|
|
if err != nil {
|
2024-02-14 02:35:47 -06:00
|
|
|
log.Log(WARN, "hash: read failed", filename, rs.String())
|
2024-01-23 22:47:39 -06:00
|
|
|
return
|
|
|
|
}
|
2024-02-14 02:35:47 -06:00
|
|
|
log.Log(INFO, " hash:", newhash)
|
2024-01-23 22:47:39 -06:00
|
|
|
rs.gitConfig.hashes[branch] = newhash
|
|
|
|
if ok {
|
|
|
|
if hash != newhash {
|
2024-02-14 02:35:47 -06:00
|
|
|
log.Log(WARN, "hash changed", hash, rs.String())
|
2024-01-23 22:47:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-16 17:55:13 -06:00
|
|
|
name, _ := rs.gitDescribeByHash(newhash)
|
|
|
|
rs.gitConfig.versions[newhash] = name
|
|
|
|
log.Log(INFO, " hash: version", name)
|
2024-01-23 15:20:54 -06:00
|
|
|
}
|
2024-02-22 05:24:31 -06:00
|
|
|
|
|
|
|
func (rs *RepoStatus) BranchExists(branch string) bool {
|
|
|
|
hash, ok := rs.gitConfig.hashes[branch]
|
|
|
|
if ok {
|
|
|
|
log.Log(REPOWARN, rs.Path(), "found branch", branch, hash)
|
|
|
|
return true
|
|
|
|
}
|
2024-03-21 16:16:35 -05:00
|
|
|
for i, t := range rs.Tags.tags {
|
|
|
|
base := filepath.Base(t.tag.String())
|
|
|
|
if base == branch {
|
2024-11-13 11:56:02 -06:00
|
|
|
log.Info("found branch tag:", i, t.tag.String())
|
2024-03-21 16:16:35 -05:00
|
|
|
return true
|
|
|
|
}
|
2024-11-13 11:56:02 -06:00
|
|
|
// log.Info("not tag:", i, t.tag.String())
|
2024-03-21 16:16:35 -05:00
|
|
|
}
|
|
|
|
log.Log(REPOWARN, rs.Path(), "did not find branch", branch)
|
2024-02-22 05:24:31 -06:00
|
|
|
return false
|
|
|
|
}
|