Skip to content

Commit 2e39f4f

Browse files
authored
[fix] Recognize full-index git patch format correctly (#45)
* Validate full index patch format Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Add support for full-index patch format Signed-off-by: Uilian Ries <uilianr@jfrog.com> * Update validation for Windows Signed-off-by: Uilian Ries <uilianr@jfrog.com> --------- Signed-off-by: Uilian Ries <uilianr@jfrog.com>
1 parent 57adef3 commit 2e39f4f

File tree

5 files changed

+103
-1
lines changed

5 files changed

+103
-1
lines changed

patch_ng.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,9 @@ def _detect_type(self, p):
707707
break
708708
if p.header[idx].startswith(b'diff --git a/'):
709709
if (idx+1 < len(p.header)
710-
and re.match(b'(?:index \\w{7}..\\w{7} \\d{6}|new file mode \\d*)', p.header[idx+1])):
710+
and re.match(
711+
b'(?:index \\w{4,40}\\.\\.\\w{4,40}(?: \\d{6})?|new file mode \\d+|deleted file mode \\d+)',
712+
p.header[idx+1])):
711713
if DVCS:
712714
return GIT
713715

tests/patchformat/create.patch

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
From 8cacee878aff9a79603c87366d3eeefa68d80cf3 Mon Sep 17 00:00:00 2001
2+
From: John Doe <john.doe@email.com>
3+
Date: Wed, 1 Oct 2025 10:59:19 +0200
4+
Subject: [PATCH] Add quotes.txt
5+
6+
Signed-off-by: John Doe <john.doe@email.com>
7+
---
8+
quotes.txt | 1 +
9+
1 file changed, 1 insertion(+)
10+
create mode 100644 quotes.txt
11+
12+
diff --git a/quotes.txt b/quotes.txt
13+
new file mode 100644
14+
index 0000000000000000000000000000000000000000..edcfcc5846847fffa4986b8437f1d7a3bd8aa8cf
15+
--- /dev/null
16+
+++ b/quotes.txt
17+
@@ -0,0 +1 @@
18+
+habita fides ipsam plerumque obligat fidem.
19+
--
20+
2.51.0

tests/patchformat/remove.patch

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
From 22751ffae72c0b955b55e68ec0e25a80e434e890 Mon Sep 17 00:00:00 2001
2+
From: John Doe <john.doe@email.com>
3+
Date: Wed, 1 Oct 2025 10:59:47 +0200
4+
Subject: [PATCH] Remove quotes.txt
5+
6+
Signed-off-by: John Doe <john.doe@email.com>
7+
---
8+
quotes.txt | 1 -
9+
1 file changed, 1 deletion(-)
10+
delete mode 100644 quotes.txt
11+
12+
diff --git a/quotes.txt b/quotes.txt
13+
deleted file mode 100644
14+
index 22174d6b96bbdd674d38f8be446d22758f996aba..0000000000000000000000000000000000000000
15+
--- a/quotes.txt
16+
+++ /dev/null
17+
@@ -1 +0,0 @@
18+
-Si vis pacem cole justitiam.
19+
--
20+
2.51.0

tests/patchformat/update.patch

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
From 3360e00554544de3a68a227ac9eff5bb687da723 Mon Sep 17 00:00:00 2001
2+
From: John Doe <john.doe@email.com>
3+
Date: Wed, 1 Oct 2025 10:59:37 +0200
4+
Subject: [PATCH] Update quote
5+
6+
Signed-off-by: John Doe <john.doe@email.com>
7+
---
8+
quotes.txt | 2 +-
9+
1 file changed, 1 insertion(+), 1 deletion(-)
10+
11+
diff --git a/quotes.txt b/quotes.txt
12+
index edcfcc5846847fffa4986b8437f1d7a3bd8aa8cf..22174d6b96bbdd674d38f8be446d22758f996aba 100644
13+
--- a/quotes.txt
14+
+++ b/quotes.txt
15+
@@ -1 +1 @@
16+
-habita fides ipsam plerumque obligat fidem.
17+
+Si vis pacem cole justitiam.
18+
--
19+
2.51.0

tests/run_tests.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,47 @@ def test_pathstrip(self):
518518
self.assertEqual(patch_ng.pathstrip(b'path/name.diff', 1), b'name.diff')
519519
self.assertEqual(patch_ng.pathstrip(b'path/name.diff', 0), b'path/name.diff')
520520

521+
522+
class TestPatchFormat(unittest.TestCase):
523+
def setUp(self):
524+
self.save_cwd = os.getcwd()
525+
self.tmpdir = mkdtemp(prefix=self.__class__.__name__)
526+
shutil.copytree(join(TESTS, 'patchformat'), join(self.tmpdir, 'patchformat'))
527+
528+
def tearDown(self):
529+
os.chdir(self.save_cwd)
530+
remove_tree_force(self.tmpdir)
531+
532+
def test_handle_full_index_patch_format(self):
533+
"""Test that a full index patch format is handled correctly
534+
535+
When parsing a git patch with a full index line (40 hex chars),
536+
the patch type should be detected as GIT and the patch should be
537+
applied correctly (create, update, remove a file).
538+
"""
539+
540+
os.chdir(self.tmpdir)
541+
pto = patch_ng.fromfile(join(self.tmpdir, 'patchformat', 'create.patch'))
542+
self.assertEqual(len(pto), 1)
543+
self.assertEqual(pto.items[0].type, patch_ng.GIT)
544+
self.assertTrue(pto.apply())
545+
self.assertTrue(os.path.exists(join(self.tmpdir, 'quotes.txt')))
546+
547+
pto = patch_ng.fromfile(join(self.tmpdir, 'patchformat', 'update.patch'))
548+
self.assertEqual(len(pto), 1)
549+
self.assertEqual(pto.items[0].type, patch_ng.GIT)
550+
self.assertTrue(pto.apply())
551+
with open(join(self.tmpdir, 'quotes.txt'), 'rb') as f:
552+
content = f.read()
553+
self.assertTrue(b'Si vis pacem cole justitiam.' in content)
554+
555+
pto = patch_ng.fromfile(join(self.tmpdir, 'patchformat', 'remove.patch'))
556+
self.assertEqual(len(pto), 1)
557+
self.assertEqual(pto.items[0].type, patch_ng.GIT)
558+
self.assertTrue(pto.apply())
559+
self.assertFalse(os.path.exists(join(self.tmpdir, 'quotes.txt')))
560+
561+
521562
def remove_tree_force(folder):
522563
for root, _, files in os.walk(folder):
523564
for it in files:

0 commit comments

Comments
 (0)