diff options
| author | Jeff Carr <[email protected]> | 2025-11-03 13:58:41 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-11-03 13:58:41 -0600 |
| commit | 61fac349e9cfb6dd60af2eec219886fce2f4a216 (patch) | |
| tree | 27e4fcf3b393ff51d0344d48dec54f0359ba23ca | |
| parent | ed9c0917ae16ad2a5e2343799a16645497482918 (diff) | |
more refs update and check functions
| -rw-r--r-- | stat.makeRemote.go | 137 | ||||
| -rw-r--r-- | stat.makeRemoteRefs.go | 5 | ||||
| -rw-r--r-- | stat.updateRefs.go | 3 |
3 files changed, 137 insertions, 8 deletions
diff --git a/stat.makeRemote.go b/stat.makeRemote.go new file mode 100644 index 0000000..a5d746a --- /dev/null +++ b/stat.makeRemote.go @@ -0,0 +1,137 @@ +package gitpb + +import ( + "errors" + "path/filepath" + "strings" + + "go.wit.com/lib/config" + "go.wit.com/lib/env" + "go.wit.com/log" +) + +// loads what is on disk already. that is all +func (r *Repo) LoadRemote(remoteName string) (*Stats, error) { + fullname := filepath.Join(r.FullPath, ".git", remoteName+".pb") + stats := NewStats() + stats.Filename = fullname + err := config.ForceCreatePB(stats) + return stats, err +} + +var standardFmts []string = []string{"H", "T", "at", "ct", "f"} +var standardSeperator string = "___FORGE___" + +func makeFmts() string { + // fmts := strings.Fields(config.GetPanic("standardFmts")) + // fmts := strings.Fields(config.GetPanic("standardSeperator")) + var all []string + for _, fmtvar := range standardFmts { + all = append(all, "%"+fmtvar) + } + return "--format=" + strings.Join(all, standardSeperator) +} + +// makes a new file. File must be empty at start +func (r *Repo) MakeRemote(remoteName string) (*Stats, error) { + stats, err := r.LoadRemote(remoteName) + if err != nil { + return stats, err + } + if stats.Len() != 0 { + return stats, errors.New("refs file was already created") + } + cmd := []string{"git", "log", "-n", "100", makeFmts(), "origin/HEAD"} // HEAD is _NOT_ always set + if env.True("stats") { + log.Info("STATS VERBOSE Run:", cmd) + } + cmdout := r.Run(cmd) + if len(cmdout.Stdout) == 0 { + return stats, errors.New("got nothing from git ls-remote") + } + var counter int + for _, line := range cmdout.Stdout { + line = strings.TrimSpace(line) + parts := strings.Fields(line) + if len(parts) != 2 { + log.Printf("Repo: %s\n", r.FullPath) + log.Printf("CMD: %v\n", cmd) + log.Printf("LINE:%s\n", line) + return stats, errors.New(line) + } + if env.True("stats") { + log.Printf("LINE:%v %d %s\n", parts, counter, r.FullPath) + } + counter += 1 + newstat := new(Stat) + newstat.Hash = parts[0] + stats.Append(newstat) + } + if counter > 0 { + stats.SaveByHash() + } + return stats, nil +} + +// looks for new hashes. Run this after 'git fetch' +func (r *Repo) UpdateRemote(remoteName string) error { + stats, err := r.LoadRemote(remoteName) + if err != nil { + return err + } + if stats.Len() == 0 { + return errors.New("need to make file instead") + } + /* + cmd := []string{"git", "ls-remote", remoteName} // must use 'master' as queried from the git server // GO has 250k remote refs + if env.True("stats") { + log.Info("STATS VERBOSE Run:", cmd) + } + cmdout := r.Run(cmd) + if len(cmdout.Stdout) == 0 { + return errors.New("got nothing back") + } + var ticker int + var counter int + var allnew []*Stat + for _, line := range cmdout.Stdout { + line = strings.TrimSpace(line) + parts := strings.Fields(line) + if len(parts) != 2 { + log.Printf("Repo: %s\n", r.FullPath) + log.Printf("CMD: %v\n", cmd) + log.Printf("LINE:%s\n", line) + return errors.New(line) + } + if env.True("stats") { + log.Printf("LINE:%v %d %s\n", parts, ticker, r.FullPath) + } + // refpath := filepath.Join("refs/remote", remoteName) + // if strings.HasPrefix(parts[1], refpath) { + // this ref is not from the remote + // continue + // } + counter += 1 + newstat := new(Stat) + newstat.Hash = parts[0] + n, found := slices.BinarySearchFunc(stats.Stats, newstat, func(a, b *Stat) int { + return strings.Compare(a.Hash, b.Hash) + }) + _ = n + + if found { + // log.Info("found", n) + continue + } + ticker += 1 + log.Info("not found", newstat) + allnew = append(allnew, newstat) + } + if ticker > 0 { + stats.Stats = append(stats.Stats, allnew...) + stats.SaveByHash() + return errors.New(fmt.Sprintf("len(%d), ticker(%d) counter(%d) refs changed", stats.Len(), ticker, counter)) + } + */ + return nil +} diff --git a/stat.makeRemoteRefs.go b/stat.makeRemoteRefs.go index e47938a..b1a5736 100644 --- a/stat.makeRemoteRefs.go +++ b/stat.makeRemoteRefs.go @@ -21,11 +21,6 @@ func (r *Repo) LoadRemoteRefs(remoteName string) (*Stats, error) { return stats, err } -// cmd := []string{"git", "show-ref"} // must use 'master' as queried from the git server -// cmd := []string{"git", "log", "-n", "100", makeFmts(), "origin/HEAD"} // HEAD is _NOT_ always set -// cmd := []string{"git", "show-ref"} // must use 'master' as queried from the git server -// cmd := []string{"git", "log", "-n", "100", makeFmts(), "origin/HEAD"} // HEAD is _NOT_ always set - // returns err if anything changes or anything is wrong (todo: should these be different?) func (r *Repo) MakeRemoteRefs(remoteName string) (*Stats, error) { stats, err := r.LoadRemoteRefs(remoteName) diff --git a/stat.updateRefs.go b/stat.updateRefs.go index 1405303..4676fe9 100644 --- a/stat.updateRefs.go +++ b/stat.updateRefs.go @@ -20,9 +20,6 @@ func (r *Repo) LoadRefs() (*Stats, error) { return stats, err } -// cmd := []string{"git", "log", "-n", "100", makeFmts(), "origin/HEAD"} // HEAD is _NOT_ always set -// cmd := []string{"git", "log", "-n", "100", makeFmts(), "origin/HEAD"} // HEAD is _NOT_ always set - // parses "git show-ref" into a protobuf // returns err if anything changes or anything is wrong (todo: should these be different?) // todo: purge old ones |
