summaryrefslogtreecommitdiff
path: root/stat.updateRefs.go
blob: 4676fe96707aae91b4f75ad01f55bc29a8e5f0e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
}