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.Submodules = make(map[string]string)
repo.GitConfig.Versions = make(map[string]string) repo.GitConfig.Versions = make(map[string]string)
repo.GitConfig.Hashes = 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 // 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") filename := filepath.Join(repo.GetFullPath(), ".git/config")
file, err := os.Open(filename) file, err := os.Open(filename)
defer file.Close() defer file.Close()
if err != nil { if err != nil {
return err return "", err
} }
var currentSection string = "" var currentSection string = ""
@ -92,6 +104,13 @@ func (repo *Repo) readGitConfig() error {
log.Log(INFO, "switch currentSection", currentSection, currentName) log.Log(INFO, "switch currentSection", currentSection, currentName)
switch key { switch key {
case "url": 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 { if test.Url == value {
continue continue
} }
@ -164,10 +183,10 @@ func (repo *Repo) readGitConfig() error {
} }
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
return err return "", err
} }
return nil return foundURL, nil
} }
func (repo *Repo) processBranch(branch string) { func (repo *Repo) processBranch(branch string) {

View File

@ -2,6 +2,8 @@ package gitpb
import ( import (
"errors" "errors"
"net/url"
"path/filepath"
"go.wit.com/log" "go.wit.com/log"
) )
@ -103,16 +105,29 @@ func (all *Repos) NewRepo(fullpath string, namespace string) (*Repo, error) {
func NewRepo(fullpath string) (*Repo, error) { func NewRepo(fullpath string) (*Repo, error) {
// add a new one here // add a new one here
newr := Repo{ repo := Repo{
FullPath: fullpath, FullPath: fullpath,
} }
newr.Times = new(GitTimes) repo.Times = new(GitTimes)
// everything happens in here // everything happens in here
newr.Reload() repo.Reload()
newr.ValidateUTF8() repo.ValidateUTF8()
if newr.Namespace == "" { if repo.Namespace == "" {
log.Info("GET Namespace from URL", newr.GetURL()) 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
} }