attempt to use go.wit.com/apps/autogenpb for sort

This commit is contained in:
Jeff Carr 2024-11-29 18:15:25 -06:00
parent 7615317ca7
commit 4a79cccffd
14 changed files with 64 additions and 524 deletions

View File

@ -5,7 +5,7 @@
# go install
all: refs.pb.go gitTags.pb.go godep.pb.go repo.pb.go
all: gitTag.pb.go goDep.pb.go repo.pb.go
make -C scanGoSrc/
vet: lint
@ -29,26 +29,28 @@ clean:
-rm -f go.*
make -C scanGoSrc clean
refs.pb.go: refs.proto
cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/lib/protobuf/gitpb \
--go_opt=Mrefs.proto=go.wit.com/lib/protobuf/gitpb \
refs.proto
#refs.pb.go: refs.proto
# cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/lib/protobuf/gitpb \
# --go_opt=Mrefs.proto=go.wit.com/lib/protobuf/gitpb \
# refs.proto
gitTags.pb.go: gitTags.proto
gitTag.pb.go: gitTag.proto
cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/lib/protobuf/gitpb \
--go_opt=MgitTags.proto=go.wit.com/lib/protobuf/gitpb \
gitTags.proto
--go_opt=MgitTag.proto=go.wit.com/lib/protobuf/gitpb \
gitTag.proto
autogenpb --proto gitTag.proto --sort "ByName,Refname" --sort "ByHash,Hash" --no-marshal
godep.pb.go: godep.proto
goDep.pb.go: goDep.proto
cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/lib/protobuf/gitpb \
--go_opt=Mgodep.proto=go.wit.com/lib/protobuf/gitpb \
godep.proto
--go_opt=MgoDep.proto=go.wit.com/lib/protobuf/gitpb \
goDep.proto
autogenpb --proto goDep.proto --sort "ByPath,GoPath" --no-marshal
repo.pb.go: repo.proto
cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/lib/protobuf/gitpb \
--go_opt=Mrefs.proto=go.wit.com/lib/protobuf/gitpb \
--go_opt=Mgodep.proto=go.wit.com/lib/protobuf/gitpb \
--go_opt=MgoDep.proto=go.wit.com/lib/protobuf/gitpb \
--go_opt=Mrepo.proto=go.wit.com/lib/protobuf/gitpb \
--go_opt=MgitTags.proto=go.wit.com/lib/protobuf/gitpb \
--go_opt=MgitTag.proto=go.wit.com/lib/protobuf/gitpb \
repo.proto
autogenpb --proto repo.proto --sort "ByPath,GoPath"

19
gitTag.proto Normal file
View File

@ -0,0 +1,19 @@
syntax = "proto3";
package gitpb;
import "google/protobuf/timestamp.proto"; // Import the well-known type for Timestamp
message GitTag {
string refname = 1; // tag name. treated as unique
google.protobuf.Timestamp creatordate = 2; // git creatordate
google.protobuf.Timestamp authordate = 3; // git author date
string hash = 4; // git hash
string subject = 5; // git tag subject
}
message GitTags {
string uuid = 1; // I guess why not just have this on each file
string version = 2; // maybe can be used for protobuf schema change violations
repeated GitTag gitTags = 3;
}

View File

@ -113,7 +113,7 @@ func (repo *Repo) gitVersionByName(name string) (string, error) {
// 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.AllTags()
loop := repo.Tags.All()
for loop.Scan() {
t := loop.Next()
// log.Info("LocalTagExists() tag:", t.Refname)

View File

@ -14,7 +14,7 @@ import (
func (repo *Repo) UpdateGitTags() error {
// delete the old hash
// r.DeleteByHash(hash)
repo.GitTags = nil
repo.Tags.GitTags = nil
tags := []string{"%(objectname)", "%(creatordate)", "%(*authordate)", "%(refname)", "%(subject)"}
format := strings.Join(tags, "_,,,_")
@ -58,13 +58,13 @@ func (repo *Repo) UpdateGitTags() error {
newr := GitTag{
Refname: refname,
Objectname: hash,
Hash: hash,
Subject: subject,
Creatordate: ctime,
Authordate: atime,
}
repo.AppendGitTag(&newr)
repo.Tags.Append(&newr)
}
return nil
}

View File

@ -1,13 +0,0 @@
syntax = "proto3";
package gitpb;
import "google/protobuf/timestamp.proto"; // Import the well-known type for Timestamp
message GitTag {
string refname = 1; // tag name. treated as unique
google.protobuf.Timestamp creatordate = 2; // git creatordate
google.protobuf.Timestamp authordate = 3; // git creatordate
string objectname = 4; // git hash
string subject = 5; // git tag subject
}

View File

@ -1,149 +0,0 @@
package gitpb
// this is becoming a standard format
// todo: autogenerate this from the .proto file?
import (
"fmt"
"os"
"sort"
sync "sync"
"time"
)
// bad global lock until I figure out some other plan
var gitTagslock sync.RWMutex
type GitTagIterator struct {
sync.RWMutex
packs []*GitTag
index int
}
// NewGitTagIterator initializes a new iterator.
func NewGitTagIterator(packs []*GitTag) *GitTagIterator {
return &GitTagIterator{packs: packs}
}
// Scan moves to the next element and returns false if there are no more packs.
func (it *GitTagIterator) Scan() bool {
if it.index >= len(it.packs) {
return false
}
it.index++
return true
}
// GitTag returns the current repo.
func (it *GitTagIterator) Next() *GitTag {
if it.packs[it.index-1] == nil {
for i, d := range it.packs {
fmt.Println("i =", i, d)
}
fmt.Println("len =", len(it.packs))
fmt.Println("repo == nil", it.index, it.index-1)
os.Exit(-1)
}
return it.packs[it.index-1]
}
// Use Scan() in a loop, similar to a while loop
//
// for iterator.Scan() {
// d := iterator.GitTag()
// fmt.Println("GitTag UUID:", d.Uuid)
// }
func (r *Repo) AllTags() *GitTagIterator {
repoPointers := r.selectAllGitTags()
iterator := NewGitTagIterator(repoPointers)
return iterator
}
func (r *Repo) SortTagsByName() *GitTagIterator {
packs := r.selectAllGitTags()
sort.Sort(GitTagsByName(packs))
iterator := NewGitTagIterator(packs)
return iterator
}
// enforces no duplicate package names
func (repo *Repo) AppendGitTag(newP *GitTag) bool {
gitTagslock.Lock()
defer gitTagslock.Unlock()
for _, p := range repo.GitTags {
if p.Refname == newP.Refname {
return false
}
}
repo.GitTags = append(repo.GitTags, newP)
return true
}
// returns time.Duration since last Update()
func (r *GitTag) Age(newP *GitTag) time.Duration {
t := time.Since(r.Creatordate.AsTime())
return t
}
// find a package by name
func (repo *Repo) FindGitTagByName(name string) *GitTag {
gitTagslock.RLock()
defer gitTagslock.RUnlock()
for _, p := range repo.GitTags {
if p.Refname == name {
return p
}
}
return nil
}
func (repo *Repo) LenGitTags() int {
gitTagslock.RLock()
defer gitTagslock.RUnlock()
return len(repo.GitTags)
}
type GitTagsByName []*GitTag
func (a GitTagsByName) Len() int { return len(a) }
func (a GitTagsByName) Less(i, j int) bool { return a[i].Refname < a[j].Refname }
func (a GitTagsByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// safely returns a slice of pointers to the GitTag protobufs
func (repo *Repo) selectAllGitTags() []*GitTag {
gitTagslock.RLock()
defer gitTagslock.RUnlock()
// Create a new slice to hold pointers to eachGitTag
var allGitTags []*GitTag
allGitTags = make([]*GitTag, len(repo.GitTags))
for i, p := range repo.GitTags {
allGitTags[i] = p // Copy pointers for safe iteration
}
return allGitTags
}
func (repo *Repo) DeleteTagByHash(hash string) *GitTag {
gitTagslock.Lock()
defer gitTagslock.Unlock()
for i, _ := range repo.GitTags {
if repo.GitTags[i].Objectname == hash {
repo.GitTags[i] = repo.GitTags[len(repo.GitTags)-1]
repo.GitTags = repo.GitTags[:len(repo.GitTags)-1]
return nil
}
}
return nil
}

View File

@ -8,6 +8,7 @@ import (
)
func (repo *Repo) DeleteGoDepByHash(hash string) *GoDep {
/*
refslock.Lock()
defer refslock.Unlock()
@ -18,11 +19,13 @@ func (repo *Repo) DeleteGoDepByHash(hash string) *GoDep {
return nil
}
}
*/
return nil
}
// enforces no duplicate package names
func (repo *Repo) AppendGoDep(newP *GoDep) bool {
/*
refslock.Lock()
defer refslock.Unlock()
@ -33,7 +36,8 @@ func (repo *Repo) AppendGoDep(newP *GoDep) bool {
}
repo.GoDeps = append(repo.GoDeps, newP)
return true
*/
return repo.GoDeps.AppendUniqueGoPath(newP)
}
// returns time.Duration since last scan of go.sum & go.mod
@ -44,14 +48,5 @@ func (repo *Repo) AgeGoDep() time.Duration {
// find a dependancy by the go path
func (repo *Repo) FindGoDepByPath(gopath string) *GoDep {
refslock.RLock()
defer refslock.RUnlock()
for _, p := range repo.GoDeps {
if p.GoPath == gopath {
return p
}
}
return nil
return repo.GoDeps.FindByGoPath(gopath)
}

View File

@ -13,3 +13,9 @@ message GoDep {
string goPath = 4; // "go.wit.com/lib/foo"
string goVersion = 5; // version of golang the developer used to make this package version
}
message GoDeps {
string uuid = 1; // I guess why not just have this on each file
string version = 2; // maybe can be used for protobuf schema change violations
repeated GoDep goDeps = 3;
}

View File

@ -83,7 +83,7 @@ func (repo *Repo) parseGoSum() (bool, error) {
GoPath: godep,
Version: version,
}
repo.AppendGoDep(&new1)
repo.GoDeps.AppendUniqueGoPath(&new1)
/*
found := repo.FindGoDepByPath(godep)
if found == nil {

View File

@ -1,99 +0,0 @@
package gitpb
// this is becoming a standard format
// todo: autogenerate this from the .proto file?
import (
"fmt"
"os"
"sort"
sync "sync"
)
// bad global lock until I figure out some other plan
var godeplock sync.RWMutex
type GoDepIterator struct {
sync.RWMutex
packs []*GoDep
index int
}
// NewGoDepGoDepIterator initializes a new iterator.
func NewGoDepIterator(packs []*GoDep) *GoDepIterator {
return &GoDepIterator{packs: packs}
}
// Scan moves to the next element and returns false if there are no more packs.
func (it *GoDepIterator) Scan() bool {
if it.index >= len(it.packs) {
return false
}
it.index++
return true
}
// GoDep returns the current repo.
func (it *GoDepIterator) GoDep() *GoDep {
if it.packs[it.index-1] == nil {
for i, d := range it.packs {
fmt.Println("i =", i, d)
}
fmt.Println("len =", len(it.packs))
fmt.Println("repo == nil", it.index, it.index-1)
os.Exit(-1)
}
return it.packs[it.index-1]
}
// Use Scan() in a loop, similar to a while loop
//
// for iterator.Scan() {
// d := iterator.GoDep()
// fmt.Println("GoDep UUID:", d.Uuid)
// }
func (r *Repo) AllGoDeps() *GoDepIterator {
repoPointers := r.selectAllGoDeps()
iterator := NewGoDepIterator(repoPointers)
return iterator
}
func (r *Repo) SortGoDepsByName() *GoDepIterator {
packs := r.selectAllGoDeps()
sort.Sort(GoDepByPath(packs))
iterator := NewGoDepIterator(packs)
return iterator
}
func (repo *Repo) Len() int {
refslock.RLock()
defer refslock.RUnlock()
return len(repo.GoDeps)
}
type GoDepByPath []*GoDep
func (a GoDepByPath) Len() int { return len(a) }
func (a GoDepByPath) Less(i, j int) bool { return a[i].GoPath < a[j].GoPath }
func (a GoDepByPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// safely returns a slice of pointers to the GoDep protobufs
func (r *Repo) selectAllGoDeps() []*GoDep {
refslock.RLock()
defer refslock.RUnlock()
// Create a new slice to hold pointers to each GoDep
var allPacks []*GoDep
allPacks = make([]*GoDep, len(r.GoDeps))
for i, p := range r.GoDeps {
allPacks[i] = p // Copy pointers for safe iteration
}
return allPacks
}

View File

@ -1,149 +0,0 @@
package gitpb
// this is becoming a standard format
// todo: autogenerate this from the .proto file?
import (
"fmt"
"os"
"sort"
sync "sync"
"time"
)
// bad global lock until I figure out some other plan
var refslock sync.RWMutex
type RefIterator struct {
sync.RWMutex
packs []*Ref
index int
}
// NewRefIterator initializes a new iterator.
func NewRefIterator(packs []*Ref) *RefIterator {
return &RefIterator{packs: packs}
}
// Scan moves to the next element and returns false if there are no more packs.
func (it *RefIterator) Scan() bool {
if it.index >= len(it.packs) {
return false
}
it.index++
return true
}
// Ref returns the current repo.
func (it *RefIterator) Ref() *Ref {
if it.packs[it.index-1] == nil {
for i, d := range it.packs {
fmt.Println("i =", i, d)
}
fmt.Println("len =", len(it.packs))
fmt.Println("repo == nil", it.index, it.index-1)
os.Exit(-1)
}
return it.packs[it.index-1]
}
// Use Scan() in a loop, similar to a while loop
//
// for iterator.Scan() {
// d := iterator.Ref()
// fmt.Println("Ref UUID:", d.Uuid)
// }
func (r *Repo) All() *RefIterator {
repoPointers := r.selectAllRefs()
iterator := NewRefIterator(repoPointers)
return iterator
}
func (r *Repo) SortByName() *RefIterator {
packs := r.selectAllRefs()
sort.Sort(RefsByName(packs))
iterator := NewRefIterator(packs)
return iterator
}
// enforces no duplicate package names
func (repo *Repo) AppendRef(newP *Ref) bool {
refslock.Lock()
defer refslock.Unlock()
for _, p := range repo.Refs {
if p.RefName == newP.RefName {
return false
}
}
repo.Refs = append(repo.Refs, newP)
return true
}
// returns time.Duration since last Update()
func (r *Ref) Age(newP *Ref) time.Duration {
t := time.Since(r.Ctime.AsTime())
return t
}
// find a package by name
func (repo *Repo) FindRefByName(name string) *Ref {
refslock.RLock()
defer refslock.RUnlock()
for _, p := range repo.Refs {
if p.RefName == name {
return p
}
}
return nil
}
func (repo *Repo) LenRefs() int {
refslock.RLock()
defer refslock.RUnlock()
return len(repo.Refs)
}
type RefsByName []*Ref
func (a RefsByName) Len() int { return len(a) }
func (a RefsByName) Less(i, j int) bool { return a[i].RefName < a[j].RefName }
func (a RefsByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// safely returns a slice of pointers to the Ref protobufs
func (repo *Repo) selectAllRefs() []*Ref {
refslock.RLock()
defer refslock.RUnlock()
// Create a new slice to hold pointers to each Ref
var allRefs []*Ref
allRefs = make([]*Ref, len(repo.Refs))
for i, p := range repo.Refs {
allRefs[i] = p // Copy pointers for safe iteration
}
return allRefs
}
func (repo *Repo) DeleteByHash(hash string) *Ref {
refslock.Lock()
defer refslock.Unlock()
for i, _ := range repo.Refs {
if repo.Refs[i].Hash == hash {
repo.Refs[i] = repo.Refs[len(repo.Refs)-1]
repo.Refs = repo.Refs[:len(repo.Refs)-1]
return nil
}
}
return nil
}

View File

@ -1,55 +0,0 @@
package gitpb
/*
// 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
}
*/

View File

@ -16,20 +16,6 @@ func (all *Repos) DeleteByPath(gopath string) *Repo {
return nil
}
// find a package by gopath
func (all *Repos) FindByGoPath(gopath string) *Repo {
reposMu.RLock()
defer reposMu.RUnlock()
for _, p := range all.Repos {
if p.GoPath == gopath {
return p
}
}
return nil
}
// enforces no duplicate gopath's
func (all *Repos) add(newP *Repo) bool {
reposMu.Lock()

View File

@ -5,28 +5,25 @@ package gitpb;
// stores information about git repos
// If the project is in golang, also gets the go language dependacies
import "refs.proto";
import "godep.proto";
import "gitTags.proto";
import "goDep.proto";
import "gitTag.proto";
import "google/protobuf/timestamp.proto"; // Import the well-known type for Timestamp
message Repo {
string fullPath = 1; // the actual path to the .git directory: '/home/devel/golang.org/x/tools'
repeated Ref refs = 2;
google.protobuf.Timestamp lastPull = 3; // last time a git pull was done
google.protobuf.Timestamp lastPull = 2; // last time a git pull was done
string masterBranchName = 3; // git 'main' or 'master' branch name
string develBranchName = 4; // whatever the git 'devel' branch name is
string userBranchName = 5; // whatever your username branch is
// things specific to golang projects
string goPath = 4; // the logical path as used by golang: 'go.wit.com/apps/helloworld'
repeated GoDep GoDeps = 6;
google.protobuf.Timestamp lastGoDep = 7; // last time go.sum was processed
bool goLibrary = 8; // if this is a golang library
bool goPrimitive = 9; // if this is a golang primitive
repeated GitTag gitTags = 10;
string masterBranchName = 11; // git 'main' or 'master' branch name
string develBranchName = 12; // whatever the git 'devel' branch name is
string userBranchName = 13; // whatever your username branch is
string goPath = 6; // the logical path as used by golang: 'go.wit.com/apps/helloworld'
bool goLibrary = 7; // if this is a golang library
bool goPrimitive = 8; // if this is a golang primitive
GitTags tags = 9;
GoDeps goDeps = 10;
google.protobuf.Timestamp lastGoDep = 11; // last time go.sum was processed
}
message Repos {