Skip to content

Commit

Permalink
Merge pull request #90 from matiasb/make-git-prefix-optional
Browse files Browse the repository at this point in the history
Make git prefix optional in regex
  • Loading branch information
matiasb authored Jan 28, 2022
2 parents 3b5cf9a + a4dcf22 commit 512359b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
30 changes: 30 additions & 0 deletions tests/samples/git_no_prefix.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
diff --git file1 file1
deleted file mode 100644
index 42f90fd..0000000
--- file1
+++ /dev/null
@@ -1,3 +0,0 @@
-line11
-line12
-line13
diff --git file2 file2
index c337bf1..1cb02b9 100644
--- file2
+++ file2
@@ -4,0 +5,3 @@ line24
+line24n
+line24n2
+line24n3
@@ -15,0 +19,3 @@ line215
+line215n
+line215n2
+line215n3
diff --git file3 file3
new file mode 100644
index 0000000..632e269
--- /dev/null
+++ file3
@@ -0,0 +1,3 @@
+line31
+line32
+line33
22 changes: 22 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,28 @@ def test_parse_round_trip_with_binary_files_in_diff(self):
res2 = PatchSet(str(res1))
self.assertEqual(res1, res2)

def test_parse_diff_git_no_prefix(self):
utf8_file = os.path.join(self.samples_dir, 'samples/git_no_prefix.diff')
with open(utf8_file, 'r') as diff_file:
res = PatchSet(diff_file)

self.assertEqual(len(res), 3)

self.assertEqual(res[0].source_file, 'file1')
self.assertEqual(res[0].target_file, '/dev/null')
self.assertTrue(res[0].is_removed_file)
self.assertEqual(res[0].path, 'file1')

self.assertEqual(res[1].source_file, 'file2')
self.assertEqual(res[1].target_file, 'file2')
self.assertTrue(res[1].is_modified_file)
self.assertEqual(res[1].path, 'file2')

self.assertEqual(res[2].source_file, '/dev/null')
self.assertEqual(res[2].target_file, 'file3')
self.assertTrue(res[2].is_added_file)
self.assertEqual(res[2].path, 'file3')

def test_diff_lines_linenos(self):
with open(self.sample_file, 'rb') as diff_file:
res = PatchSet(diff_file, encoding='utf-8')
Expand Down
2 changes: 1 addition & 1 deletion unidiff/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

# check diff git line for git renamed files support
RE_DIFF_GIT_HEADER = re.compile(
r'^diff --git (?P<source>a/[^\t\n]+) (?P<target>b/[^\t\n]+)')
r'^diff --git (?P<source>(a/)?[^\t\n]+) (?P<target>(b/)?[^\t\n]+)')

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

0 comments on commit 512359b

Please sign in to comment.