@matheusbsbs / matheus-bagliano / commits / 2ae1c53b51
Fork 0

apply --whitespace: configuration option.

The new configuration option apply.whitespace can take one of "warn", "error", "error-all", or "strip". When git-apply is run to apply the patch to the index, they are used as the default value if there is no command line --whitespace option. Andrew can now tell people who feed him git trees to update to this version and say: git repo-config apply.whitespace error Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Feb 27, 2006 at 14:47 UTC 2ae1c53b51ff78b13cc8abf8e9798a12140b7638
3 files changed +51 -24
apply.c
+48 -24
@@ -35,16 +35,42 @@ static const char apply_usage[] =
35 "git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] <patch>...";
36
37 static enum whitespace_eol {
38 - nowarn,
38 + nowarn_whitespace,
39 warn_on_whitespace,
40 error_on_whitespace,
41 - strip_and_apply,
42 -} new_whitespace = nowarn;
41 + strip_whitespace,
42 +} new_whitespace = nowarn_whitespace;
43 static int whitespace_error = 0;
44 static int squelch_whitespace_errors = 5;
45 static int applied_after_stripping = 0;
46 static const char *patch_input_file = NULL;
47
48 +static void parse_whitespace_option(const char *option)
49 +{
50 + if (!option) {
51 + new_whitespace = nowarn_whitespace;
52 + return;
53 + }
54 + if (!strcmp(option, "warn")) {
55 + new_whitespace = warn_on_whitespace;
56 + return;
57 + }
58 + if (!strcmp(option, "error")) {
59 + new_whitespace = error_on_whitespace;
60 + return;
61 + }
62 + if (!strcmp(option, "error-all")) {
63 + new_whitespace = error_on_whitespace;
64 + squelch_whitespace_errors = 0;
65 + return;
66 + }
67 + if (!strcmp(option, "strip")) {
68 + new_whitespace = strip_whitespace;
69 + return;
70 + }
71 + die("unrecognized whitespace option '%s'", option);
72 +}
73 +
74 /*
75 * For "diff-stat" like behaviour, we keep track of the biggest change
76 * we've seen, and the longest filename. That allows us to do simple
@@ -832,7 +858,7 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
858 * That is, an addition of an empty line would check
859 * the '+' here. Sneaky...
860 */
835 - if ((new_whitespace != nowarn) &&
861 + if ((new_whitespace != nowarn_whitespace) &&
862 isspace(line[len-2])) {
863 whitespace_error++;
864 if (squelch_whitespace_errors &&
@@ -1129,7 +1155,7 @@ static int apply_line(char *output, const char *patch, int plen)
1155 * patch[plen] is '\n'.
1156 */
1157 int add_nl_to_tail = 0;
1132 - if ((new_whitespace == strip_and_apply) &&
1158 + if ((new_whitespace == strip_whitespace) &&
1159 1 < plen && isspace(patch[plen-1])) {
1160 if (patch[plen] == '\n')
1161 add_nl_to_tail = 1;
@@ -1824,10 +1850,21 @@ static int apply_patch(int fd, const char *filename)
1850 return 0;
1851 }
1852
1853 +static int git_apply_config(const char *var, const char *value)
1854 +{
1855 + if (!strcmp(var, "apply.whitespace")) {
1856 + apply_default_whitespace = strdup(value);
1857 + return 0;
1858 + }
1859 + return git_default_config(var, value);
1860 +}
1861 +
1862 +
1863 int main(int argc, char **argv)
1864 {
1865 int i;
1866 int read_stdin = 1;
1867 + const char *whitespace_option = NULL;
1868
1869 for (i = 1; i < argc; i++) {
1870 const char *arg = argv[i];
@@ -1895,30 +1932,17 @@ int main(int argc, char **argv)
1932 continue;
1933 }
1934 if (!strncmp(arg, "--whitespace=", 13)) {
1898 - if (!strcmp(arg+13, "warn")) {
1899 - new_whitespace = warn_on_whitespace;
1900 - continue;
1901 - }
1902 - if (!strcmp(arg+13, "error")) {
1903 - new_whitespace = error_on_whitespace;
1904 - continue;
1905 - }
1906 - if (!strcmp(arg+13, "error-all")) {
1907 - new_whitespace = error_on_whitespace;
1908 - squelch_whitespace_errors = 0;
1909 - continue;
1910 - }
1911 - if (!strcmp(arg+13, "strip")) {
1912 - new_whitespace = strip_and_apply;
1913 - continue;
1914 - }
1915 - die("unrecognized whitespace option '%s'", arg+13);
1935 + whitespace_option = arg + 13;
1936 + parse_whitespace_option(arg + 13);
1937 + continue;
1938 }
1939
1940 if (check_index && prefix_length < 0) {
1941 prefix = setup_git_directory();
1942 prefix_length = prefix ? strlen(prefix) : 0;
1921 - git_config(git_default_config);
1943 + git_config(git_apply_config);
1944 + if (!whitespace_option && apply_default_whitespace)
1945 + parse_whitespace_option(apply_default_whitespace);
1946 }
1947 if (0 < prefix_length)
1948 arg = prefix_filename(prefix, prefix_length, arg);
cache.h
+2
@@ -161,11 +161,13 @@ extern int hold_index_file_for_update(struct cache_file *, const char *path);
161 extern int commit_index_file(struct cache_file *);
162 extern void rollback_index_file(struct cache_file *);
163
164 +/* Environment bits from configuration mechanism */
165 extern int trust_executable_bit;
166 extern int assume_unchanged;
167 extern int only_use_symrefs;
168 extern int diff_rename_limit_default;
169 extern int shared_repository;
170 +extern const char *apply_default_whitespace;
171
172 #define GIT_REPO_VERSION 0
173 extern int repository_format_version;
environment.c
+1
@@ -17,6 +17,7 @@ int only_use_symrefs = 0;
17 int repository_format_version = 0;
18 char git_commit_encoding[MAX_ENCODING_LENGTH] = "utf-8";
19 int shared_repository = 0;
20 +const char *apply_default_whitespace = NULL;
21
22 static char *git_dir, *git_object_dir, *git_index_file, *git_refs_dir,
23 *git_graft_file;