Skip to content

Commit a8ae923

Browse files
KarthikNayakgitster
authored andcommitted
refs: support symrefs in 'reference-transaction' hook
The 'reference-transaction' hook runs whenever a reference update is made to the system. In a previous commit, we added the `old_target` and `new_target` fields to the `reference_transaction_update()`. In following commits we'll also add the code to handle symref's in the reference backends. Support symrefs also in the 'reference-transaction' hook, by modifying the current format: <old-oid> SP <new-oid> SP <ref-name> LF to be be: <old-value> SP <new-value> SP <ref-name> LF where for regular refs the output would not change and remain the same. But when either 'old-value' or 'new-value' is a symref, we print the ref as 'ref:<ref-target>'. This does break backward compatibility, but the 'reference-transaction' hook's documentation always stated that support for symbolic references may be added in the future. We do not add any tests in this commit since there is no git command which activates this flow, in an upcoming commit, we'll start using transaction based symref updates as the default, we'll add tests there for the hook too. Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 57d0b1e commit a8ae923

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

Documentation/githooks.txt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ reference-transaction
486486
This hook is invoked by any Git command that performs reference
487487
updates. It executes whenever a reference transaction is prepared,
488488
committed or aborted and may thus get called multiple times. The hook
489-
does not cover symbolic references (but that may change in the future).
489+
also supports symbolic reference updates.
490490

491491
The hook takes exactly one argument, which is the current state the
492492
given reference transaction is in:
@@ -503,16 +503,20 @@ given reference transaction is in:
503503
For each reference update that was added to the transaction, the hook
504504
receives on standard input a line of the format:
505505

506-
<old-oid> SP <new-oid> SP <ref-name> LF
506+
<old-value> SP <new-value> SP <ref-name> LF
507507

508-
where `<old-oid>` is the old object name passed into the reference
509-
transaction, `<new-oid>` is the new object name to be stored in the
508+
where `<old-value>` is the old object name passed into the reference
509+
transaction, `<new-value>` is the new object name to be stored in the
510510
ref and `<ref-name>` is the full name of the ref. When force updating
511511
the reference regardless of its current value or when the reference is
512-
to be created anew, `<old-oid>` is the all-zeroes object name. To
512+
to be created anew, `<old-value>` is the all-zeroes object name. To
513513
distinguish these cases, you can inspect the current value of
514514
`<ref-name>` via `git rev-parse`.
515515

516+
For symbolic reference updates the `<old_value>` and `<new-value>`
517+
fields could denote references instead of objects. A reference will be
518+
denoted with a 'ref:' prefix, like `ref:<ref-target>`.
519+
516520
The exit status of the hook is ignored for any state except for the
517521
"prepared" state. In the "prepared" state, a non-zero exit status will
518522
cause the transaction to be aborted. The hook will not be called with

refs.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,10 +2350,22 @@ static int run_transaction_hook(struct ref_transaction *transaction,
23502350
struct ref_update *update = transaction->updates[i];
23512351

23522352
strbuf_reset(&buf);
2353-
strbuf_addf(&buf, "%s %s %s\n",
2354-
oid_to_hex(&update->old_oid),
2355-
oid_to_hex(&update->new_oid),
2356-
update->refname);
2353+
2354+
if (!(update->flags & REF_HAVE_OLD))
2355+
strbuf_addf(&buf, "%s ", oid_to_hex(null_oid()));
2356+
else if (update->old_target)
2357+
strbuf_addf(&buf, "ref:%s ", update->old_target);
2358+
else
2359+
strbuf_addf(&buf, "%s ", oid_to_hex(&update->old_oid));
2360+
2361+
if (!(update->flags & REF_HAVE_NEW))
2362+
strbuf_addf(&buf, "%s ", oid_to_hex(null_oid()));
2363+
else if (update->new_target)
2364+
strbuf_addf(&buf, "ref:%s ", update->new_target);
2365+
else
2366+
strbuf_addf(&buf, "%s ", oid_to_hex(&update->new_oid));
2367+
2368+
strbuf_addf(&buf, "%s\n", update->refname);
23572369

23582370
if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
23592371
if (errno != EPIPE) {

0 commit comments

Comments
 (0)