@matheusbsbs / matheus-bagliano / commits / ea56188a24
Fork 0

Update git-apply to use C-style quoting for funny pathnames.

This is a backport so that maintenance branch can understand diff output that uses C-style quoting produced by newer tools. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Oct 16, 2005 at 11:05 UTC ea56188a2488eff449661e2b4de656f38c88e62d
1 file changed +169 -39
apply.c
+169 -39
@@ -15,6 +15,7 @@
15 #include <ctype.h>
16 #include <fnmatch.h>
17 #include "cache.h"
18 +#include "quote.h"
19
20 // We default to the merge behaviour, since that's what most people would
21 // expect.
@@ -142,6 +143,35 @@ static char * find_name(const char *line, char *def, int p_value, int terminate)
143 const char *start = line;
144 char *name;
145
146 + if (*line == '"') {
147 + /* Proposed "new-style" GNU patch/diff format; see
148 + * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2
149 + */
150 + name = unquote_c_style(line, NULL);
151 + if (name) {
152 + char *cp = name;
153 + while (p_value) {
154 + cp = strchr(name, '/');
155 + if (!cp)
156 + break;
157 + cp++;
158 + p_value--;
159 + }
160 + if (cp) {
161 + /* name can later be freed, so we need
162 + * to memmove, not just return cp
163 + */
164 + memmove(name, cp, strlen(cp) + 1);
165 + free(def);
166 + return name;
167 + }
168 + else {
169 + free(name);
170 + name = NULL;
171 + }
172 + }
173 + }
174 +
175 for (;;) {
176 char c = *line;
177
@@ -231,37 +261,29 @@ static int gitdiff_hdrend(const char *line, struct patch *patch)
261 */
262 static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
263 {
234 - int len;
235 - const char *name;
236 -
264 if (!orig_name && !isnull)
265 return find_name(line, NULL, 1, 0);
266
240 - name = "/dev/null";
241 - len = 9;
267 if (orig_name) {
268 + int len;
269 + const char *name;
270 + char *another;
271 name = orig_name;
272 len = strlen(name);
273 if (isnull)
274 die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
247 - }
248 -
249 - if (*name == '/')
250 - goto absolute_path;
251 -
252 - for (;;) {
253 - char c = *line++;
254 - if (c == '\n')
255 - break;
256 - if (c != '/')
257 - continue;
258 -absolute_path:
259 - if (memcmp(line, name, len) || line[len] != '\n')
260 - break;
275 + another = find_name(line, NULL, 1, 0);
276 + if (!another || memcmp(another, name, len))
277 + die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
278 + free(another);
279 return orig_name;
280 }
263 - die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
264 - return NULL;
281 + else {
282 + /* expect "/dev/null" */
283 + if (memcmp("/dev/null", line, 9) || line[9] != '\n')
284 + die("git-apply: bad git-diff - expected /dev/null on line %d", linenr);
285 + return NULL;
286 + }
287 }
288
289 static int gitdiff_oldname(const char *line, struct patch *patch)
@@ -353,29 +375,124 @@ static int gitdiff_unrecognized(const char *line, struct patch *patch)
375 return -1;
376 }
377
356 -static char *git_header_name(char *line)
378 +static const char *stop_at_slash(const char *line, int llen)
379 +{
380 + int i;
381 +
382 + for (i = 0; i < llen; i++) {
383 + int ch = line[i];
384 + if (ch == '/')
385 + return line + i;
386 + }
387 + return NULL;
388 +}
389 +
390 +/* This is to extract the same name that appears on "diff --git"
391 + * line. We do not find and return anything if it is a rename
392 + * patch, and it is OK because we will find the name elsewhere.
393 + * We need to reliably find name only when it is mode-change only,
394 + * creation or deletion of an empty file. In any of these cases,
395 + * both sides are the same name under a/ and b/ respectively.
396 + */
397 +static char *git_header_name(char *line, int llen)
398 {
399 int len;
359 - char *name, *second;
400 + const char *name;
401 + const char *second = NULL;
402
361 - /*
362 - * Find the first '/'
363 - */
364 - name = line;
365 - for (;;) {
366 - char c = *name++;
367 - if (c == '\n')
403 + line += strlen("diff --git ");
404 + llen -= strlen("diff --git ");
405 +
406 + if (*line == '"') {
407 + const char *cp;
408 + char *first = unquote_c_style(line, &second);
409 + if (!first)
410 return NULL;
369 - if (c == '/')
370 - break;
411 +
412 + /* advance to the first slash */
413 + cp = stop_at_slash(first, strlen(first));
414 + if (!cp || cp == first) {
415 + /* we do not accept absolute paths */
416 + free_first_and_fail:
417 + free(first);
418 + return NULL;
419 + }
420 + len = strlen(cp+1);
421 + memmove(first, cp+1, len+1); /* including NUL */
422 +
423 + /* second points at one past closing dq of name.
424 + * find the second name.
425 + */
426 + while ((second < line + llen) && isspace(*second))
427 + second++;
428 +
429 + if (line + llen <= second)
430 + goto free_first_and_fail;
431 + if (*second == '"') {
432 + char *sp = unquote_c_style(second, NULL);
433 + if (!sp)
434 + goto free_first_and_fail;
435 + cp = stop_at_slash(sp, strlen(sp));
436 + if (!cp || cp == sp) {
437 + free_both_and_fail:
438 + free(sp);
439 + goto free_first_and_fail;
440 + }
441 + /* They must match, otherwise ignore */
442 + if (strcmp(cp+1, first))
443 + goto free_both_and_fail;
444 + free(sp);
445 + return first;
446 + }
447 +
448 + /* unquoted second */
449 + cp = stop_at_slash(second, line + llen - second);
450 + if (!cp || cp == second)
451 + goto free_first_and_fail;
452 + cp++;
453 + if (line + llen - cp != len + 1 ||
454 + memcmp(first, cp, len))
455 + goto free_first_and_fail;
456 + return first;
457 }
458
373 - /*
374 - * We don't accept absolute paths (/dev/null) as possibly valid
375 - */
376 - if (name == line+1)
459 + /* unquoted first name */
460 + name = stop_at_slash(line, llen);
461 + if (!name || name == line)
462 return NULL;
463
464 + name++;
465 +
466 + /* since the first name is unquoted, a dq if exists must be
467 + * the beginning of the second name.
468 + */
469 + for (second = name; second < line + llen; second++) {
470 + if (*second == '"') {
471 + const char *cp = second;
472 + const char *np;
473 + char *sp = unquote_c_style(second, NULL);
474 +
475 + if (!sp)
476 + return NULL;
477 + np = stop_at_slash(sp, strlen(sp));
478 + if (!np || np == sp) {
479 + free_second_and_fail:
480 + free(sp);
481 + return NULL;
482 + }
483 + np++;
484 + len = strlen(np);
485 + if (len < cp - name &&
486 + !strncmp(np, name, len) &&
487 + isspace(name[len])) {
488 + /* Good */
489 + memmove(sp, np, len + 1);
490 + return sp;
491 + }
492 + goto free_second_and_fail;
493 + }
494 + }
495 +
496 /*
497 * Accept a name only if it shows up twice, exactly the same
498 * form.
@@ -423,7 +540,7 @@ static int parse_git_header(char *line, int len, unsigned int size, struct patch
540 * or removing or adding empty files), so we get
541 * the default name from the header.
542 */
426 - patch->def_name = git_header_name(line + strlen("diff --git "));
543 + patch->def_name = git_header_name(line, len);
544
545 line += len;
546 size -= len;
@@ -756,11 +873,18 @@ static void show_stats(struct patch *patch)
873 {
874 const char *prefix = "";
875 char *name = patch->new_name;
876 + char *qname = NULL;
877 int len, max, add, del, total;
878
879 if (!name)
880 name = patch->old_name;
881
882 + if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
883 + qname = xmalloc(len + 1);
884 + quote_c_style(name, qname, NULL, 0);
885 + name = qname;
886 + }
887 +
888 /*
889 * "scale" the filename
890 */
@@ -798,6 +922,8 @@ static void show_stats(struct patch *patch)
922 printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
923 len, name, patch->lines_added + patch->lines_deleted,
924 add, pluses, del, minuses);
925 + if (qname)
926 + free(qname);
927 }
928
929 static int read_old_data(struct stat *st, const char *path, void *buf, unsigned long size)
@@ -1220,12 +1346,16 @@ static void patch_stats(struct patch *patch)
1346 if (lines > max_change)
1347 max_change = lines;
1348 if (patch->old_name) {
1223 - int len = strlen(patch->old_name);
1349 + int len = quote_c_style(patch->old_name, NULL, NULL, 0);
1350 + if (!len)
1351 + len = strlen(patch->old_name);
1352 if (len > max_len)
1353 max_len = len;
1354 }
1355 if (patch->new_name) {
1228 - int len = strlen(patch->new_name);
1356 + int len = quote_c_style(patch->new_name, NULL, NULL, 0);
1357 + if (!len)
1358 + len = strlen(patch->new_name);
1359 if (len > max_len)
1360 max_len = len;
1361 }