File: ref_group_builder.go

package info (click to toggle)
git-sizer 1.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 616 kB
  • sloc: sh: 100; makefile: 61
file content (344 lines) | stat: -rw-r--r-- 9,405 bytes parent folder | download
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package refopts

import (
	"fmt"
	"strings"

	"github.com/spf13/pflag"

	"github.com/github/git-sizer/git"
	"github.com/github/git-sizer/sizes"
)

// Configger is an abstraction for a thing that can read gitconfig.
type Configger interface {
	GetConfig(prefix string) (*git.Config, error)
}

// RefGroupBuilder handles reference-related options and puts together
// a `sizes.RefGrouper` to be used by the main part of the program.
type RefGroupBuilder struct {
	topLevelGroup *refGroup
	groups        map[sizes.RefGroupSymbol]*refGroup
}

// NewRefGroupBuilder creates and returns a `RefGroupBuilder`
// instance.
func NewRefGroupBuilder(configger Configger) (*RefGroupBuilder, error) {
	tlg := refGroup{
		RefGroup: sizes.RefGroup{
			Symbol: "",
			Name:   "Refs to walk",
		},
	}

	rgb := RefGroupBuilder{
		topLevelGroup: &tlg,
		groups: map[sizes.RefGroupSymbol]*refGroup{
			"": &tlg,
		},
	}

	rgb.initializeStandardRefgroups()
	if err := rgb.readRefgroupsFromGitconfig(configger); err != nil {
		return nil, err
	}

	return &rgb, nil
}

// getGroup returns the `refGroup` for the symbol with the specified
// name, first creating it (and any missing parents) if needed.
func (rgb *RefGroupBuilder) getGroup(symbol sizes.RefGroupSymbol) *refGroup {
	if rg, ok := rgb.groups[symbol]; ok {
		return rg
	}

	parentSymbol := parentName(symbol)
	parent := rgb.getGroup(parentSymbol)

	rg := refGroup{
		RefGroup: sizes.RefGroup{
			Symbol: symbol,
		},
		parent: parent,
	}

	rgb.groups[symbol] = &rg
	parent.subgroups = append(parent.subgroups, &rg)
	return &rg
}

// parentName returns the symbol of the refgroup that is the parent of
// `symbol`, or "" if `symbol` is the top-level group.
func parentName(symbol sizes.RefGroupSymbol) sizes.RefGroupSymbol {
	i := strings.LastIndexByte(string(symbol), '.')
	if i == -1 {
		return ""
	}
	return symbol[:i]
}

// initializeStandardRefgroups initializes the built-in refgroups
// ("branches", "tags", etc).
func (rgb *RefGroupBuilder) initializeStandardRefgroups() {
	initializeGroup := func(
		symbol sizes.RefGroupSymbol, name string, filter git.ReferenceFilter,
	) {
		rg := rgb.getGroup(symbol)
		rg.Name = name
		rg.filter = filter
	}

	initializeGroup("branches", "Branches", git.PrefixFilter("refs/heads/"))
	initializeGroup("tags", "Tags", git.PrefixFilter("refs/tags/"))
	initializeGroup("remotes", "Remote-tracking refs", git.PrefixFilter("refs/remotes/"))
	initializeGroup("pulls", "Pull request refs", git.PrefixFilter("refs/pull/"))

	filter, err := git.RegexpFilter(`refs/changes/\d{2}/\d+/\d+`)
	if err != nil {
		panic("internal error")
	}
	initializeGroup("changes", "Changeset refs", filter)

	initializeGroup("notes", "Git notes", git.PrefixFilter("refs/notes/"))

	filter, err = git.RegexpFilter(`refs/stash`)
	if err != nil {
		panic("internal error")
	}
	initializeGroup("stash", "Git stash", filter)
}

// readRefgroupsFromGitconfig reads any refgroups defined in the
// gitconfig into `rgb`. Any configuration settings for the built-in
// groups are added to the pre-existing definitions of those groups.
func (rgb *RefGroupBuilder) readRefgroupsFromGitconfig(configger Configger) error {
	if configger == nil {
		// At this point, it is not yet certain that the command was
		// run inside a Git repository. If not, ignore this option
		// (the command will error out anyway).
		return nil
	}

	config, err := configger.GetConfig("refgroup")
	if err != nil {
		return err
	}

	seen := make(map[sizes.RefGroupSymbol]bool)
	for _, entry := range config.Entries {
		symbol, _ := splitKey(entry.Key)
		if symbol == "" || seen[symbol] {
			// The point of this loop is only to find
			// _which_ groups are defined, so we only need
			// to visit each one once.
			continue
		}

		rg := rgb.getGroup(symbol)
		if err := rg.augmentFromConfig(configger); err != nil {
			return err
		}

		seen[symbol] = true
	}

	return nil
}

// splitKey splits `key`, which is part of a gitconfig key, into the
// refgroup symbol to which it applies and the field name within that
// section.
func splitKey(key string) (sizes.RefGroupSymbol, string) {
	i := strings.LastIndexByte(key, '.')
	if i == -1 {
		return "", key
	}
	return sizes.RefGroupSymbol(key[:i]), key[i+1:]
}

// AddRefopts adds the reference-related options to `flags`.
func (rgb *RefGroupBuilder) AddRefopts(flags *pflag.FlagSet) {
	flags.Var(
		&filterValue{rgb, git.Include, "", false}, "include",
		"include specified references",
	)

	flag := flags.VarPF(
		&filterValue{rgb, git.Include, "", true}, "include-regexp", "",
		"include references matching the specified regular expression",
	)
	flag.Hidden = true
	flag.Deprecated = "use --include=/REGEXP/"

	flags.Var(
		&filterValue{rgb, git.Exclude, "", false}, "exclude",
		"exclude specified references",
	)

	flag = flags.VarPF(
		&filterValue{rgb, git.Exclude, "", true}, "exclude-regexp", "",
		"exclude references matching the specified regular expression",
	)
	flag.Hidden = true
	flag.Deprecated = "use --exclude=/REGEXP/"

	flag = flags.VarPF(
		&filterValue{rgb, git.Include, "refs/heads", false}, "branches", "",
		"process all branches",
	)
	flag.NoOptDefVal = "true"

	flag = flags.VarPF(
		&filterValue{rgb, git.Exclude, "refs/heads", false}, "no-branches", "",
		"exclude all branches",
	)
	flag.NoOptDefVal = "true"

	flag = flags.VarPF(
		&filterValue{rgb, git.Include, "refs/tags", false}, "tags", "",
		"process all tags",
	)
	flag.NoOptDefVal = "true"

	flag = flags.VarPF(
		&filterValue{rgb, git.Exclude, "refs/tags", false}, "no-tags", "",
		"exclude all tags",
	)
	flag.NoOptDefVal = "true"

	flag = flags.VarPF(
		&filterValue{rgb, git.Include, "refs/remotes", false}, "remotes", "",
		"process all remote-tracking references",
	)
	flag.NoOptDefVal = "true"

	flag = flags.VarPF(
		&filterValue{rgb, git.Exclude, "refs/remotes", false}, "no-remotes", "",
		"exclude all remote-tracking references",
	)
	flag.NoOptDefVal = "true"

	flag = flags.VarPF(
		&filterValue{rgb, git.Include, "refs/notes", false}, "notes", "",
		"process all git-notes references",
	)
	flag.NoOptDefVal = "true"

	flag = flags.VarPF(
		&filterValue{rgb, git.Exclude, "refs/notes", false}, "no-notes", "",
		"exclude all git-notes references",
	)
	flag.NoOptDefVal = "true"

	flag = flags.VarPF(
		&filterValue{rgb, git.Include, "refs/stash", true}, "stash", "",
		"process refs/stash",
	)
	flag.NoOptDefVal = "true"

	flag = flags.VarPF(
		&filterValue{rgb, git.Exclude, "refs/stash", true}, "no-stash", "",
		"exclude refs/stash",
	)
	flag.NoOptDefVal = "true"

	flag = flags.VarPF(
		&filterGroupValue{rgb}, "refgroup", "",
		"process references in refgroup defined by gitconfig",
	)
	flag.Hidden = true
	flag.Deprecated = "use --include=@REFGROUP"
}

// Finish collects the information gained from processing the options
// and returns a `sizes.RefGrouper`.
func (rgb *RefGroupBuilder) Finish() (sizes.RefGrouper, error) {
	if rgb.topLevelGroup.filter == nil {
		rgb.topLevelGroup.filter = git.AllReferencesFilter
	}

	refGrouper := refGrouper{
		topLevelGroup: rgb.topLevelGroup,
	}

	if err := refGrouper.fillInTree(refGrouper.topLevelGroup); err != nil {
		return nil, err
	}

	if refGrouper.topLevelGroup.filter != nil {
		refGrouper.ignoredRefGroup = &sizes.RefGroup{
			Symbol: "ignored",
			Name:   "Ignored",
		}
		refGrouper.refGroups = append(refGrouper.refGroups, *refGrouper.ignoredRefGroup)
	}

	return &refGrouper, nil
}

// refGrouper is a `sizes.RefGrouper` based on a hierarchy of nested
// refgroups.
type refGrouper struct {
	topLevelGroup *refGroup
	refGroups     []sizes.RefGroup

	// ignoredRefGroup, if set, is the reference group for
	// tallying references that don't match at all.
	ignoredRefGroup *sizes.RefGroup
}

// fillInTree processes the refgroups in the tree rooted at `rg`,
// setting default names where they are missing, verifying that they
// are all defined, adding "Other" groups where needed, and adding the
// refgroups in depth-first-traversal order to `refGrouper.refGroups`.
func (refGrouper *refGrouper) fillInTree(rg *refGroup) error {
	if rg.Name == "" {
		_, rg.Name = splitKey(string(rg.Symbol))
	}

	if rg.filter == nil && len(rg.subgroups) == 0 {
		return fmt.Errorf("refgroup '%s' is not defined", rg.Symbol)
	}

	refGrouper.refGroups = append(refGrouper.refGroups, rg.RefGroup)

	for _, rg := range rg.subgroups {
		if err := refGrouper.fillInTree(rg); err != nil {
			return err
		}
	}

	if len(rg.subgroups) != 0 {
		var otherSymbol sizes.RefGroupSymbol
		if rg.Symbol == "" {
			otherSymbol = "other"
		} else {
			otherSymbol = sizes.RefGroupSymbol(fmt.Sprintf("%s.other", rg.Symbol))
		}
		rg.otherRefGroup = &sizes.RefGroup{
			Symbol: otherSymbol,
			Name:   "Other",
		}
		refGrouper.refGroups = append(refGrouper.refGroups, *rg.otherRefGroup)
	}

	return nil
}

// Categorize decides whether to walk the reference named `refname`
// and which refgroup(s) it should be counted in.
func (refGrouper *refGrouper) Categorize(refname string) (bool, []sizes.RefGroupSymbol) {
	walk, symbols := refGrouper.topLevelGroup.collectSymbols(refname)
	if !walk && refGrouper.ignoredRefGroup != nil {
		symbols = append(symbols, refGrouper.ignoredRefGroup.Symbol)
	}
	return walk, symbols
}

// Groups returns a list of all defined refgroups, in the order that
// they should be output.
func (refGrouper *refGrouper) Groups() []sizes.RefGroup {
	return refGrouper.refGroups
}