From 455ea31d70b3dfa95ca4f269e42cf6a8dcf5f0d9 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 1 Dec 2024 00:49:25 -0600 Subject: checkDirty() --- Makefile | 10 +-- checkDirty.go | 45 +++++++++++ currentVersions.go | 215 +++++++++++++++++++++++++++++++++++++++++++++++++++++ gitTag.query.go | 137 ---------------------------------- goDep.helpers.go | 4 +- repo.proto | 4 +- 6 files changed, 269 insertions(+), 146 deletions(-) create mode 100644 checkDirty.go create mode 100644 currentVersions.go delete mode 100644 gitTag.query.go diff --git a/Makefile b/Makefile index e730ca1..7bed3b0 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/checkDirty.go b/checkDirty.go new file mode 100644 index 0000000..0db0ff4 --- /dev/null +++ b/checkDirty.go @@ -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 + +} diff --git a/currentVersions.go b/currentVersions.go new file mode 100644 index 0000000..c5c7395 --- /dev/null +++ b/currentVersions.go @@ -0,0 +1,215 @@ +package gitpb + +// runs git, parses output +// types faster than you can + +import ( + "errors" + "path/filepath" + "strings" + "unicode" + + "go.wit.com/log" +) + +func (repo *Repo) GetLastTag() string { + cmd := []string{"git", "rev-list", "--tags", "--max-count=1"} + result := repo.RunQuiet(cmd) + // log.Info("getLastTagVersion()", result.Stdout) + + if len(result.Stdout) != 1 { + log.Log(GITPBWARN, "git LastTag() error:", result.Stdout) + return "" + } + + hash := result.Stdout[0] + + cmd = []string{"git", "describe", "--tags", "--always", hash} + result = repo.RunQuiet(cmd) + + if len(result.Stdout) != 1 { + log.Log(GITPBWARN, "git LastTag() error:", result.Stdout) + return "" + } + + return result.Stdout[0] +} + +func (repo *Repo) GitMasterVersion() string { + bname := repo.GetMasterBranchName() + v, err := repo.gitVersionByName(bname) + /* + count := repo.LenGitTags() + log.Info(repo.GoPath, "tag count", count) + repo.UpdateGitTags() + count = repo.LenGitTags() + log.Info(repo.GoPath, "tag count", count) + */ + + if err == nil { + return v + } else { + log.Log(GITPBWARN, "gitpb.GitMasterVersion() error:", err) + return "" + } +} + +func (repo *Repo) GitDevelVersion() string { + bname := repo.GetDevelBranchName() + v, err := repo.gitVersionByName(bname) + if err == nil { + return v + } else { + log.Log(GITPBWARN, "gitpb.GitDevelVersion() error:", err) + return "" + } +} + +func (repo *Repo) GitUserVersion() string { + bname := repo.GetUserBranchName() + v, err := repo.gitVersionByName(bname) + if err == nil { + return v + } else { + log.Log(GITPBWARN, "gitpb.GitUserVersion() error:", err) + 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) + + if name == "" { + // git will return the current tag + r := repo.RunQuiet([]string{"git", "describe", "--tags", "--always"}) + output := strings.Join(r.Stdout, "\n") + if r.Error != nil { + log.Log(GITPBWARN, "gitDescribeByName() output might have worked anyway:", output) + log.Log(GITPBWARN, "gitDescribeByName() not in a git repo?", r.Error, repo.GoPath) + return "", r.Error + } + return strings.TrimSpace(output), nil + } + if !repo.IsBranch(name) { + // tag does not exist + log.Log(GITPBWARN, "LocalTagExists()", name, "did not exist") + return "", errors.New("gitDescribeByName() git fatal: Not a valid object name: " + name) + } + cmd := []string{"git", "describe", "--tags", "--always", name} + result := repo.RunQuiet(cmd) + output := strings.Join(result.Stdout, "\n") + if result.Error != nil { + log.Log(GITPBWARN, "cmd =", cmd) + log.Log(GITPBWARN, "err =", result.Error) + log.Log(GITPBWARN, "output (might have worked with error?) =", output) + log.Log(GITPBWARN, "not in a git repo or bad tag?", repo.GoPath) + return "", result.Error + } + + return strings.TrimSpace(output), nil +} + +// find a branch name +// will find "master" or "devel" +// will also find "v0.1.1" +// or will find "patches-from-foo" +// will return *any* match on any git branch because it doesn't +// matter much here yet +// eventually this will be worked out by forge in some future code that hasn't been made yet +func (repo *Repo) IsBranch(findname string) bool { + loop := repo.Tags.All() + for loop.Scan() { + t := loop.Next() + // log.Info("LocalTagExists() tag:", t.Refname) + + tagname := t.Refname + if strings.HasPrefix(tagname, "refs/remotes") { + continue + } + path, filename := filepath.Split(tagname) + log.Log(GITPB, "gitpb.IsBranch() tag:", path, filename, "from", repo.GoPath) + if filename == findname { + log.Log(GITPB, "gitpb.IsBranch() found tag:", path, filename, "from", repo.GoPath) + return true + } + } + 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 "" +} diff --git a/gitTag.query.go b/gitTag.query.go deleted file mode 100644 index 2c902ad..0000000 --- a/gitTag.query.go +++ /dev/null @@ -1,137 +0,0 @@ -package gitpb - -// runs git, parses output -// types faster than you can - -import ( - "errors" - "path/filepath" - "strings" - - "go.wit.com/log" -) - -func (repo *Repo) GetLastTag() string { - cmd := []string{"git", "rev-list", "--tags", "--max-count=1"} - result := repo.RunQuiet(cmd) - // log.Info("getLastTagVersion()", result.Stdout) - - if len(result.Stdout) != 1 { - log.Log(GITPBWARN, "git LastTag() error:", result.Stdout) - return "" - } - - hash := result.Stdout[0] - - cmd = []string{"git", "describe", "--tags", "--always", hash} - result = repo.RunQuiet(cmd) - - if len(result.Stdout) != 1 { - log.Log(GITPBWARN, "git LastTag() error:", result.Stdout) - return "" - } - - return result.Stdout[0] -} - -func (repo *Repo) GitMasterVersion() string { - bname := repo.GetMasterBranchName() - v, err := repo.gitVersionByName(bname) - /* - count := repo.LenGitTags() - log.Info(repo.GoPath, "tag count", count) - repo.UpdateGitTags() - count = repo.LenGitTags() - log.Info(repo.GoPath, "tag count", count) - */ - - if err == nil { - return v - } else { - log.Log(GITPBWARN, "gitpb.GitMasterVersion() error:", err) - return "" - } -} - -func (repo *Repo) GitDevelVersion() string { - bname := repo.GetDevelBranchName() - v, err := repo.gitVersionByName(bname) - if err == nil { - return v - } else { - log.Log(GITPBWARN, "gitpb.GitDevelVersion() error:", err) - return "" - } -} - -func (repo *Repo) GitUserVersion() string { - bname := repo.GetUserBranchName() - v, err := repo.gitVersionByName(bname) - if err == nil { - return v - } else { - log.Log(GITPBWARN, "gitpb.GitUserVersion() error:", err) - return "" - } -} - -func (repo *Repo) gitVersionByName(name string) (string, error) { - name = strings.TrimSpace(name) - - if name == "" { - // git will return the current tag - r := repo.RunQuiet([]string{"git", "describe", "--tags", "--always"}) - output := strings.Join(r.Stdout, "\n") - if r.Error != nil { - log.Log(GITPBWARN, "gitDescribeByName() output might have worked anyway:", output) - log.Log(GITPBWARN, "gitDescribeByName() not in a git repo?", r.Error, repo.GoPath) - return "", r.Error - } - return strings.TrimSpace(output), nil - } - if !repo.IsBranch(name) { - // tag does not exist - log.Log(GITPBWARN, "LocalTagExists()", name, "did not exist") - return "", errors.New("gitDescribeByName() git fatal: Not a valid object name: " + name) - } - cmd := []string{"git", "describe", "--tags", "--always", name} - result := repo.RunQuiet(cmd) - output := strings.Join(result.Stdout, "\n") - if result.Error != nil { - log.Log(GITPBWARN, "cmd =", cmd) - log.Log(GITPBWARN, "err =", result.Error) - log.Log(GITPBWARN, "output (might have worked with error?) =", output) - log.Log(GITPBWARN, "not in a git repo or bad tag?", repo.GoPath) - return "", result.Error - } - - return strings.TrimSpace(output), nil -} - -// find a branch name -// will find "master" or "devel" -// will also find "v0.1.1" -// or will find "patches-from-foo" -// will return *any* match on any git branch because it doesn't -// matter much here yet -// eventually this will be worked out by forge in some future code that hasn't been made yet -func (repo *Repo) IsBranch(findname string) bool { - loop := repo.Tags.All() - for loop.Scan() { - t := loop.Next() - // log.Info("LocalTagExists() tag:", t.Refname) - - tagname := t.Refname - if strings.HasPrefix(tagname, "refs/remotes") { - continue - } - path, filename := filepath.Split(tagname) - log.Log(GITPB, "gitpb.IsBranch() tag:", path, filename, "from", repo.GoPath) - if filename == findname { - log.Log(GITPB, "gitpb.IsBranch() found tag:", path, filename, "from", repo.GoPath) - return true - } - } - log.Log(GITPB, "did not find tag:", findname, "in", repo.GoPath) - return false -} diff --git a/goDep.helpers.go b/goDep.helpers.go index 0af28be..ced3fea 100644 --- a/goDep.helpers.go +++ b/goDep.helpers.go @@ -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 diff --git a/repo.proto b/repo.proto index 1d59e4f..fff5579 100644 --- a/repo.proto +++ b/repo.proto @@ -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; } -- cgit v1.2.3