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 <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Taylor Blau committed
May 18, 2020 at 13:27 UTC
fa8953cb401d2c73c280eb6c70e7a7918dbe8bfb
1 file changed
+29
-29
builtin/commit-graph.c
+29
-29
@@ -138,12 +138,24 @@ static int write_option_parse_split(const struct option *opt, const char *arg,
138
return 0;
139
}
140
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
+
153
static int graph_write(int argc, const char **argv)
154
{
143
- struct string_list *pack_indexes = NULL;
155
+ struct string_list pack_indexes = STRING_LIST_INIT_NODUP;
156
+ struct strbuf buf = STRBUF_INIT;
157
struct oidset commits = OIDSET_INIT;
158
struct object_directory *odb = NULL;
146
- struct string_list lines;
159
int result = 0;
160
enum commit_graph_write_flags flags = 0;
161
@@ -209,44 +221,32 @@ static int graph_write(int argc, const char **argv)
221
return 0;
222
}
223
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) {
225
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;
236
}
236
- flags |= COMMIT_GRAPH_WRITE_CHECK_OIDS;
237
}
238
-
239
- UNLEAK(buf);
238
}
239
240
if (write_commit_graph(odb,
243
- pack_indexes,
241
+ opts.stdin_packs ? &pack_indexes : NULL,
242
opts.stdin_commits ? &commits : NULL,
243
flags,
244
&split_opts))
245
result = 1;
246
249
- UNLEAK(lines);
247
+cleanup:
248
+ string_list_clear(&pack_indexes, 0);
249
+ strbuf_release(&buf);
250
return result;
251
}
252