-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: disregard diffs that are just reordering of lines
- Loading branch information
1 parent
5ff1e9a
commit ed96b99
Showing
7 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package utils | ||
|
||
import ( | ||
"bufio" | ||
"strings" | ||
) | ||
|
||
// IsReorderingDiff returns true if the diff only contains reordered lines. | ||
func IsReorderingDiff(diff string) bool { | ||
scanner := bufio.NewScanner(strings.NewReader(diff)) | ||
lines := make(map[string]struct{}) | ||
|
||
// discard the headers of the diff. | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if strings.HasPrefix(line, "@@") && strings.Contains(line, "@@") { | ||
break | ||
} | ||
} | ||
|
||
for scanner.Scan() { | ||
line := scanner.Text() | ||
|
||
// For each line addition, we try to find the corresponding line removal | ||
// and vice versa. | ||
// If all lines are paired, then it's a reordering change. | ||
|
||
if strings.HasPrefix(line, "+") { | ||
opposite := strings.Replace(line, "+", "-", 1) | ||
if _, ok := lines[opposite]; ok { | ||
delete(lines, opposite) | ||
} else { | ||
lines[line] = struct{}{} | ||
} | ||
} | ||
|
||
if strings.HasPrefix(line, "-") { | ||
opposite := strings.Replace(line, "-", "+", 1) | ||
if _, ok := lines[opposite]; ok { | ||
delete(lines, opposite) | ||
} else { | ||
lines[line] = struct{}{} | ||
} | ||
} | ||
|
||
} | ||
|
||
return len(lines) == 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package utils | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestIsReorderingDiff(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
diff string | ||
want bool | ||
}{ | ||
{ | ||
name: "reordered lines", | ||
diff: "testdata/reordered.diff", | ||
want: true, | ||
}, | ||
{ | ||
name: "new line addition with reordered lines", | ||
diff: "testdata/non-reordered.diff", | ||
want: false, | ||
}, | ||
{ | ||
name: "reordered lines", | ||
diff: "testdata/number-reordered.diff", | ||
want: true, | ||
}, | ||
{ | ||
name: "config change", | ||
diff: "testdata/config-change.diff", | ||
want: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
diff, err := os.ReadFile(tt.diff) | ||
if err != nil { | ||
t.Errorf("error reading file: %v", err) | ||
} | ||
|
||
if got := IsReorderingDiff(string(diff)); got != tt.want { | ||
t.Errorf("IsDiffAnOrderChange() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- a/config.yml | ||
+++ b/config.yml | ||
@@ -1,7 +1,8 @@ | ||
server: | ||
- host: localhost | ||
+ host: 0.0.0.0 | ||
port: 8080 | ||
|
||
database: | ||
name: myapp | ||
- user: admin | ||
+ user: app_user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- before | ||
+++ after | ||
@@ -145,11 +145,11 @@ | ||
"conditions": [ | ||
{ | ||
"status": "True", | ||
- "type": "Initialized" | ||
+ "type": "Ready" | ||
}, | ||
{ | ||
"status": "True", | ||
- "type": "Ready" | ||
+ "type": "Initialized" | ||
+ "color": "Blue" | ||
}, | ||
{ | ||
"status": "True", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- before | ||
+++ after | ||
@@ -1,5 +1,5 @@ | ||
+Third line | ||
First line | ||
Second line | ||
-Third line | ||
Fourth line | ||
Fifth line |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- before | ||
+++ after | ||
@@ -145,11 +145,11 @@ | ||
"conditions": [ | ||
{ | ||
"status": "True", | ||
- "type": "Initialized" | ||
+ "type": "Ready" | ||
}, | ||
{ | ||
"status": "True", | ||
- "type": "Ready" | ||
+ "type": "Initialized" | ||
}, | ||
{ | ||
"status": "True", |