From c51b8c96b12de401cc277b97552f5f9df959c74e Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 27 Nov 2024 14:41:57 -0600 Subject: ideas on code structure --- refs.update.go | 76 ++++++++++++++++++++++++++++++++++++++++++ repo.new.go | 26 +++++++++++++++ update.go | 102 --------------------------------------------------------- 3 files changed, 102 insertions(+), 102 deletions(-) create mode 100644 refs.update.go create mode 100644 repo.new.go delete mode 100644 update.go diff --git a/refs.update.go b/refs.update.go new file mode 100644 index 0000000..3266fd1 --- /dev/null +++ b/refs.update.go @@ -0,0 +1,76 @@ +package gitpb + +import ( + "slices" + "strings" + "time" + + "go.wit.com/lib/gui/shell" + "go.wit.com/log" + timestamppb "google.golang.org/protobuf/types/known/timestamppb" +) + +// Update repo.Refs from .git/ +func (repo *Repo) UpdateGit() error { + // delete the old hash + // r.DeleteByHash(hash) + repo.Refs = nil + + tags := []string{"%(objectname)", "%(creatordate)", "%(*authordate)", "%(refname)", "%(subject)"} + format := strings.Join(tags, "_,,,_") + cmd := []string{"git", "for-each-ref", "--sort=taggerdate", "--format", format} + // log.Info("RUNNING:", strings.Join(cmd, " ")) + result := shell.PathRunQuiet(repo.FullPath, cmd) + if result.Error != nil { + log.Warn("git for-each-ref error:", result.Error) + return result.Error + } + + lines := result.Stdout + // reverse the git order + slices.Reverse(lines) + + var refName string + var hash string + var subject string + var ctime time.Time + + for i, line := range lines { + var parts []string + parts = make([]string, 0) + parts = strings.Split(line, "_,,,_") + if len(parts) != 5 { + log.Info("tag error:", i, parts) + continue + } + refName = parts[3] + hash = parts[0] + + ctime = getGitDateStamp(parts[1]) + + subject = parts[4] + } + + newr := Ref{ + Hash: hash, + Subject: subject, + RefName: refName, + Ctime: timestamppb.New(ctime), + } + + repo.AppendRef(&newr) + return nil +} + +// converts a git for-each-ref date. "Wed Feb 7 10:13:38 2024 -0600" +func getGitDateStamp(gitdefault string) time.Time { + // now := time.Now().Format("Wed Feb 7 10:13:38 2024 -0600") + const gitLayout = "Mon Jan 2 15:04:05 2006 -0700" + tagTime, err := time.Parse(gitLayout, gitdefault) + if err != nil { + log.Warn("GOT THIS IN PARSE AAA." + gitdefault + ".AAA") + log.Warn(err) + return time.Now() + } + return tagTime +} diff --git a/repo.new.go b/repo.new.go new file mode 100644 index 0000000..6a37b8f --- /dev/null +++ b/repo.new.go @@ -0,0 +1,26 @@ +package gitpb + +import ( + "path/filepath" +) + +// scans in a new git repo. If it detects the repo is a golang project, +// then it parses the go.mod/go.sum files +// TODO: try adding python, rails, perl, rust, other language things? +// I probably will never have time to try that, but I'd take patches for anyone +// that might see this note and feel so inclined. +func (r *Repos) InitNewGoPath(basepath string, gopath string) *Repo { + if oldr := r.FindByPath(gopath); oldr != nil { + // already had this gopath + return oldr + } + // add a new one here + newr := Repo{ + FullPath: filepath.Join(basepath, gopath), + GoPath: gopath, + } + newr.UpdateGit() + + r.Append(&newr) + return &newr +} diff --git a/update.go b/update.go deleted file mode 100644 index 8e2a12b..0000000 --- a/update.go +++ /dev/null @@ -1,102 +0,0 @@ -package gitpb - -import ( - "path/filepath" - "slices" - "strings" - "time" - - "go.wit.com/lib/gui/shell" - "go.wit.com/log" - timestamppb "google.golang.org/protobuf/types/known/timestamppb" -) - -// this is becoming a standard format -// todo: autogenerate this from the .proto file? - -// Update version and timestamp. -// returns ok (ok == true if not found) -func (r *Repo) Update() error { - // delete the old hash - // r.DeleteByHash(hash) - r.Refs = nil - - tags := []string{"%(objectname)", "%(creatordate)", "%(*authordate)", "%(refname)", "%(subject)"} - format := strings.Join(tags, "_,,,_") - cmd := []string{"git", "for-each-ref", "--sort=taggerdate", "--format", format} - // log.Info("RUNNING:", strings.Join(cmd, " ")) - result := shell.PathRunQuiet(r.FullPath, cmd) - if result.Error != nil { - log.Warn("git for-each-ref error:", result.Error) - return result.Error - } - - lines := result.Stdout - // reverse the git order - slices.Reverse(lines) - - var refName string - var hash string - var subject string - var ctime time.Time - - for i, line := range lines { - var parts []string - parts = make([]string, 0) - parts = strings.Split(line, "_,,,_") - if len(parts) != 5 { - log.Info("tag error:", i, parts) - continue - } - refName = parts[3] - hash = parts[0] - - ctime = getGitDateStamp(parts[1]) - - subject = parts[4] - } - - newr := Ref{ - Hash: hash, - Subject: subject, - RefName: refName, - Ctime: timestamppb.New(ctime), - } - - r.AppendRef(&newr) - return nil -} - -// converts a git for-each-ref date. "Wed Feb 7 10:13:38 2024 -0600" -func getGitDateStamp(gitdefault string) time.Time { - // now := time.Now().Format("Wed Feb 7 10:13:38 2024 -0600") - const gitLayout = "Mon Jan 2 15:04:05 2006 -0700" - tagTime, err := time.Parse(gitLayout, gitdefault) - if err != nil { - log.Warn("GOT THIS IN PARSE AAA." + gitdefault + ".AAA") - log.Warn(err) - return time.Now() - } - return tagTime -} - -// scans in a new git repo. If it detects the repo is a golang project, -// then it parses the go.mod/go.sum files -// TODO: try adding python, rails, perl, rust, other language things? -// I probably will never have time to try that, but I'd take patches for anyone -// that might see this note and feel so inclined. -func (r *Repos) InitNewGoPath(basepath string, gopath string) *Repo { - if oldr := r.FindByPath(gopath); oldr != nil { - // already had this gopath - return oldr - } - // add a new one here - newr := Repo{ - FullPath: filepath.Join(basepath, gopath), - GoPath: gopath, - } - newr.Update() - - r.Append(&newr) - return &newr -} -- cgit v1.2.3