apply: handle patches with funny filename and colon in timezone
Some patches have a timezone formatted like '-08:00' instead of '-0800' in their ---/+++ lines (e.g. http://lwn.net/Articles/131729/). Take this into account when searching for the start of the timezone (which is the end of the filename). This does not actually affect the outcome of patching unless (1) a file being patched has a non-' ' whitespace character (e.g., tab) in its filename, or (2) the patch is whitespace-damaged, so the tab between filename and timestamp has been replaced with spaces. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Nieder committed
Sep 29, 2010 at 16:41 UTC
2d502e1f378a39ef375d6d5c1b3312d286ed2524
4 files changed
+48
-2
builtin/apply.c
+22
-2
@@ -449,7 +449,7 @@ static char *find_name_gnu(const char *line, char *def, int p_value)
449
return squash_slash(strbuf_detach(&name, NULL));
450
}
451
452
-static size_t tz_len(const char *line, size_t len)
452
+static size_t sane_tz_len(const char *line, size_t len)
453
{
454
const char *tz, *p;
455
@@ -467,6 +467,24 @@ static size_t tz_len(const char *line, size_t len)
467
return line + len - tz;
468
}
469
470
+static size_t tz_with_colon_len(const char *line, size_t len)
471
+{
472
+ const char *tz, *p;
473
+
474
+ if (len < strlen(" +08:00") || line[len - strlen(":00")] != ':')
475
+ return 0;
476
+ tz = line + len - strlen(" +08:00");
477
+
478
+ if (tz[0] != ' ' || (tz[1] != '+' && tz[1] != '-'))
479
+ return 0;
480
+ p = tz + 2;
481
+ if (!isdigit(*p++) || !isdigit(*p++) || *p++ != ':' ||
482
+ !isdigit(*p++) || !isdigit(*p++))
483
+ return 0;
484
+
485
+ return line + len - tz;
486
+}
487
+
488
static size_t date_len(const char *line, size_t len)
489
{
490
const char *date, *p;
@@ -561,7 +579,9 @@ static size_t diff_timestamp_len(const char *line, size_t len)
579
if (!isdigit(end[-1]))
580
return 0;
581
564
- n = tz_len(line, end - line);
582
+ n = sane_tz_len(line, end - line);
583
+ if (!n)
584
+ n = tz_with_colon_len(line, end - line);
585
end -= n;
586
587
n = short_time_len(line, end - line);
t/t4135-apply-weird-filenames.sh
+16
@@ -72,4 +72,20 @@ test_expect_success 'whitespace-damaged traditional patch' '
72
test_cmp expected postimage.txt
73
'
74
75
+test_expect_success 'traditional patch with colon in timezone' '
76
+ echo postimage >expected &&
77
+ reset_preimage &&
78
+ rm -f "post image.txt" &&
79
+ git apply "$vector/funny-tz.diff" &&
80
+ test_cmp expected "post image.txt"
81
+'
82
+
83
+test_expect_success 'traditional, whitespace-damaged, colon in timezone' '
84
+ echo postimage >expected &&
85
+ reset_preimage &&
86
+ rm -f "post image.txt" &&
87
+ git apply "$vector/damaged-tz.diff" &&
88
+ test_cmp expected "post image.txt"
89
+'
90
+
91
test_done
t/t4135/damaged-tz.diff
new
+5
@@ -0,0 +1,5 @@
1
+diff -urN -X /usr/people/jes/exclude-linux linux-2.6.12-rc2-mm3-vanilla/post image.txt linux-2.6.12-rc2-mm3/post image.txt
2
+--- linux-2.6.12-rc2-mm3-vanilla/post image.txt 1969-12-31 16:00:00 -08:00
3
++++ linux-2.6.12-rc2-mm3/post image.txt 2005-04-12 02:14:06 -07:00
4
+@@ -0,0 +1 @@
5
++postimage
t/t4135/funny-tz.diff
new
+5
@@ -0,0 +1,5 @@
1
+diff -urN -X /usr/people/jes/exclude-linux linux-2.6.12-rc2-mm3-vanilla/post image.txt linux-2.6.12-rc2-mm3/post image.txt
2
+--- linux-2.6.12-rc2-mm3-vanilla/post image.txt 1969-12-31 16:00:00 -08:00
3
++++ linux-2.6.12-rc2-mm3/post image.txt 2005-04-12 02:14:06 -07:00
4
+@@ -0,0 +1 @@
5
++postimage