summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-11-27 13:57:17 -0600
committerJeff Carr <[email protected]>2024-11-27 13:57:17 -0600
commit2e3e4f98d33d8794a1c8a6aa13e6c9fa92fbcadb (patch)
treecd6222e4dc98e9de94981ade9f15f34030cc3d61
parent44caec2e7fc59697ef01835aacf8a03171a42bea (diff)
start full refactor to have repo be here
Signed-off-by: Jeff Carr <[email protected]>
-rw-r--r--Makefile9
-rw-r--r--refs.sort.go2
-rw-r--r--repo.marshal.go42
-rw-r--r--repo.proto25
-rw-r--r--repo.sort.go150
-rw-r--r--update.go21
6 files changed, 245 insertions, 4 deletions
diff --git a/Makefile b/Makefile
index 191b07b..362df41 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@
# go install
-all: refs.pb.go godep.pb.go vet
+all: refs.pb.go godep.pb.go repo.pb.go vet
vet: lint
GO111MODULE=off go vet
@@ -36,3 +36,10 @@ 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
+
+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=Mrepo.proto=go.wit.com/lib/protobuf/gitpb \
+ repo.proto
diff --git a/refs.sort.go b/refs.sort.go
index 94d3124..b48bce5 100644
--- a/refs.sort.go
+++ b/refs.sort.go
@@ -72,7 +72,7 @@ func (r *Refs) SortByName() *RefIterator {
}
// enforces no duplicate package names
-func (r *Refs) Append(newP *Ref) bool {
+func (r *Repo) AppendRef(newP *Ref) bool {
refslock.Lock()
defer refslock.Unlock()
diff --git a/repo.marshal.go b/repo.marshal.go
new file mode 100644
index 0000000..738ae35
--- /dev/null
+++ b/repo.marshal.go
@@ -0,0 +1,42 @@
+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/repo.proto b/repo.proto
new file mode 100644
index 0000000..9ee5b2c
--- /dev/null
+++ b/repo.proto
@@ -0,0 +1,25 @@
+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 "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'
+ string goPath = 2; // the logical path as used by golang: 'go.wit.com/apps/helloworld'
+ bool library = 3; // if this is a golang library
+ repeated Ref refs = 4;
+ repeated GoDep GoDeps = 5;
+ google.protobuf.Timestamp lastPull = 6; // last time a git pull was done
+}
+
+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/repo.sort.go b/repo.sort.go
new file mode 100644
index 0000000..6495676
--- /dev/null
+++ b/repo.sort.go
@@ -0,0 +1,150 @@
+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) Repo() *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) SortByName() *RepoIterator {
+ packs := r.selectAllRepo()
+
+ sort.Sort(RepoByName(packs))
+
+ iterator := NewRepoIterator(packs)
+ return iterator
+}
+
+// enforces no duplicate package names
+func (all *Repos) Append(newP *Repo) bool {
+ repolock.Lock()
+ defer repolock.Unlock()
+
+ for _, p := range all.Repos {
+ if p.GoPath == newP.GoPath {
+ return false
+ }
+ }
+
+ all.Repos = append(all.Repos, newP)
+ return true
+}
+
+/*
+// returns time.Duration since last Update()
+func (r *Repo) LastPull() time.Duration {
+ t := time.Since(r.LastPull.AsTime())
+ return t
+}
+*/
+
+// find a package by gopath
+func (all *Repos) FindByPath(gopath string) *Repo {
+ repolock.RLock()
+ defer repolock.RUnlock()
+
+ for _, p := range all.Repos {
+ if p.GoPath == gopath {
+ return p
+ }
+ }
+
+ return nil
+}
+
+func (all *Repos) Len() int {
+ repolock.RLock()
+ defer repolock.RUnlock()
+
+ return len(all.Repos)
+}
+
+type RepoByName []*Repo
+
+func (a RepoByName) Len() int { return len(a) }
+func (a RepoByName) Less(i, j int) bool { return a[i].GoPath < a[j].GoPath }
+func (a RepoByName) 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
+}
+
+func (all *Repos) DeleteByPath(gopath string) *Repo {
+ repolock.Lock()
+ defer repolock.Unlock()
+
+ for i, _ := range all.Repos {
+ if all.Repos[i].GoPath == gopath {
+ all.Repos[i] = all.Repos[len(all.Repos)-1]
+ all.Repos = all.Repos[:len(all.Repos)-1]
+ return nil
+ }
+ }
+ return nil
+}
diff --git a/update.go b/update.go
index 5074cf3..953ad91 100644
--- a/update.go
+++ b/update.go
@@ -1,6 +1,7 @@
package gitpb
import (
+ "path/filepath"
"slices"
"strings"
"time"
@@ -15,7 +16,7 @@ import (
// Update version and timestamp.
// returns ok (ok == true if not found)
-func (r *Refs) Update(path string) error {
+func (r *Repo) Update(path string) error {
// delete the old hash
// r.DeleteByHash(hash)
r.Refs = nil
@@ -54,6 +55,7 @@ func (r *Refs) Update(path string) error {
subject = parts[4]
}
+
newr := Ref{
Hash: hash,
Subject: subject,
@@ -61,7 +63,7 @@ func (r *Refs) Update(path string) error {
Ctime: timestamppb.New(ctime),
}
- r.Append(&newr)
+ r.AppendRef(&newr)
return nil
}
@@ -77,3 +79,18 @@ func getGitDateStamp(gitdefault string) time.Time {
}
return tagTime
}
+
+func (r *Repos) NewGoPath(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)
+}