git/log: only count the messages we show when limiting counts

when logging with `git/log -c count', we would count all messages
that we inspected, rather than just the ones that got printed.
This isn't what we should have done.

While we're in here, refactor show() to only show the message,
rather than filtering and then showing.
This commit is contained in:
Ori Bernstein 2022-07-19 03:56:00 +00:00
parent d1acc58316
commit dd8a2d3964

View file

@ -64,7 +64,7 @@ lookup(Pfilt *pf, Object *o)
} }
int int
filtermatch1(Pfilt *pf, Object *t, Object *pt) matchesfilter1(Pfilt *pf, Object *t, Object *pt)
{ {
Object *a, *b; Object *a, *b;
Hash ha, hb; Hash ha, hb;
@ -88,7 +88,7 @@ filtermatch1(Pfilt *pf, Object *t, Object *pt)
sysfatal("read %H: %r", ha); sysfatal("read %H: %r", ha);
if((b = readobject(hb)) == nil) if((b = readobject(hb)) == nil)
sysfatal("read %H: %r", hb); sysfatal("read %H: %r", hb);
r = filtermatch1(&pf->sub[i], a, b); r = matchesfilter1(&pf->sub[i], a, b);
unref(a); unref(a);
unref(b); unref(b);
if(r) if(r)
@ -98,11 +98,12 @@ filtermatch1(Pfilt *pf, Object *t, Object *pt)
} }
int int
filtermatch(Object *o) matchesfilter(Object *o)
{ {
Object *t, *p, *pt; Object *t, *p, *pt;
int i, r; int i, r;
assert(o->type == GCommit);
if(pathfilt == nil) if(pathfilt == nil)
return 1; return 1;
if((t = readobject(o->commit->tree)) == nil) if((t = readobject(o->commit->tree)) == nil)
@ -112,7 +113,7 @@ filtermatch(Object *o)
sysfatal("read %H: %r", o->commit->parent[i]); sysfatal("read %H: %r", o->commit->parent[i]);
if((pt = readobject(p->commit->tree)) == nil) if((pt = readobject(p->commit->tree)) == nil)
sysfatal("read %H: %r", o->commit->tree); sysfatal("read %H: %r", o->commit->tree);
r = filtermatch1(pathfilt, t, pt); r = matchesfilter1(pathfilt, t, pt);
unref(p); unref(p);
unref(pt); unref(pt);
if(r) if(r)
@ -131,16 +132,13 @@ nextline(char *p, char *e)
return p; return p;
} }
static void static int
show(Object *o) show(Object *o)
{ {
Tm tm; Tm tm;
char *p, *q, *e; char *p, *q, *e;
assert(o->type == GCommit); assert(o->type == GCommit);
if(!filtermatch(o))
return;
if(shortlog){ if(shortlog){
p = o->commit->msg; p = o->commit->msg;
e = p + o->commit->nmsg; e = p + o->commit->nmsg;
@ -170,6 +168,7 @@ show(Object *o)
Bprint(out, "\n"); Bprint(out, "\n");
} }
Bflush(out); Bflush(out);
return 1;
} }
static void static void
@ -181,10 +180,14 @@ showquery(char *q)
if((n = resolverefs(&h, q)) == -1) if((n = resolverefs(&h, q)) == -1)
sysfatal("resolve: %r"); sysfatal("resolve: %r");
for(i = 0; i < n && (msgcount == -1 || msgcount-- > 0); i++){ for(i = 0; i < n && (msgcount == -1 || msgcount > 0); i++){
if((o = readobject(h[i])) == nil) if((o = readobject(h[i])) == nil)
sysfatal("read %H: %r", h[i]); sysfatal("read %H: %r", h[i]);
show(o); if(matchesfilter(o)){
show(o);
if(msgcount != -1)
msgcount--;
}
unref(o); unref(o);
} }
exits(nil); exits(nil);
@ -207,8 +210,12 @@ showcommits(char *c)
qinit(&objq); qinit(&objq);
osinit(&done); osinit(&done);
qput(&objq, o, 0); qput(&objq, o, 0);
while(qpop(&objq, &e) && (msgcount == -1 || msgcount-- > 0)){ while(qpop(&objq, &e) && (msgcount == -1 || msgcount > 0)){
show(e.o); if(matchesfilter(e.o)){
show(e.o);
if(msgcount != -1)
msgcount--;
}
for(i = 0; i < e.o->commit->nparent; i++){ for(i = 0; i < e.o->commit->nparent; i++){
if(oshas(&done, e.o->commit->parent[i])) if(oshas(&done, e.o->commit->parent[i]))
continue; continue;