grep: convert to struct object_id

Convert the remaining parts of grep to use struct object_id.

Signed-off-by: Brandon Williams <bmwill@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Brandon Williams 2017-05-30 10:30:44 -07:00 committed by Junio C Hamano
parent 5ee8a954e0
commit 1c41c82bc4
4 changed files with 27 additions and 21 deletions

View File

@ -327,7 +327,7 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
#ifndef NO_PTHREADS #ifndef NO_PTHREADS
if (num_threads) { if (num_threads) {
add_work(opt, GREP_SOURCE_SHA1, pathbuf.buf, path, oid); add_work(opt, GREP_SOURCE_OID, pathbuf.buf, path, oid);
strbuf_release(&pathbuf); strbuf_release(&pathbuf);
return 0; return 0;
} else } else
@ -336,7 +336,7 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
struct grep_source gs; struct grep_source gs;
int hit; int hit;
grep_source_init(&gs, GREP_SOURCE_SHA1, pathbuf.buf, path, oid); grep_source_init(&gs, GREP_SOURCE_OID, pathbuf.buf, path, oid);
strbuf_release(&pathbuf); strbuf_release(&pathbuf);
hit = grep_source(opt, &gs); hit = grep_source(opt, &gs);
@ -570,7 +570,7 @@ static int grep_submodule_launch(struct grep_opt *opt,
* with the object's name: 'tree-name:filename'. In order to * with the object's name: 'tree-name:filename'. In order to
* provide uniformity of output we want to pass the name of the * provide uniformity of output we want to pass the name of the
* parent project's object name to the submodule so the submodule can * parent project's object name to the submodule so the submodule can
* prefix its output with the parent's name and not its own SHA1. * prefix its output with the parent's name and not its own OID.
*/ */
if (gs->identifier && end_of_base) if (gs->identifier && end_of_base)
argv_array_pushf(&cp.args, "--parent-basename=%.*s", argv_array_pushf(&cp.args, "--parent-basename=%.*s",
@ -583,12 +583,12 @@ static int grep_submodule_launch(struct grep_opt *opt,
* If there is a tree identifier for the submodule, add the * If there is a tree identifier for the submodule, add the
* rev after adding the submodule options but before the * rev after adding the submodule options but before the
* pathspecs. To do this we listen for the '--' and insert the * pathspecs. To do this we listen for the '--' and insert the
* sha1 before pushing the '--' onto the child process argv * oid before pushing the '--' onto the child process argv
* array. * array.
*/ */
if (gs->identifier && if (gs->identifier &&
!strcmp("--", submodule_options.argv[i])) { !strcmp("--", submodule_options.argv[i])) {
argv_array_push(&cp.args, sha1_to_hex(gs->identifier)); argv_array_push(&cp.args, oid_to_hex(gs->identifier));
} }
argv_array_push(&cp.args, submodule_options.argv[i]); argv_array_push(&cp.args, submodule_options.argv[i]);
@ -618,11 +618,11 @@ static int grep_submodule_launch(struct grep_opt *opt,
/* /*
* Prep grep structures for a submodule grep * Prep grep structures for a submodule grep
* sha1: the sha1 of the submodule or NULL if using the working tree * oid: the oid of the submodule or NULL if using the working tree
* filename: name of the submodule including tree name of parent * filename: name of the submodule including tree name of parent
* path: location of the submodule * path: location of the submodule
*/ */
static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1, static int grep_submodule(struct grep_opt *opt, const struct object_id *oid,
const char *filename, const char *path) const char *filename, const char *path)
{ {
if (!is_submodule_initialized(path)) if (!is_submodule_initialized(path))
@ -632,7 +632,7 @@ static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
* If searching history, check for the presense of the * If searching history, check for the presense of the
* submodule's gitdir before skipping the submodule. * submodule's gitdir before skipping the submodule.
*/ */
if (sha1) { if (oid) {
const struct submodule *sub = const struct submodule *sub =
submodule_from_path(null_sha1, path); submodule_from_path(null_sha1, path);
if (sub) if (sub)
@ -647,7 +647,7 @@ static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
#ifndef NO_PTHREADS #ifndef NO_PTHREADS
if (num_threads) { if (num_threads) {
add_work(opt, GREP_SOURCE_SUBMODULE, filename, path, sha1); add_work(opt, GREP_SOURCE_SUBMODULE, filename, path, oid);
return 0; return 0;
} else } else
#endif #endif
@ -656,7 +656,7 @@ static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
int hit; int hit;
grep_source_init(&gs, GREP_SOURCE_SUBMODULE, grep_source_init(&gs, GREP_SOURCE_SUBMODULE,
filename, path, sha1); filename, path, oid);
hit = grep_submodule_launch(opt, &gs); hit = grep_submodule_launch(opt, &gs);
grep_source_clear(&gs); grep_source_clear(&gs);
@ -775,7 +775,7 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
check_attr); check_attr);
free(data); free(data);
} else if (recurse_submodules && S_ISGITLINK(entry.mode)) { } else if (recurse_submodules && S_ISGITLINK(entry.mode)) {
hit |= grep_submodule(opt, entry.oid->hash, base->buf, hit |= grep_submodule(opt, entry.oid, base->buf,
base->buf + tn_len); base->buf + tn_len);
} }

View File

@ -1026,6 +1026,13 @@ static inline void oidcpy(struct object_id *dst, const struct object_id *src)
hashcpy(dst->hash, src->hash); hashcpy(dst->hash, src->hash);
} }
static inline struct object_id *oiddup(const struct object_id *src)
{
struct object_id *dst = xmalloc(sizeof(struct object_id));
oidcpy(dst, src);
return dst;
}
static inline void hashclr(unsigned char *hash) static inline void hashclr(unsigned char *hash)
{ {
memset(hash, 0, GIT_SHA1_RAWSZ); memset(hash, 0, GIT_SHA1_RAWSZ);

17
grep.c
View File

@ -1403,7 +1403,7 @@ static int fill_textconv_grep(struct userdiff_driver *driver,
*/ */
df = alloc_filespec(gs->path); df = alloc_filespec(gs->path);
switch (gs->type) { switch (gs->type) {
case GREP_SOURCE_SHA1: case GREP_SOURCE_OID:
fill_filespec(df, gs->identifier, 1, 0100644); fill_filespec(df, gs->identifier, 1, 0100644);
break; break;
case GREP_SOURCE_FILE: case GREP_SOURCE_FILE:
@ -1747,9 +1747,8 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,
* If the identifier is non-NULL (in the submodule case) it * If the identifier is non-NULL (in the submodule case) it
* will be a SHA1 that needs to be copied. * will be a SHA1 that needs to be copied.
*/ */
case GREP_SOURCE_SHA1: case GREP_SOURCE_OID:
gs->identifier = xmalloc(20); gs->identifier = oiddup(identifier);
hashcpy(gs->identifier, identifier);
break; break;
case GREP_SOURCE_BUF: case GREP_SOURCE_BUF:
gs->identifier = NULL; gs->identifier = NULL;
@ -1772,7 +1771,7 @@ void grep_source_clear_data(struct grep_source *gs)
{ {
switch (gs->type) { switch (gs->type) {
case GREP_SOURCE_FILE: case GREP_SOURCE_FILE:
case GREP_SOURCE_SHA1: case GREP_SOURCE_OID:
case GREP_SOURCE_SUBMODULE: case GREP_SOURCE_SUBMODULE:
free(gs->buf); free(gs->buf);
gs->buf = NULL; gs->buf = NULL;
@ -1784,7 +1783,7 @@ void grep_source_clear_data(struct grep_source *gs)
} }
} }
static int grep_source_load_sha1(struct grep_source *gs) static int grep_source_load_oid(struct grep_source *gs)
{ {
enum object_type type; enum object_type type;
@ -1795,7 +1794,7 @@ static int grep_source_load_sha1(struct grep_source *gs)
if (!gs->buf) if (!gs->buf)
return error(_("'%s': unable to read %s"), return error(_("'%s': unable to read %s"),
gs->name, gs->name,
sha1_to_hex(gs->identifier)); oid_to_hex(gs->identifier));
return 0; return 0;
} }
@ -1841,8 +1840,8 @@ static int grep_source_load(struct grep_source *gs)
switch (gs->type) { switch (gs->type) {
case GREP_SOURCE_FILE: case GREP_SOURCE_FILE:
return grep_source_load_file(gs); return grep_source_load_file(gs);
case GREP_SOURCE_SHA1: case GREP_SOURCE_OID:
return grep_source_load_sha1(gs); return grep_source_load_oid(gs);
case GREP_SOURCE_BUF: case GREP_SOURCE_BUF:
return gs->buf ? 0 : -1; return gs->buf ? 0 : -1;
case GREP_SOURCE_SUBMODULE: case GREP_SOURCE_SUBMODULE:

2
grep.h
View File

@ -158,7 +158,7 @@ struct grep_source {
char *name; char *name;
enum grep_source_type { enum grep_source_type {
GREP_SOURCE_SHA1, GREP_SOURCE_OID,
GREP_SOURCE_FILE, GREP_SOURCE_FILE,
GREP_SOURCE_BUF, GREP_SOURCE_BUF,
GREP_SOURCE_SUBMODULE, GREP_SOURCE_SUBMODULE,