Skip to content

Commit efa3d64

Browse files
pks-tgitster
authored andcommitted
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 e48cf33 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 225bc32 commit efa3d64

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

builtin/update-ref.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,12 @@ static void parse_cmd_verify(struct ref_transaction *transaction,
302302
strbuf_release(&err);
303303
}
304304

305+
static void report_ok(const char *command)
306+
{
307+
fprintf(stdout, "%s: ok\n", command);
308+
fflush(stdout);
309+
}
310+
305311
static void parse_cmd_option(struct ref_transaction *transaction,
306312
const char *next, const char *end)
307313
{
@@ -317,7 +323,7 @@ static void parse_cmd_start(struct ref_transaction *transaction,
317323
{
318324
if (*next != line_termination)
319325
die("start: extra input: %s", next);
320-
puts("start: ok");
326+
report_ok("start");
321327
}
322328

323329
static void parse_cmd_prepare(struct ref_transaction *transaction,
@@ -328,7 +334,7 @@ static void parse_cmd_prepare(struct ref_transaction *transaction,
328334
die("prepare: extra input: %s", next);
329335
if (ref_transaction_prepare(transaction, &error))
330336
die("prepare: %s", error.buf);
331-
puts("prepare: ok");
337+
report_ok("prepare");
332338
}
333339

334340
static void parse_cmd_abort(struct ref_transaction *transaction,
@@ -339,7 +345,7 @@ static void parse_cmd_abort(struct ref_transaction *transaction,
339345
die("abort: extra input: %s", next);
340346
if (ref_transaction_abort(transaction, &error))
341347
die("abort: %s", error.buf);
342-
puts("abort: ok");
348+
report_ok("abort");
343349
}
344350

345351
static void parse_cmd_commit(struct ref_transaction *transaction,
@@ -350,7 +356,7 @@ static void parse_cmd_commit(struct ref_transaction *transaction,
350356
die("commit: extra input: %s", next);
351357
if (ref_transaction_commit(transaction, &error))
352358
die("commit: %s", error.buf);
353-
puts("commit: ok");
359+
report_ok("commit");
354360
ref_transaction_free(transaction);
355361
}
356362

t/t1400-update-ref.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,38 @@ test_expect_success 'transaction cannot restart ongoing transaction' '
15981598
test_must_fail git show-ref --verify refs/heads/restart
15991599
'
16001600

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+
16011633
test_expect_success 'directory not created deleting packed ref' '
16021634
git branch d1/d2/r1 HEAD &&
16031635
git pack-refs --all &&

0 commit comments

Comments
 (0)