Skip to content

Commit fa8953c

Browse files
ttaylorrgitster
authored andcommitted
builtin/commit-graph.c: extract 'read_one_commit()'
With either '--stdin-commits' or '--stdin-packs', the commit-graph builtin will read line-delimited input, and interpret it either as a series of commit OIDs, or pack names. In a subsequent commit, we will begin handling '--stdin-commits' differently by processing each line as it comes in, instead of in one shot at the end. To make adequate room for this additional logic, split the '--stdin-commits' case from '--stdin-packs' by only storing the input when '--stdin-packs' is given. In the case of '--stdin-commits', feed each line to a new 'read_one_commit' helper, which (for now) will merely call 'parse_oid_hex'. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 630cd51 commit fa8953c

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

builtin/commit-graph.c

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,24 @@ static int write_option_parse_split(const struct option *opt, const char *arg,
138138
return 0;
139139
}
140140

141+
static int read_one_commit(struct oidset *commits, const char *hash)
142+
{
143+
struct object_id oid;
144+
const char *end;
145+
146+
if (parse_oid_hex(hash, &oid, &end))
147+
return error(_("unexpected non-hex object ID: %s"), hash);
148+
149+
oidset_insert(commits, &oid);
150+
return 0;
151+
}
152+
141153
static int graph_write(int argc, const char **argv)
142154
{
143-
struct string_list *pack_indexes = NULL;
155+
struct string_list pack_indexes = STRING_LIST_INIT_NODUP;
156+
struct strbuf buf = STRBUF_INIT;
144157
struct oidset commits = OIDSET_INIT;
145158
struct object_directory *odb = NULL;
146-
struct string_list lines;
147159
int result = 0;
148160
enum commit_graph_write_flags flags = 0;
149161

@@ -209,44 +221,32 @@ static int graph_write(int argc, const char **argv)
209221
return 0;
210222
}
211223

212-
string_list_init(&lines, 0);
213-
if (opts.stdin_packs || opts.stdin_commits) {
214-
struct strbuf buf = STRBUF_INIT;
215-
224+
if (opts.stdin_packs) {
216225
while (strbuf_getline(&buf, stdin) != EOF)
217-
string_list_append(&lines, strbuf_detach(&buf, NULL));
218-
219-
if (opts.stdin_packs)
220-
pack_indexes = &lines;
221-
if (opts.stdin_commits) {
222-
struct string_list_item *item;
223-
oidset_init(&commits, lines.nr);
224-
for_each_string_list_item(item, &lines) {
225-
struct object_id oid;
226-
const char *end;
227-
228-
if (parse_oid_hex(item->string, &oid, &end)) {
229-
error(_("unexpected non-hex object ID: "
230-
"%s"), item->string);
231-
return 1;
232-
}
233-
234-
oidset_insert(&commits, &oid);
226+
string_list_append(&pack_indexes,
227+
strbuf_detach(&buf, NULL));
228+
} else if (opts.stdin_commits) {
229+
oidset_init(&commits, 0);
230+
flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
231+
232+
while (strbuf_getline(&buf, stdin) != EOF) {
233+
if (read_one_commit(&commits, buf.buf)) {
234+
result = 1;
235+
goto cleanup;
235236
}
236-
flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
237237
}
238-
239-
UNLEAK(buf);
240238
}
241239

242240
if (write_commit_graph(odb,
243-
pack_indexes,
241+
opts.stdin_packs ? &pack_indexes : NULL,
244242
opts.stdin_commits ? &commits : NULL,
245243
flags,
246244
&split_opts))
247245
result = 1;
248246

249-
UNLEAK(lines);
247+
cleanup:
248+
string_list_clear(&pack_indexes, 0);
249+
strbuf_release(&buf);
250250
return result;
251251
}
252252

0 commit comments

Comments
 (0)