Skip to content

Commit e5e062b

Browse files
pcloudsgitster
authored andcommitted
grep: use writable strbuf from caller for grep_tree()
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2ed2437 commit e5e062b

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

builtin/grep.c

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -619,43 +619,29 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
619619
}
620620

621621
static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
622-
struct tree_desc *tree,
623-
const char *tree_name, const char *base)
622+
struct tree_desc *tree, struct strbuf *base, int tn_len)
624623
{
625-
int len;
626624
int hit = 0;
627625
struct name_entry entry;
628-
char *down;
629-
int tn_len = strlen(tree_name);
630-
struct strbuf pathbuf;
631-
632-
strbuf_init(&pathbuf, PATH_MAX + tn_len);
633-
634-
if (tn_len) {
635-
strbuf_add(&pathbuf, tree_name, tn_len);
636-
strbuf_addch(&pathbuf, ':');
637-
tn_len = pathbuf.len;
638-
}
639-
strbuf_addstr(&pathbuf, base);
640-
len = pathbuf.len;
626+
int old_baselen = base->len;
641627

642628
while (tree_entry(tree, &entry)) {
643629
int te_len = tree_entry_len(entry.path, entry.sha1);
644-
pathbuf.len = len;
645-
strbuf_add(&pathbuf, entry.path, te_len);
630+
631+
strbuf_add(base, entry.path, te_len);
646632

647633
if (S_ISDIR(entry.mode))
648634
/* Match "abc/" against pathspec to
649635
* decide if we want to descend into "abc"
650636
* directory.
651637
*/
652-
strbuf_addch(&pathbuf, '/');
638+
strbuf_addch(base, '/');
653639

654-
down = pathbuf.buf + tn_len;
655-
if (!pathspec_matches(pathspec->raw, down, opt->max_depth))
640+
if (!pathspec_matches(pathspec->raw, base->buf + tn_len, opt->max_depth))
656641
;
657-
else if (S_ISREG(entry.mode))
658-
hit |= grep_sha1(opt, entry.sha1, pathbuf.buf, tn_len);
642+
else if (S_ISREG(entry.mode)) {
643+
hit |= grep_sha1(opt, entry.sha1, base->buf, tn_len);
644+
}
659645
else if (S_ISDIR(entry.mode)) {
660646
enum object_type type;
661647
struct tree_desc sub;
@@ -667,13 +653,14 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
667653
die("unable to read tree (%s)",
668654
sha1_to_hex(entry.sha1));
669655
init_tree_desc(&sub, data, size);
670-
hit |= grep_tree(opt, pathspec, &sub, tree_name, down);
656+
hit |= grep_tree(opt, pathspec, &sub, base, tn_len);
671657
free(data);
672658
}
659+
strbuf_setlen(base, old_baselen);
660+
673661
if (hit && opt->status_only)
674662
break;
675663
}
676-
strbuf_release(&pathbuf);
677664
return hit;
678665
}
679666

@@ -686,13 +673,23 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
686673
struct tree_desc tree;
687674
void *data;
688675
unsigned long size;
689-
int hit;
676+
struct strbuf base;
677+
int hit, len;
678+
690679
data = read_object_with_reference(obj->sha1, tree_type,
691680
&size, NULL);
692681
if (!data)
693682
die("unable to read tree (%s)", sha1_to_hex(obj->sha1));
683+
684+
len = name ? strlen(name) : 0;
685+
strbuf_init(&base, PATH_MAX + len + 1);
686+
if (len) {
687+
strbuf_add(&base, name, len);
688+
strbuf_addch(&base, ':');
689+
}
694690
init_tree_desc(&tree, data, size);
695-
hit = grep_tree(opt, pathspec, &tree, name, "");
691+
hit = grep_tree(opt, pathspec, &tree, &base, base.len);
692+
strbuf_release(&base);
696693
free(data);
697694
return hit;
698695
}

0 commit comments

Comments
 (0)