update-ref: add support for 'symref-verify' command

The 'symref-verify' command allows users to verify if a provided <ref> contains the provided <old-target> without changing the <ref>. If <old-target> is not provided, the command will verify that the <ref> doesn't exist. The command allows users to verify symbolic refs within a transaction, and this means users can perform a set of changes in a transaction only when the verification holds good. Since we're checking for symbolic refs, this command will only work with the 'no-deref' mode. This is because any dereferenced symbolic ref will point to an object and not a ref and the regular 'verify' command can be used in such situations. Add required tests for symref support in 'verify'. Since we're here, also add reflog checks for the pre-existing 'verify' tests, there is no divergence from behavior, but we never tested to ensure that reflog wasn't affected by the 'verify' command. Helped-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Jun 7, 2024 at 15:33 UTC 1451ac734ffc1b9c45af12a2485c6d2ee69a6a76
6 files changed +208 -15
Documentation/git-update-ref.txt
+7
@@ -65,6 +65,7 @@ performs all modifications together. Specify commands of the form:
65 create SP <ref> SP <new-oid> LF
66 delete SP <ref> [SP <old-oid>] LF
67 verify SP <ref> [SP <old-oid>] LF
68 + symref-verify SP <ref> [SP <old-target>] LF
69 option SP <opt> LF
70 start LF
71 prepare LF
@@ -86,6 +87,7 @@ quoting:
87 create SP <ref> NUL <new-oid> NUL
88 delete SP <ref> NUL [<old-oid>] NUL
89 verify SP <ref> NUL [<old-oid>] NUL
90 + symref-verify SP <ref> [NUL <old-target>] NUL
91 option SP <opt> NUL
92 start NUL
93 prepare NUL
@@ -117,6 +119,11 @@ verify::
119 Verify <ref> against <old-oid> but do not change it. If
120 <old-oid> is zero or missing, the ref must not exist.
121
122 +symref-verify::
123 + Verify symbolic <ref> against <old-target> but do not change it.
124 + If <old-target> is missing, the ref must not exist. Can only be
125 + used in `no-deref` mode.
126 +
127 option::
128 Modify the behavior of the next command naming a <ref>.
129 The only valid option is `no-deref` to avoid dereferencing
builtin/update-ref.c
+70 -10
@@ -76,6 +76,29 @@ static char *parse_refname(const char **next)
76 return strbuf_detach(&ref, NULL);
77 }
78
79 +/*
80 + * Wrapper around parse_refname which skips the next delimiter.
81 + */
82 +static char *parse_next_refname(const char **next)
83 +{
84 + if (line_termination) {
85 + /* Without -z, consume SP and use next argument */
86 + if (!**next || **next == line_termination)
87 + return NULL;
88 + if (**next != ' ')
89 + die("expected SP but got: %s", *next);
90 + } else {
91 + /* With -z, read the next NUL-terminated line */
92 + if (**next)
93 + return NULL;
94 + }
95 + /* Skip the delimiter */
96 + (*next)++;
97 +
98 + return parse_refname(next);
99 +}
100 +
101 +
102 /*
103 * The value being parsed is <old-oid> (as opposed to <new-oid>; the
104 * difference affects which error messages are generated):
@@ -297,11 +320,47 @@ static void parse_cmd_verify(struct ref_transaction *transaction,
320 die("verify %s: extra input: %s", refname, next);
321
322 if (ref_transaction_verify(transaction, refname, &old_oid,
300 - update_flags, &err))
323 + NULL, update_flags, &err))
324 + die("%s", err.buf);
325 +
326 + update_flags = default_flags;
327 + free(refname);
328 + strbuf_release(&err);
329 +}
330 +
331 +static void parse_cmd_symref_verify(struct ref_transaction *transaction,
332 + const char *next, const char *end)
333 +{
334 + struct strbuf err = STRBUF_INIT;
335 + struct object_id old_oid;
336 + char *refname, *old_target;
337 +
338 + if (!(update_flags & REF_NO_DEREF))
339 + die("symref-verify: cannot operate with deref mode");
340 +
341 + refname = parse_refname(&next);
342 + if (!refname)
343 + die("symref-verify: missing <ref>");
344 +
345 + /*
346 + * old_ref is optional, if not provided, we need to ensure that the
347 + * ref doesn't exist.
348 + */
349 + old_target = parse_next_refname(&next);
350 + if (!old_target)
351 + oidcpy(&old_oid, null_oid());
352 +
353 + if (*next != line_termination)
354 + die("symref-verify %s: extra input: %s", refname, next);
355 +
356 + if (ref_transaction_verify(transaction, refname,
357 + old_target ? NULL : &old_oid,
358 + old_target, update_flags, &err))
359 die("%s", err.buf);
360
361 update_flags = default_flags;
362 free(refname);
363 + free(old_target);
364 strbuf_release(&err);
365 }
366
@@ -380,15 +439,16 @@ static const struct parse_cmd {
439 unsigned args;
440 enum update_refs_state state;
441 } command[] = {
383 - { "update", parse_cmd_update, 3, UPDATE_REFS_OPEN },
384 - { "create", parse_cmd_create, 2, UPDATE_REFS_OPEN },
385 - { "delete", parse_cmd_delete, 2, UPDATE_REFS_OPEN },
386 - { "verify", parse_cmd_verify, 2, UPDATE_REFS_OPEN },
387 - { "option", parse_cmd_option, 1, UPDATE_REFS_OPEN },
388 - { "start", parse_cmd_start, 0, UPDATE_REFS_STARTED },
389 - { "prepare", parse_cmd_prepare, 0, UPDATE_REFS_PREPARED },
390 - { "abort", parse_cmd_abort, 0, UPDATE_REFS_CLOSED },
391 - { "commit", parse_cmd_commit, 0, UPDATE_REFS_CLOSED },
442 + { "update", parse_cmd_update, 3, UPDATE_REFS_OPEN },
443 + { "create", parse_cmd_create, 2, UPDATE_REFS_OPEN },
444 + { "delete", parse_cmd_delete, 2, UPDATE_REFS_OPEN },
445 + { "verify", parse_cmd_verify, 2, UPDATE_REFS_OPEN },
446 + { "symref-verify", parse_cmd_symref_verify, 2, UPDATE_REFS_OPEN },
447 + { "option", parse_cmd_option, 1, UPDATE_REFS_OPEN },
448 + { "start", parse_cmd_start, 0, UPDATE_REFS_STARTED },
449 + { "prepare", parse_cmd_prepare, 0, UPDATE_REFS_PREPARED },
450 + { "abort", parse_cmd_abort, 0, UPDATE_REFS_CLOSED },
451 + { "commit", parse_cmd_commit, 0, UPDATE_REFS_CLOSED },
452 };
453
454 static void update_refs_stdin(void)
refs.c
+8 -3
@@ -1331,14 +1331,19 @@ int ref_transaction_delete(struct ref_transaction *transaction,
1331 int ref_transaction_verify(struct ref_transaction *transaction,
1332 const char *refname,
1333 const struct object_id *old_oid,
1334 + const char *old_target,
1335 unsigned int flags,
1336 struct strbuf *err)
1337 {
1337 - if (!old_oid)
1338 - BUG("verify called with old_oid set to NULL");
1338 + if (!old_target && !old_oid)
1339 + BUG("verify called with old_oid and old_target set to NULL");
1340 + if (old_oid && old_target)
1341 + BUG("verify called with both old_oid and old_target set");
1342 + if (old_target && !(flags & REF_NO_DEREF))
1343 + BUG("verify cannot operate on symrefs with deref mode");
1344 return ref_transaction_update(transaction, refname,
1345 NULL, old_oid,
1341 - NULL, NULL,
1346 + NULL, old_target,
1347 flags, NULL, err);
1348 }
1349
refs.h
+1
@@ -781,6 +781,7 @@ int ref_transaction_delete(struct ref_transaction *transaction,
781 int ref_transaction_verify(struct ref_transaction *transaction,
782 const char *refname,
783 const struct object_id *old_oid,
784 + const char *old_target,
785 unsigned int flags,
786 struct strbuf *err);
787
t/t1400-update-ref.sh
+92 -2
@@ -890,17 +890,23 @@ test_expect_success 'stdin update/create/verify combination works' '
890 '
891
892 test_expect_success 'stdin verify succeeds for correct value' '
893 + test-tool ref-store main for-each-reflog-ent $m >before &&
894 git rev-parse $m >expect &&
895 echo "verify $m $m" >stdin &&
896 git update-ref --stdin <stdin &&
897 git rev-parse $m >actual &&
897 - test_cmp expect actual
898 + test_cmp expect actual &&
899 + test-tool ref-store main for-each-reflog-ent $m >after &&
900 + test_cmp before after
901 '
902
903 test_expect_success 'stdin verify succeeds for missing reference' '
904 + test-tool ref-store main for-each-reflog-ent $m >before &&
905 echo "verify refs/heads/missing $Z" >stdin &&
906 git update-ref --stdin <stdin &&
903 - test_must_fail git rev-parse --verify -q refs/heads/missing
907 + test_must_fail git rev-parse --verify -q refs/heads/missing &&
908 + test-tool ref-store main for-each-reflog-ent $m >after &&
909 + test_cmp before after
910 '
911
912 test_expect_success 'stdin verify treats no value as missing' '
@@ -1641,4 +1647,88 @@ test_expect_success PIPE 'transaction flushes status updates' '
1647 test_cmp expected actual
1648 '
1649
1650 +format_command () {
1651 + if test "$1" = "-z"
1652 + then
1653 + shift
1654 + printf "$F" "$@"
1655 + else
1656 + echo "$@"
1657 + fi
1658 +}
1659 +
1660 +for type in "" "-z"
1661 +do
1662 +
1663 + test_expect_success "stdin $type symref-verify fails without --no-deref" '
1664 + git symbolic-ref refs/heads/symref $a &&
1665 + format_command $type "symref-verify refs/heads/symref" "$a" >stdin &&
1666 + test_must_fail git update-ref --stdin $type <stdin 2>err &&
1667 + grep "fatal: symref-verify: cannot operate with deref mode" err
1668 + '
1669 +
1670 + test_expect_success "stdin $type symref-verify fails with too many arguments" '
1671 + format_command $type "symref-verify refs/heads/symref" "$a" "$a" >stdin &&
1672 + test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1673 + if test "$type" = "-z"
1674 + then
1675 + grep "fatal: unknown command: $a" err
1676 + else
1677 + grep "fatal: symref-verify refs/heads/symref: extra input: $a" err
1678 + fi
1679 + '
1680 +
1681 + test_expect_success "stdin $type symref-verify succeeds for correct value" '
1682 + git symbolic-ref refs/heads/symref >expect &&
1683 + test-tool ref-store main for-each-reflog-ent refs/heads/symref >before &&
1684 + format_command $type "symref-verify refs/heads/symref" "$a" >stdin &&
1685 + git update-ref --stdin $type --no-deref <stdin &&
1686 + git symbolic-ref refs/heads/symref >actual &&
1687 + test_cmp expect actual &&
1688 + test-tool ref-store main for-each-reflog-ent refs/heads/symref >after &&
1689 + test_cmp before after
1690 + '
1691 +
1692 + test_expect_success "stdin $type symref-verify fails with no value" '
1693 + git symbolic-ref refs/heads/symref >expect &&
1694 + format_command $type "symref-verify refs/heads/symref" "" >stdin &&
1695 + test_must_fail git update-ref --stdin $type --no-deref <stdin
1696 + '
1697 +
1698 + test_expect_success "stdin $type symref-verify succeeds for dangling reference" '
1699 + test_when_finished "git symbolic-ref -d refs/heads/symref2" &&
1700 + test_must_fail git symbolic-ref refs/heads/nonexistent &&
1701 + git symbolic-ref refs/heads/symref2 refs/heads/nonexistent &&
1702 + format_command $type "symref-verify refs/heads/symref2" "refs/heads/nonexistent" >stdin &&
1703 + git update-ref --stdin $type --no-deref <stdin
1704 + '
1705 +
1706 + test_expect_success "stdin $type symref-verify fails for missing reference" '
1707 + test-tool ref-store main for-each-reflog-ent refs/heads/symref >before &&
1708 + format_command $type "symref-verify refs/heads/missing" "refs/heads/unknown" >stdin &&
1709 + test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1710 + grep "fatal: cannot lock ref ${SQ}refs/heads/missing${SQ}: unable to resolve reference ${SQ}refs/heads/missing${SQ}" err &&
1711 + test_must_fail git rev-parse --verify -q refs/heads/missing &&
1712 + test-tool ref-store main for-each-reflog-ent refs/heads/symref >after &&
1713 + test_cmp before after
1714 + '
1715 +
1716 + test_expect_success "stdin $type symref-verify fails for wrong value" '
1717 + git symbolic-ref refs/heads/symref >expect &&
1718 + format_command $type "symref-verify refs/heads/symref" "$b" >stdin &&
1719 + test_must_fail git update-ref --stdin $type --no-deref <stdin &&
1720 + git symbolic-ref refs/heads/symref >actual &&
1721 + test_cmp expect actual
1722 + '
1723 +
1724 + test_expect_success "stdin $type symref-verify fails for mistaken null value" '
1725 + git symbolic-ref refs/heads/symref >expect &&
1726 + format_command $type "symref-verify refs/heads/symref" "$Z" >stdin &&
1727 + test_must_fail git update-ref --stdin $type --no-deref <stdin &&
1728 + git symbolic-ref refs/heads/symref >actual &&
1729 + test_cmp expect actual
1730 + '
1731 +
1732 +done
1733 +
1734 test_done
t/t1416-ref-transaction-hooks.sh
+30
@@ -157,4 +157,34 @@ test_expect_success 'hook captures git-symbolic-ref updates' '
157 test_cmp expect actual
158 '
159
160 +test_expect_success 'hook gets all queued symref updates' '
161 + test_when_finished "rm actual" &&
162 +
163 + git update-ref refs/heads/branch $POST_OID &&
164 + git symbolic-ref refs/heads/symref refs/heads/main &&
165 +
166 + test_hook reference-transaction <<-\EOF &&
167 + echo "$*" >>actual
168 + while read -r line
169 + do
170 + printf "%s\n" "$line"
171 + done >>actual
172 + EOF
173 +
174 + cat >expect <<-EOF &&
175 + prepared
176 + ref:refs/heads/main $ZERO_OID refs/heads/symref
177 + committed
178 + ref:refs/heads/main $ZERO_OID refs/heads/symref
179 + EOF
180 +
181 + git update-ref --no-deref --stdin <<-EOF &&
182 + start
183 + symref-verify refs/heads/symref refs/heads/main
184 + prepare
185 + commit
186 + EOF
187 + test_cmp expect actual
188 +'
189 +
190 test_done