From e395456c536b2458563d5e872a73e8667cc08659 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 11 Sep 2025 13:40:48 -0500 Subject: cleaning up .git/config parsing --- gitTag.proto | 1 - reloadGitConfig.go | 196 ------------------------------------------------ reloadParseGitConfig.go | 196 ++++++++++++++++++++++++++++++++++++++++++++++++ repo.new.go | 16 ++++ 4 files changed, 212 insertions(+), 197 deletions(-) delete mode 100644 reloadGitConfig.go create mode 100644 reloadParseGitConfig.go diff --git a/gitTag.proto b/gitTag.proto index eb04274..8c84f2b 100644 --- a/gitTag.proto +++ b/gitTag.proto @@ -15,7 +15,6 @@ message GitBranch { // `autogenpb:nomutex` string name = 3; // the branch name from the config file } -// readGitConfig reads and parses the .git/config file message GitConfig { // `autogenpb:nomutex` map core = 1; // map[origin] = "https:/git.wit.org/gui/gadgets" map remotes = 2; // map[origin] = "https:/git.wit.org/gui/gadgets" diff --git a/reloadGitConfig.go b/reloadGitConfig.go deleted file mode 100644 index 5373e2e..0000000 --- a/reloadGitConfig.go +++ /dev/null @@ -1,196 +0,0 @@ -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) updateGitConfig() error { - if repo == nil { - return fmt.Errorf("gitpb.updateGitConfig() repo == nil") - } - if repo.GitConfig == nil { - repo.GitConfig = new(GitConfig) - } - - repo.GitConfig.Core = make(map[string]string) - repo.GitConfig.Remotes = make(map[string]*GitRemote) - repo.GitConfig.Branches = make(map[string]*GitBranch) - repo.GitConfig.Submodules = make(map[string]string) - repo.GitConfig.Versions = make(map[string]string) - repo.GitConfig.Hashes = make(map[string]string) - return repo.readGitConfig() -} - -// readGitConfig reads and parses the .git/config file -func (repo *Repo) readGitConfig() error { - 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.GitConfig.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.GitConfig.Remotes[currentName] - if !ok { - test = new(GitRemote) - repo.GitConfig.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(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.GitConfig.Branches[currentName] - if !ok { - test = new(GitBranch) - repo.GitConfig.Branches[currentName] = test - repo.processBranch(currentName) - } - switch key { - case "remote": - repo.GitConfig.Branches[currentName].Remote = value - case "merge": - repo.GitConfig.Branches[currentName].Merge = value - default: - log.Log(INFO, "error unknown remote:", currentSection, currentName, key, value) - log.Log(INFO, "unknown branch:", line) - } - case "submodule": - // test, ok := rs.gitConfig.submodules[currentName] - switch key { - case "active": - // probably 'true' or 'false' - case "url": - repo.GitConfig.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 nil -} - -func (repo *Repo) processBranch(branch string) { - log.Log(INFO, " ", branch) - hash, ok := repo.GitConfig.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 { - log.Log(WARN, "hash: read failed", filename) - return - } - newhash := strings.TrimSpace(string(data)) - log.Log(INFO, " hash:", newhash) - repo.GitConfig.Hashes[branch] = newhash - if ok { - if hash != newhash { - log.Log(WARN, "hash changed", hash) - } - } - - name, _ := repo.gitDescribeByHash(newhash) - repo.GitConfig.Versions[newhash] = name - log.Log(INFO, " hash: version", name) -} diff --git a/reloadParseGitConfig.go b/reloadParseGitConfig.go new file mode 100644 index 0000000..5373e2e --- /dev/null +++ b/reloadParseGitConfig.go @@ -0,0 +1,196 @@ +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) updateGitConfig() error { + if repo == nil { + return fmt.Errorf("gitpb.updateGitConfig() repo == nil") + } + if repo.GitConfig == nil { + repo.GitConfig = new(GitConfig) + } + + repo.GitConfig.Core = make(map[string]string) + repo.GitConfig.Remotes = make(map[string]*GitRemote) + repo.GitConfig.Branches = make(map[string]*GitBranch) + repo.GitConfig.Submodules = make(map[string]string) + repo.GitConfig.Versions = make(map[string]string) + repo.GitConfig.Hashes = make(map[string]string) + return repo.readGitConfig() +} + +// readGitConfig reads and parses the .git/config file +func (repo *Repo) readGitConfig() error { + 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.GitConfig.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.GitConfig.Remotes[currentName] + if !ok { + test = new(GitRemote) + repo.GitConfig.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(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.GitConfig.Branches[currentName] + if !ok { + test = new(GitBranch) + repo.GitConfig.Branches[currentName] = test + repo.processBranch(currentName) + } + switch key { + case "remote": + repo.GitConfig.Branches[currentName].Remote = value + case "merge": + repo.GitConfig.Branches[currentName].Merge = value + default: + log.Log(INFO, "error unknown remote:", currentSection, currentName, key, value) + log.Log(INFO, "unknown branch:", line) + } + case "submodule": + // test, ok := rs.gitConfig.submodules[currentName] + switch key { + case "active": + // probably 'true' or 'false' + case "url": + repo.GitConfig.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 nil +} + +func (repo *Repo) processBranch(branch string) { + log.Log(INFO, " ", branch) + hash, ok := repo.GitConfig.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 { + log.Log(WARN, "hash: read failed", filename) + return + } + newhash := strings.TrimSpace(string(data)) + log.Log(INFO, " hash:", newhash) + repo.GitConfig.Hashes[branch] = newhash + if ok { + if hash != newhash { + log.Log(WARN, "hash changed", hash) + } + } + + name, _ := repo.gitDescribeByHash(newhash) + repo.GitConfig.Versions[newhash] = name + log.Log(INFO, " hash: version", name) +} diff --git a/repo.new.go b/repo.new.go index 453fa0a..c48fde7 100644 --- a/repo.new.go +++ b/repo.new.go @@ -100,3 +100,19 @@ func (all *Repos) NewRepo(fullpath string, namespace string) (*Repo, error) { // todo: use Repos.Lock() return nil, errors.New("gitpb.NewRepo() append failed " + fullpath) } + +func NewRepo(fullpath string) (*Repo, error) { + // add a new one here + newr := Repo{ + FullPath: fullpath, + } + newr.Times = new(GitTimes) + + // everything happens in here + newr.Reload() + newr.ValidateUTF8() + if newr.Namespace == "" { + log.Info("GET Namespace from URL", newr.GetURL()) + } + return &newr, nil +} -- cgit v1.2.3