grep: add submodules as a grep source type

Add `GREP_SOURCE_SUBMODULE` as a grep_source type and cases for this new type in the various switch statements in grep.c. When initializing a grep_source with type `GREP_SOURCE_SUBMODULE` the identifier can either be NULL (to indicate that the working tree will be used) or a SHA1 (the REV of the submodule to be grep'd). If the identifier is a SHA1 then we want to fall through to the `GREP_SOURCE_SHA1` case to handle the copying of the SHA1. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Dec 16, 2016 at 11:03 UTC 4538eef564c81c96f2874ccadc54d3c69cc0e19c
2 files changed +16 -1
grep.c
+15 -1
@@ -1735,12 +1735,23 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,
1735 case GREP_SOURCE_FILE:
1736 gs->identifier = xstrdup(identifier);
1737 break;
1738 + case GREP_SOURCE_SUBMODULE:
1739 + if (!identifier) {
1740 + gs->identifier = NULL;
1741 + break;
1742 + }
1743 + /*
1744 + * FALL THROUGH
1745 + * If the identifier is non-NULL (in the submodule case) it
1746 + * will be a SHA1 that needs to be copied.
1747 + */
1748 case GREP_SOURCE_SHA1:
1749 gs->identifier = xmalloc(20);
1750 hashcpy(gs->identifier, identifier);
1751 break;
1752 case GREP_SOURCE_BUF:
1753 gs->identifier = NULL;
1754 + break;
1755 }
1756 }
1757
@@ -1760,6 +1771,7 @@ void grep_source_clear_data(struct grep_source *gs)
1771 switch (gs->type) {
1772 case GREP_SOURCE_FILE:
1773 case GREP_SOURCE_SHA1:
1774 + case GREP_SOURCE_SUBMODULE:
1775 free(gs->buf);
1776 gs->buf = NULL;
1777 gs->size = 0;
@@ -1831,8 +1843,10 @@ static int grep_source_load(struct grep_source *gs)
1843 return grep_source_load_sha1(gs);
1844 case GREP_SOURCE_BUF:
1845 return gs->buf ? 0 : -1;
1846 + case GREP_SOURCE_SUBMODULE:
1847 + break;
1848 }
1835 - die("BUG: invalid grep_source type");
1849 + die("BUG: invalid grep_source type to load");
1850 }
1851
1852 void grep_source_load_driver(struct grep_source *gs)
grep.h
+1
@@ -161,6 +161,7 @@ struct grep_source {
161 GREP_SOURCE_SHA1,
162 GREP_SOURCE_FILE,
163 GREP_SOURCE_BUF,
164 + GREP_SOURCE_SUBMODULE,
165 } type;
166 void *identifier;
167