37
static enum whitespace_eol {
38
nowarn,
39
warn_on_whitespace,
40
- error_on_whitespace
40
+ error_on_whitespace,
41
+ strip_and_apply,
42
} new_whitespace = nowarn;
43
+static int whitespace_error = 0;
44
+static const char *patch_input_file = NULL;
45
46
/*
47
* For "diff-stat" like behaviour, we keep track of the biggest change
826
case '+':
827
/*
828
* We know len is at least two, since we have a '+' and
826
- * we checked that the last character was a '\n' above
829
+ * we checked that the last character was a '\n' above.
830
+ * That is, an addition of an empty line would check
831
+ * the '+' here. Sneaky...
832
*/
828
- if (isspace(line[len-2])) {
829
- switch (new_whitespace) {
830
- case nowarn:
831
- break;
832
- case warn_on_whitespace:
833
- new_whitespace = nowarn; /* Just once */
834
- error("Added whitespace at end of line at line %d", linenr);
835
- break;
836
- case error_on_whitespace:
837
- die("Added whitespace at end of line at line %d", linenr);
838
- }
833
+ if ((new_whitespace != nowarn) &&
834
+ isspace(line[len-2])) {
835
+ fprintf(stderr, "Added whitespace\n");
836
+ fprintf(stderr, "%s:%d:%.*s\n",
837
+ patch_input_file,
838
+ linenr, len-2, line+1);
839
+ whitespace_error = 1;
840
}
841
added++;
842
newlines--;
1115
unsigned long alloc;
1116
};
1117
1118
+static int apply_line(char *output, const char *patch, int plen)
1119
+{
1120
+ /* plen is number of bytes to be copied from patch,
1121
+ * starting at patch+1 (patch[0] is '+'). Typically
1122
+ * patch[plen] is '\n'.
1123
+ */
1124
+ int add_nl_to_tail = 0;
1125
+ if ((new_whitespace == strip_and_apply) &&
1126
+ 1 < plen && isspace(patch[plen-1])) {
1127
+ if (patch[plen] == '\n')
1128
+ add_nl_to_tail = 1;
1129
+ plen--;
1130
+ while (0 < plen && isspace(patch[plen]))
1131
+ plen--;
1132
+ }
1133
+ memcpy(output, patch + 1, plen);
1134
+ if (add_nl_to_tail)
1135
+ output[plen++] = '\n';
1136
+ return plen;
1137
+}
1138
+
1139
static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
1140
{
1141
char *buf = desc->buffer;
1171
break;
1172
/* Fall-through for ' ' */
1173
case '+':
1152
- if (*patch != '+' || !no_add) {
1153
- memcpy(new + newsize, patch + 1, plen);
1154
- newsize += plen;
1155
- }
1174
+ if (*patch != '+' || !no_add)
1175
+ newsize += apply_line(new + newsize, patch,
1176
+ plen);
1177
break;
1178
case '@': case '\\':
1179
/* Ignore it, we already handled it */
1734
return 1;
1735
}
1736
1716
-static int apply_patch(int fd)
1737
+static int apply_patch(int fd, const char *filename)
1738
{
1739
int newfd;
1740
unsigned long offset, size;
1742
struct patch *list = NULL, **listp = &list;
1743
int skipped_patch = 0;
1744
1745
+ patch_input_file = filename;
1746
if (!buffer)
1747
return -1;
1748
offset = 0;
1769
}
1770
1771
newfd = -1;
1772
+ if (whitespace_error && (new_whitespace == error_on_whitespace))
1773
+ apply = 0;
1774
+
1775
write_index = check_index && apply;
1776
if (write_index)
1777
newfd = hold_index_file_for_update(&cache_file, get_index_file());
1818
int fd;
1819
1820
if (!strcmp(arg, "-")) {
1796
- apply_patch(0);
1821
+ apply_patch(0, "<stdin>");
1822
read_stdin = 0;
1823
continue;
1824
}
1879
continue;
1880
}
1881
if (!strncmp(arg, "--whitespace=", 13)) {
1857
- if (strcmp(arg+13, "warn")) {
1882
+ if (!strcmp(arg+13, "warn")) {
1883
new_whitespace = warn_on_whitespace;
1884
continue;
1885
}
1861
- if (strcmp(arg+13, "error")) {
1886
+ if (!strcmp(arg+13, "error")) {
1887
new_whitespace = error_on_whitespace;
1888
continue;
1889
}
1890
+ if (!strcmp(arg+13, "strip")) {
1891
+ new_whitespace = strip_and_apply;
1892
+ continue;
1893
+ }
1894
die("unrecognixed whitespace option '%s'", arg+13);
1895
}
1896
1906
if (fd < 0)
1907
usage(apply_usage);
1908
read_stdin = 0;
1880
- apply_patch(fd);
1909
+ apply_patch(fd, arg);
1910
close(fd);
1911
}
1912
if (read_stdin)
1884
- apply_patch(0);
1913
+ apply_patch(0, "<stdin>");
1914
+ if (whitespace_error && new_whitespace == error_on_whitespace)
1915
+ return 1;
1916
return 0;
1917
}