@matheusbsbs / matheus-bagliano / commits / 5c7b580c94
Fork 0

apply --whitespace fixes and enhancements.

In addition to fixing obvious command line parsing bugs in the previous round, this changes the following: * Adds "--whitespace=strip". This applies after stripping the new trailing whitespaces introduced to the patch. * The output error message format is changed to say "patch-filename:linenumber:contents of the line". This makes it similar to typical compiler error message format, and helps C-x ` (next-error) in Emacs compilation buffer. * --whitespace=error and --whitespace=warn do not stop at the first error. We might want to limit the output to say first 20 such lines to prevent cluttering, but on the other hand if you are willing to hand-fix after inspecting them, getting everything with a single run might be easier to work with. After all, somebody has to do the clean-up work somewhere. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Feb 26, 2006 at 18:13 UTC 5c7b580c94b4217803a116e24a3ad924bfb35f86
1 file changed +54 -23
apply.c
+54 -23
@@ -37,8 +37,11 @@ static const char apply_usage[] =
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
@@ -823,19 +826,17 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
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--;
@@ -1114,6 +1115,27 @@ struct buffer_desc {
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;
@@ -1149,10 +1171,9 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag)
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 */
@@ -1713,7 +1734,7 @@ static int use_patch(struct patch *p)
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;
@@ -1721,6 +1742,7 @@ static int apply_patch(int fd)
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;
@@ -1747,6 +1769,9 @@ static int apply_patch(int fd)
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());
@@ -1793,7 +1818,7 @@ int main(int argc, char **argv)
1818 int fd;
1819
1820 if (!strcmp(arg, "-")) {
1796 - apply_patch(0);
1821 + apply_patch(0, "<stdin>");
1822 read_stdin = 0;
1823 continue;
1824 }
@@ -1854,14 +1879,18 @@ int main(int argc, char **argv)
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
@@ -1877,10 +1906,12 @@ int main(int argc, char **argv)
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 }