From bdcaba32f89cc635f39300aa213931c8eeffb909 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 29 Nov 2024 16:22:19 -0600 Subject: autogenpb marshal.pb.go and sort.pb.go Signed-off-by: Jeff Carr --- Makefile | 9 +++--- repo.proto | 36 +++++++++++++++++++++ repos.helpers.go | 12 +++---- repos.marshal.go | 42 ------------------------ repos.proto | 32 ------------------ repos.sort.go | 99 -------------------------------------------------------- 6 files changed, 47 insertions(+), 183 deletions(-) create mode 100644 repo.proto delete mode 100644 repos.marshal.go delete mode 100644 repos.proto delete mode 100644 repos.sort.go diff --git a/Makefile b/Makefile index ffbb48f..cefd157 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ # go install -all: refs.pb.go gitTags.pb.go godep.pb.go repos.pb.go vet +all: refs.pb.go gitTags.pb.go godep.pb.go repo.pb.go make -C scanGoSrc/ vet: lint @@ -44,10 +44,11 @@ godep.pb.go: godep.proto --go_opt=Mgodep.proto=go.wit.com/lib/protobuf/gitpb \ godep.proto -repos.pb.go: repos.proto +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=Mrepos.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 \ - repos.proto + repo.proto + autogenpb --proto repo.proto --sort "ByPath,GoPath" diff --git a/repo.proto b/repo.proto new file mode 100644 index 0000000..57ba63c --- /dev/null +++ b/repo.proto @@ -0,0 +1,36 @@ +syntax = "proto3"; + +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 "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 + + // 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 +} + +message Repos { + 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 Repo repos = 3; +} diff --git a/repos.helpers.go b/repos.helpers.go index 3420526..50f3277 100644 --- a/repos.helpers.go +++ b/repos.helpers.go @@ -3,8 +3,8 @@ package gitpb // delete a gopath: // myrepos.DeleteByPath("go.wit.com/apps/go-clone") func (all *Repos) DeleteByPath(gopath string) *Repo { - repolock.Lock() - defer repolock.Unlock() + reposMu.Lock() + defer reposMu.Unlock() for i, _ := range all.Repos { if all.Repos[i].GoPath == gopath { @@ -18,8 +18,8 @@ func (all *Repos) DeleteByPath(gopath string) *Repo { // find a package by gopath func (all *Repos) FindByGoPath(gopath string) *Repo { - repolock.RLock() - defer repolock.RUnlock() + reposMu.RLock() + defer reposMu.RUnlock() for _, p := range all.Repos { if p.GoPath == gopath { @@ -32,8 +32,8 @@ func (all *Repos) FindByGoPath(gopath string) *Repo { // enforces no duplicate gopath's func (all *Repos) add(newP *Repo) bool { - repolock.Lock() - defer repolock.Unlock() + reposMu.Lock() + defer reposMu.Unlock() for _, p := range all.Repos { if p.GoPath == newP.GoPath { diff --git a/repos.marshal.go b/repos.marshal.go deleted file mode 100644 index 738ae35..0000000 --- a/repos.marshal.go +++ /dev/null @@ -1,42 +0,0 @@ -package gitpb - -// todo: autogen this -// functions to import and export the protobuf - -import ( - "google.golang.org/protobuf/encoding/protojson" - "google.golang.org/protobuf/encoding/prototext" - "google.golang.org/protobuf/proto" -) - -// human readable JSON -func (r *Repo) FormatJSON() string { - return protojson.Format(r) -} - -// apparently this isn't supposed to be used? -// https://protobuf.dev/reference/go/faq/#unstable-text -// this is a shame because this is much nicer output than JSON Format() -func (r *Repo) FormatTEXT() string { - return prototext.Format(r) -} - -// marshal json -func (r *Repo) MarshalJSON() ([]byte, error) { - return protojson.Marshal(r) -} - -// unmarshal -func (r *Repo) UnmarshalJSON(data []byte) error { - return protojson.Unmarshal(data, r) -} - -// marshal to wire -func (r *Repo) Marshal() ([]byte, error) { - return proto.Marshal(r) -} - -// unmarshal from wire -func (r *Repo) Unmarshal(data []byte) error { - return proto.Unmarshal(data, r) -} diff --git a/repos.proto b/repos.proto deleted file mode 100644 index 9e02c08..0000000 --- a/repos.proto +++ /dev/null @@ -1,32 +0,0 @@ -syntax = "proto3"; - -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 "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 - - // 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; -} - -message Repos { - 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 Repo repos = 3; -} diff --git a/repos.sort.go b/repos.sort.go deleted file mode 100644 index 8bde45a..0000000 --- a/repos.sort.go +++ /dev/null @@ -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 repolock sync.RWMutex - -type RepoIterator struct { - sync.RWMutex - - packs []*Repo - index int -} - -// NewRepoIterator initializes a new iterator. -func NewRepoIterator(packs []*Repo) *RepoIterator { - return &RepoIterator{packs: packs} -} - -// Scan moves to the next element and returns false if there are no more packs. -func (it *RepoIterator) Scan() bool { - if it.index >= len(it.packs) { - return false - } - it.index++ - return true -} - -// Repo returns the current repo. -func (it *RepoIterator) Next() *Repo { - 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.Repo() -// fmt.Println("Repo UUID:", d.Uuid) -// } - -func (r *Repos) All() *RepoIterator { - repoPointers := r.selectAllRepo() - - iterator := NewRepoIterator(repoPointers) - return iterator -} - -func (r *Repos) SortByPath() *RepoIterator { - packs := r.selectAllRepo() - - sort.Sort(RepoByPath(packs)) - - iterator := NewRepoIterator(packs) - return iterator -} - -func (all *Repos) Len() int { - repolock.RLock() - defer repolock.RUnlock() - - return len(all.Repos) -} - -type RepoByPath []*Repo - -func (a RepoByPath) Len() int { return len(a) } -func (a RepoByPath) Less(i, j int) bool { return a[i].GoPath < a[j].GoPath } -func (a RepoByPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] } - -// safely returns a slice of pointers to the Repo protobufs -func (all *Repos) selectAllRepo() []*Repo { - repolock.RLock() - defer repolock.RUnlock() - - // Create a new slice to hold pointers to each Repo - var aRepos []*Repo - aRepos = make([]*Repo, len(all.Repos)) - for i, p := range all.Repos { - aRepos[i] = p // Copy pointers for safe iteration - } - - return aRepos -} -- cgit v1.2.3