update-ref: move transaction handling into `update_refs_stdin()`
While the actual logic to update the transaction is handled in `update_refs_stdin()`, the transaction itself is started and committed in `cmd_update_ref()` itself. This makes it hard to handle transaction abortion and commits as part of `update_refs_stdin()` itself, which is required in order to introduce transaction handling features to `git update-refs --stdin`. Refactor the code to move all transaction handling into `update_refs_stdin()` to prepare for transaction handling features. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Apr 2, 2020 at 09:09 UTC
de0e0d650a1c2afc81a4c5e8d37d266ffa2b8ccf
1 file changed
+14
-13
builtin/update-ref.c
+14
-13
@@ -332,15 +332,21 @@ static const struct parse_cmd {
332
{ "option", parse_cmd_option },
333
};
334
335
-static void update_refs_stdin(struct ref_transaction *transaction)
335
+static void update_refs_stdin(void)
336
{
337
- struct strbuf input = STRBUF_INIT;
337
+ struct strbuf input = STRBUF_INIT, err = STRBUF_INIT;
338
+ struct ref_transaction *transaction;
339
const char *next;
340
int i;
341
342
if (strbuf_read(&input, 0, 1000) < 0)
343
die_errno("could not read from stdin");
344
next = input.buf;
345
+
346
+ transaction = ref_transaction_begin(&err);
347
+ if (!transaction)
348
+ die("%s", err.buf);
349
+
350
/* Read each line dispatch its command */
351
while (next < input.buf + input.len) {
352
const struct parse_cmd *cmd = NULL;
@@ -367,6 +373,11 @@ static void update_refs_stdin(struct ref_transaction *transaction)
373
next++;
374
}
375
376
+ if (ref_transaction_commit(transaction, &err))
377
+ die("%s", err.buf);
378
+
379
+ ref_transaction_free(transaction);
380
+ strbuf_release(&err);
381
strbuf_release(&input);
382
}
383
@@ -401,21 +412,11 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
412
}
413
414
if (read_stdin) {
404
- struct strbuf err = STRBUF_INIT;
405
- struct ref_transaction *transaction;
406
-
407
- transaction = ref_transaction_begin(&err);
408
- if (!transaction)
409
- die("%s", err.buf);
415
if (delete || argc > 0)
416
usage_with_options(git_update_ref_usage, options);
417
if (end_null)
418
line_termination = '\0';
414
- update_refs_stdin(transaction);
415
- if (ref_transaction_commit(transaction, &err))
416
- die("%s", err.buf);
417
- ref_transaction_free(transaction);
418
- strbuf_release(&err);
419
+ update_refs_stdin();
420
return 0;
421
}
422