@@ -84,6 +84,28 @@ def test_fix_patch_headers_counts_dashed_content_lines(self):
8484 self .assertIn ("---removed--line\n " , fixed )
8585 self .assertIn ("+++added++line\n " , fixed )
8686
87+ def test_fix_patch_headers_counts_dashed_line_ending_a_hunk (self ):
88+ # A dashed content line as the hunk's LAST body line is followed
89+ # directly by the next hunk header, which looks exactly like a
90+ # file header pair to a plain "peek for @@" check: it must still
91+ # be counted, or the (correct) input header is rewritten wrong
92+ # and `patch` rejects the whole diff.
93+ diff = (
94+ "@@ -2,3 +2,2 @@\n id INT\n );\n --- old comment\n @@ -8,1 +8,1 @@\n --- tail\n +-- new\n "
95+ )
96+ fixed = _fix_patch_headers (diff )
97+ self .assertIn ("@@ -2,3 +2,2 @@" , fixed )
98+ self .assertIn ("--- old comment\n " , fixed )
99+
100+ def test_fix_patch_headers_counts_dashed_pair_ending_a_hunk (self ):
101+ # Same, for a ---/+++ pair whose content carries no space after
102+ # the marker (real file headers always name a path).
103+ diff = "@@ -1,3 +1,3 @@\n a\n ---removed\n +++added\n @@ -20,1 +20,1 @@\n z\n "
104+ fixed = _fix_patch_headers (diff )
105+ self .assertIn ("@@ -1,2 +1,2 @@" , fixed )
106+ self .assertIn ("---removed\n " , fixed )
107+ self .assertIn ("+++added\n " , fixed )
108+
87109 def test_fix_patch_headers_multifile_headers_not_counted (self ):
88110 # A new file's ---/+++ header pair (followed by a hunk header) ends
89111 # the previous hunk's body instead of being miscounted as content.
@@ -1146,6 +1168,27 @@ def test_dashed_content_lines_apply(self):
11461168 with open (path ) as f :
11471169 self .assertEqual (f .read (), "a\n ++added\n zzz\n c\n " )
11481170
1171+ def test_dashed_content_line_ending_a_hunk_applies (self ):
1172+ # A removed line whose content starts with "--" as the LAST body
1173+ # line of a hunk, with a second hunk following: the header must
1174+ # keep its counts or `patch` rejects the diff as malformed.
1175+ with tempfile .TemporaryDirectory () as d :
1176+ path = os .path .join (d , "schema.sql" )
1177+ with open (path , "w" ) as f :
1178+ f .write ("CREATE TABLE t (\n id INT\n );\n -- old comment\n SELECT 1;\n -- tail\n " )
1179+ ctx , _ = make_ctx ()
1180+ diff = (
1181+ "--- a/schema.sql\n +++ b/schema.sql\n "
1182+ "@@ -2,3 +2,2 @@\n id INT\n );\n --- old comment\n "
1183+ "@@ -6,1 +6,1 @@\n --- tail\n +-- new tail\n "
1184+ )
1185+ result = Edit ().run ({"path" : path , "new_str" : diff , "diff" : True }, ctx )
1186+ self .assertIn ("Diff successfully applied" , result )
1187+ with open (path ) as f :
1188+ self .assertEqual (
1189+ f .read (), "CREATE TABLE t (\n id INT\n );\n SELECT 1;\n -- new tail\n "
1190+ )
1191+
11491192 def test_directory_multifile_diff_applies (self ):
11501193 """A directory path (with trailing slash) + a multi-file unified
11511194 diff edits several files at once, mirroring gptel's directory
0 commit comments