update-ref: add support for 'symref-create' command
Add 'symref-create' command to the '--stdin' mode 'git-update-ref' to allow creation of symbolic refs in a transaction. The 'symref-create' command takes in a <new-target>, which the created <ref> will point to. Also, support the 'core.prefersymlinkrefs' config, wherein if the config is set and the filesystem supports symlinks, we create the symbolic ref as a symlink. We fallback to creating a regular symref if creating the symlink is unsuccessful. 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
ed3272720eb5ac8fca0e555d1f64a145140e69dd
9 files changed
+146
-6
Documentation/git-update-ref.txt
+6
@@ -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-create SP <ref> SP <new-target> LF
69
symref-delete SP <ref> [SP <old-target>] LF
70
symref-verify SP <ref> [SP <old-target>] LF
71
option SP <opt> LF
@@ -88,6 +89,7 @@ quoting:
89
create SP <ref> NUL <new-oid> NUL
90
delete SP <ref> NUL [<old-oid>] NUL
91
verify SP <ref> NUL [<old-oid>] NUL
92
+ symref-create SP <ref> NUL <new-target> NUL
93
symref-delete SP <ref> [NUL <old-target>] NUL
94
symref-verify SP <ref> [NUL <old-target>] NUL
95
option SP <opt> NUL
@@ -121,6 +123,10 @@ verify::
123
Verify <ref> against <old-oid> but do not change it. If
124
<old-oid> is zero or missing, the ref must not exist.
125
126
+symref-create:
127
+ Create symbolic ref <ref> with <new-target> after verifying
128
+ it does not exist.
129
+
130
symref-delete::
131
Delete <ref> after verifying it exists with <old-target>, if given.
132
builtin/clone.c
+1
-1
@@ -547,7 +547,7 @@ static void write_remote_refs(const struct ref *local_refs)
547
if (!r->peer_ref)
548
continue;
549
if (ref_transaction_create(t, r->peer_ref->name, &r->old_oid,
550
- 0, NULL, &err))
550
+ NULL, 0, NULL, &err))
551
die("%s", err.buf);
552
}
553
builtin/update-ref.c
+31
-1
@@ -257,7 +257,7 @@ static void parse_cmd_create(struct ref_transaction *transaction,
257
if (*next != line_termination)
258
die("create %s: extra input: %s", refname, next);
259
260
- if (ref_transaction_create(transaction, refname, &new_oid,
260
+ if (ref_transaction_create(transaction, refname, &new_oid, NULL,
261
update_flags | create_reflog_flag,
262
msg, &err))
263
die("%s", err.buf);
@@ -267,6 +267,35 @@ static void parse_cmd_create(struct ref_transaction *transaction,
267
strbuf_release(&err);
268
}
269
270
+
271
+static void parse_cmd_symref_create(struct ref_transaction *transaction,
272
+ const char *next, const char *end)
273
+{
274
+ struct strbuf err = STRBUF_INIT;
275
+ char *refname, *new_target;
276
+
277
+ refname = parse_refname(&next);
278
+ if (!refname)
279
+ die("symref-create: missing <ref>");
280
+
281
+ new_target = parse_next_refname(&next);
282
+ if (!new_target)
283
+ die("symref-create %s: missing <new-target>", refname);
284
+
285
+ if (*next != line_termination)
286
+ die("symref-create %s: extra input: %s", refname, next);
287
+
288
+ if (ref_transaction_create(transaction, refname, NULL, new_target,
289
+ update_flags | create_reflog_flag,
290
+ msg, &err))
291
+ die("%s", err.buf);
292
+
293
+ update_flags = default_flags;
294
+ free(refname);
295
+ free(new_target);
296
+ strbuf_release(&err);
297
+}
298
+
299
static void parse_cmd_delete(struct ref_transaction *transaction,
300
const char *next, const char *end)
301
{
@@ -473,6 +502,7 @@ static const struct parse_cmd {
502
{ "create", parse_cmd_create, 2, UPDATE_REFS_OPEN },
503
{ "delete", parse_cmd_delete, 2, UPDATE_REFS_OPEN },
504
{ "verify", parse_cmd_verify, 2, UPDATE_REFS_OPEN },
505
+ { "symref-create", parse_cmd_symref_create, 2, UPDATE_REFS_OPEN },
506
{ "symref-delete", parse_cmd_symref_delete, 2, UPDATE_REFS_OPEN },
507
{ "symref-verify", parse_cmd_symref_verify, 2, UPDATE_REFS_OPEN },
508
{ "option", parse_cmd_option, 1, UPDATE_REFS_OPEN },
refs.c
+6
-3
@@ -1302,15 +1302,18 @@ int ref_transaction_update(struct ref_transaction *transaction,
1302
int ref_transaction_create(struct ref_transaction *transaction,
1303
const char *refname,
1304
const struct object_id *new_oid,
1305
+ const char *new_target,
1306
unsigned int flags, const char *msg,
1307
struct strbuf *err)
1308
{
1308
- if (!new_oid || is_null_oid(new_oid)) {
1309
- strbuf_addf(err, "'%s' has a null OID", refname);
1309
+ if (new_oid && new_target)
1310
+ BUG("create called with both new_oid and new_target set");
1311
+ if ((!new_oid || is_null_oid(new_oid)) && !new_target) {
1312
+ strbuf_addf(err, "'%s' has neither a valid OID nor a target", refname);
1313
return 1;
1314
}
1315
return ref_transaction_update(transaction, refname, new_oid,
1313
- null_oid(), NULL, NULL, flags,
1316
+ null_oid(), new_target, NULL, flags,
1317
msg, err);
1318
}
1319
refs.h
+1
@@ -753,6 +753,7 @@ int ref_transaction_update(struct ref_transaction *transaction,
753
int ref_transaction_create(struct ref_transaction *transaction,
754
const char *refname,
755
const struct object_id *new_oid,
756
+ const char *new_target,
757
unsigned int flags, const char *msg,
758
struct strbuf *err);
759
t/t0600-reffiles-backend.sh
+32
@@ -472,4 +472,36 @@ test_expect_success POSIXPERM 'git reflog expire honors core.sharedRepository' '
472
esac
473
'
474
475
+test_expect_success SYMLINKS 'symref transaction supports symlinks' '
476
+ test_when_finished "git symbolic-ref -d TEST_SYMREF_HEAD" &&
477
+ git update-ref refs/heads/new @ &&
478
+ test_config core.prefersymlinkrefs true &&
479
+ cat >stdin <<-EOF &&
480
+ start
481
+ symref-create TEST_SYMREF_HEAD refs/heads/new
482
+ prepare
483
+ commit
484
+ EOF
485
+ git update-ref --no-deref --stdin <stdin &&
486
+ test_path_is_symlink .git/TEST_SYMREF_HEAD &&
487
+ test "$(test_readlink .git/TEST_SYMREF_HEAD)" = refs/heads/new
488
+'
489
+
490
+test_expect_success 'symref transaction supports false symlink config' '
491
+ test_when_finished "git symbolic-ref -d TEST_SYMREF_HEAD" &&
492
+ git update-ref refs/heads/new @ &&
493
+ test_config core.prefersymlinkrefs false &&
494
+ cat >stdin <<-EOF &&
495
+ start
496
+ symref-create TEST_SYMREF_HEAD refs/heads/new
497
+ prepare
498
+ commit
499
+ EOF
500
+ git update-ref --no-deref --stdin <stdin &&
501
+ test_path_is_file .git/TEST_SYMREF_HEAD &&
502
+ git symbolic-ref TEST_SYMREF_HEAD >actual &&
503
+ echo refs/heads/new >expect &&
504
+ test_cmp expect actual
505
+'
506
+
507
test_done
t/t1400-update-ref.sh
+65
@@ -1797,6 +1797,71 @@ do
1797
git update-ref --stdin $type --no-deref <stdin
1798
'
1799
1800
+ test_expect_success "stdin $type symref-create fails with too many arguments" '
1801
+ format_command $type "symref-create refs/heads/symref" "$a" "$a" >stdin &&
1802
+ test_must_fail git update-ref --stdin $type --no-deref <stdin 2>err &&
1803
+ if test "$type" = "-z"
1804
+ then
1805
+ grep "fatal: unknown command: $a" err
1806
+ else
1807
+ grep "fatal: symref-create refs/heads/symref: extra input: $a" err
1808
+ fi
1809
+ '
1810
+
1811
+ test_expect_success "stdin $type symref-create fails with no target" '
1812
+ format_command $type "symref-create refs/heads/symref" >stdin &&
1813
+ test_must_fail git update-ref --stdin $type --no-deref <stdin
1814
+ '
1815
+
1816
+ test_expect_success "stdin $type symref-create fails with empty target" '
1817
+ format_command $type "symref-create refs/heads/symref" "" >stdin &&
1818
+ test_must_fail git update-ref --stdin $type --no-deref <stdin
1819
+ '
1820
+
1821
+ test_expect_success "stdin $type symref-create works" '
1822
+ test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1823
+ format_command $type "symref-create refs/heads/symref" "$a" >stdin &&
1824
+ git update-ref --stdin $type --no-deref <stdin &&
1825
+ git symbolic-ref refs/heads/symref >expect &&
1826
+ echo $a >actual &&
1827
+ test_cmp expect actual
1828
+ '
1829
+
1830
+ test_expect_success "stdin $type symref-create works with --no-deref" '
1831
+ test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1832
+ format_command $type "symref-create refs/heads/symref" "$a" &&
1833
+ git update-ref --stdin $type <stdin 2>err
1834
+ '
1835
+
1836
+ test_expect_success "stdin $type create dangling symref ref works" '
1837
+ test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1838
+ format_command $type "symref-create refs/heads/symref" "refs/heads/unkown" >stdin &&
1839
+ git update-ref --stdin $type --no-deref <stdin &&
1840
+ git symbolic-ref refs/heads/symref >expect &&
1841
+ echo refs/heads/unkown >actual &&
1842
+ test_cmp expect actual
1843
+ '
1844
+
1845
+ test_expect_success "stdin $type symref-create does not create reflogs by default" '
1846
+ test_when_finished "git symbolic-ref -d refs/symref" &&
1847
+ format_command $type "symref-create refs/symref" "$a" >stdin &&
1848
+ git update-ref --stdin $type --no-deref <stdin &&
1849
+ git symbolic-ref refs/symref >expect &&
1850
+ echo $a >actual &&
1851
+ test_cmp expect actual &&
1852
+ test_must_fail git reflog exists refs/symref
1853
+ '
1854
+
1855
+ test_expect_success "stdin $type symref-create reflogs with --create-reflog" '
1856
+ test_when_finished "git symbolic-ref -d refs/heads/symref" &&
1857
+ format_command $type "symref-create refs/heads/symref" "$a" >stdin &&
1858
+ git update-ref --create-reflog --stdin $type --no-deref <stdin &&
1859
+ git symbolic-ref refs/heads/symref >expect &&
1860
+ echo $a >actual &&
1861
+ test_cmp expect actual &&
1862
+ git reflog exists refs/heads/symref
1863
+ '
1864
+
1865
done
1866
1867
test_done
t/t1416-ref-transaction-hooks.sh
+3
@@ -189,15 +189,18 @@ test_expect_success 'hook gets all queued symref updates' '
189
prepared
190
ref:refs/heads/main $ZERO_OID refs/heads/symref
191
ref:refs/heads/main $ZERO_OID refs/heads/symrefd
192
+ $ZERO_OID ref:refs/heads/main refs/heads/symrefc
193
committed
194
ref:refs/heads/main $ZERO_OID refs/heads/symref
195
ref:refs/heads/main $ZERO_OID refs/heads/symrefd
196
+ $ZERO_OID ref:refs/heads/main refs/heads/symrefc
197
EOF
198
199
git update-ref --no-deref --stdin <<-EOF &&
200
start
201
symref-verify refs/heads/symref refs/heads/main
202
symref-delete refs/heads/symrefd refs/heads/main
203
+ symref-create refs/heads/symrefc refs/heads/main
204
prepare
205
commit
206
EOF
t/t5605-clone-local.sh
+1
-1
@@ -163,7 +163,7 @@ test_expect_success REFFILES 'local clone from repo with corrupt refs fails grac
163
echo a >corrupt/.git/refs/heads/topic &&
164
165
test_must_fail git clone corrupt working 2>err &&
166
- grep "has a null OID" err
166
+ grep "has neither a valid OID nor a target" err
167
'
168
169
test_done