Skip to content

Commit 4538eef

Browse files
bmwillgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9ebf689 commit 4538eef

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

grep.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1735,12 +1735,23 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,
17351735
case GREP_SOURCE_FILE:
17361736
gs->identifier = xstrdup(identifier);
17371737
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+
*/
17381748
case GREP_SOURCE_SHA1:
17391749
gs->identifier = xmalloc(20);
17401750
hashcpy(gs->identifier, identifier);
17411751
break;
17421752
case GREP_SOURCE_BUF:
17431753
gs->identifier = NULL;
1754+
break;
17441755
}
17451756
}
17461757

@@ -1760,6 +1771,7 @@ void grep_source_clear_data(struct grep_source *gs)
17601771
switch (gs->type) {
17611772
case GREP_SOURCE_FILE:
17621773
case GREP_SOURCE_SHA1:
1774+
case GREP_SOURCE_SUBMODULE:
17631775
free(gs->buf);
17641776
gs->buf = NULL;
17651777
gs->size = 0;
@@ -1831,8 +1843,10 @@ static int grep_source_load(struct grep_source *gs)
18311843
return grep_source_load_sha1(gs);
18321844
case GREP_SOURCE_BUF:
18331845
return gs->buf ? 0 : -1;
1846+
case GREP_SOURCE_SUBMODULE:
1847+
break;
18341848
}
1835-
die("BUG: invalid grep_source type");
1849+
die("BUG: invalid grep_source type to load");
18361850
}
18371851

18381852
void grep_source_load_driver(struct grep_source *gs)

grep.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ struct grep_source {
161161
GREP_SOURCE_SHA1,
162162
GREP_SOURCE_FILE,
163163
GREP_SOURCE_BUF,
164+
GREP_SOURCE_SUBMODULE,
164165
} type;
165166
void *identifier;
166167

0 commit comments

Comments
 (0)