code now moved to gitpb

This commit is contained in:
Jeff Carr 2025-01-18 15:48:52 -06:00
parent a15d447fbd
commit ac2958e559
6 changed files with 17 additions and 216 deletions

17
git.go
View File

@ -1,14 +1,5 @@
package repostatus package repostatus
import (
"errors"
"strings"
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
)
// remove this everything // remove this everything
func (rs *RepoStatus) Path() string { func (rs *RepoStatus) Path() string {
return rs.realPath.String() return rs.realPath.String()
@ -48,7 +39,8 @@ func (rs *RepoStatus) checkCurrentBranchName() string {
return out return out
} }
func (rs *RepoStatus) gitDescribeByHash(hash string) (string, error) { /*
func (rs *RepoStatus) oldgitDescribeByHash(hash string) (string, error) {
if hash == "" { if hash == "" {
return "", errors.New("hash was blank") return "", errors.New("hash was blank")
} }
@ -61,7 +53,7 @@ func (rs *RepoStatus) gitDescribeByHash(hash string) (string, error) {
return out, r.Error return out, r.Error
} }
func (rs *RepoStatus) gitDescribeByName(name string) (string, error) { func (rs *RepoStatus) oldgitDescribeByName(name string) (string, error) {
name = strings.TrimSpace(name) name = strings.TrimSpace(name)
if name == "" { if name == "" {
@ -88,7 +80,9 @@ func (rs *RepoStatus) gitDescribeByName(name string) (string, error) {
return strings.TrimSpace(output), r.Error return strings.TrimSpace(output), r.Error
} }
*/
/*
func (rs *RepoStatus) populateTags() { func (rs *RepoStatus) populateTags() {
tmp := rs.realPath.String() + "/.git/refs/tags" tmp := rs.realPath.String() + "/.git/refs/tags"
log.Log(REPO, "populateTags() path =", tmp) log.Log(REPO, "populateTags() path =", tmp)
@ -101,3 +95,4 @@ func (rs *RepoStatus) populateTags() {
} }
// rs.tagsDrop.SetText(rs.lasttagrev) // rs.tagsDrop.SetText(rs.lasttagrev)
} }
*/

View File

@ -1,194 +0,0 @@
package repostatus
import (
"bufio"
"io/ioutil"
"os"
"path/filepath"
"strings"
"go.wit.com/log"
)
// GitConfig represents the parsed .git/config data
// type GitConfig map[string]map[string]string
// TODO: switch to protobuf
type remote struct {
url string
fetch string
}
type branch struct {
remote string
merge string
}
type GitConfig struct {
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
}
// readGitConfig reads and parses the .git/config file
func (rs *RepoStatus) readGitConfig() error {
filename := filepath.Join(rs.realPath.String(), "/.git/config")
file, err := os.Open(filename)
if err != nil {
log.Log(WARN, "readGitConfig() failed for file:", filename)
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 {
return err
}
}
defer file.Close()
var currentSection string = ""
var currentName string = ""
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)
rs.gitConfig.submodules = make(map[string]string)
rs.gitConfig.versions = make(map[string]string)
rs.gitConfig.hashes = make(map[string]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":
rs.gitConfig.core[key] = value
case "gui":
// don't really need gui stuff right now
case "pull":
// don't store git config pull settings here
// probably has 'rebase = false'
case "remote":
test, ok := rs.gitConfig.remotes[currentName]
if !ok {
test = new(remote)
rs.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(REPO, "error url mismatch", test.url, value)
case "fetch":
if test.fetch == value {
continue
}
if test.fetch == "" {
test.fetch = value
continue
}
log.Log(REPO, "error fetch mismatch", test.fetch, value)
default:
log.Log(REPO, "unknown remote:", line)
}
case "branch":
test, ok := rs.gitConfig.branches[currentName]
if !ok {
test = new(branch)
rs.gitConfig.branches[currentName] = test
rs.processBranch(currentName)
}
switch key {
case "remote":
rs.gitConfig.branches[currentName].remote = value
case "merge":
rs.gitConfig.branches[currentName].merge = value
default:
log.Log(REPO, "error unknown remote:", currentSection, currentName, key, value)
log.Log(REPO, "unknown branch:", 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:", line)
}
default:
log.Log(REPOWARN, "unknown line:", line)
}
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}
func (rs *RepoStatus) processBranch(branch string) {
fullpath := rs.realPath.String()
log.Log(INFO, " ", branch)
hash, ok := rs.gitConfig.hashes[branch]
filename := fullpath + "/.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)
rs.gitConfig.hashes[branch] = newhash
if ok {
if hash != newhash {
log.Log(WARN, "hash changed", hash)
}
}
name, _ := rs.gitDescribeByHash(newhash)
rs.gitConfig.versions[newhash] = name
log.Log(INFO, " hash: version", name)
}

View File

@ -23,18 +23,18 @@ func (rs *RepoStatus) drawGitCommands(box *gui.Node) {
}) })
newgrid.NewButton("show .git/config", func() { newgrid.NewButton("show .git/config", func() {
if rs.gitConfig == nil { if rs.pb.GitConfig == nil {
log.Log(WARN, "Nonexistant or damaged .git/config", rs.Path()) log.Log(WARN, "Nonexistant or damaged .git/config", rs.Path())
return return
} }
log.Log(WARN, ".git/config:", rs.realPath.String()) log.Log(WARN, ".git/config:", rs.realPath.String())
// The info: // The info:
for name, remote := range rs.gitConfig.remotes { for name, remote := range rs.pb.GitConfig.Remotes {
log.Log(WARN, " ", name, "url:", remote.url) log.Log(WARN, " ", name, "url:", remote.Url)
} }
for name, branch := range rs.gitConfig.branches { for name, branch := range rs.pb.GitConfig.Branches {
log.Log(WARN, " ", name, "remote:", branch.remote, "merge", branch.merge) log.Log(WARN, " ", name, "remote:", branch.Remote, "merge", branch.Merge)
} }
}) })
newgrid.NextRow() newgrid.NextRow()

4
new.go
View File

@ -54,7 +54,7 @@ func NewRepoStatusWindow(repo *gitpb.Repo) (*RepoStatus, error) {
// realpath := repo.FullPath // realpath := repo.FullPath
// isGoLang := true // isGoLang := true
rs.tags = make(map[string]string) // rs.tags = make(map[string]string)
rs.window = gadgets.RawBasicWindow("GO Repo Details " + path) rs.window = gadgets.RawBasicWindow("GO Repo Details " + path)
rs.window.Horizontal() rs.window.Horizontal()
rs.window.Make() rs.window.Make()
@ -86,7 +86,7 @@ func NewRepoStatusWindow(repo *gitpb.Repo) (*RepoStatus, error) {
// add all the tags // add all the tags
rs.makeTagBox(box2) rs.makeTagBox(box2)
rs.readGitConfig() // rs.readGitConfig()
if rs.pb.GetReadOnly() { if rs.pb.GetReadOnly() {
rs.readOnly.SetValue("true") rs.readOnly.SetValue("true")

View File

@ -10,7 +10,7 @@ type RepoStatus struct {
ready bool ready bool
changed bool // keeps track of changes that might have happened changed bool // keeps track of changes that might have happened
changes string changes string
tags map[string]string // tags map[string]string
InitOk bool // it takes a second or so to init these InitOk bool // it takes a second or so to init these
pb *gitpb.Repo // the protobuf pb *gitpb.Repo // the protobuf
@ -74,7 +74,7 @@ type RepoStatus struct {
speed *gadgets.OneLiner speed *gadgets.OneLiner
speedActual *gadgets.OneLiner speedActual *gadgets.OneLiner
gitConfig *GitConfig // gitConfig *GitConfig
// goConfig GoConfig // goConfig GoConfig
switchBranchB *gui.Node switchBranchB *gui.Node

View File

@ -21,7 +21,7 @@ func (rs *RepoStatus) Update() {
rs.currentVersion.SetValue(out) rs.currentVersion.SetValue(out)
// read in the tags // read in the tags
rs.populateTags() // rs.populateTags()
// record if the repo is dirty // record if the repo is dirty
pb.CheckDirty() pb.CheckDirty()