@matheusbsbs / matheus-bagliano / commits / 383e20b614
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 383e20b6144d01286c90c0c82d88f92173a038f6
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;
@@ -1816,10 +1842,21 @@ static int apply_patch(int fd, const char *filename)
1842 return 0;
1843 }
1844
1845 +static int git_apply_config(const char *var, const char *value)
1846 +{
1847 + if (!strcmp(var, "apply.whitespace")) {
1848 + apply_default_whitespace = strdup(value);
1849 + return 0;
1850 + }
1851 + return git_default_config(var, value);
1852 +}
1853 +
1854 +
1855 int main(int argc, char **argv)
1856 {
1857 int i;
1858 int read_stdin = 1;
1859 + const char *whitespace_option = NULL;
1860
1861 for (i = 1; i < argc; i++) {
1862 const char *arg = argv[i];
@@ -1887,30 +1924,17 @@ int main(int argc, char **argv)
1924 continue;
1925 }
1926 if (!strncmp(arg, "--whitespace=", 13)) {
1890 - if (!strcmp(arg+13, "warn")) {
1891 - new_whitespace = warn_on_whitespace;
1892 - continue;
1893 - }
1894 - if (!strcmp(arg+13, "error")) {
1895 - new_whitespace = error_on_whitespace;
1896 - continue;
1897 - }
1898 - if (!strcmp(arg+13, "error-all")) {
1899 - new_whitespace = error_on_whitespace;
1900 - squelch_whitespace_errors = 0;
1901 - continue;
1902 - }
1903 - if (!strcmp(arg+13, "strip")) {
1904 - new_whitespace = strip_and_apply;
1905 - continue;
1906 - }
1907 - die("unrecognized whitespace option '%s'", arg+13);
1927 + whitespace_option = arg + 13;
1928 + parse_whitespace_option(arg + 13);
1929 + continue;
1930 }
1931
1932 if (check_index && prefix_length < 0) {
1933 prefix = setup_git_directory();
1934 prefix_length = prefix ? strlen(prefix) : 0;
1913 - git_config(git_default_config);
1935 + git_config(git_apply_config);
1936 + if (!whitespace_option && apply_default_whitespace)
1937 + parse_whitespace_option(apply_default_whitespace);
1938 }
1939 if (0 < prefix_length)
1940 arg = prefix_filename(prefix, prefix_length, arg);
cache.h
+2
@@ -160,10 +160,12 @@ extern int hold_index_file_for_update(struct cache_file *, const char *path);
160 extern int commit_index_file(struct cache_file *);
161 extern void rollback_index_file(struct cache_file *);
162
163 +/* Environment bits from configuration mechanism */
164 extern int trust_executable_bit;
165 extern int only_use_symrefs;
166 extern int diff_rename_limit_default;
167 extern int shared_repository;
168 +extern const char *apply_default_whitespace;
169
170 #define GIT_REPO_VERSION 0
171 extern int repository_format_version;
environment.c
+1
@@ -16,6 +16,7 @@ int only_use_symrefs = 0;
16 int repository_format_version = 0;
17 char git_commit_encoding[MAX_ENCODING_LENGTH] = "utf-8";
18 int shared_repository = 0;
19 +const char *apply_default_whitespace = NULL;
20
21 static char *git_dir, *git_object_dir, *git_index_file, *git_refs_dir,
22 *git_graft_file;