summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: c1beba5)
raw | patch | inline | side by side (parent: c1beba5)
author | Junio C Hamano <gitster@pobox.com> | |
Tue, 15 Jan 2008 08:59:05 +0000 (00:59 -0800) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Tue, 5 Feb 2008 08:38:41 +0000 (00:38 -0800) |
This new error mode allows a line to have a carriage return at the
end of the line when checking and fixing trailing whitespace errors.
Some people like to keep CRLF line ending recorded in the repository,
and still want to take advantage of the automated trailing whitespace
stripping. We still show ^M in the diff output piped to "less" to
remind them that they do have the CR at the end, but these carriage
return characters at the end are no longer flagged as errors.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
end of the line when checking and fixing trailing whitespace errors.
Some people like to keep CRLF line ending recorded in the repository,
and still want to take advantage of the automated trailing whitespace
stripping. We still show ^M in the diff output piped to "less" to
remind them that they do have the CR at the end, but these carriage
return characters at the end are no longer flagged as errors.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/config.txt | patch | blob | history | |
builtin-apply.c | patch | blob | history | |
cache.h | patch | blob | history | |
t/t4019-diff-wserror.sh | patch | blob | history | |
ws.c | patch | blob | history |
index 4e222f15a5b2b84eb5d55bc79c605510bd89b7b6..44cb640fb869b5302b932cee1e4503eaefe413f6 100644 (file)
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
error (enabled by default).
* `indent-with-non-tab` treats a line that is indented with 8 or more
space characters as an error (not enabled by default).
+* `cr-at-eol` treats a carriage-return at the end of line as
+ part of the line terminator, i.e. with it, `trailing-space`
+ does not trigger if the character before such a carriage-return
+ is not a whitespace (not enabled by default).
alias.*::
Command aliases for the linkgit:git[1] command wrapper - e.g.
diff --git a/builtin-apply.c b/builtin-apply.c
index fccf4a40cc0a405c683ac0f06903e53318bfd353..2b8ba81d81aa717ea6456f82ea09369fe2a1f937 100644 (file)
--- a/builtin-apply.c
+++ b/builtin-apply.c
*/
int i;
int add_nl_to_tail = 0;
+ int add_cr_to_tail = 0;
int fixed = 0;
int last_tab_in_indent = -1;
int last_space_in_indent = -1;
*/
if ((ws_rule & WS_TRAILING_SPACE) &&
(2 < plen && isspace(patch[plen-2]))) {
- if (patch[plen-1] == '\n')
+ if (patch[plen - 1] == '\n') {
add_nl_to_tail = 1;
- plen--;
- while (0 < plen && isspace(patch[plen-1]))
plen--;
- fixed = 1;
+ if (1 < plen && patch[plen - 1] == '\r') {
+ add_cr_to_tail = !!(ws_rule & WS_CR_AT_EOL);
+ plen--;
+ }
+ }
+ if (0 < plen && isspace(patch[plen - 1])) {
+ while (0 < plen && isspace(patch[plen-1]))
+ plen--;
+ fixed = 1;
+ }
}
/*
}
memcpy(output, patch, plen);
+ if (add_cr_to_tail)
+ output[plen++] = '\r';
if (add_nl_to_tail)
output[plen++] = '\n';
if (fixed && count_error)
index 549f4bbac7242714283e58dd576460ca86015d52..ad11c90caf56b3bf5bca37ed185452967ff56a2b 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -652,6 +652,7 @@ void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, i
#define WS_TRAILING_SPACE 01
#define WS_SPACE_BEFORE_TAB 02
#define WS_INDENT_WITH_NON_TAB 04
+#define WS_CR_AT_EOL 010
#define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB)
extern unsigned whitespace_rule_cfg;
extern unsigned whitespace_rule(const char *);
index 67e080bdbe5a98a9c446af8aee41acc9fb9129b4..0d9cbb62615c0d94da784f63907989988b0e8151 100755 (executable)
--- a/t/t4019-diff-wserror.sh
+++ b/t/t4019-diff-wserror.sh
echo " Eight SP indent" >>F &&
echo " HT and SP indent" >>F &&
echo "With trailing SP " >>F &&
+ echo "Carriage ReturnQ" | tr Q "\015" >>F &&
echo "No problem" >>F
'
grep Eight normal >/dev/null &&
grep HT error >/dev/null &&
grep With error >/dev/null &&
+ grep Return error >/dev/null &&
grep No normal >/dev/null
'
grep Eight normal >/dev/null &&
grep HT error >/dev/null &&
grep With normal >/dev/null &&
+ grep Return normal >/dev/null &&
grep No normal >/dev/null
'
grep Eight normal >/dev/null &&
grep HT error >/dev/null &&
grep With normal >/dev/null &&
+ grep Return normal >/dev/null &&
grep No normal >/dev/null
'
grep Eight normal >/dev/null &&
grep HT normal >/dev/null &&
grep With error >/dev/null &&
+ grep Return error >/dev/null &&
grep No normal >/dev/null
'
grep Eight normal >/dev/null &&
grep HT normal >/dev/null &&
grep With error >/dev/null &&
+ grep Return error >/dev/null &&
grep No normal >/dev/null
'
grep Eight error >/dev/null &&
grep HT normal >/dev/null &&
grep With normal >/dev/null &&
+ grep Return normal >/dev/null &&
grep No normal >/dev/null
'
grep Eight error >/dev/null &&
grep HT normal >/dev/null &&
grep With normal >/dev/null &&
+ grep Return normal >/dev/null &&
+ grep No normal >/dev/null
+
+'
+
+test_expect_success 'with cr-at-eol' '
+
+ rm -f .gitattributes
+ git config core.whitespace cr-at-eol
+ git diff --color >output
+ grep "$blue_grep" output >error
+ grep -v "$blue_grep" output >normal
+
+ grep Eight normal >/dev/null &&
+ grep HT error >/dev/null &&
+ grep With error >/dev/null &&
+ grep Return normal >/dev/null &&
+ grep No normal >/dev/null
+
+'
+
+test_expect_success 'with cr-at-eol (attribute)' '
+
+ git config --unset core.whitespace
+ echo "F whitespace=trailing,cr-at-eol" >.gitattributes
+ git diff --color >output
+ grep "$blue_grep" output >error
+ grep -v "$blue_grep" output >normal
+
+ grep Eight normal >/dev/null &&
+ grep HT error >/dev/null &&
+ grep With error >/dev/null &&
+ grep Return normal >/dev/null &&
grep No normal >/dev/null
'
index d09b9df89a7e19367640d4c6ad64ff828d01d26f..5a9ac457db41ff8347e807051d41fa0d5c6842ab 100644 (file)
--- a/ws.c
+++ b/ws.c
{ "trailing-space", WS_TRAILING_SPACE },
{ "space-before-tab", WS_SPACE_BEFORE_TAB },
{ "indent-with-non-tab", WS_INDENT_WITH_NON_TAB },
+ { "cr-at-eol", WS_CR_AT_EOL },
};
unsigned parse_whitespace_rule(const char *string)
int written = 0;
int trailing_whitespace = -1;
int trailing_newline = 0;
+ int trailing_carriage_return = 0;
int i;
/* Logic is simpler if we temporarily ignore the trailing newline. */
trailing_newline = 1;
len--;
}
+ if ((ws_rule & WS_CR_AT_EOL) &&
+ len > 0 && line[len - 1] == '\r') {
+ trailing_carriage_return = 1;
+ len--;
+ }
/* Check for trailing whitespace. */
if (ws_rule & WS_TRAILING_SPACE) {
}
if (stream) {
- /* Now the rest of the line starts at written.
- * The non-highlighted part ends at trailing_whitespace. */
+ /*
+ * Now the rest of the line starts at "written".
+ * The non-highlighted part ends at "trailing_whitespace".
+ */
if (trailing_whitespace == -1)
trailing_whitespace = len;
len - trailing_whitespace, 1, stream);
fputs(reset, stream);
}
+ if (trailing_carriage_return)
+ fputc('\r', stream);
if (trailing_newline)
fputc('\n', stream);
}