Browse Source

Add FileDiff.CleanupDisabled option

Vadim Markovtsev 7 năm trước cách đây
mục cha
commit
def2a5302c
2 tập tin đã thay đổi với 69 bổ sung4 xóa
  1. 19 2
      diff.go
  2. 50 2
      diff_test.go

+ 19 - 2
diff.go

@@ -15,9 +15,12 @@ import (
 
 // FileDiff calculates the difference of files which were modified.
 type FileDiff struct {
+	CleanupDisabled bool
 }
 
 const (
+	ConfigFileDiffDisableCleanup = "FileDiff.NoCleanup"
+
 	DependencyFileDiff = "file_diff"
 )
 
@@ -42,10 +45,21 @@ func (diff *FileDiff) Requires() []string {
 }
 
 func (diff *FileDiff) ListConfigurationOptions() []ConfigurationOption {
-	return []ConfigurationOption{}
+	options := [...]ConfigurationOption{{
+		Name:        ConfigFileDiffDisableCleanup,
+		Description: "Do not apply additional heuristics to improve diffs.",
+		Flag:        "no-diff-cleanup",
+		Type:        BoolConfigurationOption,
+		Default:     false},
+	}
+	return options[:]
 }
 
-func (diff *FileDiff) Configure(facts map[string]interface{}) {}
+func (diff *FileDiff) Configure(facts map[string]interface{}) {
+	if val, exists := facts[ConfigFileDiffDisableCleanup].(bool); exists {
+		diff.CleanupDisabled = val
+	}
+}
 
 func (diff *FileDiff) Initialize(repository *git.Repository) {}
 
@@ -75,6 +89,9 @@ func (diff *FileDiff) Consume(deps map[string]interface{}) (map[string]interface
 			dmp := diffmatchpatch.New()
 			src, dst, _ := dmp.DiffLinesToRunes(str_from, str_to)
 			diffs := dmp.DiffMainRunes(src, dst, false)
+			if !diff.CleanupDisabled {
+				diffs = dmp.DiffCleanupSemanticLossless(diffs)
+			}
 			result[change.To.Name] = FileDiffData{
 				OldLinesOfCode: len(src),
 				NewLinesOfCode: len(dst),

+ 50 - 2
diff_test.go

@@ -24,8 +24,12 @@ func TestFileDiffMeta(t *testing.T) {
 	assert.Equal(t, len(fd.Requires()), 2)
 	assert.Equal(t, fd.Requires()[0], DependencyTreeChanges)
 	assert.Equal(t, fd.Requires()[1], DependencyBlobCache)
-	assert.Len(t, fd.ListConfigurationOptions(), 0)
-	fd.Configure(nil)
+	assert.Len(t, fd.ListConfigurationOptions(), 1)
+	assert.Equal(t, fd.ListConfigurationOptions()[0].Name, ConfigFileDiffDisableCleanup)
+	facts := map[string]interface{}{}
+	facts[ConfigFileDiffDisableCleanup] = true
+	fd.Configure(facts)
+	assert.True(t, fd.CleanupDisabled)
 }
 
 func TestFileDiffRegistration(t *testing.T) {
@@ -227,3 +231,47 @@ notifications:
 	assert.Equal(t, str, "")
 	assert.NotNil(t, err)
 }
+
+func TestFileDiffDarkMagic(t *testing.T) {
+  fd := fixtureFileDiff()
+	deps := map[string]interface{}{}
+	cache := map[plumbing.Hash]*object.Blob{}
+	hash := plumbing.NewHash("448eb3f312849b0ca766063d06b09481c987b309")
+	cache[hash], _ = testRepository.BlobObject(hash)  // 1.java
+	hash = plumbing.NewHash("3312c92f3e8bdfbbdb30bccb6acd1b85bc338dfc")
+	cache[hash], _ = testRepository.BlobObject(hash)  // 2.java
+	deps[DependencyBlobCache] = cache
+	changes := make(object.Changes, 1)
+	treeFrom, _ := testRepository.TreeObject(plumbing.NewHash(
+		"f02289bfe843388a1bb3c7dea210374082dd86b9"))
+	treeTo, _ := testRepository.TreeObject(plumbing.NewHash(
+		"eca91acf1fd828f20dcb653a061d8c97d965bc6c"))
+	changes[0] = &object.Change{From: object.ChangeEntry{
+		Name: "test.java",
+		Tree: treeFrom,
+		TreeEntry: object.TreeEntry{
+			Name: "test.java",
+			Mode: 0100644,
+			Hash: plumbing.NewHash("448eb3f312849b0ca766063d06b09481c987b309"),
+		},
+	}, To: object.ChangeEntry{
+		Name: "test.java",
+		Tree: treeTo,
+		TreeEntry: object.TreeEntry{
+			Name: "test.java",
+			Mode: 0100644,
+			Hash: plumbing.NewHash("3312c92f3e8bdfbbdb30bccb6acd1b85bc338dfc"),
+		},
+	}}
+	deps[DependencyTreeChanges] = changes
+	res, err := fd.Consume(deps)
+	assert.Nil(t, err)
+	magicDiffs := res[DependencyFileDiff].(map[string]FileDiffData)["test.java"]
+	fd.CleanupDisabled = true
+	res, err = fd.Consume(deps)
+	assert.Nil(t, err)
+	plainDiffs := res[DependencyFileDiff].(map[string]FileDiffData)["test.java"]
+	assert.NotEqual(t, magicDiffs.Diffs, plainDiffs.Diffs)
+	assert.Equal(t, magicDiffs.OldLinesOfCode, plainDiffs.OldLinesOfCode)
+	assert.Equal(t, magicDiffs.NewLinesOfCode, plainDiffs.NewLinesOfCode)
+}