Skip to content

Commit 74293ea

Browse files
committed
patch_parse: handle absence of "index" header for new/deleted cases
This follows up on 11de594 which added support for parsing patches without extended headers (the "index <hash>..<hash> <mode>" line); issue libgit2#5267. We now allow transition from "file mode" state to "path" state directly if there is no "index", which will happen for patches adding or deleting files as demonstrated in added test case.
1 parent 931bd3b commit 74293ea

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/patch_parse.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ static const parse_header_transition transitions[] = {
407407

408408
{ "--- " , STATE_DIFF, STATE_PATH, parse_header_git_oldpath },
409409
{ "--- " , STATE_INDEX, STATE_PATH, parse_header_git_oldpath },
410+
{ "--- " , STATE_FILEMODE, STATE_PATH, parse_header_git_oldpath },
410411
{ "+++ " , STATE_PATH, STATE_END, parse_header_git_newpath },
411412
{ "GIT binary patch" , STATE_INDEX, STATE_END, NULL },
412413
{ "Binary files " , STATE_INDEX, STATE_END, NULL },

tests/diff/parse.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,29 @@ void test_diff_parse__no_extended_headers(void)
107107
git_diff_free(diff);
108108
}
109109

110+
void test_diff_parse__add_delete_no_index(void)
111+
{
112+
const char *content =
113+
"diff --git a/file.txt b/file.txt\n"
114+
"new file mode 100644\n"
115+
"--- /dev/null\n"
116+
"+++ b/file.txt\n"
117+
"@@ -0,0 +1,2 @@\n"
118+
"+one\n"
119+
"+two\n"
120+
"diff --git a/otherfile.txt b/otherfile.txt\n"
121+
"deleted file mode 100644\n"
122+
"--- a/otherfile.txt\n"
123+
"+++ /dev/null\n"
124+
"@@ -1,1 +0,0 @@\n"
125+
"-three\n";
126+
git_diff *diff;
127+
128+
cl_git_pass(git_diff_from_buffer(
129+
&diff, content, strlen(content)));
130+
git_diff_free(diff);
131+
}
132+
110133
void test_diff_parse__invalid_patches_fails(void)
111134
{
112135
test_parse_invalid_diff(PATCH_CORRUPT_MISSING_NEW_FILE);

0 commit comments

Comments
 (0)