diff options
Diffstat (limited to 'stat.updateRefs.go')
| -rw-r--r-- | stat.updateRefs.go | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/stat.updateRefs.go b/stat.updateRefs.go new file mode 100644 index 0000000..1405303 --- /dev/null +++ b/stat.updateRefs.go @@ -0,0 +1,66 @@ +package gitpb + +import ( + "errors" + "fmt" + "path/filepath" + "strings" + + "go.wit.com/lib/config" + "go.wit.com/lib/env" + "go.wit.com/log" +) + +// returns err if anything changes or anything is wrong (todo: should these be different?) +func (r *Repo) LoadRefs() (*Stats, error) { + fullname := filepath.Join(r.FullPath, ".git", "refs.pb") + stats := NewStats() + stats.Filename = fullname + err := config.ForceCreatePB(stats) + 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 +func (r *Repo) UpdateRefs(stats *Stats) error { + cmd := []string{"git", "show-ref"} // must use 'master' as queried from the git server + 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 counter int // any new refs? + 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, counter, r.FullPath) + } + newstat := new(Stat) + newstat.Hash = parts[0] + // newstat.Name = parts[1] + + teststat := stats.FindByHash(newstat.Hash) + if teststat == nil { + counter += 1 + stats.Append(newstat) + } + } + if counter > 0 { + stats.SaveByHash() + return errors.New(fmt.Sprintf("len(%d) refs changed: (%d)", stats.Len(), counter)) + } + return nil +} |
