summaryrefslogtreecommitdiff
path: root/grep.c
diff options
context:
space:
mode:
authorRené Scharfe <l.s.r@web.de>2017-11-18 18:07:13 (GMT)
committerJunio C Hamano <gitster@pobox.com>2017-11-21 00:36:06 (GMT)
commit6653a01bf2dd1e2aabbcf1f83269bd75737acc94 (patch)
tree9ac23d46c137a73337ae5fd9c441899d426af95c /grep.c
parent76e650d7d95662749fa5cbcc9f4faefb510334b0 (diff)
downloadgit-6653a01bf2dd1e2aabbcf1f83269bd75737acc94.zip
git-6653a01bf2dd1e2aabbcf1f83269bd75737acc94.tar.gz
git-6653a01bf2dd1e2aabbcf1f83269bd75737acc94.tar.bz2
grep: update boundary variable for pre-context
Function context can be bigger than -A/-B/-C context. To find the beginning of the combined context we search backwards. Currently we check at each loop iteration what we're looking for and determine the effective upper boundary based on that. Simplify this a bit by setting the variable "from" to the lowest unshown line number up front if we're looking for a function line and set it back to the required -B/-C context line number when we find one. This prepares the ground for the next patch; no functional change intended. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'grep.c')
-rw-r--r--grep.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/grep.c b/grep.c
index d0b9b6c..2c55d10 100644
--- a/grep.c
+++ b/grep.c
@@ -1479,20 +1479,21 @@ static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
char *bol, char *end, unsigned lno)
{
- unsigned cur = lno, from = 1, funcname_lno = 0;
+ unsigned cur = lno, from = 1, funcname_lno = 0, orig_from;
int funcname_needed = !!opt->funcname;
- if (opt->funcbody && !match_funcname(opt, gs, bol, end))
- funcname_needed = 2;
-
if (opt->pre_context < lno)
from = lno - opt->pre_context;
if (from <= opt->last_shown)
from = opt->last_shown + 1;
+ orig_from = from;
+ if (opt->funcbody && !match_funcname(opt, gs, bol, end)) {
+ funcname_needed = 1;
+ from = opt->last_shown + 1;
+ }
/* Rewind. */
- while (bol > gs->buf &&
- cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) {
+ while (bol > gs->buf && cur > from) {
char *eol = --bol;
while (bol > gs->buf && bol[-1] != '\n')
@@ -1501,6 +1502,7 @@ static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
funcname_lno = cur;
funcname_needed = 0;
+ from = orig_from;
}
}