diff_grep: use textconv buffers for add/deleted files
authorJeff King <peff@peff.net>
Sun, 28 Oct 2012 11:40:00 +0000 (07:40 -0400)
committerJeff King <peff@peff.net>
Sun, 28 Oct 2012 11:59:44 +0000 (07:59 -0400)
If you use "-G" to grep a diff, we will apply a configured
textconv filter to the data before generating the diff.
However, if the diff is an addition or deletion, we do not
bother running the diff at all, and just look for the token
in the added (or removed) content. This works because we
know that the diff must contain every line of content.

However, while we used the textconv-derived buffers in the
regular diff, we accidentally passed the original unmodified
buffers to regexec when checking the added or removed
content. This could lead to an incorrect answer.

Worse, in some cases we might have a textconv buffer but no
original buffer (e.g., if we pulled the textconv data from
cache, or if we reused a working tree file when generating
it). In that case, we could actually feed NULL to regexec
and segfault.

Reported-by: Peter Oberndorfer <kumbayo84@arcor.de>
Signed-off-by: Jeff King <peff@peff.net>
diffcore-pickaxe.c
t/t4030-diff-textconv.sh
index ed23eb4bdda3b595ac956ba65557b1dc7d50426d..a20937635435f6672f801454c992cbeeb8d17a1a 100644 (file)
@@ -104,10 +104,10 @@ static int diff_grep(struct diff_filepair *p, struct diff_options *o,
                if (!mf2.ptr)
                        return 0; /* ignore unmerged */
                /* created "two" -- does it have what we are looking for? */
-               hit = !regexec(regexp, p->two->data, 1, &regmatch, 0);
+               hit = !regexec(regexp, mf2.ptr, 1, &regmatch, 0);
        } else if (!mf2.ptr) {
                /* removed "one" -- did it have what we are looking for? */
-               hit = !regexec(regexp, p->one->data, 1, &regmatch, 0);
+               hit = !regexec(regexp, mf1.ptr, 1, &regmatch, 0);
        } else {
                /*
                 * We have both sides; need to run textual diff and see if
index eebb1eed8b1e084ddc048dc20797dfcd2f37e3ea..461d27ac202cc51f1f09b7a18de8b747198700e4 100755 (executable)
@@ -84,6 +84,18 @@ test_expect_success 'status -v produces text' '
        git reset --soft HEAD@{1}
 '
 
+test_expect_success 'grep-diff (-G) operates on textconv data (add)' '
+       echo one >expect &&
+       git log --root --format=%s -G0 >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'grep-diff (-G) operates on textconv data (modification)' '
+       echo two >expect &&
+       git log --root --format=%s -G1 >actual &&
+       test_cmp expect actual
+'
+
 cat >expect.stat <<'EOF'
  file | Bin 2 -> 4 bytes
  1 file changed, 0 insertions(+), 0 deletions(-)