fucking ya

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-11-27 14:08:47 -06:00
parent 2e3e4f98d3
commit 6dd1bba42b
4 changed files with 29 additions and 71 deletions

View File

@ -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 *Refs) 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 *Refs) FormatTEXT() string {
return prototext.Format(r)
}
// marshal json
func (r *Refs) MarshalJSON() ([]byte, error) {
return protojson.Marshal(r)
}
// unmarshal
func (r *Refs) UnmarshalJSON(data []byte) error {
return protojson.Unmarshal(data, r)
}
// marshal to wire
func (r *Refs) Marshal() ([]byte, error) {
return proto.Marshal(r)
}
// unmarshal from wire
func (r *Refs) Unmarshal(data []byte) error {
return proto.Unmarshal(data, r)
}

View File

@ -11,9 +11,3 @@ message Ref {
string author = 4; // git author
string subject = 5; // git subject
}
message Refs {
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 Ref refs = 3;
}

View File

@ -55,14 +55,14 @@ func (it *RefIterator) Ref() *Ref {
// fmt.Println("Ref UUID:", d.Uuid)
// }
func (r *Refs) All() *RefIterator {
func (r *Repo) All() *RefIterator {
repoPointers := r.selectAllRefs()
iterator := NewRefIterator(repoPointers)
return iterator
}
func (r *Refs) SortByName() *RefIterator {
func (r *Repo) SortByName() *RefIterator {
packs := r.selectAllRefs()
sort.Sort(RefsByName(packs))
@ -72,17 +72,17 @@ func (r *Refs) SortByName() *RefIterator {
}
// enforces no duplicate package names
func (r *Repo) AppendRef(newP *Ref) bool {
func (repo *Repo) AppendRef(newP *Ref) bool {
refslock.Lock()
defer refslock.Unlock()
for _, p := range r.Refs {
for _, p := range repo.Refs {
if p.RefName == newP.RefName {
return false
}
}
r.Refs = append(r.Refs, newP)
repo.Refs = append(repo.Refs, newP)
return true
}
@ -93,11 +93,11 @@ func (r *Ref) Age(newP *Ref) time.Duration {
}
// find a package by name
func (r *Refs) FindByName(name string) *Ref {
func (repo *Repo) FindRefByName(name string) *Ref {
refslock.RLock()
defer refslock.RUnlock()
for _, p := range r.Refs {
for _, p := range repo.Refs {
if p.RefName == name {
return p
}
@ -106,11 +106,11 @@ func (r *Refs) FindByName(name string) *Ref {
return nil
}
func (r *Refs) Len() int {
func (repo *Repo) LenRefs() int {
refslock.RLock()
defer refslock.RUnlock()
return len(r.Refs)
return len(repo.Refs)
}
type RefsByName []*Ref
@ -120,28 +120,28 @@ 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 (r *Refs) selectAllRefs() []*Ref {
func (repo *Repo) selectAllRefs() []*Ref {
refslock.RLock()
defer refslock.RUnlock()
// Create a new slice to hold pointers to each Ref
var allPacks []*Ref
allPacks = make([]*Ref, len(r.Refs))
for i, p := range r.Refs {
allPacks[i] = p // Copy pointers for safe iteration
var allRefs []*Ref
allRefs = make([]*Ref, len(repo.Refs))
for i, p := range repo.Refs {
allRefs[i] = p // Copy pointers for safe iteration
}
return allPacks
return allRefs
}
func (all *Refs) DeleteByHash(hash string) *Ref {
func (repo *Repo) DeleteByHash(hash string) *Ref {
refslock.Lock()
defer refslock.Unlock()
for i, _ := range all.Refs {
if all.Refs[i].Hash == hash {
all.Refs[i] = all.Refs[len(all.Refs)-1]
all.Refs = all.Refs[:len(all.Refs)-1]
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
}
}

View File

@ -16,7 +16,7 @@ import (
// Update version and timestamp.
// returns ok (ok == true if not found)
func (r *Repo) Update(path string) error {
func (r *Repo) Update() error {
// delete the old hash
// r.DeleteByHash(hash)
r.Refs = nil
@ -25,7 +25,7 @@ func (r *Repo) Update(path string) error {
format := strings.Join(tags, "_,,,_")
cmd := []string{"git", "for-each-ref", "--sort=taggerdate", "--format", format}
// log.Info("RUNNING:", strings.Join(cmd, " "))
result := shell.PathRunQuiet("", cmd)
result := shell.PathRunQuiet(r.FullPath, cmd)
if result.Error != nil {
log.Warn("git for-each-ref error:", result.Error)
return result.Error
@ -80,7 +80,12 @@ func getGitDateStamp(gitdefault string) time.Time {
return tagTime
}
func (r *Repos) NewGoPath(basepath string, gopath string) *Repo {
// 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
@ -93,4 +98,5 @@ func (r *Repos) NewGoPath(basepath string, gopath string) *Repo {
newr.Update()
r.Append(&newr)
return &newr
}