Skip to content

Commit f8cd299

Browse files
authored
Fix parsing of deleted empty files without a/ b/ prefixes (#67)
When no prefixes are present, the parsing failed before and the orig filename was empty. This fixes it. This broke rendering diffs with deleted empty files in Sourcegraph.
1 parent 7ef5f68 commit f8cd299

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

diff/diff_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,56 @@ func TestParseMultiFileDiffHeaders(t *testing.T) {
783783
},
784784
},
785785
},
786+
{
787+
filename: "delete_empty_file.diff",
788+
wantDiffs: []*FileDiff{
789+
{
790+
OrigName: "Euler 0011/README.txt~",
791+
NewName: "/dev/null",
792+
Extended: []string{
793+
"diff --git Euler 0011/README.txt~ Euler 0011/README.txt~",
794+
"deleted file mode 100644",
795+
"index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000",
796+
},
797+
},
798+
{
799+
OrigName: "Euler 0011/Euler0011.cpp",
800+
NewName: "/dev/null",
801+
Extended: []string{
802+
"diff --git Euler 0011/Euler0011.cpp Euler 0011/Euler0011.cpp",
803+
"deleted file mode 100644",
804+
"index 6490416c8cb4bbf2afbafa66251a9eab983086d1..0000000000000000000000000000000000000000",
805+
},
806+
},
807+
{
808+
OrigName: "Euler 0011/README.txt~",
809+
NewName: "/dev/null",
810+
Extended: []string{
811+
"diff --git Euler 0011/README.txt~ Euler 0011/README.txt~",
812+
"deleted file mode 100644",
813+
"index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000",
814+
},
815+
},
816+
{
817+
OrigName: "Euler 0011/README.txt",
818+
NewName: "/dev/null",
819+
Extended: []string{
820+
"diff --git Euler 0011/README.txt Euler 0011/README.txt",
821+
"deleted file mode 100644",
822+
"index f8ea904baa27c54eb73cc02d5a555878b28672ff..0000000000000000000000000000000000000000",
823+
},
824+
},
825+
{
826+
OrigName: "Euler 0011/README.txt~",
827+
NewName: "/dev/null",
828+
Extended: []string{
829+
"diff --git Euler 0011/README.txt~ Euler 0011/README.txt~",
830+
"deleted file mode 100644",
831+
"index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000",
832+
},
833+
},
834+
},
835+
},
786836
}
787837
for _, test := range tests {
788838
t.Run(test.filename, func(t *testing.T) {

diff/parse.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,17 @@ func parseDiffGitArgs(diffArgs string) (string, string, bool) {
436436
// to handle that case here.
437437
first := diffArgs[:length/2]
438438
second := diffArgs[length/2+1:]
439-
if len(first) >= 3 && length%2 == 1 && first[1] == '/' && first[1:] == second[1:] {
440-
return first, second, true
439+
440+
// If the two strings could be equal, based on length, proceed.
441+
if length%2 == 1 {
442+
// If the name minus the a/ b/ prefixes is equal, proceed.
443+
if len(first) >= 3 && first[1] == '/' && first[1:] == second[1:] {
444+
return first, second, true
445+
}
446+
// If the names don't have the a/ and b/ prefixes and they're equal, proceed.
447+
if !(first[:2] == "a/" && second[:2] == "b/") && first == second {
448+
return first, second, true
449+
}
441450
}
442451

443452
// The syntax is (unfortunately) valid, but we could not extract

diff/testdata/delete_empty_file.diff

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
diff --git Euler 0011/README.txt~ Euler 0011/README.txt~
2+
deleted file mode 100644
3+
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000
4+
diff --git Euler 0011/Euler0011.cpp Euler 0011/Euler0011.cpp
5+
deleted file mode 100644
6+
index 6490416c8cb4bbf2afbafa66251a9eab983086d1..0000000000000000000000000000000000000000
7+
--- Euler 0011/Euler0011.cpp
8+
+++ /dev/null
9+
@@ -1,1 +0,0 @@
10+
-#include <iostream>
11+
\ No newline at end of file
12+
diff --git Euler 0011/README.txt~ Euler 0011/README.txt~
13+
deleted file mode 100644
14+
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000
15+
diff --git Euler 0011/README.txt Euler 0011/README.txt
16+
deleted file mode 100644
17+
index f8ea904baa27c54eb73cc02d5a555878b28672ff..0000000000000000000000000000000000000000
18+
--- Euler 0011/README.txt
19+
+++ /dev/null
20+
@@ -1,1 +0,0 @@
21+
-In the 20�20 grid below, four numbers along a diagonal line have been marked in red.
22+
\ No newline at end of file
23+
diff --git Euler 0011/README.txt~ Euler 0011/README.txt~
24+
deleted file mode 100644
25+
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000

0 commit comments

Comments
 (0)