From 1d8380b9e781ce7087dbd0d8bead69f11ced7527 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 11 Sep 2025 14:21:37 -0500 Subject: [PATCH] pull out the namespace --- reloadParseGitConfig.go | 29 ++++++++++++++++++++++++----- repo.new.go | 29 ++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/reloadParseGitConfig.go b/reloadParseGitConfig.go index 5373e2e..c6d01e6 100644 --- a/reloadParseGitConfig.go +++ b/reloadParseGitConfig.go @@ -27,16 +27,28 @@ func (repo *Repo) updateGitConfig() error { repo.GitConfig.Submodules = make(map[string]string) repo.GitConfig.Versions = make(map[string]string) repo.GitConfig.Hashes = make(map[string]string) - return repo.readGitConfig() + url, err := repo.readGitConfig() + if repo.URL != "" { + log.Info("gitpb: url already set", url, repo.URL) + } + + if url == "" { + log.Info(repo.FullPath, "url was blank. warn user this repo is only on the local disk") + } else { + repo.URL = url + } + + return err } // readGitConfig reads and parses the .git/config file -func (repo *Repo) readGitConfig() error { +func (repo *Repo) readGitConfig() (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 + return "", err } var currentSection string = "" @@ -92,6 +104,13 @@ func (repo *Repo) readGitConfig() error { 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 } @@ -164,10 +183,10 @@ func (repo *Repo) readGitConfig() error { } if err := scanner.Err(); err != nil { - return err + return "", err } - return nil + return foundURL, nil } func (repo *Repo) processBranch(branch string) { diff --git a/repo.new.go b/repo.new.go index c48fde7..bd8af6a 100644 --- a/repo.new.go +++ b/repo.new.go @@ -2,6 +2,8 @@ package gitpb import ( "errors" + "net/url" + "path/filepath" "go.wit.com/log" ) @@ -103,16 +105,29 @@ func (all *Repos) NewRepo(fullpath string, namespace string) (*Repo, error) { func NewRepo(fullpath string) (*Repo, error) { // add a new one here - newr := Repo{ + repo := Repo{ FullPath: fullpath, } - newr.Times = new(GitTimes) + repo.Times = new(GitTimes) // everything happens in here - newr.Reload() - newr.ValidateUTF8() - if newr.Namespace == "" { - log.Info("GET Namespace from URL", newr.GetURL()) + repo.Reload() + repo.ValidateUTF8() + if repo.Namespace == "" { + giturl := repo.GetURL() + if giturl == "" { + log.Info(repo.FullPath, "Namespace & URL are both blank. Warn the user of a local repo.") + return &repo, nil + } + // log.Info("GET Namespace from URL", giturl) + tmpURL, err := url.Parse(giturl) + if err != nil { + log.Info(repo.FullPath, "URL parse failed", giturl, err) + return &repo, nil + } + // log.Info(repo.FullPath, "namespace might be:", tmpURL.Hostname(), tmpURL.Path) + repo.Namespace = filepath.Join(tmpURL.Hostname(), tmpURL.Path) + // log.Info(repo.FullPath, "Namesapce =", repo.Namespace) } - return &newr, nil + return &repo, nil }