git apply: option to ignore whitespace differences
PATCH v4 1/1 git apply: option to ignore whitespace differences
Introduce --ignore-whitespace / --ignore-space-change option and corresponding config apply.ignorewhitespace to ignore changes in whitespace of context lines while applying patches. The two names match the corresponding option resp. in 'patch' and 'git diff'.
'git am', 'git rebase' and the bash git completion are made aware of the option.
---
Changes wrt the previous version:
* the option has a dual name
* the option management has a cleaner structure, allowing an easier implementation of options such as --ignore-space-at-eol or
--ignore-all-space
* the documentation mentions that ignoring changes in whitespace prevents context lines from being whitespace-fixed
Documentation/config.txt | 8 ++ Documentation/git-am.txt | 5 +- Documentation/git-apply.txt | 13 +++ Documentation/git-rebase.txt | 3 +- builtin-apply.c | 172 ++++++++++++++++++++++++++++++-- cache.h | 1 + contrib/completion/git-completion.bash | 3 + environment.c | 1 + git-am.sh | 4 +- git-rebase.sh | 3 + t/t4107-apply-ignore-whitespace.sh | 149 +++++++++++++++++++++++++++ 11 files changed, 352 insertions(+), 10 deletions(-) create mode 100755 t/t4107-apply-ignore-whitespace.sh
Documentation/config.txt +8 -0
@@ -461,6 +461,14 @@ it will be treated as a shell command. For example, defining executed from the top-level directory of a repository, which may not necessarily be the current directory. +apply.ignorewhitespace:: + When set to 'change', tells 'git-apply' to ignore changes in + whitespace, in the same way as the '--ignore-space-change' + option. + When set to one of: no, none, never, false tells 'git-apply' to + respect all whitespace differences. + See linkgit:git-apply[1]. + apply.whitespace:: Tells 'git-apply' how to handle whitespaces, in the same way as the '--whitespace' option. See linkgit:git-apply[1].
Documentation/git-am.txt +4 -1
@@ -11,7 +11,7 @@ SYNOPSIS [verse] 'git am' [--signoff] [--keep] [--utf8 | --no-utf8] [--3way] [--interactive] [--committer-date-is-author-date] - [--ignore-date] + [--ignore-date] [--ignore-space-change | --ignore-whitespace] [--whitespace=<option>] [-C<n>] [-p<n>] [--directory=<dir>] [--reject] [-q | --quiet] [<mbox> | <Maildir>...]
@@ -65,6 +65,9 @@ default. You can use `--no-utf8` to override this. it is supposed to apply to and we have those blobs available locally. +--ignore-date:: +--ignore-space-change:: +--ignore-whitespace:: --whitespace=<option>:: -C<n>:: -p<n>::
Documentation/git-apply.txt +13 -0
@@ -13,6 +13,7 @@ SYNOPSIS [--apply] [--no-add] [--build-fake-ancestor=<file>] [-R | --reverse] [--allow-binary-replacement | --binary] [--reject] [-z] [-pNUM] [-CNUM] [--inaccurate-eof] [--recount] [--cached] + [--ignore-space-change | --ignore-whitespace ] [--whitespace=<nowarn|warn|fix|error|error-all>] [--exclude=PATH] [--include=PATH] [--directory=<root>] [--verbose] [<patch>...]
@@ -149,6 +150,14 @@ patch to each path is used. A patch to a path that does not match any include/exclude pattern is used by default if there is no include pattern on the command line, and ignored if there is any include pattern. +--ignore-space-change:: +--ignore-whitespace:: + When applying a patch, ignore changes in whitespace in context + lines if necessary. + Context lines will preserve their whitespace, and they will not + undergo whitespace fixing regardless of the value of the + `--whitespace` option. New lines will still be fixed, though. + --whitespace=<action>:: When applying a patch, detect a new or modified line that has whitespace errors. What are considered whitespace errors is
@@ -205,6 +214,10 @@ running `git apply --directory=modules/git-gui`. Configuration ------------- +apply.ignorewhitespace:: + Set to 'change' if you want changes in whitespace to be ignored by default. + Set to one of: no, none, never, false if you want changes in + whitespace to be significant. apply.whitespace:: When no `--whitespace` flag is given from the command line, this configuration item is used as the default.
Documentation/git-rebase.txt +2 -1
@@ -268,8 +268,9 @@ OPTIONS exit with the message "Current branch is up to date" in such a situation. +--ignore-whitespace:: --whitespace=<option>:: - This flag is passed to the 'git-apply' program + These flag are passed to the 'git-apply' program (see linkgit:git-apply[1]) that applies the patch. Incompatible with the --interactive option.
builtin-apply.c +165 -7
247 lines not on this page · raw message
cache.h +1 -0
@@ -509,6 +509,7 @@ extern int log_all_ref_updates;extern int warn_ambiguous_refs;extern int shared_repository;extern const char *apply_default_whitespace; +extern const char *apply_default_ignorewhitespace;extern int zlib_compression_level;extern int core_compression_level;extern int core_compression_seen;
contrib/completion/git-completion.bash +3 -0
@@ -674,6 +674,7 @@ _git_am () --*) __gitcomp " --3way --committer-date-is-author-date --ignore-date + --ignore-whitespace --ignore-space-change --interactive --keep --no-utf8 --signoff --utf8 --whitespace= "
@@ -695,6 +696,7 @@ _git_apply () --stat --numstat --summary --check --index --cached --index-info --reverse --reject --unidiff-zero --apply --no-add --exclude= + --ignore-whitespace --ignore-space-change --whitespace= --inaccurate-eof --verbose " return
@@ -1537,6 +1539,7 @@ _git_config () __gitcomp " add.ignore-errors alias. + apply.ignorewhitespace apply.whitespace branch.autosetupmerge branch.autosetuprebase
environment.c +1 -0
@@ -26,6 +26,7 @@ const char *git_commit_encoding;const char *git_log_output_encoding;int shared_repository =PERM_UMASK ;const char *apply_default_whitespace; +const char *apply_default_ignorewhitespace;int zlib_compression_level =Z_BEST_SPEED ;int core_compression_level;int core_compression_seen;
git-am.sh +3 -1
@@ -16,6 +16,8 @@ s,signoff add a Signed-off-by line to the commit message u,utf8 recode into utf8 (default) k,keep pass -k flag to git-mailinfo whitespace= pass it through git-apply +ignore-space-change pass it through git-apply +ignore-whitespace pass it through git-apply directory= pass it through git-apply C= pass it through git-apply p= pass it through git-apply
@@ -303,7 +305,7 @@ do git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;; --patch-format) shift ; patch_format="$1" ;; - --reject) + --reject|--ignore-whitespace|--ignore-space-change) git_apply_opt="$git_apply_opt $1" ;; --committer-date-is-author-date) committer_date_is_author_date=t ;;
git-rebase.sh +3 -0
@@ -333,6 +333,9 @@ do ;; esac ;; + --ignore-whitespace) + git_am_opt="$git_am_opt $1" + ;; --committer-date-is-author-date|--ignore-date) git_am_opt="$git_am_opt $1" force_rebase=t
t/t4107-apply-ignore-whitespace.sh new file +149 -0
149 lines not on this page · raw message
Signature · 1 line
1.6.4.216.g15680.dirty
Quoted 6 lines · Giuseppe Bilotta
t/t4107-apply-ignore-whitespace.sh new file +0
-0
t/t4107-apply-ignore-whitespace.sh line 65
Quoted 8 lines
t/t4107-apply-ignore-whitespace.sh new file +7
-0@@ -0,0 +1,149 @@ +cat > patch3.patch <<\EOF +diff --git a/main.c b/main.c +--- a/main.c ++++ b/main.c +@@ -10,1 +10,1 @@ + print_int(func(i)); +EOF
This part is triply troublesome in that:
(1) the payload ends with trailing whitespace which can be eaten by MUAs
(and I almost always use "git am" with --whitespace=fix);
(2) even if we apply your patch correctly, it is very unobvious that you are testing the case where the line has an unwanted trailing whitespace; and
(3) a hunk without any added/deleted lines is an obviously artificial test input that would not appear in real life, something one would never think of doing unless one knows how "git apply" internally works. It makes the test too knowledgable about the implementation.
You can fix the first two issues by doing:
sed -e 's/Z/ /g' >patch3.patch <<\EOF
...
+Z print_int(func(i));Z
EOFto make invisible SP stand out more for the benefit of people reading the test script (I know you did not have leading SP before HT in yours, but the above illustrates the visibility issues). For other tests with test vector patches, visibility of whitespace is not much an issue, but this script is _all about_ whitespace, so anything that clarifies what is going on better would help.
t/t4107-apply-ignore-whitespace.sh line 115
Quoted 5 lines
t/t4107-apply-ignore-whitespace.sh new file +5
-0+test_expect_success "S = patch1" \ + 'git-apply patch1.patch' + +test_expect_failure "F = patch2" \ + 'git-apply patch2.patch'
Please say
test_expect_success "whitespace corrupted patch does not apply" '
test_must_fail git apply patch2.patch
'
instead.
"test_expect_failure" is a declaration that the command being tested (in this case "git apply") is faulty. It also is a request for somebody interested to later fix it (again, in this case "git apply") to make this test pass.
But you do _not_ want this to pass without an explicit ignore option, so test_expect_failure is inappropriate here.
Quoted 19 lines · Junio C Hamano
Quoted 6 lines · Giuseppe Bilotta
t/t4107-apply-ignore-whitespace.sh new file +0
-0t/t4107-apply-ignore-whitespace.sh line 65
Quoted 8 lines
t/t4107-apply-ignore-whitespace.sh new file +7
-0@@ -0,0 +1,149 @@ +cat > patch3.patch <<\EOF +diff --git a/main.c b/main.c +--- a/main.c ++++ b/main.c +@@ -10,1 +10,1 @@ + print_int(func(i)); +EOFThis part is triply troublesome in that:
Quoted 2 lines
(1) the payload ends with trailing whitespace which can be eaten by MUAs (and I almost always use "git am" with --whitespace=fix);
This is also a problem with some other parts of that patch. I'll apply the suggestion you mention below to all of them.
Quoted 3 lines
(2) even if we apply your patch correctly, it is very unobvious that you are testing the case where the line has an unwanted trailing whitespace; and
I'll add a comment for that that.
Quoted 4 lines
(3) a hunk without any added/deleted lines is an obviously artificial test input that would not appear in real life, something one would never think of doing unless one knows how "git apply" internally works. It makes the test too knowledgable about the implementation.
Right. I'll make it do something else too.
Quoted 7 lines
You can fix the first two issues by doing:
sed -e 's/Z/ /g' >patch3.patch <<\EOF
...
+Z print_int(func(i));Z
EOF
Quoted 6 lines
to make invisible SP stand out more for the benefit of people reading the test script (I know you did not have leading SP before HT in yours, but the above illustrates the visibility issues). For other tests with test vector patches, visibility of whitespace is not much an issue, but this script is
_allabout_whitespace, so anything that clarifies what is going on better would help.
Indeed.
Quoted 14 lines
t/t4107-apply-ignore-whitespace.sh line 115
Quoted 5 lines
t/t4107-apply-ignore-whitespace.sh new file +5
-0+test_expect_success "S = patch1" \ + 'git-apply patch1.patch' + +test_expect_failure "F = patch2" \ + 'git-apply patch2.patch'Please say
test_expect_success "whitespace corrupted patch does not apply" '
test_must_failgit apply patch2.patch
'instead.
Quoted 7 lines
"
test_expect_failure" is a declaration that the command being tested (in this case "git apply") is faulty. It also is a request for somebody interested to later fix it (again, in this case "git apply") to make this test pass.But you do _not_ want this to pass without an explicit ignore option, so
test_expect_failureis inappropriate here.
Ah, interesting. This was the first test I wrote (mostly copied from the original patch) and I didn't have a clear idea of the difference between the expected failures (although the comment about 'fixing' that the test printed when some failing case didn't should have put me on the right track).
I'll resend with a fixed test.
Signature · 1 line
Giuseppe "Oblomov" Bilotta