11
11
#include "tree-walk.h"
12
12
#include "builtin.h"
13
13
#include "grep.h"
14
+ #include "quote.h"
14
15
15
16
#ifndef NO_EXTERNAL_GREP
16
17
#ifdef __unix__
@@ -114,35 +115,22 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char
114
115
unsigned long size ;
115
116
char * data ;
116
117
enum object_type type ;
117
- char * to_free = NULL ;
118
118
int hit ;
119
+ struct strbuf pathbuf = STRBUF_INIT ;
119
120
120
121
data = read_sha1_file (sha1 , & type , & size );
121
122
if (!data ) {
122
123
error ("'%s': unable to read %s" , name , sha1_to_hex (sha1 ));
123
124
return 0 ;
124
125
}
125
126
if (opt -> relative && opt -> prefix_length ) {
126
- static char name_buf [PATH_MAX ];
127
- char * cp ;
128
- int name_len = strlen (name ) - opt -> prefix_length + 1 ;
129
-
130
- if (!tree_name_len )
131
- name += opt -> prefix_length ;
132
- else {
133
- if (ARRAY_SIZE (name_buf ) <= name_len )
134
- cp = to_free = xmalloc (name_len );
135
- else
136
- cp = name_buf ;
137
- memcpy (cp , name , tree_name_len );
138
- strcpy (cp + tree_name_len ,
139
- name + tree_name_len + opt -> prefix_length );
140
- name = cp ;
141
- }
127
+ quote_path_relative (name + tree_name_len , -1 , & pathbuf , opt -> prefix );
128
+ strbuf_insert (& pathbuf , 0 , name , tree_name_len );
129
+ name = pathbuf .buf ;
142
130
}
143
131
hit = grep_buffer (opt , name , data , size );
132
+ strbuf_release (& pathbuf );
144
133
free (data );
145
- free (to_free );
146
134
return hit ;
147
135
}
148
136
@@ -152,6 +140,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
152
140
int i ;
153
141
char * data ;
154
142
size_t sz ;
143
+ struct strbuf buf = STRBUF_INIT ;
155
144
156
145
if (lstat (filename , & st ) < 0 ) {
157
146
err_ret :
@@ -176,8 +165,9 @@ static int grep_file(struct grep_opt *opt, const char *filename)
176
165
}
177
166
close (i );
178
167
if (opt -> relative && opt -> prefix_length )
179
- filename += opt -> prefix_length ;
168
+ filename = quote_path_relative ( filename , -1 , & buf , opt -> prefix ) ;
180
169
i = grep_buffer (opt , filename , data , sz );
170
+ strbuf_release (& buf );
181
171
free (data );
182
172
return i ;
183
173
}
@@ -582,6 +572,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
582
572
int i ;
583
573
584
574
memset (& opt , 0 , sizeof (opt ));
575
+ opt .prefix = prefix ;
585
576
opt .prefix_length = (prefix && * prefix ) ? strlen (prefix ) : 0 ;
586
577
opt .relative = 1 ;
587
578
opt .pathname = 1 ;
@@ -857,15 +848,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
857
848
verify_filename (prefix , argv [j ]);
858
849
}
859
850
860
- if (i < argc ) {
851
+ if (i < argc )
861
852
paths = get_pathspec (prefix , argv + i );
862
- if (opt .prefix_length && opt .relative ) {
863
- /* Make sure we do not get outside of paths */
864
- for (i = 0 ; paths [i ]; i ++ )
865
- if (strncmp (prefix , paths [i ], opt .prefix_length ))
866
- die ("git grep: cannot generate relative filenames containing '..'" );
867
- }
868
- }
869
853
else if (prefix ) {
870
854
paths = xcalloc (2 , sizeof (const char * ));
871
855
paths [0 ] = prefix ;
0 commit comments