@matheusbsbs / matheus-bagliano / commits / e5e062b6dc
Fork 0

grep: use writable strbuf from caller for grep_tree()

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Dec 17, 2010 at 19:44 UTC e5e062b6dcdbbc338a0501b97a35e2e5efa56075
1 file changed +24 -27
builtin/grep.c
+24 -27
@@ -619,43 +619,29 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
619 }
620
621 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)
623 {
625 - int len;
624 int hit = 0;
625 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;
627
628 while (tree_entry(tree, &entry)) {
629 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);
632
633 if (S_ISDIR(entry.mode))
634 /* Match "abc/" against pathspec to
635 * decide if we want to descend into "abc"
636 * directory.
637 */
652 - strbuf_addch(&pathbuf, '/');
638 + strbuf_addch(base, '/');
639
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))
641 ;
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 + }
645 else if (S_ISDIR(entry.mode)) {
646 enum object_type type;
647 struct tree_desc sub;
@@ -667,13 +653,14 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
653 die("unable to read tree (%s)",
654 sha1_to_hex(entry.sha1));
655 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);
657 free(data);
658 }
659 + strbuf_setlen(base, old_baselen);
660 +
661 if (hit && opt->status_only)
662 break;
663 }
676 - strbuf_release(&pathbuf);
664 return hit;
665 }
666
@@ -686,13 +673,23 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
673 struct tree_desc tree;
674 void *data;
675 unsigned long size;
689 - int hit;
676 + struct strbuf base;
677 + int hit, len;
678 +
679 data = read_object_with_reference(obj->sha1, tree_type,
680 &size, NULL);
681 if (!data)
682 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 + }
690 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);
693 free(data);
694 return hit;
695 }