start full refactor to have repo be here
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
44caec2e7f
commit
2e3e4f98d3
9
Makefile
9
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
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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
|
||||
}
|
21
update.go
21
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue