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 } // 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 }