git apply: option to ignore whitespace differences
PATCH v4 1/1 git apply: option to ignore whitespace differences
Introduce --ignore-whitespace option and corresponding config bool to ignore whitespace differences while applying patches, akin to the 'patch' program.
'git am', 'git rebase' and the bash git completion are made aware of this option.
---
Following Junio's suggestion, the whitespace in the test has been replaced as appropriate with Z to prevent mangling on patch application. Some test cases have also been made more credible, and documentation has been added to all of them.
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 | 176 ++++++++++++++++++++++++++++++++ 11 files changed, 379 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 +176 -0
176 lines not on this page · raw message
Signature · 1 line
1.6.4.216.g15680.dirty
Quoted 41 lines · Giuseppe Bilotta
builtin-apply.c +33
-0@@ -1773,12 +1866,57 @@ static int match_fragment(struct image *img, !memcmp(img->buf + try, preimage->buf, preimage->len))return 1 ; +/* +* No exact match. If we are ignoring whitespace, run a line-by-line +* fuzzy matching. We collect all the line length information because +* we need it to adjust whitespace if we match. +*/ +if (ws_ignore_action == ignore_ws_change) { +size_t imgoff =0 ; +size_t preoff =0 ; +size_t postlen = postimage->len; +size_t imglen[preimage->nr]; +for (i =0 ; i < preimage->nr; i++) { + imglen[i] = img->line[try_lno+i].len; +size_t prelen = preimage->line[i].len; +if (!fuzzy_matchlines ( + img->buf + try + imgoff, imglen[i], + preimage->buf + preoff, prelen)) +return 0 ; +if (preimage->line[i].flag &LINE_COMMON ) + postlen += imglen[i] - prelen; + imgoff += imglen[i]; + preoff += prelen; + } + +/* +* Ok, the preimage matches with whitespace fuzz. Update it and +* the common postimage lines to use the same whitespace as the +* target. imgoff now holds the true length of the target that +* matches the preimage, and we need to update the line lengths +* of the preimage to match the target ones. +*/ + fixed_buf =xmalloc (imgoff); +memcpy (fixed_buf, img->buf + try, imgoff); +for (i =0 ; i < preimage->nr; i++)
builtin-apply.c static int match_fragment(struct image *img, · line 1911
Quoted 10 lines
builtin-apply.c +10
-0+ preimage->line[i].len = imglen[i]; + +/* +* Update the preimage buffer and the postimage context lines. +*/ +update_pre_post_images (preimage, postimage, + fixed_buf, imgoff, postlen); +return 1 ; + } +
Why do you need imglen[] vla here? IOW, can't the above be simply like this?
builtin-apply.c +6 -8
@@ -1874,20 +1874,18 @@ static int match_fragment(struct image *img,if (ws_ignore_action == ignore_ws_change) {size_t imgoff =0 ;size_t preoff =0 ;size_t postlen = postimage->len; -size_t imglen[preimage->nr];for (i =0 ; i < preimage->nr; i++) {size_t prelen = preimage->line[i].len; +size_t imglen = img->line[try_lno+i].len; - imglen[i] = img->line[try_lno+i].len; -if (!fuzzy_matchlines ( - img->buf + try + imgoff, imglen[i], - preimage->buf + preoff, prelen)) +if (!fuzzy_matchlines (img->buf + try + imgoff, imglen, + preimage->buf + preoff, prelen))return 0 ;if (preimage->line[i].flag &LINE_COMMON ) - postlen += imglen[i] - prelen; - imgoff += imglen[i]; + postlen += imglen - prelen; + imgoff += imglen; preoff += prelen; }/*
@@ -1899,9 +1897,9 @@ static int match_fragment(struct image *img,*/ fixed_buf =xmalloc (imgoff);memcpy (fixed_buf, img->buf + try, imgoff);for (i =0 ; i < preimage->nr; i++) - preimage->line[i].len = imglen[i]; + preimage->line[i].len = img->line[try_lno+i].len;/* * Update the preimage buffer and the postimage context lines. */
Quoted 35 lines · Junio C Hamano
Why do you need imglen[] vla here? IOW, can't the above be simply like this?
builtin-apply.c +5
-7@@ -1874,20 +1874,18 @@ static int match_fragment(struct image *img, if (ws_ignore_action == ignore_ws_change) { size_t imgoff = 0; size_t preoff = 0; size_t postlen = postimage->len; - size_t imglen[preimage->nr]; for (i = 0; i < preimage->nr; i++) { size_t prelen = preimage->line[i].len; + size_t imglen = img->line[try_lno+i].len; - imglen[i] = img->line[try_lno+i].len; - if (!fuzzy_matchlines( - img->buf + try + imgoff, imglen[i], - preimage->buf + preoff, prelen)) + if (!fuzzy_matchlines(img->buf + try + imgoff, imglen, + preimage->buf + preoff, prelen)) return 0; if (preimage->line[i].flag & LINE_COMMON) - postlen += imglen[i] - prelen; - imgoff += imglen[i]; + postlen += imglen - prelen; + imgoff += imglen; preoff += prelen; } /*
builtin-apply.c static int match_fragment(struct image *img, · line 1901
Quoted 6 lines
builtin-apply.c +1
-1@@ -1899,9 +1897,9 @@ static int match_fragment(struct image *img,*/ fixed_buf = xmalloc(imgoff); memcpy(fixed_buf, img->buf + try, imgoff); for (i = 0; i < preimage->nr; i++) - preimage->line[i].len = imglen[i]; + preimage->line[i].len = img->line[try_lno+i].len;
Yep, I think that would do it. I'm not sure why I was doing it that other way. Maybe a leftover from when I was still getting confident with the code and I hadn't yet found the var that held the initial match line, or something like that.
Signature · 1 line
Giuseppe "Oblomov" Bilotta