Introduce --ignore-whitespace option to ignore whitespace differences while applying the patch. 'git am' and 'git rebase' are made aware of this option and pass it through to 'git apply', and so is the bash git completion. Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@xxxxxxxxx> --- builtin-apply.c | 49 ++++++++++++++++++++++++++++++-- contrib/completion/git-completion.bash | 2 + git-am.sh | 3 +- git-rebase.sh | 3 ++ 4 files changed, 53 insertions(+), 4 deletions(-) I must say that the only thing I don't like about this patch is the fact that context lines matching with whitespace differences are taken from the patch rather than being left untouched. diff --git a/builtin-apply.c b/builtin-apply.c index dc0ff5e..82efee7 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -39,6 +39,7 @@ static int diffstat; static int numstat; static int summary; static int check; +static int ignore_whitespace = 0; static int apply = 1; static int apply_in_reverse; static int apply_with_reject; @@ -214,6 +215,41 @@ static uint32_t hash_line(const char *cp, size_t len) return h; } +/* + * Compare two memory areas ignoring whitespace differences + */ +static int memcmp_ignore_whitespace(const char *s1, const char *s2, size_t n) +{ + const char *stop = s1 + n; + int result; + + if (!n) + return 0; + + do { + if (isspace(*s1) && isspace(*s2)) { + while (isspace(*s1)) { + s1++; + } + while (isspace(*s2)) + s2++; + if (s1 >= stop) + break; + } + result = *s1++ - *s2++; + } while (!result && (s1 < stop)); + + return result; +} + +static int memcmp_switch(const char *s1, const char *s2, size_t n) +{ + if (ignore_whitespace) + return memcmp_ignore_whitespace(s1, s2, n); + else + return memcmp(s1, s2, n); +} + static void add_line_info(struct image *img, const char *bol, size_t len, unsigned flag) { ALLOC_GROW(img->line_allocated, img->nr + 1, img->alloc); @@ -1690,10 +1726,10 @@ static int match_fragment(struct image *img, if ((match_end ? (try + preimage->len == img->len) : (try + preimage->len <= img->len)) && - !memcmp(img->buf + try, preimage->buf, preimage->len)) + !memcmp_switch(preimage->buf, img->buf + try, preimage->len)) return 1; - if (ws_error_action != correct_ws_error) + if (!ignore_whitespace && (ws_error_action != correct_ws_error)) return 0; /* @@ -1731,8 +1767,13 @@ static int match_fragment(struct image *img, * In either case, we are fixing the whitespace breakages * so we might as well take the fix together with their * real change. + * If we are ignoring whitespace differences, don't check + * for length equality. */ - match = (tgtfixlen == fixlen && !memcmp(tgtfix, buf, fixlen)); + if (ignore_whitespace) + match = !memcmp_ignore_whitespace(buf, tgtfix, fixlen); + else + match = (tgtfixlen == fixlen && !memcmp(tgtfix, buf, fixlen)); if (tgtfix != tgtfixbuf) free(tgtfix); @@ -3304,6 +3345,8 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix) { OPTION_CALLBACK, 0, "whitespace", &whitespace_option, "action", "detect new or modified lines that have whitespace errors", 0, option_parse_whitespace }, + OPT_BOOLEAN(0, "ignore-whitespace", &ignore_whitespace, + "ignore whitespace differences when finding context"), OPT_BOOLEAN('R', "reverse", &apply_in_reverse, "apply the patch in reverse"), OPT_BOOLEAN(0, "unidiff-zero", &unidiff_zero, diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index ddb71e2..d3415b5 100755 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -663,6 +663,7 @@ _git_am () --*) __gitcomp " --3way --committer-date-is-author-date --ignore-date + --ignore-whitespace --interactive --keep --no-utf8 --signoff --utf8 --whitespace= " @@ -684,6 +685,7 @@ _git_apply () --stat --numstat --summary --check --index --cached --index-info --reverse --reject --unidiff-zero --apply --no-add --exclude= + --ignore-whitespace --whitespace= --inaccurate-eof --verbose " return diff --git a/git-am.sh b/git-am.sh index 58d4eb6..8e8598c 100755 --- a/git-am.sh +++ b/git-am.sh @@ -16,6 +16,7 @@ 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-whitespace pass it through git-apply directory= pass it through git-apply C= pass it through git-apply p= pass it through git-apply @@ -180,7 +181,7 @@ do git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;; -C|-p) git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;; - --reject) + --reject|--ignore-whitespace) git_apply_opt="$git_apply_opt $1" ;; --committer-date-is-author-date) committer_date_is_author_date=t ;; diff --git a/git-rebase.sh b/git-rebase.sh index 18bc694..d741752 100755 --- a/git-rebase.sh +++ b/git-rebase.sh @@ -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 -- 1.6.3.3.487.gc7c49 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html