git apply: apply patches with -pN (N>1) properly for some cases.
PATCH v2 1/1 git apply: apply patches with -pN (N>1) properly for some cases.
In some cases when apply patch, there maybe no '--- path/to/old' and '+++ path/to/new' lines in the patch file, and the only way to get filename is from the 'diff --git path/to/old path/to/new' line in the patch header. For example, differ of binary files, pure mode changes, removing or adding empty files.
The function 'git_header_name' is broken and casue patch apply failed with -pN (N>1) for these cases. This patch fixed it and provide a testcase. I also add a testcase for fancy filename.
Signed-off-by Jiang Xin <[email protected]>
--- builtin/apply.c | 3 ++- t/t4120-apply-popt.sh | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletions(-)
builtin/apply.c +2 -1
@@ -1126,6 +1126,7 @@ static char *git_header_name(char *line, int llen)* form. */ for (len =0 ; ; len++) { +int nslash = p_value;switch (name[len]) {default :continue ;
@@ -1137,7 +1138,7 @@ static char *git_header_name(char *line, int llen)char c = *second++;if (c =='\n' )return NULL ; -if (c =='/' ) +if (c =='/' && --nslash <=0 )break ; }if (second[len] =='\n' && !memcmp (name, second, len)) {
t/t4120-apply-popt.sh +11 -0
@@ -50,10 +50,21 @@ test_expect_success 'apply (-p2) traditional diff with funny filenames' ' test_cmp expected file1 ' +test_expect_success 'apply git diff with -p2 and fancy filename' ' + cp file1.saved file1 && + git apply -p2 patch.escaped +' + test_expect_success 'apply with too large -p and fancy filename' ' cp file1.saved file1 && test_must_fail git apply --stat -p3 patch.escaped 2>err && grep "removing 3 leading" err ' +test_expect_success 'apply git diff with -p2 and use default name from header' ' + sed -e "/^\(---\|+++\) / d" patch.file > patch.newheader && + cp file1.saved file1 && + git apply -p2 patch.newheader +' + test_done
Signature · 1 line
1.7.3.2.245.g03276.dirty