summaryrefslogtreecommitdiff
path: root/stat.makeRemote.go
blob: 685238a8ab2c8c68569e00ba1446239c80035964 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package gitpb

import (
	"errors"
	"path/filepath"
	"slices"
	"strings"

	"go.wit.com/lib/cobol"
	"go.wit.com/lib/config"
	"go.wit.com/lib/env"
	"go.wit.com/log"
	timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)

// 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.Split(line, standardSeperator)
		if len(parts) != 5 {
			log.Printf("Repo: %s len(%d)\n", r.FullPath, len(parts))
			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]
		newstat.TreeHash = parts[1]
		t, err := cobol.GetTime(parts[2])
		_ = err
		if t != nil {
			newstat.AuthorTime = timestamppb.New(*t)
		}
		t, err = cobol.GetTime(parts[3])
		if t != nil {
			newstat.CommitTime = timestamppb.New(*t)
		}
		newstat.SanitizedSubject = parts[4]
		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", "log", "-n", "100", makeFmts(), "origin/HEAD"} // HEAD is _NOT_ always set
	cmd := []string{"git", "log", 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 errors.New("got nothing from git ls-remote")
	}
	var counter int
	for _, line := range cmdout.Stdout {
		line = strings.TrimSpace(line)
		parts := strings.Split(line, standardSeperator)
		if len(parts) != 5 {
			log.Printf("Repo: %s len(%d)\n", r.FullPath, len(parts))
			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)
		}
		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
		}

		newstat.TreeHash = parts[1]
		t, err := cobol.GetTime(parts[2])
		_ = err
		if t != nil {
			newstat.AuthorTime = timestamppb.New(*t)
		}
		t, err = cobol.GetTime(parts[3])
		if t != nil {
			newstat.CommitTime = timestamppb.New(*t)
		}
		newstat.SanitizedSubject = parts[4]
		stats.Append(newstat)
	}
	if counter > 0 {
		stats.SaveByHash()
	}
	return nil
}