pull out the namespace

This commit is contained in:
Jeff Carr 2025-09-11 14:21:37 -05:00
parent e395456c53
commit 1d8380b9e7
2 changed files with 46 additions and 12 deletions

View File

@ -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) {

View File

@ -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
}
return &newr, 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 &repo, nil
}