Skip to content

Commit dd35151

Browse files
committed
maintner: track hashtag edits on GerritMeta, make HashtagEdits method faster
Updates golang/go#24836 Change-Id: I75cae7de574af7525964bdf420328d3e553a044c Reviewed-on: https://go-review.googlesource.com/107305 Reviewed-by: Andrew Bonventre <[email protected]>
1 parent e8681e4 commit dd35151

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

maintner/gerrit.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -776,10 +776,7 @@ func (gp *GerritProject) processMutation(gm *maintpb.GerritMutation) {
776776

777777
if clv.Version == 0 { // is a meta commit
778778
gp.noteDirtyCL(cl) // needs processing at end of sync
779-
cl.Meta = &GerritMeta{
780-
Commit: gc,
781-
CL: cl,
782-
}
779+
cl.Meta = newGerritMeta(gc, cl)
783780
} else {
784781
cl.Commit = gc
785782
cl.Version = clv.Version
@@ -837,10 +834,7 @@ func (gp *GerritProject) finishProcessingCL(cl *GerritCL) {
837834
cl.Private = true
838835
}
839836
if gc.GerritMeta == nil {
840-
gc.GerritMeta = &GerritMeta{
841-
Commit: gc,
842-
CL: cl,
843-
}
837+
gc.GerritMeta = newGerritMeta(gc, cl)
844838
}
845839
if foundStatus == "" {
846840
foundStatus = getGerritStatus(gc)
@@ -1280,6 +1274,24 @@ type GerritMeta struct {
12801274

12811275
// CL is the Gerrit CL this metadata is for.
12821276
CL *GerritCL
1277+
1278+
flags gerritMetaFlags
1279+
}
1280+
1281+
type gerritMetaFlags uint8
1282+
1283+
const (
1284+
// metaFlagHashtagEdit indicates that the meta commit edits the hashtags on the commit.
1285+
metaFlagHashtagEdit gerritMetaFlags = 1 << iota
1286+
)
1287+
1288+
func newGerritMeta(gc *GitCommit, cl *GerritCL) *GerritMeta {
1289+
m := &GerritMeta{Commit: gc, CL: cl}
1290+
1291+
if msg := m.Commit.Msg; strings.Contains(msg, "autogenerated:gerrit:setHashtag") && m.ActionTag() == "autogenerated:gerrit:setHashtag" {
1292+
m.flags |= metaFlagHashtagEdit
1293+
}
1294+
return m
12831295
}
12841296

12851297
// Footer returns the "key: value" lines at the base of the commit.
@@ -1307,16 +1319,13 @@ func (m *GerritMeta) ActionTag() string {
13071319
// HashtagEdits returns the hashtags added and removed by this meta commit,
13081320
// and whether this meta commit actually modified hashtags.
13091321
func (m *GerritMeta) HashtagEdits() (added, removed GerritHashtags, ok bool) {
1310-
msg := m.Commit.Msg
1311-
// Fast path to return early on the majority of non-hashtag-mutating commits:
1312-
if !strings.Contains(msg, "autogenerated:gerrit:setHashtag") {
1313-
return
1314-
}
1315-
// Parse it more properly.
1316-
if m.ActionTag() != "autogenerated:gerrit:setHashtag" {
1322+
// Return early for the majority of meta commits that don't edit hashtags.
1323+
if m.flags&metaFlagHashtagEdit == 0 {
13171324
return
13181325
}
13191326

1327+
msg := m.Commit.Msg
1328+
13201329
// Parse lines of form:
13211330
//
13221331
// Hashtag removed: bar

maintner/gerrit_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ Tag: autogenerated:gerrit:otherTag
388388

389389
func TestParseHashtags(t *testing.T) {
390390
for i, tt := range hashtagTests {
391-
meta := &GerritMeta{Commit: &GitCommit{Msg: tt.commit}}
391+
meta := newGerritMeta(&GitCommit{Msg: tt.commit}, nil)
392392
added, removed, ok := meta.HashtagEdits()
393393
if ok != (added != "" || removed != "") {
394394
t.Errorf("%d. inconsistent return values: %q, %q, %v", i, added, removed, ok)

0 commit comments

Comments
 (0)