convert git-grep to use grep_source interface

The grep_source interface (as opposed to grep_buffer) will
eventually gives us a richer interface for telling the
low-level grep code about our buffers. Eventually this will
lead to things like better binary-file handling. For now, it
lets us drop a lot of now-redundant code.

The conversion is mostly straight-forward. One thing to note
is that the memory ownership rules for "struct grep_source"
are different than the "struct work_item" found here (the
former will copy things like the filename, rather than
taking ownership). Therefore you will also see some slight
tweaking of when filename buffers are released.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2012-02-02 03:19:37 -05:00 коммит произвёл Junio C Hamano
Родитель e1327023ea
Коммит 8f24a6323e
1 изменённых файлов: 23 добавлений и 119 удалений

Просмотреть файл

@ -29,25 +29,12 @@ static int use_threads = 1;
#define THREADS 8 #define THREADS 8
static pthread_t threads[THREADS]; static pthread_t threads[THREADS];
static void *load_sha1(const unsigned char *sha1, unsigned long *size,
const char *name);
static void *load_file(const char *filename, size_t *sz);
enum work_type {WORK_SHA1, WORK_FILE};
/* We use one producer thread and THREADS consumer /* We use one producer thread and THREADS consumer
* threads. The producer adds struct work_items to 'todo' and the * threads. The producer adds struct work_items to 'todo' and the
* consumers pick work items from the same array. * consumers pick work items from the same array.
*/ */
struct work_item { struct work_item {
enum work_type type; struct grep_source source;
char *name;
/* if type == WORK_SHA1, then 'identifier' is a SHA1,
* otherwise type == WORK_FILE, and 'identifier' is a NUL
* terminated filename.
*/
void *identifier;
char done; char done;
struct strbuf out; struct strbuf out;
}; };
@ -98,7 +85,8 @@ static pthread_cond_t cond_result;
static int skip_first_line; static int skip_first_line;
static void add_work(enum work_type type, char *name, void *id) static void add_work(enum grep_source_type type, const char *name,
const void *id)
{ {
grep_lock(); grep_lock();
@ -106,9 +94,7 @@ static void add_work(enum work_type type, char *name, void *id)
pthread_cond_wait(&cond_write, &grep_mutex); pthread_cond_wait(&cond_write, &grep_mutex);
} }
todo[todo_end].type = type; grep_source_init(&todo[todo_end].source, type, name, id);
todo[todo_end].name = name;
todo[todo_end].identifier = id;
todo[todo_end].done = 0; todo[todo_end].done = 0;
strbuf_reset(&todo[todo_end].out); strbuf_reset(&todo[todo_end].out);
todo_end = (todo_end + 1) % ARRAY_SIZE(todo); todo_end = (todo_end + 1) % ARRAY_SIZE(todo);
@ -136,21 +122,6 @@ static struct work_item *get_work(void)
return ret; return ret;
} }
static void grep_sha1_async(struct grep_opt *opt, char *name,
const unsigned char *sha1)
{
unsigned char *s;
s = xmalloc(20);
memcpy(s, sha1, 20);
add_work(WORK_SHA1, name, s);
}
static void grep_file_async(struct grep_opt *opt, char *name,
const char *filename)
{
add_work(WORK_FILE, name, xstrdup(filename));
}
static void work_done(struct work_item *w) static void work_done(struct work_item *w)
{ {
int old_done; int old_done;
@ -177,8 +148,7 @@ static void work_done(struct work_item *w)
write_or_die(1, p, len); write_or_die(1, p, len);
} }
free(w->name); grep_source_clear(&w->source);
free(w->identifier);
} }
if (old_done != todo_done) if (old_done != todo_done)
@ -201,25 +171,8 @@ static void *run(void *arg)
break; break;
opt->output_priv = w; opt->output_priv = w;
if (w->type == WORK_SHA1) { hit |= grep_source(opt, &w->source);
unsigned long sz; grep_source_clear_data(&w->source);
void* data = load_sha1(w->identifier, &sz, w->name);
if (data) {
hit |= grep_buffer(opt, w->name, data, sz);
free(data);
}
} else if (w->type == WORK_FILE) {
size_t sz;
void* data = load_file(w->identifier, &sz);
if (data) {
hit |= grep_buffer(opt, w->name, data, sz);
free(data);
}
} else {
assert(0);
}
work_done(w); work_done(w);
} }
free_grep_patterns(arg); free_grep_patterns(arg);
@ -365,23 +318,10 @@ static void *lock_and_read_sha1_file(const unsigned char *sha1, enum object_type
return data; return data;
} }
static void *load_sha1(const unsigned char *sha1, unsigned long *size,
const char *name)
{
enum object_type type;
void *data = lock_and_read_sha1_file(sha1, &type, size);
if (!data)
error(_("'%s': unable to read %s"), name, sha1_to_hex(sha1));
return data;
}
static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,
const char *filename, int tree_name_len) const char *filename, int tree_name_len)
{ {
struct strbuf pathbuf = STRBUF_INIT; struct strbuf pathbuf = STRBUF_INIT;
char *name;
if (opt->relative && opt->prefix_length) { if (opt->relative && opt->prefix_length) {
quote_path_relative(filename + tree_name_len, -1, &pathbuf, quote_path_relative(filename + tree_name_len, -1, &pathbuf,
@ -391,87 +331,51 @@ static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1,
strbuf_addstr(&pathbuf, filename); strbuf_addstr(&pathbuf, filename);
} }
name = strbuf_detach(&pathbuf, NULL);
#ifndef NO_PTHREADS #ifndef NO_PTHREADS
if (use_threads) { if (use_threads) {
grep_sha1_async(opt, name, sha1); add_work(GREP_SOURCE_SHA1, pathbuf.buf, sha1);
strbuf_release(&pathbuf);
return 0; return 0;
} else } else
#endif #endif
{ {
struct grep_source gs;
int hit; int hit;
unsigned long sz;
void *data = load_sha1(sha1, &sz, name);
if (!data)
hit = 0;
else
hit = grep_buffer(opt, name, data, sz);
free(data); grep_source_init(&gs, GREP_SOURCE_SHA1, pathbuf.buf, sha1);
free(name); strbuf_release(&pathbuf);
hit = grep_source(opt, &gs);
grep_source_clear(&gs);
return hit; return hit;
} }
} }
static void *load_file(const char *filename, size_t *sz)
{
struct stat st;
char *data;
int i;
if (lstat(filename, &st) < 0) {
err_ret:
if (errno != ENOENT)
error(_("'%s': %s"), filename, strerror(errno));
return NULL;
}
if (!S_ISREG(st.st_mode))
return NULL;
*sz = xsize_t(st.st_size);
i = open(filename, O_RDONLY);
if (i < 0)
goto err_ret;
data = xmalloc(*sz + 1);
if (st.st_size != read_in_full(i, data, *sz)) {
error(_("'%s': short read %s"), filename, strerror(errno));
close(i);
free(data);
return NULL;
}
close(i);
data[*sz] = 0;
return data;
}
static int grep_file(struct grep_opt *opt, const char *filename) static int grep_file(struct grep_opt *opt, const char *filename)
{ {
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
char *name;
if (opt->relative && opt->prefix_length) if (opt->relative && opt->prefix_length)
quote_path_relative(filename, -1, &buf, opt->prefix); quote_path_relative(filename, -1, &buf, opt->prefix);
else else
strbuf_addstr(&buf, filename); strbuf_addstr(&buf, filename);
name = strbuf_detach(&buf, NULL);
#ifndef NO_PTHREADS #ifndef NO_PTHREADS
if (use_threads) { if (use_threads) {
grep_file_async(opt, name, filename); add_work(GREP_SOURCE_FILE, buf.buf, filename);
strbuf_release(&buf);
return 0; return 0;
} else } else
#endif #endif
{ {
struct grep_source gs;
int hit; int hit;
size_t sz;
void *data = load_file(filename, &sz);
if (!data)
hit = 0;
else
hit = grep_buffer(opt, name, data, sz);
free(data); grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename);
free(name); strbuf_release(&buf);
hit = grep_source(opt, &gs);
grep_source_clear(&gs);
return hit; return hit;
} }
} }