Update git-apply to use C-style quoting for funny pathnames.
Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Oct 14, 2005 at 21:54 UTC
22943f1a52142b0ef669c959f8b4367e579da968
2 files changed
+186
-46
apply.c
+184
-44
@@ -8,12 +8,13 @@
8
*/
9
#include <fnmatch.h>
10
#include "cache.h"
11
+#include "quote.h"
12
13
// --check turns on checking that the working tree matches the
14
// files that are being modified, but doesn't apply the patch
15
// --stat does just a diffstat, and doesn't actually apply
16
// --show-files shows the directory changes
16
-// --show-index-info shows the old and new index info for paths if available.
17
+// --index-info shows the old and new index info for paths if available.
18
//
19
static int check_index = 0;
20
static int write_index = 0;
@@ -23,8 +24,9 @@ static int check = 0;
24
static int apply = 1;
25
static int show_files = 0;
26
static int show_index_info = 0;
27
+static int line_termination = '\n';
28
static const char apply_usage[] =
27
-"git-apply [--stat] [--summary] [--check] [--index] [--apply] [--show-files] [--show-index-info] <patch>...";
29
+"git-apply [--stat] [--summary] [--check] [--index] [--apply] [--show-files] [--index-info] [-z] <patch>...";
30
31
/*
32
* For "diff-stat" like behaviour, we keep track of the biggest change
@@ -135,6 +137,35 @@ static char * find_name(const char *line, char *def, int p_value, int terminate)
137
const char *start = line;
138
char *name;
139
140
+ if (*line == '"') {
141
+ /* Proposed "new-style" GNU patch/diff format; see
142
+ * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2
143
+ */
144
+ name = unquote_c_style(line, NULL);
145
+ if (name) {
146
+ char *cp = name;
147
+ while (p_value) {
148
+ cp = strchr(name, '/');
149
+ if (!cp)
150
+ break;
151
+ cp++;
152
+ p_value--;
153
+ }
154
+ if (cp) {
155
+ /* name can later be freed, so we need
156
+ * to memmove, not just return cp
157
+ */
158
+ memmove(name, cp, strlen(cp) + 1);
159
+ free(def);
160
+ return name;
161
+ }
162
+ else {
163
+ free(name);
164
+ name = NULL;
165
+ }
166
+ }
167
+ }
168
+
169
for (;;) {
170
char c = *line;
171
@@ -224,37 +255,29 @@ static int gitdiff_hdrend(const char *line, struct patch *patch)
255
*/
256
static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, const char *oldnew)
257
{
227
- int len;
228
- const char *name;
229
-
258
if (!orig_name && !isnull)
259
return find_name(line, NULL, 1, 0);
260
233
- name = "/dev/null";
234
- len = 9;
261
if (orig_name) {
262
+ int len;
263
+ const char *name;
264
+ char *another;
265
name = orig_name;
266
len = strlen(name);
267
if (isnull)
268
die("git-apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr);
240
- }
241
-
242
- if (*name == '/')
243
- goto absolute_path;
244
-
245
- for (;;) {
246
- char c = *line++;
247
- if (c == '\n')
248
- break;
249
- if (c != '/')
250
- continue;
251
-absolute_path:
252
- if (memcmp(line, name, len) || line[len] != '\n')
253
- break;
269
+ another = find_name(line, NULL, 1, 0);
270
+ if (!another || memcmp(another, name, len))
271
+ die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
272
+ free(another);
273
return orig_name;
274
}
256
- die("git-apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr);
257
- return NULL;
275
+ else {
276
+ /* expect "/dev/null" */
277
+ if (memcmp("/dev/null", line, 9) || line[9] != '\n')
278
+ die("git-apply: bad git-diff - expected /dev/null on line %d", linenr);
279
+ return NULL;
280
+ }
281
}
282
283
static int gitdiff_oldname(const char *line, struct patch *patch)
@@ -378,29 +401,124 @@ static int gitdiff_unrecognized(const char *line, struct patch *patch)
401
return -1;
402
}
403
381
-static char *git_header_name(char *line)
404
+static const char *stop_at_slash(const char *line, int llen)
405
+{
406
+ int i;
407
+
408
+ for (i = 0; i < llen; i++) {
409
+ int ch = line[i];
410
+ if (ch == '/')
411
+ return line + i;
412
+ }
413
+ return NULL;
414
+}
415
+
416
+/* This is to extract the same name that appears on "diff --git"
417
+ * line. We do not find and return anything if it is a rename
418
+ * patch, and it is OK because we will find the name elsewhere.
419
+ * We need to reliably find name only when it is mode-change only,
420
+ * creation or deletion of an empty file. In any of these cases,
421
+ * both sides are the same name under a/ and b/ respectively.
422
+ */
423
+static char *git_header_name(char *line, int llen)
424
{
425
int len;
384
- char *name, *second;
426
+ const char *name;
427
+ const char *second = NULL;
428
386
- /*
387
- * Find the first '/'
388
- */
389
- name = line;
390
- for (;;) {
391
- char c = *name++;
392
- if (c == '\n')
429
+ line += strlen("diff --git ");
430
+ llen -= strlen("diff --git ");
431
+
432
+ if (*line == '"') {
433
+ const char *cp;
434
+ char *first = unquote_c_style(line, &second);
435
+ if (!first)
436
return NULL;
394
- if (c == '/')
395
- break;
437
+
438
+ /* advance to the first slash */
439
+ cp = stop_at_slash(first, strlen(first));
440
+ if (!cp || cp == first) {
441
+ /* we do not accept absolute paths */
442
+ free_first_and_fail:
443
+ free(first);
444
+ return NULL;
445
+ }
446
+ len = strlen(cp+1);
447
+ memmove(first, cp+1, len+1); /* including NUL */
448
+
449
+ /* second points at one past closing dq of name.
450
+ * find the second name.
451
+ */
452
+ while ((second < line + llen) && isspace(*second))
453
+ second++;
454
+
455
+ if (line + llen <= second)
456
+ goto free_first_and_fail;
457
+ if (*second == '"') {
458
+ char *sp = unquote_c_style(second, NULL);
459
+ if (!sp)
460
+ goto free_first_and_fail;
461
+ cp = stop_at_slash(sp, strlen(sp));
462
+ if (!cp || cp == sp) {
463
+ free_both_and_fail:
464
+ free(sp);
465
+ goto free_first_and_fail;
466
+ }
467
+ /* They must match, otherwise ignore */
468
+ if (strcmp(cp+1, first))
469
+ goto free_both_and_fail;
470
+ free(sp);
471
+ return first;
472
+ }
473
+
474
+ /* unquoted second */
475
+ cp = stop_at_slash(second, line + llen - second);
476
+ if (!cp || cp == second)
477
+ goto free_first_and_fail;
478
+ cp++;
479
+ if (line + llen - cp != len + 1 ||
480
+ memcmp(first, cp, len))
481
+ goto free_first_and_fail;
482
+ return first;
483
}
484
398
- /*
399
- * We don't accept absolute paths (/dev/null) as possibly valid
400
- */
401
- if (name == line+1)
485
+ /* unquoted first name */
486
+ name = stop_at_slash(line, llen);
487
+ if (!name || name == line)
488
return NULL;
489
490
+ name++;
491
+
492
+ /* since the first name is unquoted, a dq if exists must be
493
+ * the beginning of the second name.
494
+ */
495
+ for (second = name; second < line + llen; second++) {
496
+ if (*second == '"') {
497
+ const char *cp = second;
498
+ const char *np;
499
+ char *sp = unquote_c_style(second, NULL);
500
+
501
+ if (!sp)
502
+ return NULL;
503
+ np = stop_at_slash(sp, strlen(sp));
504
+ if (!np || np == sp) {
505
+ free_second_and_fail:
506
+ free(sp);
507
+ return NULL;
508
+ }
509
+ np++;
510
+ len = strlen(np);
511
+ if (len < cp - name &&
512
+ !strncmp(np, name, len) &&
513
+ isspace(name[len])) {
514
+ /* Good */
515
+ memmove(sp, np, len + 1);
516
+ return sp;
517
+ }
518
+ goto free_second_and_fail;
519
+ }
520
+ }
521
+
522
/*
523
* Accept a name only if it shows up twice, exactly the same
524
* form.
@@ -448,7 +566,7 @@ static int parse_git_header(char *line, int len, unsigned int size, struct patch
566
* or removing or adding empty files), so we get
567
* the default name from the header.
568
*/
451
- patch->def_name = git_header_name(line + strlen("diff --git "));
569
+ patch->def_name = git_header_name(line, len);
570
571
line += len;
572
size -= len;
@@ -785,11 +903,18 @@ static void show_stats(struct patch *patch)
903
{
904
const char *prefix = "";
905
char *name = patch->new_name;
906
+ char *qname = NULL;
907
int len, max, add, del, total;
908
909
if (!name)
910
name = patch->old_name;
911
912
+ if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
913
+ qname = xmalloc(len + 1);
914
+ quote_c_style(name, qname, NULL, 0);
915
+ name = qname;
916
+ }
917
+
918
/*
919
* "scale" the filename
920
*/
@@ -827,6 +952,8 @@ static void show_stats(struct patch *patch)
952
printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
953
len, name, patch->lines_added + patch->lines_deleted,
954
add, pluses, del, minuses);
955
+ if (qname)
956
+ free(qname);
957
}
958
959
static int read_old_data(struct stat *st, const char *path, void *buf, unsigned long size)
@@ -1197,8 +1324,13 @@ static void show_index_list(struct patch *list)
1324
name);
1325
else
1326
sha1_ptr = sha1;
1200
- printf("%06o %s %s\n",patch->old_mode,
1201
- sha1_to_hex(sha1_ptr), name);
1327
+
1328
+ printf("%06o %s ",patch->old_mode, sha1_to_hex(sha1_ptr));
1329
+ if (line_termination && quote_c_style(name, NULL, NULL, 0))
1330
+ quote_c_style(name, NULL, stdout, 0);
1331
+ else
1332
+ fputs(name, stdout);
1333
+ putchar(line_termination);
1334
}
1335
}
1336
@@ -1301,12 +1433,16 @@ static void patch_stats(struct patch *patch)
1433
if (lines > max_change)
1434
max_change = lines;
1435
if (patch->old_name) {
1304
- int len = strlen(patch->old_name);
1436
+ int len = quote_c_style(patch->old_name, NULL, NULL, 0);
1437
+ if (!len)
1438
+ len = strlen(patch->old_name);
1439
if (len > max_len)
1440
max_len = len;
1441
}
1442
if (patch->new_name) {
1309
- int len = strlen(patch->new_name);
1443
+ int len = quote_c_style(patch->new_name, NULL, NULL, 0);
1444
+ if (!len)
1445
+ len = strlen(patch->new_name);
1446
if (len > max_len)
1447
max_len = len;
1448
}
@@ -1603,11 +1739,15 @@ int main(int argc, char **argv)
1739
show_files = 1;
1740
continue;
1741
}
1606
- if (!strcmp(arg, "--show-index-info")) {
1742
+ if (!strcmp(arg, "--index-info")) {
1743
apply = 0;
1744
show_index_info = 1;
1745
continue;
1746
}
1747
+ if (!strcmp(arg, "-z")) {
1748
+ line_termination = 0;
1749
+ continue;
1750
+ }
1751
fd = open(arg, O_RDONLY);
1752
if (fd < 0)
1753
usage(apply_usage);
git-am.sh
+2
-2
@@ -35,10 +35,10 @@ fall_back_3way () {
35
mkdir "$dotest/patch-merge-tmp-dir"
36
37
# First see if the patch records the index info that we can use.
38
- if git-apply --show-index-info "$dotest/patch" \
38
+ if git-apply -z --index-info "$dotest/patch" \
39
>"$dotest/patch-merge-index-info" 2>/dev/null &&
40
GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
41
- git-update-index --index-info <"$dotest/patch-merge-index-info" &&
41
+ git-update-index -z --index-info <"$dotest/patch-merge-index-info" &&
42
GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
43
git-write-tree >"$dotest/patch-merge-base+" &&
44
# index has the base tree now.