@@ -619,43 +619,29 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
619
619
}
620
620
621
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 )
624
623
{
625
- int len ;
626
624
int hit = 0 ;
627
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 ;
641
627
642
628
while (tree_entry (tree , & entry )) {
643
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 );
646
632
647
633
if (S_ISDIR (entry .mode ))
648
634
/* Match "abc/" against pathspec to
649
635
* decide if we want to descend into "abc"
650
636
* directory.
651
637
*/
652
- strbuf_addch (& pathbuf , '/' );
638
+ strbuf_addch (base , '/' );
653
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 ))
656
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
+ }
659
645
else if (S_ISDIR (entry .mode )) {
660
646
enum object_type type ;
661
647
struct tree_desc sub ;
@@ -667,13 +653,14 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
667
653
die ("unable to read tree (%s)" ,
668
654
sha1_to_hex (entry .sha1 ));
669
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 );
671
657
free (data );
672
658
}
659
+ strbuf_setlen (base , old_baselen );
660
+
673
661
if (hit && opt -> status_only )
674
662
break ;
675
663
}
676
- strbuf_release (& pathbuf );
677
664
return hit ;
678
665
}
679
666
@@ -686,13 +673,23 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
686
673
struct tree_desc tree ;
687
674
void * data ;
688
675
unsigned long size ;
689
- int hit ;
676
+ struct strbuf base ;
677
+ int hit , len ;
678
+
690
679
data = read_object_with_reference (obj -> sha1 , tree_type ,
691
680
& size , NULL );
692
681
if (!data )
693
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
+ }
694
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 );
696
693
free (data );
697
694
return hit ;
698
695
}
0 commit comments