Skip to content

Commit 6bcb735

Browse files
authored
Merge pull request libgit2#5035 from pks-t/pks/diff-with-space-in-filenames
patch_parse: fix parsing addition/deletion of file with space
2 parents 18e836c + 9d65360 commit 6bcb735

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

src/patch_parse.c

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -921,21 +921,15 @@ static int check_filenames(git_patch_parsed *patch)
921921
return git_parse_err("missing old path");
922922

923923
/* Ensure (non-renamed) paths match */
924-
if (check_header_names(
925-
patch->header_old_path, patch->old_path, "old", added) < 0 ||
926-
check_header_names(
927-
patch->header_new_path, patch->new_path, "new", deleted) < 0)
924+
if (check_header_names(patch->header_old_path, patch->old_path, "old", added) < 0 ||
925+
check_header_names(patch->header_new_path, patch->new_path, "new", deleted) < 0)
928926
return -1;
929927

930-
prefixed_old = (!added && patch->old_path) ? patch->old_path :
931-
patch->header_old_path;
932-
prefixed_new = (!deleted && patch->new_path) ? patch->new_path :
933-
patch->header_new_path;
928+
prefixed_old = (!added && patch->old_path) ? patch->old_path : patch->header_old_path;
929+
prefixed_new = (!deleted && patch->new_path) ? patch->new_path : patch->header_new_path;
934930

935-
if (check_prefix(
936-
&patch->old_prefix, &old_prefixlen, patch, prefixed_old) < 0 ||
937-
check_prefix(
938-
&patch->new_prefix, &new_prefixlen, patch, prefixed_new) < 0)
931+
if ((prefixed_old && check_prefix(&patch->old_prefix, &old_prefixlen, patch, prefixed_old) < 0) ||
932+
(prefixed_new && check_prefix(&patch->new_prefix, &new_prefixlen, patch, prefixed_new) < 0))
939933
return -1;
940934

941935
/* Prefer the rename filenames as they are unambiguous and unprefixed */
@@ -950,7 +944,7 @@ static int check_filenames(git_patch_parsed *patch)
950944
patch->base.delta->new_file.path = prefixed_new + new_prefixlen;
951945

952946
if (!patch->base.delta->old_file.path &&
953-
!patch->base.delta->new_file.path)
947+
!patch->base.delta->new_file.path)
954948
return git_parse_err("git diff header lacks old / new paths");
955949

956950
return 0;
@@ -964,14 +958,14 @@ static int check_patch(git_patch_parsed *patch)
964958
return -1;
965959

966960
if (delta->old_file.path &&
967-
delta->status != GIT_DELTA_DELETED &&
968-
!delta->new_file.mode)
961+
delta->status != GIT_DELTA_DELETED &&
962+
!delta->new_file.mode)
969963
delta->new_file.mode = delta->old_file.mode;
970964

971965
if (delta->status == GIT_DELTA_MODIFIED &&
972-
!(delta->flags & GIT_DIFF_FLAG_BINARY) &&
973-
delta->new_file.mode == delta->old_file.mode &&
974-
git_array_size(patch->base.hunks) == 0)
966+
!(delta->flags & GIT_DIFF_FLAG_BINARY) &&
967+
delta->new_file.mode == delta->old_file.mode &&
968+
git_array_size(patch->base.hunks) == 0)
975969
return git_parse_err("patch with no hunks");
976970

977971
if (delta->status == GIT_DELTA_ADDED) {

tests/diff/parse.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,21 @@ void test_diff_parse__lineinfo(void)
359359
git_patch_free(patch);
360360
git_diff_free(diff);
361361
}
362+
363+
void test_diff_parse__new_file_with_space(void)
364+
{
365+
const char *content = PATCH_ORIGINAL_NEW_FILE_WITH_SPACE;
366+
git_patch *patch;
367+
git_diff *diff;
368+
369+
cl_git_pass(git_diff_from_buffer(&diff, content, strlen(content)));
370+
cl_git_pass(git_patch_from_diff((git_patch **) &patch, diff, 0));
371+
372+
cl_assert_equal_p(patch->diff_opts.old_prefix, NULL);
373+
cl_assert_equal_p(patch->delta->old_file.path, NULL);
374+
cl_assert_equal_s(patch->diff_opts.new_prefix, "b/");
375+
cl_assert_equal_s(patch->delta->new_file.path, "sp ace.txt");
376+
377+
git_patch_free(patch);
378+
git_diff_free(diff);
379+
}

tests/patch/patch_common.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,3 +841,12 @@
841841
"diff --git a/binary.bin b/binary.bin\n" \
842842
"index 27184d9..7c94f9e 100644\n" \
843843
"Binary files a/binary.bin and b/binary.bin differ\n"
844+
845+
#define PATCH_ORIGINAL_NEW_FILE_WITH_SPACE \
846+
"diff --git a/sp ace.txt b/sp ace.txt\n" \
847+
"new file mode 100644\n" \
848+
"index 000000000..789819226\n" \
849+
"--- /dev/null\n" \
850+
"+++ b/sp ace.txt\n" \
851+
"@@ -0,0 +1 @@\n" \
852+
"+a\n"

0 commit comments

Comments
 (0)