From b0f7c7cf865e6b14ccf90f34b6fde0d08af6b50b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 22 Mar 2014 18:15:51 +0100 Subject: [PATCH 01/10] t4209: set up expectations up front Instead of creating an expect file for each test, build three files with the possible valid values during setup and use them in the tests. This shortens the test code and saves nine calls to git rev-parse. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- t/t4209-log-pickaxe.sh | 64 +++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 39 deletions(-) diff --git a/t/t4209-log-pickaxe.sh b/t/t4209-log-pickaxe.sh index 38fb80f643..ff668b5877 100755 --- a/t/t4209-log-pickaxe.sh +++ b/t/t4209-log-pickaxe.sh @@ -4,80 +4,74 @@ test_description='log --grep/--author/--regexp-ignore-case/-S/-G' . ./test-lib.sh test_expect_success setup ' + >expect_nomatch && + >file && git add file && test_tick && git commit -m initial && + git rev-parse --verify HEAD >expect_initial && echo Picked >file && + git add file && test_tick && - git commit -a --author="Another Person " -m second + git commit --author="Another Person " -m second && + git rev-parse --verify HEAD >expect_second ' test_expect_success 'log --grep' ' git log --grep=initial --format=%H >actual && - git rev-parse --verify HEAD^ >expect && - test_cmp expect actual + test_cmp expect_initial actual ' test_expect_success 'log --grep --regexp-ignore-case' ' git log --regexp-ignore-case --grep=InItial --format=%H >actual && - git rev-parse --verify HEAD^ >expect && - test_cmp expect actual + test_cmp expect_initial actual ' test_expect_success 'log --grep -i' ' git log -i --grep=InItial --format=%H >actual && - git rev-parse --verify HEAD^ >expect && - test_cmp expect actual + test_cmp expect_initial actual ' test_expect_success 'log --author --regexp-ignore-case' ' git log --regexp-ignore-case --author=person --format=%H >actual && - git rev-parse --verify HEAD >expect && - test_cmp expect actual + test_cmp expect_second actual ' test_expect_success 'log --author -i' ' git log -i --author=person --format=%H >actual && - git rev-parse --verify HEAD >expect && - test_cmp expect actual + test_cmp expect_second actual ' test_expect_success 'log -G (nomatch)' ' git log -Gpicked --format=%H >actual && - >expect && - test_cmp expect actual + test_cmp expect_nomatch actual ' test_expect_success 'log -G (match)' ' git log -GPicked --format=%H >actual && - git rev-parse --verify HEAD >expect && - test_cmp expect actual + test_cmp expect_second actual ' test_expect_success 'log -G --regexp-ignore-case (nomatch)' ' git log --regexp-ignore-case -Gpickle --format=%H >actual && - >expect && - test_cmp expect actual + test_cmp expect_nomatch actual ' test_expect_success 'log -G -i (nomatch)' ' git log -i -Gpickle --format=%H >actual && - >expect && - test_cmp expect actual + test_cmp expect_nomatch actual ' test_expect_success 'log -G --regexp-ignore-case (match)' ' git log --regexp-ignore-case -Gpicked --format=%H >actual && - git rev-parse --verify HEAD >expect && - test_cmp expect actual + test_cmp expect_second actual ' test_expect_success 'log -G -i (match)' ' git log -i -Gpicked --format=%H >actual && - git rev-parse --verify HEAD >expect && - test_cmp expect actual + test_cmp expect_second actual ' test_expect_success 'log -G --textconv (missing textconv tool)' ' @@ -89,45 +83,38 @@ test_expect_success 'log -G --textconv (missing textconv tool)' ' test_expect_success 'log -G --no-textconv (missing textconv tool)' ' echo "* diff=test" >.gitattributes && git -c diff.test.textconv=missing log -Gfoo --no-textconv >actual && - >expect && - test_cmp expect actual && + test_cmp expect_nomatch actual && rm .gitattributes ' test_expect_success 'log -S (nomatch)' ' git log -Spicked --format=%H >actual && - >expect && - test_cmp expect actual + test_cmp expect_nomatch actual ' test_expect_success 'log -S (match)' ' git log -SPicked --format=%H >actual && - git rev-parse --verify HEAD >expect && - test_cmp expect actual + test_cmp expect_second actual ' test_expect_success 'log -S --regexp-ignore-case (match)' ' git log --regexp-ignore-case -Spicked --format=%H >actual && - git rev-parse --verify HEAD >expect && - test_cmp expect actual + test_cmp expect_second actual ' test_expect_success 'log -S -i (match)' ' git log -i -Spicked --format=%H >actual && - git rev-parse --verify HEAD >expect && - test_cmp expect actual + test_cmp expect_second actual ' test_expect_success 'log -S --regexp-ignore-case (nomatch)' ' git log --regexp-ignore-case -Spickle --format=%H >actual && - >expect && - test_cmp expect actual + test_cmp expect_nomatch actual ' test_expect_success 'log -S -i (nomatch)' ' git log -i -Spickle --format=%H >actual && - >expect && - test_cmp expect actual + test_cmp expect_nomatch actual ' test_expect_success 'log -S --textconv (missing textconv tool)' ' @@ -139,8 +126,7 @@ test_expect_success 'log -S --textconv (missing textconv tool)' ' test_expect_success 'log -S --no-textconv (missing textconv tool)' ' echo "* diff=test" >.gitattributes && git -c diff.test.textconv=missing log -Sfoo --no-textconv >actual && - >expect && - test_cmp expect actual && + test_cmp expect_nomatch actual && rm .gitattributes ' From 57b6dc76f28544737b5aa6bddafa2ee5f42047d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 22 Mar 2014 18:15:52 +0100 Subject: [PATCH 02/10] t4209: factor out helper function test_log() Twelve tests in t4209 follow the same simple pattern for description, git log call and checking. Extract that shared logic into a helper function named test_log. Test specifications become a lot more compact, new tests can be added more easily. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- t/t4209-log-pickaxe.sh | 100 +++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 58 deletions(-) diff --git a/t/t4209-log-pickaxe.sh b/t/t4209-log-pickaxe.sh index ff668b5877..9c3b596366 100755 --- a/t/t4209-log-pickaxe.sh +++ b/t/t4209-log-pickaxe.sh @@ -3,6 +3,36 @@ test_description='log --grep/--author/--regexp-ignore-case/-S/-G' . ./test-lib.sh +test_log () { + expect=$1 + kind=$2 + needle=$3 + shift 3 + rest=$@ + + case $kind in + --*) + opt=$kind=$needle + ;; + *) + opt=$kind$needle + ;; + esac + case $expect in + expect_nomatch) + match=nomatch + ;; + *) + match=match + ;; + esac + + test_expect_success "log $kind${rest:+ $rest} ($match)" " + git log $rest $opt --format=%H >actual && + test_cmp $expect actual + " +} + test_expect_success setup ' >expect_nomatch && @@ -44,35 +74,12 @@ test_expect_success 'log --author -i' ' test_cmp expect_second actual ' -test_expect_success 'log -G (nomatch)' ' - git log -Gpicked --format=%H >actual && - test_cmp expect_nomatch actual -' - -test_expect_success 'log -G (match)' ' - git log -GPicked --format=%H >actual && - test_cmp expect_second actual -' - -test_expect_success 'log -G --regexp-ignore-case (nomatch)' ' - git log --regexp-ignore-case -Gpickle --format=%H >actual && - test_cmp expect_nomatch actual -' - -test_expect_success 'log -G -i (nomatch)' ' - git log -i -Gpickle --format=%H >actual && - test_cmp expect_nomatch actual -' - -test_expect_success 'log -G --regexp-ignore-case (match)' ' - git log --regexp-ignore-case -Gpicked --format=%H >actual && - test_cmp expect_second actual -' - -test_expect_success 'log -G -i (match)' ' - git log -i -Gpicked --format=%H >actual && - test_cmp expect_second actual -' +test_log expect_nomatch -G picked +test_log expect_second -G Picked +test_log expect_nomatch -G pickle --regexp-ignore-case +test_log expect_nomatch -G pickle -i +test_log expect_second -G picked --regexp-ignore-case +test_log expect_second -G picked -i test_expect_success 'log -G --textconv (missing textconv tool)' ' echo "* diff=test" >.gitattributes && @@ -87,35 +94,12 @@ test_expect_success 'log -G --no-textconv (missing textconv tool)' ' rm .gitattributes ' -test_expect_success 'log -S (nomatch)' ' - git log -Spicked --format=%H >actual && - test_cmp expect_nomatch actual -' - -test_expect_success 'log -S (match)' ' - git log -SPicked --format=%H >actual && - test_cmp expect_second actual -' - -test_expect_success 'log -S --regexp-ignore-case (match)' ' - git log --regexp-ignore-case -Spicked --format=%H >actual && - test_cmp expect_second actual -' - -test_expect_success 'log -S -i (match)' ' - git log -i -Spicked --format=%H >actual && - test_cmp expect_second actual -' - -test_expect_success 'log -S --regexp-ignore-case (nomatch)' ' - git log --regexp-ignore-case -Spickle --format=%H >actual && - test_cmp expect_nomatch actual -' - -test_expect_success 'log -S -i (nomatch)' ' - git log -i -Spickle --format=%H >actual && - test_cmp expect_nomatch actual -' +test_log expect_nomatch -S picked +test_log expect_second -S Picked +test_log expect_second -S picked --regexp-ignore-case +test_log expect_second -S picked -i +test_log expect_nomatch -S pickle --regexp-ignore-case +test_log expect_nomatch -S pickle -i test_expect_success 'log -S --textconv (missing textconv tool)' ' echo "* diff=test" >.gitattributes && From e7880fcd41dd4424a79efb0a939f1c008c5519c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 22 Mar 2014 18:15:53 +0100 Subject: [PATCH 03/10] t4209: factor out helper function test_log_icase() Reduce code duplication by introducing test_log_icase() that runs the same test with both --regexp-ignore-case and -i. The specification of the four basic test scenarios (matching/nomatching combined with case sensitive/insensitive) becomes easier to read and write. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- t/t4209-log-pickaxe.sh | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/t/t4209-log-pickaxe.sh b/t/t4209-log-pickaxe.sh index 9c3b596366..919b371169 100755 --- a/t/t4209-log-pickaxe.sh +++ b/t/t4209-log-pickaxe.sh @@ -33,6 +33,12 @@ test_log () { " } +# test -i and --regexp-ignore-case and expect both to behave the same way +test_log_icase () { + test_log $@ --regexp-ignore-case + test_log $@ -i +} + test_expect_success setup ' >expect_nomatch && @@ -74,12 +80,10 @@ test_expect_success 'log --author -i' ' test_cmp expect_second actual ' -test_log expect_nomatch -G picked -test_log expect_second -G Picked -test_log expect_nomatch -G pickle --regexp-ignore-case -test_log expect_nomatch -G pickle -i -test_log expect_second -G picked --regexp-ignore-case -test_log expect_second -G picked -i +test_log expect_nomatch -G picked +test_log expect_second -G Picked +test_log_icase expect_nomatch -G pickle +test_log_icase expect_second -G picked test_expect_success 'log -G --textconv (missing textconv tool)' ' echo "* diff=test" >.gitattributes && @@ -94,12 +98,10 @@ test_expect_success 'log -G --no-textconv (missing textconv tool)' ' rm .gitattributes ' -test_log expect_nomatch -S picked -test_log expect_second -S Picked -test_log expect_second -S picked --regexp-ignore-case -test_log expect_second -S picked -i -test_log expect_nomatch -S pickle --regexp-ignore-case -test_log expect_nomatch -S pickle -i +test_log expect_nomatch -S picked +test_log expect_second -S Picked +test_log_icase expect_second -S picked +test_log_icase expect_nomatch -S pickle test_expect_success 'log -S --textconv (missing textconv tool)' ' echo "* diff=test" >.gitattributes && From 65a3402f427e76e50e540dbfbe2317d5c66a537d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 22 Mar 2014 18:15:54 +0100 Subject: [PATCH 04/10] t4209: use helper functions to test --grep Also add tests for non-matching cases. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- t/t4209-log-pickaxe.sh | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/t/t4209-log-pickaxe.sh b/t/t4209-log-pickaxe.sh index 919b371169..eaa14a057c 100755 --- a/t/t4209-log-pickaxe.sh +++ b/t/t4209-log-pickaxe.sh @@ -55,20 +55,10 @@ test_expect_success setup ' git rev-parse --verify HEAD >expect_second ' -test_expect_success 'log --grep' ' - git log --grep=initial --format=%H >actual && - test_cmp expect_initial actual -' - -test_expect_success 'log --grep --regexp-ignore-case' ' - git log --regexp-ignore-case --grep=InItial --format=%H >actual && - test_cmp expect_initial actual -' - -test_expect_success 'log --grep -i' ' - git log -i --grep=InItial --format=%H >actual && - test_cmp expect_initial actual -' +test_log expect_initial --grep initial +test_log expect_nomatch --grep InItial +test_log_icase expect_initial --grep InItial +test_log_icase expect_nomatch --grep initail test_expect_success 'log --author --regexp-ignore-case' ' git log --regexp-ignore-case --author=person --format=%H >actual && From 31a8189ad1a0ab6e933cdafb560c513632028d59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 22 Mar 2014 18:15:55 +0100 Subject: [PATCH 05/10] t4209: use helper functions to test --author Also add tests for case sensitive and non-matching cases. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- t/t4209-log-pickaxe.sh | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/t/t4209-log-pickaxe.sh b/t/t4209-log-pickaxe.sh index eaa14a057c..138118add1 100755 --- a/t/t4209-log-pickaxe.sh +++ b/t/t4209-log-pickaxe.sh @@ -60,15 +60,10 @@ test_log expect_nomatch --grep InItial test_log_icase expect_initial --grep InItial test_log_icase expect_nomatch --grep initail -test_expect_success 'log --author --regexp-ignore-case' ' - git log --regexp-ignore-case --author=person --format=%H >actual && - test_cmp expect_second actual -' - -test_expect_success 'log --author -i' ' - git log -i --author=person --format=%H >actual && - test_cmp expect_second actual -' +test_log expect_second --author Person +test_log expect_nomatch --author person +test_log_icase expect_second --author person +test_log_icase expect_nomatch --author spreon test_log expect_nomatch -G picked test_log expect_second -G Picked From 218c45a45c4e1c3b09a1b6b2bc6e6ed457e38a9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 22 Mar 2014 18:15:56 +0100 Subject: [PATCH 06/10] pickaxe: honor -i when used with -S and --pickaxe-regex accccde4 (pickaxe: allow -i to search in patch case-insensitively) allowed case-insenitive matching for -G and -S, but for the latter only if fixed string matching is used. Allow it for -S and regular expression matching as well to make the support complete. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- diffcore-pickaxe.c | 5 ++++- t/t4209-log-pickaxe.sh | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/diffcore-pickaxe.c b/diffcore-pickaxe.c index 401eb72c61..cb758515ec 100644 --- a/diffcore-pickaxe.c +++ b/diffcore-pickaxe.c @@ -237,7 +237,10 @@ static void diffcore_pickaxe_count(struct diff_options *o) if (opts & DIFF_PICKAXE_REGEX) { int err; - err = regcomp(®ex, needle, REG_EXTENDED | REG_NEWLINE); + int cflags = REG_EXTENDED | REG_NEWLINE; + if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE)) + cflags |= REG_ICASE; + err = regcomp(®ex, needle, cflags); if (err) { /* The POSIX.2 people are surely sick */ char errbuf[1024]; diff --git a/t/t4209-log-pickaxe.sh b/t/t4209-log-pickaxe.sh index 138118add1..844df760f7 100755 --- a/t/t4209-log-pickaxe.sh +++ b/t/t4209-log-pickaxe.sh @@ -88,6 +88,11 @@ test_log expect_second -S Picked test_log_icase expect_second -S picked test_log_icase expect_nomatch -S pickle +test_log expect_nomatch -S p.cked --pickaxe-regex +test_log expect_second -S P.cked --pickaxe-regex +test_log_icase expect_second -S p.cked --pickaxe-regex +test_log_icase expect_nomatch -S p.ckle --pickaxe-regex + test_expect_success 'log -S --textconv (missing textconv tool)' ' echo "* diff=test" >.gitattributes && test_must_fail git -c diff.test.textconv=missing log -Sfoo && From 63b52afaa88bf89c781fe11c6803ff1dcc47d424 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 22 Mar 2014 18:15:57 +0100 Subject: [PATCH 07/10] pickaxe: merge diffcore_pickaxe_grep() and diffcore_pickaxe_count() into diffcore_pickaxe() diffcore_pickaxe_count() initializes the regular expression or kwset for the search term, calls pickaxe() with the callback has_changes() and cleans up afterwards. diffcore_pickaxe_grep() does the same, only it doesn't support kwset and uses the callback diff_grep() instead. Merge the two functions to form the new diffcore_pickaxe() and thus get rid of the duplicate regex setup and cleanup code. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- diffcore-pickaxe.c | 44 +++++++------------------------------------- 1 file changed, 7 insertions(+), 37 deletions(-) diff --git a/diffcore-pickaxe.c b/diffcore-pickaxe.c index cb758515ec..cfc4262e4d 100644 --- a/diffcore-pickaxe.c +++ b/diffcore-pickaxe.c @@ -108,29 +108,6 @@ static int diff_grep(mmfile_t *one, mmfile_t *two, return ecbdata.hit; } -static void diffcore_pickaxe_grep(struct diff_options *o) -{ - int err; - regex_t regex; - int cflags = REG_EXTENDED | REG_NEWLINE; - - if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE)) - cflags |= REG_ICASE; - - err = regcomp(®ex, o->pickaxe, cflags); - if (err) { - char errbuf[1024]; - regerror(err, ®ex, errbuf, 1024); - regfree(®ex); - die("invalid regex: %s", errbuf); - } - - pickaxe(&diff_queued_diff, o, ®ex, NULL, diff_grep); - - regfree(®ex); - return; -} - static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws) { unsigned int cnt; @@ -227,7 +204,7 @@ static int pickaxe_match(struct diff_filepair *p, struct diff_options *o, return ret; } -static void diffcore_pickaxe_count(struct diff_options *o) +void diffcore_pickaxe(struct diff_options *o) { const char *needle = o->pickaxe; int opts = o->pickaxe_opts; @@ -235,7 +212,7 @@ static void diffcore_pickaxe_count(struct diff_options *o) regex_t regex, *regexp = NULL; kwset_t kws = NULL; - if (opts & DIFF_PICKAXE_REGEX) { + if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) { int err; int cflags = REG_EXTENDED | REG_NEWLINE; if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE)) @@ -256,20 +233,13 @@ static void diffcore_pickaxe_count(struct diff_options *o) kwsprep(kws); } - pickaxe(&diff_queued_diff, o, regexp, kws, has_changes); + /* Might want to warn when both S and G are on; I don't care... */ + pickaxe(&diff_queued_diff, o, regexp, kws, + (opts & DIFF_PICKAXE_KIND_G) ? diff_grep : has_changes); - if (opts & DIFF_PICKAXE_REGEX) - regfree(®ex); + if (regexp) + regfree(regexp); else kwsfree(kws); return; } - -void diffcore_pickaxe(struct diff_options *o) -{ - /* Might want to warn when both S and G are on; I don't care... */ - if (o->pickaxe_opts & DIFF_PICKAXE_KIND_G) - diffcore_pickaxe_grep(o); - else - diffcore_pickaxe_count(o); -} From 3753bd1f69d3b17e0369390fb8960ae3b7855b70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 22 Mar 2014 18:15:58 +0100 Subject: [PATCH 08/10] pickaxe: move pickaxe() after pickaxe_match() pickaxe() calls pickaxe_match(); moving the definition of the former after the latter allows us to do without an explicit function declaration. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- diffcore-pickaxe.c | 79 ++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 41 deletions(-) diff --git a/diffcore-pickaxe.c b/diffcore-pickaxe.c index cfc4262e4d..827a7d7e76 100644 --- a/diffcore-pickaxe.c +++ b/diffcore-pickaxe.c @@ -12,47 +12,6 @@ typedef int (*pickaxe_fn)(mmfile_t *one, mmfile_t *two, struct diff_options *o, regex_t *regexp, kwset_t kws); -static int pickaxe_match(struct diff_filepair *p, struct diff_options *o, - regex_t *regexp, kwset_t kws, pickaxe_fn fn); - -static void pickaxe(struct diff_queue_struct *q, struct diff_options *o, - regex_t *regexp, kwset_t kws, pickaxe_fn fn) -{ - int i; - struct diff_queue_struct outq; - - DIFF_QUEUE_CLEAR(&outq); - - if (o->pickaxe_opts & DIFF_PICKAXE_ALL) { - /* Showing the whole changeset if needle exists */ - for (i = 0; i < q->nr; i++) { - struct diff_filepair *p = q->queue[i]; - if (pickaxe_match(p, o, regexp, kws, fn)) - return; /* do not munge the queue */ - } - - /* - * Otherwise we will clear the whole queue by copying - * the empty outq at the end of this function, but - * first clear the current entries in the queue. - */ - for (i = 0; i < q->nr; i++) - diff_free_filepair(q->queue[i]); - } else { - /* Showing only the filepairs that has the needle */ - for (i = 0; i < q->nr; i++) { - struct diff_filepair *p = q->queue[i]; - if (pickaxe_match(p, o, regexp, kws, fn)) - diff_q(&outq, p); - else - diff_free_filepair(p); - } - } - - free(q->queue); - *q = outq; -} - struct diffgrep_cb { regex_t *regexp; int hit; @@ -204,6 +163,44 @@ static int pickaxe_match(struct diff_filepair *p, struct diff_options *o, return ret; } +static void pickaxe(struct diff_queue_struct *q, struct diff_options *o, + regex_t *regexp, kwset_t kws, pickaxe_fn fn) +{ + int i; + struct diff_queue_struct outq; + + DIFF_QUEUE_CLEAR(&outq); + + if (o->pickaxe_opts & DIFF_PICKAXE_ALL) { + /* Showing the whole changeset if needle exists */ + for (i = 0; i < q->nr; i++) { + struct diff_filepair *p = q->queue[i]; + if (pickaxe_match(p, o, regexp, kws, fn)) + return; /* do not munge the queue */ + } + + /* + * Otherwise we will clear the whole queue by copying + * the empty outq at the end of this function, but + * first clear the current entries in the queue. + */ + for (i = 0; i < q->nr; i++) + diff_free_filepair(q->queue[i]); + } else { + /* Showing only the filepairs that has the needle */ + for (i = 0; i < q->nr; i++) { + struct diff_filepair *p = q->queue[i]; + if (pickaxe_match(p, o, regexp, kws, fn)) + diff_q(&outq, p); + else + diff_free_filepair(p); + } + } + + free(q->queue); + *q = outq; +} + void diffcore_pickaxe(struct diff_options *o) { const char *needle = o->pickaxe; From 542b2aa2c9afba71febb248edb3083ff9cacf065 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 22 Mar 2014 18:15:59 +0100 Subject: [PATCH 09/10] pickaxe: call strlen only when necessary in diffcore_pickaxe_count() We need to determine the search term's length only when fixed-string matching is used; regular expression compilation takes a NUL-terminated string directly. Only call strlen() in the former case. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- diffcore-pickaxe.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/diffcore-pickaxe.c b/diffcore-pickaxe.c index 827a7d7e76..70753d058a 100644 --- a/diffcore-pickaxe.c +++ b/diffcore-pickaxe.c @@ -205,7 +205,6 @@ void diffcore_pickaxe(struct diff_options *o) { const char *needle = o->pickaxe; int opts = o->pickaxe_opts; - unsigned long len = strlen(needle); regex_t regex, *regexp = NULL; kwset_t kws = NULL; @@ -226,7 +225,7 @@ void diffcore_pickaxe(struct diff_options *o) } else { kws = kwsalloc(DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE) ? tolower_trans_tbl : NULL); - kwsincr(kws, needle, len); + kwsincr(kws, needle, strlen(needle)); kwsprep(kws); } From e4aab50475c1d384e016c6ac6548635f1ddcd3fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 22 Mar 2014 18:16:00 +0100 Subject: [PATCH 10/10] pickaxe: simplify kwset loop in contains() Inlining the variable "found" actually makes the code shorter and easier to read. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- diffcore-pickaxe.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/diffcore-pickaxe.c b/diffcore-pickaxe.c index 70753d058a..185f86b284 100644 --- a/diffcore-pickaxe.c +++ b/diffcore-pickaxe.c @@ -94,13 +94,10 @@ static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws) while (sz) { struct kwsmatch kwsm; size_t offset = kwsexec(kws, data, sz, &kwsm); - const char *found; if (offset == -1) break; - else - found = data + offset; - sz -= found - data + kwsm.size[0]; - data = found + kwsm.size[0]; + sz -= offset + kwsm.size[0]; + data += offset + kwsm.size[0]; cnt++; } }