13
13
#include "parse-options.h"
14
14
#include "userdiff.h"
15
15
#include "grep.h"
16
+ #include "quote.h"
16
17
17
18
#ifndef NO_EXTERNAL_GREP
18
19
#ifdef __unix__
@@ -125,35 +126,22 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char
125
126
unsigned long size ;
126
127
char * data ;
127
128
enum object_type type ;
128
- char * to_free = NULL ;
129
129
int hit ;
130
+ struct strbuf pathbuf = STRBUF_INIT ;
130
131
131
132
data = read_sha1_file (sha1 , & type , & size );
132
133
if (!data ) {
133
134
error ("'%s': unable to read %s" , name , sha1_to_hex (sha1 ));
134
135
return 0 ;
135
136
}
136
137
if (opt -> relative && opt -> prefix_length ) {
137
- static char name_buf [PATH_MAX ];
138
- char * cp ;
139
- int name_len = strlen (name ) - opt -> prefix_length + 1 ;
140
-
141
- if (!tree_name_len )
142
- name += opt -> prefix_length ;
143
- else {
144
- if (ARRAY_SIZE (name_buf ) <= name_len )
145
- cp = to_free = xmalloc (name_len );
146
- else
147
- cp = name_buf ;
148
- memcpy (cp , name , tree_name_len );
149
- strcpy (cp + tree_name_len ,
150
- name + tree_name_len + opt -> prefix_length );
151
- name = cp ;
152
- }
138
+ quote_path_relative (name + tree_name_len , -1 , & pathbuf , opt -> prefix );
139
+ strbuf_insert (& pathbuf , 0 , name , tree_name_len );
140
+ name = pathbuf .buf ;
153
141
}
154
142
hit = grep_buffer (opt , name , data , size );
143
+ strbuf_release (& pathbuf );
155
144
free (data );
156
- free (to_free );
157
145
return hit ;
158
146
}
159
147
@@ -163,6 +151,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
163
151
int i ;
164
152
char * data ;
165
153
size_t sz ;
154
+ struct strbuf buf = STRBUF_INIT ;
166
155
167
156
if (lstat (filename , & st ) < 0 ) {
168
157
err_ret :
@@ -187,8 +176,9 @@ static int grep_file(struct grep_opt *opt, const char *filename)
187
176
}
188
177
close (i );
189
178
if (opt -> relative && opt -> prefix_length )
190
- filename += opt -> prefix_length ;
179
+ filename = quote_path_relative ( filename , -1 , & buf , opt -> prefix ) ;
191
180
i = grep_buffer (opt , filename , data , sz );
181
+ strbuf_release (& buf );
192
182
free (data );
193
183
return i ;
194
184
}
@@ -471,6 +461,7 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached,
471
461
hit = external_grep (opt , paths , cached );
472
462
if (hit >= 0 )
473
463
return hit ;
464
+ hit = 0 ;
474
465
}
475
466
#endif
476
467
@@ -763,6 +754,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
763
754
};
764
755
765
756
memset (& opt , 0 , sizeof (opt ));
757
+ opt .prefix = prefix ;
766
758
opt .prefix_length = (prefix && * prefix ) ? strlen (prefix ) : 0 ;
767
759
opt .relative = 1 ;
768
760
opt .pathname = 1 ;
@@ -832,15 +824,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
832
824
verify_filename (prefix , argv [j ]);
833
825
}
834
826
835
- if (i < argc ) {
827
+ if (i < argc )
836
828
paths = get_pathspec (prefix , argv + i );
837
- if (opt .prefix_length && opt .relative ) {
838
- /* Make sure we do not get outside of paths */
839
- for (i = 0 ; paths [i ]; i ++ )
840
- if (strncmp (prefix , paths [i ], opt .prefix_length ))
841
- die ("git grep: cannot generate relative filenames containing '..'" );
842
- }
843
- }
844
829
else if (prefix ) {
845
830
paths = xcalloc (2 , sizeof (const char * ));
846
831
paths [0 ] = prefix ;
0 commit comments