grep: simplify grep_oid and grep_file

In the NO_PTHREADS or !num_threads case, this doesn't change anything. In the threaded case, note that grep_source_init duplicates its third argument, so there is no need to keep [path]buf.buf alive across the call of add_work(). Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rasmus Villemoes committed Feb 23, 2018 at 15:47 UTC 38ef24dccfb696393d2e8e701bf2f81c9e6275e2
1 file changed +2 -4
builtin/grep.c
+2 -4
@@ -326,6 +326,7 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
326 }
327
328 grep_source_init(&gs, GREP_SOURCE_OID, pathbuf.buf, path, oid);
329 + strbuf_release(&pathbuf);
330
331 #ifndef NO_PTHREADS
332 if (num_threads) {
@@ -334,14 +335,12 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
335 * its fields, so do not call grep_source_clear()
336 */
337 add_work(opt, &gs);
337 - strbuf_release(&pathbuf);
338 return 0;
339 } else
340 #endif
341 {
342 int hit;
343
344 - strbuf_release(&pathbuf);
344 hit = grep_source(opt, &gs);
345
346 grep_source_clear(&gs);
@@ -360,6 +359,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
359 strbuf_addstr(&buf, filename);
360
361 grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename, filename);
362 + strbuf_release(&buf);
363
364 #ifndef NO_PTHREADS
365 if (num_threads) {
@@ -368,14 +368,12 @@ static int grep_file(struct grep_opt *opt, const char *filename)
368 * its fields, so do not call grep_source_clear()
369 */
370 add_work(opt, &gs);
371 - strbuf_release(&buf);
371 return 0;
372 } else
373 #endif
374 {
375 int hit;
376
378 - strbuf_release(&buf);
377 hit = grep_source(opt, &gs);
378
379 grep_source_clear(&gs);