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 */
1742
return 1;
1743
}
1744
1724
-static int apply_patch(int fd)
1745
+static int apply_patch(int fd, const char *filename)
1746
{
1747
int newfd;
1748
unsigned long offset, size;
1750
struct patch *list = NULL, **listp = &list;
1751
int skipped_patch = 0;
1752
1753
+ patch_input_file = filename;
1754
if (!buffer)
1755
return -1;
1756
offset = 0;
1777
}
1778
1779
newfd = -1;
1780
+ if (whitespace_error && (new_whitespace == error_on_whitespace))
1781
+ apply = 0;
1782
+
1783
write_index = check_index && apply;
1784
if (write_index)
1785
newfd = hold_index_file_for_update(&cache_file, get_index_file());
1826
int fd;
1827
1828
if (!strcmp(arg, "-")) {
1804
- apply_patch(0);
1829
+ apply_patch(0, "<stdin>");
1830
read_stdin = 0;
1831
continue;
1832
}
1887
continue;
1888
}
1889
if (!strncmp(arg, "--whitespace=", 13)) {
1865
- if (strcmp(arg+13, "warn")) {
1890
+ if (!strcmp(arg+13, "warn")) {
1891
new_whitespace = warn_on_whitespace;
1892
continue;
1893
}
1869
- if (strcmp(arg+13, "error")) {
1894
+ if (!strcmp(arg+13, "error")) {
1895
new_whitespace = error_on_whitespace;
1896
continue;
1897
}
1898
+ if (!strcmp(arg+13, "strip")) {
1899
+ new_whitespace = strip_and_apply;
1900
+ continue;
1901
+ }
1902
die("unrecognixed whitespace option '%s'", arg+13);
1903
}
1904
1914
if (fd < 0)
1915
usage(apply_usage);
1916
read_stdin = 0;
1888
- apply_patch(fd);
1917
+ apply_patch(fd, arg);
1918
close(fd);
1919
}
1920
if (read_stdin)
1892
- apply_patch(0);
1921
+ apply_patch(0, "<stdin>");
1922
+ if (whitespace_error && new_whitespace == error_on_whitespace)
1923
+ return 1;
1924
return 0;
1925
}