Skip to content

Commit 50d92b5

Browse files
jonathantanmygitster
authored andcommitted
grep: typesafe versions of grep_source_init
grep_source_init() can create "struct grep_source" objects and, depending on the value of the type passed, some void-pointer parameters have different meanings. Because one of these types (GREP_SOURCE_OID) will require an additional parameter in a subsequent patch, take the opportunity to increase clarity and type safety by replacing this function with individual functions for each type. Signed-off-by: Jonathan Tan <[email protected]> Reviewed-by: Matheus Tavares <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8d33c3a commit 50d92b5

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

builtin/grep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
333333
struct grep_source gs;
334334

335335
grep_source_name(opt, filename, tree_name_len, &pathbuf);
336-
grep_source_init(&gs, GREP_SOURCE_OID, pathbuf.buf, path, oid);
336+
grep_source_init_oid(&gs, pathbuf.buf, path, oid);
337337
strbuf_release(&pathbuf);
338338

339339
if (num_threads > 1) {
@@ -359,7 +359,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
359359
struct grep_source gs;
360360

361361
grep_source_name(opt, filename, 0, &buf);
362-
grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename, filename);
362+
grep_source_init_file(&gs, buf.buf, filename);
363363
strbuf_release(&buf);
364364

365365
if (num_threads > 1) {

grep.c

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,43 +1825,53 @@ int grep_source(struct grep_opt *opt, struct grep_source *gs)
18251825
return grep_source_1(opt, gs, 0);
18261826
}
18271827

1828+
static void grep_source_init_buf(struct grep_source *gs, char *buf,
1829+
unsigned long size)
1830+
{
1831+
gs->type = GREP_SOURCE_BUF;
1832+
gs->name = NULL;
1833+
gs->path = NULL;
1834+
gs->buf = buf;
1835+
gs->size = size;
1836+
gs->driver = NULL;
1837+
gs->identifier = NULL;
1838+
}
1839+
18281840
int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
18291841
{
18301842
struct grep_source gs;
18311843
int r;
18321844

1833-
grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL, NULL);
1834-
gs.buf = buf;
1835-
gs.size = size;
1845+
grep_source_init_buf(&gs, buf, size);
18361846

18371847
r = grep_source(opt, &gs);
18381848

18391849
grep_source_clear(&gs);
18401850
return r;
18411851
}
18421852

1843-
void grep_source_init(struct grep_source *gs, enum grep_source_type type,
1844-
const char *name, const char *path,
1845-
const void *identifier)
1853+
void grep_source_init_file(struct grep_source *gs, const char *name,
1854+
const char *path)
18461855
{
1847-
gs->type = type;
1856+
gs->type = GREP_SOURCE_FILE;
18481857
gs->name = xstrdup_or_null(name);
18491858
gs->path = xstrdup_or_null(path);
18501859
gs->buf = NULL;
18511860
gs->size = 0;
18521861
gs->driver = NULL;
1862+
gs->identifier = xstrdup(path);
1863+
}
18531864

1854-
switch (type) {
1855-
case GREP_SOURCE_FILE:
1856-
gs->identifier = xstrdup(identifier);
1857-
break;
1858-
case GREP_SOURCE_OID:
1859-
gs->identifier = oiddup(identifier);
1860-
break;
1861-
case GREP_SOURCE_BUF:
1862-
gs->identifier = NULL;
1863-
break;
1864-
}
1865+
void grep_source_init_oid(struct grep_source *gs, const char *name,
1866+
const char *path, const struct object_id *oid)
1867+
{
1868+
gs->type = GREP_SOURCE_OID;
1869+
gs->name = xstrdup_or_null(name);
1870+
gs->path = xstrdup_or_null(path);
1871+
gs->buf = NULL;
1872+
gs->size = 0;
1873+
gs->driver = NULL;
1874+
gs->identifier = oiddup(oid);
18651875
}
18661876

18671877
void grep_source_clear(struct grep_source *gs)

grep.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,10 @@ struct grep_source {
195195
struct userdiff_driver *driver;
196196
};
197197

198-
void grep_source_init(struct grep_source *gs, enum grep_source_type type,
199-
const char *name, const char *path,
200-
const void *identifier);
198+
void grep_source_init_file(struct grep_source *gs, const char *name,
199+
const char *path);
200+
void grep_source_init_oid(struct grep_source *gs, const char *name,
201+
const char *path, const struct object_id *oid);
201202
void grep_source_clear_data(struct grep_source *gs);
202203
void grep_source_clear(struct grep_source *gs);
203204
void grep_source_load_driver(struct grep_source *gs,

0 commit comments

Comments
 (0)