Skip to content

Commit 554a3cc

Browse files
authored
Merge pull request #211 from vmarkovtsev/master
Defend Couples against many files in a commit
2 parents 33b228d + 54762a9 commit 554a3cc

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

leaves/couples.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ type CouplesResult struct {
5151
reversedPeopleDict []string
5252
}
5353

54+
const (
55+
// CouplesMaximumMeaningfulContextSize is the threshold on the number of files in a commit to
56+
// consider them as grouped together.
57+
CouplesMaximumMeaningfulContextSize = 1000
58+
)
59+
5460
type rename struct {
5561
FromName string
5662
ToName string
@@ -163,14 +169,16 @@ func (couples *CouplesAnalysis) Consume(deps map[string]interface{}) (map[string
163169
}
164170
}
165171
}
166-
for _, file := range context {
167-
for _, otherFile := range context {
168-
lane, exists := couples.files[file]
169-
if !exists {
170-
lane = map[string]int{}
171-
couples.files[file] = lane
172+
if len(context) <= CouplesMaximumMeaningfulContextSize {
173+
for _, file := range context {
174+
for _, otherFile := range context {
175+
lane, exists := couples.files[file]
176+
if !exists {
177+
lane = map[string]int{}
178+
couples.files[file] = lane
179+
}
180+
lane[otherFile]++
172181
}
173-
lane[otherFile]++
174182
}
175183
}
176184
return nil, nil

leaves/couples_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,27 @@ func TestCouplesConsumeFinalizeAuthorMissing(t *testing.T) {
319319
assert.Equal(t, cr.FilesMatrix[2][2], int64(3))
320320
}
321321

322+
func TestCouplesConsumeManyFiles(t *testing.T) {
323+
c := fixtureCouples()
324+
deps := map[string]interface{}{}
325+
deps[identity.DependencyAuthor] = 0
326+
deps[core.DependencyCommit], _ = test.Repository.CommitObject(gitplumbing.NewHash(
327+
"a3ee37f91f0d705ec9c41ae88426f0ae44b2fbc3"))
328+
deps[core.DependencyIsMerge] = false
329+
changes := make(object.Changes, CouplesMaximumMeaningfulContextSize+1)
330+
for i := 0; i < len(changes); i++ {
331+
changes[i] = &object.Change{
332+
From: object.ChangeEntry{},
333+
To: object.ChangeEntry{Name: string(i)},
334+
}
335+
}
336+
deps[plumbing.DependencyTreeChanges] = changes
337+
_, err := c.Consume(deps)
338+
assert.Nil(t, err)
339+
assert.Equal(t, len(c.people[0]), len(changes))
340+
assert.Len(t, c.files, 0)
341+
}
342+
322343
func TestCouplesFork(t *testing.T) {
323344
couples1 := fixtureCouples()
324345
clones := couples1.Fork(1)

0 commit comments

Comments
 (0)