update-ref: fix streaming of status updates

When executing git-update-ref(1) with the `--stdin` flag, then the user can queue updates and, since e48cf33b61 (update-ref: implement interactive transaction handling, 2020-04-02), interactively drive the transaction's state via a set of transactional verbs. This interactivity is somewhat broken though: while the caller can use these verbs to drive the transaction's state, the status messages which confirm that a verb has been processed is not flushed. The caller may thus be left hanging waiting for the acknowledgement. Fix the bug by flushing stdout after writing the status update. Add a test which catches this bug. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Sep 3, 2021 at 11:06 UTC efa3d64ce86a600563c8bf909c46dd0985ee6c11
2 files changed +42 -4
builtin/update-ref.c
+10 -4
@@ -302,6 +302,12 @@ static void parse_cmd_verify(struct ref_transaction *transaction,
302 strbuf_release(&err);
303 }
304
305 +static void report_ok(const char *command)
306 +{
307 + fprintf(stdout, "%s: ok\n", command);
308 + fflush(stdout);
309 +}
310 +
311 static void parse_cmd_option(struct ref_transaction *transaction,
312 const char *next, const char *end)
313 {
@@ -317,7 +323,7 @@ static void parse_cmd_start(struct ref_transaction *transaction,
323 {
324 if (*next != line_termination)
325 die("start: extra input: %s", next);
320 - puts("start: ok");
326 + report_ok("start");
327 }
328
329 static void parse_cmd_prepare(struct ref_transaction *transaction,
@@ -328,7 +334,7 @@ static void parse_cmd_prepare(struct ref_transaction *transaction,
334 die("prepare: extra input: %s", next);
335 if (ref_transaction_prepare(transaction, &error))
336 die("prepare: %s", error.buf);
331 - puts("prepare: ok");
337 + report_ok("prepare");
338 }
339
340 static void parse_cmd_abort(struct ref_transaction *transaction,
@@ -339,7 +345,7 @@ static void parse_cmd_abort(struct ref_transaction *transaction,
345 die("abort: extra input: %s", next);
346 if (ref_transaction_abort(transaction, &error))
347 die("abort: %s", error.buf);
342 - puts("abort: ok");
348 + report_ok("abort");
349 }
350
351 static void parse_cmd_commit(struct ref_transaction *transaction,
@@ -350,7 +356,7 @@ static void parse_cmd_commit(struct ref_transaction *transaction,
356 die("commit: extra input: %s", next);
357 if (ref_transaction_commit(transaction, &error))
358 die("commit: %s", error.buf);
353 - puts("commit: ok");
359 + report_ok("commit");
360 ref_transaction_free(transaction);
361 }
362
t/t1400-update-ref.sh
+32
@@ -1598,6 +1598,38 @@ test_expect_success 'transaction cannot restart ongoing transaction' '
1598 test_must_fail git show-ref --verify refs/heads/restart
1599 '
1600
1601 +test_expect_success PIPE 'transaction flushes status updates' '
1602 + mkfifo in out &&
1603 + (git update-ref --stdin <in >out &) &&
1604 +
1605 + exec 9>in &&
1606 + test_when_finished "exec 9>&-" &&
1607 +
1608 + echo "start" >&9 &&
1609 + echo "start: ok" >expected &&
1610 + read line <out &&
1611 + echo "$line" >actual &&
1612 + test_cmp expected actual &&
1613 +
1614 + echo "create refs/heads/flush $A" >&9 &&
1615 +
1616 + echo prepare >&9 &&
1617 + echo "prepare: ok" >expected &&
1618 + read line <out &&
1619 + echo "$line" >actual &&
1620 + test_cmp expected actual &&
1621 +
1622 + # This must now fail given that we have locked the ref.
1623 + test_must_fail git update-ref refs/heads/flush $B 2>stderr &&
1624 + grep "fatal: update_ref failed for ref ${SQ}refs/heads/flush${SQ}: cannot lock ref" stderr &&
1625 +
1626 + echo commit >&9 &&
1627 + echo "commit: ok" >expected &&
1628 + read line <out &&
1629 + echo "$line" >actual &&
1630 + test_cmp expected actual
1631 +'
1632 +
1633 test_expect_success 'directory not created deleting packed ref' '
1634 git branch d1/d2/r1 HEAD &&
1635 git pack-refs --all &&