summaryrefslogtreecommitdiff
path: root/doStats.go
blob: 38ceb15dceeec5a0664a70c68d400602ffd193e0 (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
152
153
154
155
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"errors"
	"strings"

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

func doVerify() (string, error) {
	var allerr error
	if argv.Verify.All != nil {
		for r := range me.forge.Repos.IterByFullPath() {
			if r.Stats == nil {
				_, err := doStats(r)
				allerr = errors.Join(allerr, err)
			}
		}
		return "verify ran everywhere", nil
	}

	repo := workingDirToRepo()
	if repo == nil {
		return "no repo", errors.New("working dir isn't a repo I know about")
	}

	s, err := doStats(repo)
	allerr = errors.Join(allerr, err)

	user, err := refHash(repo, "heads/jcarr")
	allerr = errors.Join(allerr, err)
	master, err := refHash(repo, "remotes/origin/master")
	allerr = errors.Join(allerr, err)
	HEAD, err := refHash(repo, "remotes/origin/HEAD")
	allerr = errors.Join(allerr, err)
	log.Printf("user=%10.10s master=%10.10s HEAD=%10.10s\n", user, master, HEAD)
	safeDelete(repo, user, HEAD) // delete user if safely contained in HEAD

	return s, allerr
}

func doStats(r *gitpb.Repo) (string, error) {
	var allerr error
	pb, err := r.LoadStats()
	if err == nil {
		log.Info("LoadStats() ok", pb.Filename)
	} else {
		log.Info("LoadStats() err", err)
	}
	allerr = errors.Join(allerr, err)

	if hasOrigin(r) {
		log.Info("todo: detect origin")
	}

	// collect the stats
	counter, err := last100(r, pb)
	allerr = errors.Join(allerr, err)
	s := log.Sprintf("found %d new hashes", counter)

	for stat := range pb.IterAll() {
		if stat.PatchId == "" {
			stat.PatchId, err = r.FindPatchIdByHash(stat.Hash)
			allerr = errors.Join(allerr, err)
			log.Info("patchid for hash", stat.Hash, "is", stat.PatchId)
			counter += 1
		}
	}

	if counter > 0 {
		pb.SaveVerbose()
	}
	// build()
	return s, nil
}

func collectStats(r *gitpb.Repo, pb *gitpb.Stats) error {
	return nil
}

// git show-ref refs/heads/devel
func refHash(r *gitpb.Repo, name string) (string, error) {
	var hash string
	refname := "refs/" + name
	cmd := []string{"git", "show-ref", refname}
	cmdout := r.Run(cmd)
	for i, line := range cmdout.Stdout {
		parts := strings.Fields(line)
		if config.If("stats") {
			log.Info(parts[0], "LINE:", i, line)
		}
		hash = parts[0]
	}
	if len(cmdout.Stdout) != 1 {
		return "", errors.New("no refname:" + name)
	}
	return hash, nil
}

// git show-ref --verify refs/heads/devel
func hasOrigin(r *gitpb.Repo) bool {
	// git show-ref refs/heads/devel
	return true
}

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

func last100(r *gitpb.Repo, pb *gitpb.Stats) (int, error) {
	var allerr error
	var counter int
	cmd := []string{"git", "log", "-n", "100", makeFmts(), "origin/" + r.GetMasterBranchName()}
	if config.If("stats") {
		log.Info("Run:", cmd)
	}
	cmdout := r.Run(cmd)
	for i, line := range cmdout.Stdout {
		parts := strings.Split(line, standardSeperator)
		hash := parts[0]
		if config.If("stats") {
			log.Printf("LINE:%8.8s %2d %v\n", hash, i, parts[1:])
		}
		found := pb.FindByHash(hash)
		if found != nil {
			// already have this hash
			continue
		}
		counter += 1
		astat := new(gitpb.Stat)
		astat.Hash = hash
		ctime, err := cobol.GetTime(parts[2])
		allerr = errors.Join(allerr, err)
		astat.Ctime = timestamppb.New(*ctime)
		astat.Subject = parts[4]
		pb.Append(astat)
	}
	return counter, allerr
}