@matheusbsbs / matheus-bagliano / commits / 3e8a5db966
Fork 0

git-apply: guess correct -p<n> value for non-git patches.

This enhances the third point in the previous commit. When applying a non-git patch that begins like this: --- 2.6.orig/mm/slab.c +++ 2.6/mm/slab.c @@ -N,M +L,K @@@ ... and if you are in 'mm' subdirectory, we notice that -p2 is the right option to use to apply the patch in file slab.c in the current directory (i.e. mm/slab.c) The guess function also knows about this pattern, where you would need to use -p0 if applying from the top-level: --- mm/slab.c +++ mm/slab.c @@ -N,M +L,K @@@ ... Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Feb 21, 2007 at 16:05 UTC 3e8a5db966c26a0a986161103d59683b909a6c78
2 files changed +111 -4
builtin-apply.c
+57 -2
@@ -28,6 +28,7 @@ static int newfd = -1;
28
29 static int unidiff_zero;
30 static int p_value = 1;
31 +static int p_value_known;
32 static int check_index;
33 static int write_index;
34 static int cached;
@@ -312,11 +313,54 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
313 return name;
314 }
315
316 +static int count_slashes(const char *cp)
317 +{
318 + int cnt = 0;
319 + char ch;
320 +
321 + while ((ch = *cp++))
322 + if (ch == '/')
323 + cnt++;
324 + return cnt;
325 +}
326 +
327 +/*
328 + * Given the string after "--- " or "+++ ", guess the appropriate
329 + * p_value for the given patch.
330 + */
331 +static int guess_p_value(const char *nameline)
332 +{
333 + char *name, *cp;
334 + int val = -1;
335 +
336 + if (is_dev_null(nameline))
337 + return -1;
338 + name = find_name(nameline, NULL, 0, TERM_SPACE | TERM_TAB);
339 + if (!name)
340 + return -1;
341 + cp = strchr(name, '/');
342 + if (!cp)
343 + val = 0;
344 + else if (prefix) {
345 + /*
346 + * Does it begin with "a/$our-prefix" and such? Then this is
347 + * very likely to apply to our directory.
348 + */
349 + if (!strncmp(name, prefix, prefix_length))
350 + val = count_slashes(prefix);
351 + else {
352 + cp++;
353 + if (!strncmp(cp, prefix, prefix_length))
354 + val = count_slashes(prefix) + 1;
355 + }
356 + }
357 + free(name);
358 + return val;
359 +}
360 +
361 /*
362 * Get the name etc info from the --/+++ lines of a traditional patch header
363 *
318 - * NOTE! This hardcodes "-p1" behaviour in filename detection.
319 - *
364 * FIXME! The end-of-filename heuristics are kind of screwy. For existing
365 * files, we can happily check the index for a match, but for creating a
366 * new file we should try to match whatever "patch" does. I have no idea.
@@ -327,6 +371,16 @@ static void parse_traditional_patch(const char *first, const char *second, struc
371
372 first += 4; /* skip "--- " */
373 second += 4; /* skip "+++ " */
374 + if (!p_value_known) {
375 + int p, q;
376 + p = guess_p_value(first);
377 + q = guess_p_value(second);
378 + if (p < 0) p = q;
379 + if (0 <= p && p == q) {
380 + p_value = p;
381 + p_value_known = 1;
382 + }
383 + }
384 if (is_dev_null(first)) {
385 patch->is_new = 1;
386 patch->is_delete = 0;
@@ -2656,6 +2710,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
2710 }
2711 if (!strncmp(arg, "-p", 2)) {
2712 p_value = atoi(arg + 2);
2713 + p_value_known = 1;
2714 continue;
2715 }
2716 if (!strcmp(arg, "--no-add")) {
t/t4119-apply-config.sh
+54 -2
@@ -19,7 +19,7 @@ test_expect_success setup '
19 '
20
21 # Also handcraft GNU diff output; note this has trailing whitespace.
22 -cat >gpatch.file <<\EOF
22 +cat >gpatch.file <<\EOF &&
23 --- file1 2007-02-21 01:04:24.000000000 -0800
24 +++ file1+ 2007-02-21 01:07:44.000000000 -0800
25 @@ -1 +1 @@
@@ -27,6 +27,12 @@ cat >gpatch.file <<\EOF
27 +B
28 EOF
29
30 +sed -e 's|file1|sub/&|' gpatch.file >gpatch-sub.file &&
31 +sed -e '
32 + /^--- /s|file1|a/sub/&|
33 + /^+++ /s|file1|b/sub/&|
34 +' gpatch.file >gpatch-ab-sub.file &&
35 +
36 test_expect_success 'apply --whitespace=strip' '
37
38 rm -f sub/file1 &&
@@ -124,7 +130,53 @@ test_expect_success 'same in subdir but with traditional patch input' '
130 git update-index --refresh &&
131
132 cd sub &&
127 - git apply -p0 ../gpatch.file &&
133 + git apply ../gpatch.file &&
134 + if grep " " file1
135 + then
136 + echo "Eh?"
137 + false
138 + elif grep B file1
139 + then
140 + echo Happy
141 + else
142 + echo "Huh?"
143 + false
144 + fi
145 +'
146 +
147 +test_expect_success 'same but with traditional patch input of depth 1' '
148 +
149 + cd "$D" &&
150 + git config apply.whitespace strip &&
151 + rm -f sub/file1 &&
152 + cp saved sub/file1 &&
153 + git update-index --refresh &&
154 +
155 + cd sub &&
156 + git apply ../gpatch-sub.file &&
157 + if grep " " file1
158 + then
159 + echo "Eh?"
160 + false
161 + elif grep B file1
162 + then
163 + echo Happy
164 + else
165 + echo "Huh?"
166 + false
167 + fi
168 +'
169 +
170 +test_expect_success 'same but with traditional patch input of depth 2' '
171 +
172 + cd "$D" &&
173 + git config apply.whitespace strip &&
174 + rm -f sub/file1 &&
175 + cp saved sub/file1 &&
176 + git update-index --refresh &&
177 +
178 + cd sub &&
179 + git apply ../gpatch-ab-sub.file &&
180 if grep " " file1
181 then
182 echo "Eh?"