Skip to content

Commit a3faffc

Browse files
Remove newline from RE_DIFF_GIT_{DELETED,NEW}_FILE
RE_DIFF_GIT_DELETED_FILE and RE_DIFF_GIT_NEW_FILE match on a complete line pattern 'r^$' so including `\n` in the pattern leads to the pattern not matching. Remove the `\n`.
1 parent bc8a9e9 commit a3faffc

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

tests/samples/git_delete.diff

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
diff --git a/somefile.c b/somefile.c
2+
deleted file mode 100644
3+
index abcdefbbb8..0000000000
4+
--- a/somefile.c
5+
+++ /dev/null
6+
@@ -1,10 +0,0 @@
7+
-/**
8+
- * @file somefile.c
9+
- */
10+
-#include <stdio.h>
11+
-
12+
-int main(int argc, cahr *argv[])
13+
-{
14+
- printf("Hello World\n");
15+
- return 0;
16+
-}

tests/test_parser.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,16 @@ def test_parse_filename_with_spaces(self):
325325
self.assertTrue(res[0].is_added_file)
326326
self.assertEqual(res[0].path, 'has spaces/t.sql')
327327

328+
def test_deleted_file(self):
329+
filename = os.path.join(self.samples_dir, 'samples/git_delete.diff')
330+
with open(filename) as f:
331+
res = PatchSet(f)
332+
333+
self.assertEqual(len(res), 1)
334+
self.assertEqual(res[0].source_file, 'a/somefile.c')
335+
self.assertEqual(res[0].target_file, '/dev/null')
336+
self.assertTrue(res[0].is_removed_file)
337+
328338
def test_diff_lines_linenos(self):
329339
with open(self.sample_file, 'rb') as diff_file:
330340
res = PatchSet(diff_file, encoding='utf-8')

unidiff/constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@
4242
r'^diff --git (?P<source>[^\t\n]+) (?P<target>[^\t\n]+)')
4343

4444
# check diff git new file marker `deleted file mode 100644`
45-
RE_DIFF_GIT_DELETED_FILE = re.compile(r'^deleted file mode \d+\n$')
45+
RE_DIFF_GIT_DELETED_FILE = re.compile(r'^deleted file mode \d+$')
4646

4747
# check diff git new file marker `new file mode 100644`
48-
RE_DIFF_GIT_NEW_FILE = re.compile(r'^new file mode \d+\n$')
48+
RE_DIFF_GIT_NEW_FILE = re.compile(r'^new file mode \d+$')
4949

5050

5151
# @@ (source offset, length) (target offset, length) @@ (section header)

0 commit comments

Comments
 (0)