From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: [PATCH v2 3/4] apply: count the size of postimage correctly
Date: Thu, 22 Jan 2015 14:58:24 -0800 [thread overview]
Message-ID: <1421967505-16879-4-git-send-email-gitster@pobox.com> (raw)
In-Reply-To: <1421967505-16879-1-git-send-email-gitster@pobox.com>
Under --whitespace=fix option, match_fragment() function examines
the preimage (the common context and the removed lines in the patch)
and the file being patched and checks if they match after correcting
all whitespace errors. When they are found to match, the common
context lines in the preimage is replaced with the fixed copy,
because these lines will then be copied to the corresponding place
in the postimage by a later call to update_pre_post_images(). Lines
that are added in the postimage, under --whitespace=fix, have their
whitespace errors already fixed when apply_one_fragment() prepares
the preimage and the postimage, so in the end, application of the
patch can be done by replacing the block of text in the file being
patched that matched the preimage with what is in the postimage that
was updated by update_pre_post_images().
In the earlier days, fixing whitespace errors always resulted in
reduction of size, either collapsing runs of spaces in the indent to
a tab or removing the trailing whitespaces. These days, however,
some whitespace error fix results in extending the size.
250b3c6c (apply --whitespace=fix: avoid running over the postimage
buffer, 2013-03-22) tried to compute the final postimage size but
its math was flawed. It counted the size of the block of text in
the original being patched after fixing the whitespace errors on its
lines that correspond to the preimage. That number does not have
much to do with how big the final postimage would be.
Instead count (1) the added lines in the postimage, whose size is
the same as in the final patch result because their whitespace
errors have already been corrected, and (2) the fixed size of the
lines that are common.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
builtin/apply.c | 23 +++++++++++++++++++++--
1 file changed, 21 insertions(+), 2 deletions(-)
diff --git a/builtin/apply.c b/builtin/apply.c
index da6fb35..e895340 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -2336,6 +2336,23 @@ static int match_fragment(struct image *img,
* ignore whitespace, we were asked to correct whitespace
* errors, so let's try matching after whitespace correction.
*
+ * While checking the preimage against the target, whitespace
+ * errors in both fixed, we count how large the corresponding
+ * postimage needs to be. The postimage prepared by
+ * apply_one_fragment() has whitespace errors fixed on added
+ * lines already, but the common lines were propagated as-is,
+ * which may become longer when their whitespace errors are
+ * fixed.
+ */
+
+ /* First count added lines in postimage */
+ postlen = 0;
+ for (i = 0; i < postimage->nr; i++) {
+ if (!(postimage->line[i].flag & LINE_COMMON))
+ postlen += postimage->line[i].len;
+ }
+
+ /*
* The preimage may extend beyond the end of the file,
* but in this loop we will only handle the part of the
* preimage that falls within the file.
@@ -2343,7 +2360,6 @@ static int match_fragment(struct image *img,
strbuf_init(&fixed, preimage->len + 1);
orig = preimage->buf;
target = img->buf + try;
- postlen = 0;
for (i = 0; i < preimage_limit; i++) {
size_t oldlen = preimage->line[i].len;
size_t tgtlen = img->line[try_lno + i].len;
@@ -2371,7 +2387,10 @@ static int match_fragment(struct image *img,
match = (tgtfix.len == fixed.len - fixstart &&
!memcmp(tgtfix.buf, fixed.buf + fixstart,
fixed.len - fixstart));
- postlen += tgtfix.len;
+
+ /* Add the length if this is common with the postimage */
+ if (preimage->line[i].flag & LINE_COMMON)
+ postlen += tgtfix.len;
strbuf_release(&tgtfix);
if (!match)
--
2.3.0-rc1-116-g84c5016
next prev parent reply other threads:[~2015-01-22 22:58 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-01-14 18:20 Segmentation fault in git apply Michael Blume
2015-01-14 18:40 ` Michael Blume
2015-01-14 18:44 ` Michael Blume
2015-01-14 18:48 ` Michael Blume
2015-01-14 18:58 ` Michael Blume
2015-01-14 19:09 ` Michael Blume
2015-01-15 8:26 ` Kyle J. McKay
2015-01-15 9:10 ` Kyle J. McKay
2015-01-16 19:58 ` Junio C Hamano
2015-01-16 23:54 ` [PATCH] apply: count the size of postimage correctly Junio C Hamano
2015-01-18 10:49 ` [PATCH] test: add git apply whitespace expansion tests Kyle J. McKay
2015-01-18 22:11 ` Junio C Hamano
2015-01-19 3:54 ` Kyle J. McKay
2015-01-21 22:33 ` Junio C Hamano
2015-01-22 6:55 ` Kyle J. McKay
2015-01-22 19:23 ` Junio C Hamano
2015-01-23 0:12 ` Kyle J. McKay
2015-01-22 22:58 ` [PATCH v2 0/4] apply --whitespace=fix buffer corruption fix Junio C Hamano
2015-01-22 22:58 ` [PATCH v2 1/4] apply.c: typofix Junio C Hamano
2015-01-22 23:17 ` Stefan Beller
2015-01-22 23:42 ` Junio C Hamano
2015-01-22 23:48 ` Stefan Beller
2015-01-22 22:58 ` [PATCH v2 2/4] apply: make update_pre_post_images() sanity check the given postlen Junio C Hamano
2015-01-22 22:58 ` Junio C Hamano [this message]
2015-01-22 22:58 ` [PATCH v2 4/4] apply: detect and mark whitespace errors in context lines when fixing Junio C Hamano
2015-01-14 18:50 ` Segmentation fault in git apply Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1421967505-16879-4-git-send-email-gitster@pobox.com \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).