Pārlūkot izejas kodu

Merge pull request #211 from vmarkovtsev/master

Defend Couples against many files in a commit
Vadim Markovtsev 6 gadi atpakaļ
vecāks
revīzija
554a3cc8c0
2 mainītis faili ar 36 papildinājumiem un 7 dzēšanām
  1. 15 7
      leaves/couples.go
  2. 21 0
      leaves/couples_test.go

+ 15 - 7
leaves/couples.go

@@ -51,6 +51,12 @@ type CouplesResult struct {
 	reversedPeopleDict []string
 }
 
+const (
+	// CouplesMaximumMeaningfulContextSize is the threshold on the number of files in a commit to
+	// consider them as grouped together.
+	CouplesMaximumMeaningfulContextSize = 1000
+)
+
 type rename struct {
 	FromName string
 	ToName   string
@@ -163,14 +169,16 @@ func (couples *CouplesAnalysis) Consume(deps map[string]interface{}) (map[string
 			}
 		}
 	}
-	for _, file := range context {
-		for _, otherFile := range context {
-			lane, exists := couples.files[file]
-			if !exists {
-				lane = map[string]int{}
-				couples.files[file] = lane
+	if len(context) <= CouplesMaximumMeaningfulContextSize {
+		for _, file := range context {
+			for _, otherFile := range context {
+				lane, exists := couples.files[file]
+				if !exists {
+					lane = map[string]int{}
+					couples.files[file] = lane
+				}
+				lane[otherFile]++
 			}
-			lane[otherFile]++
 		}
 	}
 	return nil, nil

+ 21 - 0
leaves/couples_test.go

@@ -319,6 +319,27 @@ func TestCouplesConsumeFinalizeAuthorMissing(t *testing.T) {
 	assert.Equal(t, cr.FilesMatrix[2][2], int64(3))
 }
 
+func TestCouplesConsumeManyFiles(t *testing.T) {
+	c := fixtureCouples()
+	deps := map[string]interface{}{}
+	deps[identity.DependencyAuthor] = 0
+	deps[core.DependencyCommit], _ = test.Repository.CommitObject(gitplumbing.NewHash(
+		"a3ee37f91f0d705ec9c41ae88426f0ae44b2fbc3"))
+	deps[core.DependencyIsMerge] = false
+	changes := make(object.Changes, CouplesMaximumMeaningfulContextSize+1)
+	for i := 0; i < len(changes); i++ {
+		changes[i] = &object.Change{
+			From: object.ChangeEntry{},
+			To:   object.ChangeEntry{Name: string(i)},
+		}
+	}
+	deps[plumbing.DependencyTreeChanges] = changes
+	_, err := c.Consume(deps)
+	assert.Nil(t, err)
+	assert.Equal(t, len(c.people[0]), len(changes))
+	assert.Len(t, c.files, 0)
+}
+
 func TestCouplesFork(t *testing.T) {
 	couples1 := fixtureCouples()
 	clones := couples1.Fork(1)