checkDirty()

This commit is contained in:
Jeff Carr 2024-12-01 00:49:25 -06:00
parent 8a6fb6d442
commit 455ea31d70
5 changed files with 132 additions and 9 deletions

View File

@ -5,16 +5,14 @@
# go install
all: gitTag.pb.go goDep.pb.go repo.pb.go
all: gitTag.pb.go goDep.pb.go repo.pb.go vet
test:
make -C scanGoSrc/
vet: lint
GO111MODULE=off go vet
lint:
# -buf lint refs.proto # todo: figure out where buf comes from again
vet:
@GO111MODULE=off go vet
@echo this go library package builds okay
# autofixes your import headers in your golang files
goimports:

45
checkDirty.go Normal file
View File

@ -0,0 +1,45 @@
package gitpb
// runs git, parses output
// types faster than you can
import (
"fmt"
"strings"
"go.wit.com/lib/gui/shell"
"go.wit.com/log"
)
func (repo *Repo) NoteChange(s string) {
log.Warn("NoteChange()", s)
}
// just return the current value
func (repo *Repo) IsDirty() bool {
return repo.Dirty
}
// actually os.Exec('git')
func (repo *Repo) CheckDirty() bool {
cmd := []string{"git", "status", "--porcelain"}
r := shell.PathRunLog(repo.FullPath, cmd, GITPB)
if r.Error != nil {
log.Warn("CheckDirty() status cmd =", cmd)
out := strings.Join(r.Stdout, "\n")
log.Warn("CheckDirty() status out =", out)
log.Warn("CheckDirty() status err =", r.Error)
log.Error(r.Error, "CheckDirty() git status error")
repo.NoteChange("git status is in error " + fmt.Sprint(r.Error))
repo.Dirty = true
return true
}
if len(r.Stdout) == 0 {
repo.Dirty = false
return false
}
repo.Dirty = true
return true
}

View File

@ -7,6 +7,7 @@ import (
"errors"
"path/filepath"
"strings"
"unicode"
"go.wit.com/log"
)
@ -74,6 +75,74 @@ func (repo *Repo) GitUserVersion() string {
return ""
}
}
func (repo *Repo) GetCurrentBranchName() string {
r := repo.RunQuiet([]string{"git", "branch", "--show-current"})
output := strings.Join(r.Stdout, "\n")
if r.Error != nil {
log.Log(GITPBWARN, "GetCurrentBranchName() not in a git repo?", r.Error, repo.GoPath)
log.Log(GITPBWARN, "GetCurrentBranchName() output might have worked anyway:", output)
}
return strings.TrimSpace(output)
}
func (repo *Repo) GetCurrentBranchVersion() string {
r := repo.RunQuiet([]string{"git", "describe", "--tags", "--always"})
output := strings.Join(r.Stdout, "\n")
if r.Error != nil {
log.Log(GITPBWARN, "GetCurrentBranchVersion() not in a git repo?", r.Error, repo.GoPath)
log.Log(GITPBWARN, "GetCurrentBranchVersion() output might have worked anyway:", output)
}
return strings.TrimSpace(output)
}
func (repo *Repo) gitDescribeByHash(hash string) (string, error) {
if hash == "" {
return "", errors.New("hash was blank")
}
r := repo.RunQuiet([]string{"git", "describe", "--tags", "--always", hash})
out := strings.Join(r.Stdout, "\n")
if r.Error != nil {
log.Warn("not in a git repo or bad hash?", r.Error, repo.GoPath)
return out, r.Error
}
return out, r.Error
}
// this should get the most recent tag
func (repo *Repo) GetLastTagVersion() string {
r := repo.RunQuiet([]string{"git", "rev-list", "--tags", "--max-count=1"})
hash := strings.Join(r.Stdout, "\n")
hash = strings.TrimSpace(hash)
log.Log(GITPB, "getLastTagVersion()", hash)
name, _ := repo.gitDescribeByHash(hash)
return name
}
func (repo *Repo) DebianReleaseVersion() string {
lasttag := repo.GetLastTagVersion()
newv := trimNonNumericFromStart(lasttag)
if newv == "" {
newv = "0.0"
if lasttag != "" {
newv += "-" + lasttag
}
}
return newv
}
func (repo *Repo) DebianCurrentVersion() string {
cbversion := repo.GetCurrentBranchVersion()
newv := trimNonNumericFromStart(cbversion)
if newv == "" {
newv = "0.0"
}
if repo.CheckDirty() {
newv += "-dirty"
}
return newv
}
func (repo *Repo) gitVersionByName(name string) (string, error) {
name = strings.TrimSpace(name)
@ -135,3 +204,12 @@ func (repo *Repo) IsBranch(findname string) bool {
log.Log(GITPB, "did not find tag:", findname, "in", repo.GoPath)
return false
}
func trimNonNumericFromStart(s string) string {
for i, r := range s {
if unicode.IsDigit(r) {
return s[i:]
}
}
return ""
}

View File

@ -7,8 +7,8 @@ import (
"time"
)
func (repo *Repo) DeleteGoDepByHash(hash string) *GoDep {
return repo.GoDeps.DeleteByHash(hash)
func (repo *Repo) DeleteGoDepByHash(hash string) {
repo.GoDeps.DeleteByHash(hash)
}
// enforces no duplicate package names

View File

@ -23,10 +23,12 @@ message Repo { // `autogenpb:marshal`
bool goPrimitive = 9; // if this is a golang primitive
GoDeps goDeps = 10;
google.protobuf.Timestamp lastGoDep = 11; // last time go.sum was processed
bool dirty = 12; // if git says things have been changed
}
message Repos { // `autogenpb:marshal`
string uuid = 1; // I guess why not just have this on each file
string uuid = 1; // `autogenpb:uuid:8daaeba1-fb1f-4762-ae6e-95a55d352673`
string version = 2; // maybe can be used for protobuf schema change violations
repeated Repo repos = 3;
}