Skip to content

Commit de0e0d6

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 804dba5 commit de0e0d6

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

builtin/update-ref.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,21 @@ static const struct parse_cmd {
332332
{ "option", parse_cmd_option },
333333
};
334334

335-
static void update_refs_stdin(struct ref_transaction *transaction)
335+
static void update_refs_stdin(void)
336336
{
337-
struct strbuf input = STRBUF_INIT;
337+
struct strbuf input = STRBUF_INIT, err = STRBUF_INIT;
338+
struct ref_transaction *transaction;
338339
const char *next;
339340
int i;
340341

341342
if (strbuf_read(&input, 0, 1000) < 0)
342343
die_errno("could not read from stdin");
343344
next = input.buf;
345+
346+
transaction = ref_transaction_begin(&err);
347+
if (!transaction)
348+
die("%s", err.buf);
349+
344350
/* Read each line dispatch its command */
345351
while (next < input.buf + input.len) {
346352
const struct parse_cmd *cmd = NULL;
@@ -367,6 +373,11 @@ static void update_refs_stdin(struct ref_transaction *transaction)
367373
next++;
368374
}
369375

376+
if (ref_transaction_commit(transaction, &err))
377+
die("%s", err.buf);
378+
379+
ref_transaction_free(transaction);
380+
strbuf_release(&err);
370381
strbuf_release(&input);
371382
}
372383

@@ -401,21 +412,11 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
401412
}
402413

403414
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);
410415
if (delete || argc > 0)
411416
usage_with_options(git_update_ref_usage, options);
412417
if (end_null)
413418
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();
419420
return 0;
420421
}
421422

0 commit comments

Comments
 (0)