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>

Brandon Williams committed May 30, 2017 at 10:30 UTC 1c41c82bc4878138a4c58e4d5df200de17cb8565
4 files changed +27 -21
builtin/grep.c
+11 -11
@@ -327,7 +327,7 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
327
328 #ifndef NO_PTHREADS
329 if (num_threads) {
330 - add_work(opt, GREP_SOURCE_SHA1, pathbuf.buf, path, oid);
330 + add_work(opt, GREP_SOURCE_OID, pathbuf.buf, path, oid);
331 strbuf_release(&pathbuf);
332 return 0;
333 } else
@@ -336,7 +336,7 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
336 struct grep_source gs;
337 int hit;
338
339 - grep_source_init(&gs, GREP_SOURCE_SHA1, pathbuf.buf, path, oid);
339 + grep_source_init(&gs, GREP_SOURCE_OID, pathbuf.buf, path, oid);
340 strbuf_release(&pathbuf);
341 hit = grep_source(opt, &gs);
342
@@ -570,7 +570,7 @@ static int grep_submodule_launch(struct grep_opt *opt,
570 * with the object's name: 'tree-name:filename'. In order to
571 * provide uniformity of output we want to pass the name of the
572 * parent project's object name to the submodule so the submodule can
573 - * prefix its output with the parent's name and not its own SHA1.
573 + * prefix its output with the parent's name and not its own OID.
574 */
575 if (gs->identifier && end_of_base)
576 argv_array_pushf(&cp.args, "--parent-basename=%.*s",
@@ -583,12 +583,12 @@ static int grep_submodule_launch(struct grep_opt *opt,
583 * If there is a tree identifier for the submodule, add the
584 * rev after adding the submodule options but before the
585 * pathspecs. To do this we listen for the '--' and insert the
586 - * sha1 before pushing the '--' onto the child process argv
586 + * oid before pushing the '--' onto the child process argv
587 * array.
588 */
589 if (gs->identifier &&
590 !strcmp("--", submodule_options.argv[i])) {
591 - argv_array_push(&cp.args, sha1_to_hex(gs->identifier));
591 + argv_array_push(&cp.args, oid_to_hex(gs->identifier));
592 }
593
594 argv_array_push(&cp.args, submodule_options.argv[i]);
@@ -618,11 +618,11 @@ static int grep_submodule_launch(struct grep_opt *opt,
618
619 /*
620 * Prep grep structures for a submodule grep
621 - * sha1: the sha1 of the submodule or NULL if using the working tree
621 + * oid: the oid of the submodule or NULL if using the working tree
622 * filename: name of the submodule including tree name of parent
623 * path: location of the submodule
624 */
625 -static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
625 +static int grep_submodule(struct grep_opt *opt, const struct object_id *oid,
626 const char *filename, const char *path)
627 {
628 if (!is_submodule_initialized(path))
@@ -632,7 +632,7 @@ static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
632 * If searching history, check for the presense of the
633 * submodule's gitdir before skipping the submodule.
634 */
635 - if (sha1) {
635 + if (oid) {
636 const struct submodule *sub =
637 submodule_from_path(null_sha1, path);
638 if (sub)
@@ -647,7 +647,7 @@ static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
647
648 #ifndef NO_PTHREADS
649 if (num_threads) {
650 - add_work(opt, GREP_SOURCE_SUBMODULE, filename, path, sha1);
650 + add_work(opt, GREP_SOURCE_SUBMODULE, filename, path, oid);
651 return 0;
652 } else
653 #endif
@@ -656,7 +656,7 @@ static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
656 int hit;
657
658 grep_source_init(&gs, GREP_SOURCE_SUBMODULE,
659 - filename, path, sha1);
659 + filename, path, oid);
660 hit = grep_submodule_launch(opt, &gs);
661
662 grep_source_clear(&gs);
@@ -775,7 +775,7 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
775 check_attr);
776 free(data);
777 } else if (recurse_submodules && S_ISGITLINK(entry.mode)) {
778 - hit |= grep_submodule(opt, entry.oid->hash, base->buf,
778 + hit |= grep_submodule(opt, entry.oid, base->buf,
779 base->buf + tn_len);
780 }
781
cache.h
+7
@@ -1026,6 +1026,13 @@ static inline void oidcpy(struct object_id *dst, const struct object_id *src)
1026 hashcpy(dst->hash, src->hash);
1027 }
1028
1029 +static inline struct object_id *oiddup(const struct object_id *src)
1030 +{
1031 + struct object_id *dst = xmalloc(sizeof(struct object_id));
1032 + oidcpy(dst, src);
1033 + return dst;
1034 +}
1035 +
1036 static inline void hashclr(unsigned char *hash)
1037 {
1038 memset(hash, 0, GIT_SHA1_RAWSZ);
grep.c
+8 -9
@@ -1403,7 +1403,7 @@ static int fill_textconv_grep(struct userdiff_driver *driver,
1403 */
1404 df = alloc_filespec(gs->path);
1405 switch (gs->type) {
1406 - case GREP_SOURCE_SHA1:
1406 + case GREP_SOURCE_OID:
1407 fill_filespec(df, gs->identifier, 1, 0100644);
1408 break;
1409 case GREP_SOURCE_FILE:
@@ -1747,9 +1747,8 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,
1747 * If the identifier is non-NULL (in the submodule case) it
1748 * will be a SHA1 that needs to be copied.
1749 */
1750 - case GREP_SOURCE_SHA1:
1751 - gs->identifier = xmalloc(20);
1752 - hashcpy(gs->identifier, identifier);
1750 + case GREP_SOURCE_OID:
1751 + gs->identifier = oiddup(identifier);
1752 break;
1753 case GREP_SOURCE_BUF:
1754 gs->identifier = NULL;
@@ -1772,7 +1771,7 @@ void grep_source_clear_data(struct grep_source *gs)
1771 {
1772 switch (gs->type) {
1773 case GREP_SOURCE_FILE:
1775 - case GREP_SOURCE_SHA1:
1774 + case GREP_SOURCE_OID:
1775 case GREP_SOURCE_SUBMODULE:
1776 free(gs->buf);
1777 gs->buf = NULL;
@@ -1784,7 +1783,7 @@ void grep_source_clear_data(struct grep_source *gs)
1783 }
1784 }
1785
1787 -static int grep_source_load_sha1(struct grep_source *gs)
1786 +static int grep_source_load_oid(struct grep_source *gs)
1787 {
1788 enum object_type type;
1789
@@ -1795,7 +1794,7 @@ static int grep_source_load_sha1(struct grep_source *gs)
1794 if (!gs->buf)
1795 return error(_("'%s': unable to read %s"),
1796 gs->name,
1798 - sha1_to_hex(gs->identifier));
1797 + oid_to_hex(gs->identifier));
1798 return 0;
1799 }
1800
@@ -1841,8 +1840,8 @@ static int grep_source_load(struct grep_source *gs)
1840 switch (gs->type) {
1841 case GREP_SOURCE_FILE:
1842 return grep_source_load_file(gs);
1844 - case GREP_SOURCE_SHA1:
1845 - return grep_source_load_sha1(gs);
1843 + case GREP_SOURCE_OID:
1844 + return grep_source_load_oid(gs);
1845 case GREP_SOURCE_BUF:
1846 return gs->buf ? 0 : -1;
1847 case GREP_SOURCE_SUBMODULE:
grep.h
+1 -1
@@ -158,7 +158,7 @@ struct grep_source {
158 char *name;
159
160 enum grep_source_type {
161 - GREP_SOURCE_SHA1,
161 + GREP_SOURCE_OID,
162 GREP_SOURCE_FILE,
163 GREP_SOURCE_BUF,
164 GREP_SOURCE_SUBMODULE,