grep: refactor the concept of "grep source" into an object
The main interface to the low-level grep code is grep_buffer, which takes a pointer to a buffer and a size. This is convenient and flexible (we use it to grep commit bodies, files on disk, and blobs by sha1), but it makes it hard to pass extra information about what we are grepping (either for correctness, like overriding binary auto-detection, or for optimizations, like lazily loading blob contents). Instead, let's encapsulate the idea of a "grep source", including the buffer, its size, and where the data is coming from. This is similar to the diff_filespec structure used by the diff code (unsurprising, since future patches will implement some of the same optimizations found there). The diffstat is slightly scarier than the actual patch content. Most of the modified lines are simply replacing access to raw variables with their counterparts that are now in a "struct grep_source". Most of the added lines were taken from builtin/grep.c, which partially abstracted the idea of grep sources (for file vs sha1 sources). Instead of dropping the now-redundant code, this patch leaves builtin/grep.c using the traditional grep_buffer interface (which now wraps the grep_source interface). That makes it easy to test that there is no change of behavior (yet). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
b3aeb285d0
commit
e1327023ea
22
grep.h
22
grep.h
@ -129,6 +129,28 @@ extern void compile_grep_patterns(struct grep_opt *opt);
|
||||
extern void free_grep_patterns(struct grep_opt *opt);
|
||||
extern int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size);
|
||||
|
||||
struct grep_source {
|
||||
char *name;
|
||||
|
||||
enum grep_source_type {
|
||||
GREP_SOURCE_SHA1,
|
||||
GREP_SOURCE_FILE,
|
||||
GREP_SOURCE_BUF,
|
||||
} type;
|
||||
void *identifier;
|
||||
|
||||
char *buf;
|
||||
unsigned long size;
|
||||
};
|
||||
|
||||
void grep_source_init(struct grep_source *gs, enum grep_source_type type,
|
||||
const char *name, const void *identifier);
|
||||
int grep_source_load(struct grep_source *gs);
|
||||
void grep_source_clear_data(struct grep_source *gs);
|
||||
void grep_source_clear(struct grep_source *gs);
|
||||
|
||||
int grep_source(struct grep_opt *opt, struct grep_source *gs);
|
||||
|
||||
extern struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
|
||||
extern int grep_threads_ok(const struct grep_opt *opt);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user