Skip to content

Commit 2ae1c53

Browse files
author
Junio C Hamano
committed
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>
1 parent fc96b7c commit 2ae1c53

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

apply.c

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,42 @@ static const char apply_usage[] =
3535
"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] <patch>...";
3636

3737
static enum whitespace_eol {
38-
nowarn,
38+
nowarn_whitespace,
3939
warn_on_whitespace,
4040
error_on_whitespace,
41-
strip_and_apply,
42-
} new_whitespace = nowarn;
41+
strip_whitespace,
42+
} new_whitespace = nowarn_whitespace;
4343
static int whitespace_error = 0;
4444
static int squelch_whitespace_errors = 5;
4545
static int applied_after_stripping = 0;
4646
static const char *patch_input_file = NULL;
4747

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+
4874
/*
4975
* For "diff-stat" like behaviour, we keep track of the biggest change
5076
* 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
832858
* That is, an addition of an empty line would check
833859
* the '+' here. Sneaky...
834860
*/
835-
if ((new_whitespace != nowarn) &&
861+
if ((new_whitespace != nowarn_whitespace) &&
836862
isspace(line[len-2])) {
837863
whitespace_error++;
838864
if (squelch_whitespace_errors &&
@@ -1129,7 +1155,7 @@ static int apply_line(char *output, const char *patch, int plen)
11291155
* patch[plen] is '\n'.
11301156
*/
11311157
int add_nl_to_tail = 0;
1132-
if ((new_whitespace == strip_and_apply) &&
1158+
if ((new_whitespace == strip_whitespace) &&
11331159
1 < plen && isspace(patch[plen-1])) {
11341160
if (patch[plen] == '\n')
11351161
add_nl_to_tail = 1;
@@ -1824,10 +1850,21 @@ static int apply_patch(int fd, const char *filename)
18241850
return 0;
18251851
}
18261852

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+
18271863
int main(int argc, char **argv)
18281864
{
18291865
int i;
18301866
int read_stdin = 1;
1867+
const char *whitespace_option = NULL;
18311868

18321869
for (i = 1; i < argc; i++) {
18331870
const char *arg = argv[i];
@@ -1895,30 +1932,17 @@ int main(int argc, char **argv)
18951932
continue;
18961933
}
18971934
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;
19161938
}
19171939

19181940
if (check_index && prefix_length < 0) {
19191941
prefix = setup_git_directory();
19201942
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);
19221946
}
19231947
if (0 < prefix_length)
19241948
arg = prefix_filename(prefix, prefix_length, arg);

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,13 @@ extern int hold_index_file_for_update(struct cache_file *, const char *path);
161161
extern int commit_index_file(struct cache_file *);
162162
extern void rollback_index_file(struct cache_file *);
163163

164+
/* Environment bits from configuration mechanism */
164165
extern int trust_executable_bit;
165166
extern int assume_unchanged;
166167
extern int only_use_symrefs;
167168
extern int diff_rename_limit_default;
168169
extern int shared_repository;
170+
extern const char *apply_default_whitespace;
169171

170172
#define GIT_REPO_VERSION 0
171173
extern int repository_format_version;

environment.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ int only_use_symrefs = 0;
1717
int repository_format_version = 0;
1818
char git_commit_encoding[MAX_ENCODING_LENGTH] = "utf-8";
1919
int shared_repository = 0;
20+
const char *apply_default_whitespace = NULL;
2021

2122
static char *git_dir, *git_object_dir, *git_index_file, *git_refs_dir,
2223
*git_graft_file;

0 commit comments

Comments
 (0)