git-apply: second war on whitespace.
This makes --whitespace={warn,error,strip} option to also notice the leading whitespace errors in addition to the trailing whitespace errors. Spaces that are followed by a tab in indent are detected as errors, and --whitespace=strip option fixes them. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 23, 2006 at 00:37 UTC
d0c25035df4897bb58422b4d64f00b54cf11f07e
1 file changed
+97
-25
builtin-apply.c
+97
-25
@@ -854,6 +854,49 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
854
return -1;
855
}
856
857
+static void check_whitespace(const char *line, int len)
858
+{
859
+ const char *err = "Adds trailing whitespace";
860
+ int seen_space = 0;
861
+ int i;
862
+
863
+ /*
864
+ * We know len is at least two, since we have a '+' and we
865
+ * checked that the last character was a '\n' before calling
866
+ * this function. That is, an addition of an empty line would
867
+ * check the '+' here. Sneaky...
868
+ */
869
+ if (isspace(line[len-2]))
870
+ goto error;
871
+
872
+ /*
873
+ * Make sure that there is no space followed by a tab in
874
+ * indentation.
875
+ */
876
+ err = "Space in indent is followed by a tab";
877
+ for (i = 1; i < len; i++) {
878
+ if (line[i] == '\t') {
879
+ if (seen_space)
880
+ goto error;
881
+ }
882
+ else if (line[i] == ' ')
883
+ seen_space = 1;
884
+ else
885
+ break;
886
+ }
887
+ return;
888
+
889
+ error:
890
+ whitespace_error++;
891
+ if (squelch_whitespace_errors &&
892
+ squelch_whitespace_errors < whitespace_error)
893
+ ;
894
+ else
895
+ fprintf(stderr, "%s.\n%s:%d:%.*s\n",
896
+ err, patch_input_file, linenr, len-2, line+1);
897
+}
898
+
899
+
900
/*
901
* Parse a unified diff. Note that this really needs to parse each
902
* fragment separately, since the only way to know the difference
@@ -904,25 +947,8 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
947
trailing = 0;
948
break;
949
case '+':
907
- /*
908
- * We know len is at least two, since we have a '+' and
909
- * we checked that the last character was a '\n' above.
910
- * That is, an addition of an empty line would check
911
- * the '+' here. Sneaky...
912
- */
913
- if ((new_whitespace != nowarn_whitespace) &&
914
- isspace(line[len-2])) {
915
- whitespace_error++;
916
- if (squelch_whitespace_errors &&
917
- squelch_whitespace_errors <
918
- whitespace_error)
919
- ;
920
- else {
921
- fprintf(stderr, "Adds trailing whitespace.\n%s:%d:%.*s\n",
922
- patch_input_file,
923
- linenr, len-2, line+1);
924
- }
925
- }
950
+ if (new_whitespace != nowarn_whitespace)
951
+ check_whitespace(line, len);
952
added++;
953
newlines--;
954
trailing = 0;
@@ -1494,22 +1520,68 @@ static int apply_line(char *output, const char *patch, int plen)
1520
{
1521
/* plen is number of bytes to be copied from patch,
1522
* starting at patch+1 (patch[0] is '+'). Typically
1497
- * patch[plen] is '\n'.
1523
+ * patch[plen] is '\n', unless this is the incomplete
1524
+ * last line.
1525
*/
1526
+ int i;
1527
int add_nl_to_tail = 0;
1500
- if ((new_whitespace == strip_whitespace) &&
1501
- 1 < plen && isspace(patch[plen-1])) {
1528
+ int fixed = 0;
1529
+ int last_tab_in_indent = -1;
1530
+ int last_space_in_indent = -1;
1531
+ int need_fix_leading_space = 0;
1532
+ char *buf;
1533
+
1534
+ if ((new_whitespace != strip_whitespace) || !whitespace_error) {
1535
+ memcpy(output, patch + 1, plen);
1536
+ return plen;
1537
+ }
1538
+
1539
+ if (1 < plen && isspace(patch[plen-1])) {
1540
if (patch[plen] == '\n')
1541
add_nl_to_tail = 1;
1542
plen--;
1543
while (0 < plen && isspace(patch[plen]))
1544
plen--;
1507
- applied_after_stripping++;
1545
+ fixed = 1;
1546
}
1509
- memcpy(output, patch + 1, plen);
1547
+
1548
+ for (i = 1; i < plen; i++) {
1549
+ char ch = patch[i];
1550
+ if (ch == '\t') {
1551
+ last_tab_in_indent = i;
1552
+ if (0 <= last_space_in_indent)
1553
+ need_fix_leading_space = 1;
1554
+ }
1555
+ else if (ch == ' ')
1556
+ last_space_in_indent = i;
1557
+ else
1558
+ break;
1559
+ }
1560
+
1561
+ buf = output;
1562
+ if (need_fix_leading_space) {
1563
+ /* between patch[1..last_tab_in_indent] strip the
1564
+ * funny spaces, updating them to tab as needed.
1565
+ */
1566
+ for (i = 1; i < last_tab_in_indent; i++, plen--) {
1567
+ char ch = patch[i];
1568
+ if (ch != ' ')
1569
+ *output++ = ch;
1570
+ else if ((i % 8) == 0)
1571
+ *output++ = '\t';
1572
+ }
1573
+ fixed = 1;
1574
+ i = last_tab_in_indent;
1575
+ }
1576
+ else
1577
+ i = 1;
1578
+
1579
+ memcpy(output, patch + i, plen);
1580
if (add_nl_to_tail)
1581
output[plen++] = '\n';
1512
- return plen;
1582
+ if (fixed)
1583
+ applied_after_stripping++;
1584
+ return output + plen - buf;
1585
}
1586
1587
static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, int inaccurate_eof)