Skip to content

Commit ed32727

Browse files
KarthikNayakgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2343720 commit ed32727

File tree

9 files changed

+146
-6
lines changed

9 files changed

+146
-6
lines changed

Documentation/git-update-ref.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ performs all modifications together. Specify commands of the form:
6565
create SP <ref> SP <new-oid> LF
6666
delete SP <ref> [SP <old-oid>] LF
6767
verify SP <ref> [SP <old-oid>] LF
68+
symref-create SP <ref> SP <new-target> LF
6869
symref-delete SP <ref> [SP <old-target>] LF
6970
symref-verify SP <ref> [SP <old-target>] LF
7071
option SP <opt> LF
@@ -88,6 +89,7 @@ quoting:
8889
create SP <ref> NUL <new-oid> NUL
8990
delete SP <ref> NUL [<old-oid>] NUL
9091
verify SP <ref> NUL [<old-oid>] NUL
92+
symref-create SP <ref> NUL <new-target> NUL
9193
symref-delete SP <ref> [NUL <old-target>] NUL
9294
symref-verify SP <ref> [NUL <old-target>] NUL
9395
option SP <opt> NUL
@@ -121,6 +123,10 @@ verify::
121123
Verify <ref> against <old-oid> but do not change it. If
122124
<old-oid> is zero or missing, the ref must not exist.
123125

126+
symref-create:
127+
Create symbolic ref <ref> with <new-target> after verifying
128+
it does not exist.
129+
124130
symref-delete::
125131
Delete <ref> after verifying it exists with <old-target>, if given.
126132

builtin/clone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ static void write_remote_refs(const struct ref *local_refs)
547547
if (!r->peer_ref)
548548
continue;
549549
if (ref_transaction_create(t, r->peer_ref->name, &r->old_oid,
550-
0, NULL, &err))
550+
NULL, 0, NULL, &err))
551551
die("%s", err.buf);
552552
}
553553

builtin/update-ref.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ static void parse_cmd_create(struct ref_transaction *transaction,
257257
if (*next != line_termination)
258258
die("create %s: extra input: %s", refname, next);
259259

260-
if (ref_transaction_create(transaction, refname, &new_oid,
260+
if (ref_transaction_create(transaction, refname, &new_oid, NULL,
261261
update_flags | create_reflog_flag,
262262
msg, &err))
263263
die("%s", err.buf);
@@ -267,6 +267,35 @@ static void parse_cmd_create(struct ref_transaction *transaction,
267267
strbuf_release(&err);
268268
}
269269

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+
270299
static void parse_cmd_delete(struct ref_transaction *transaction,
271300
const char *next, const char *end)
272301
{
@@ -473,6 +502,7 @@ static const struct parse_cmd {
473502
{ "create", parse_cmd_create, 2, UPDATE_REFS_OPEN },
474503
{ "delete", parse_cmd_delete, 2, UPDATE_REFS_OPEN },
475504
{ "verify", parse_cmd_verify, 2, UPDATE_REFS_OPEN },
505+
{ "symref-create", parse_cmd_symref_create, 2, UPDATE_REFS_OPEN },
476506
{ "symref-delete", parse_cmd_symref_delete, 2, UPDATE_REFS_OPEN },
477507
{ "symref-verify", parse_cmd_symref_verify, 2, UPDATE_REFS_OPEN },
478508
{ "option", parse_cmd_option, 1, UPDATE_REFS_OPEN },

refs.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,15 +1302,18 @@ int ref_transaction_update(struct ref_transaction *transaction,
13021302
int ref_transaction_create(struct ref_transaction *transaction,
13031303
const char *refname,
13041304
const struct object_id *new_oid,
1305+
const char *new_target,
13051306
unsigned int flags, const char *msg,
13061307
struct strbuf *err)
13071308
{
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);
13101313
return 1;
13111314
}
13121315
return ref_transaction_update(transaction, refname, new_oid,
1313-
null_oid(), NULL, NULL, flags,
1316+
null_oid(), new_target, NULL, flags,
13141317
msg, err);
13151318
}
13161319

refs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ int ref_transaction_update(struct ref_transaction *transaction,
753753
int ref_transaction_create(struct ref_transaction *transaction,
754754
const char *refname,
755755
const struct object_id *new_oid,
756+
const char *new_target,
756757
unsigned int flags, const char *msg,
757758
struct strbuf *err);
758759

t/t0600-reffiles-backend.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,36 @@ test_expect_success POSIXPERM 'git reflog expire honors core.sharedRepository' '
472472
esac
473473
'
474474

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+
475507
test_done

t/t1400-update-ref.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,71 @@ do
17971797
git update-ref --stdin $type --no-deref <stdin
17981798
'
17991799

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+
18001865
done
18011866

18021867
test_done

t/t1416-ref-transaction-hooks.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,18 @@ test_expect_success 'hook gets all queued symref updates' '
189189
prepared
190190
ref:refs/heads/main $ZERO_OID refs/heads/symref
191191
ref:refs/heads/main $ZERO_OID refs/heads/symrefd
192+
$ZERO_OID ref:refs/heads/main refs/heads/symrefc
192193
committed
193194
ref:refs/heads/main $ZERO_OID refs/heads/symref
194195
ref:refs/heads/main $ZERO_OID refs/heads/symrefd
196+
$ZERO_OID ref:refs/heads/main refs/heads/symrefc
195197
EOF
196198
197199
git update-ref --no-deref --stdin <<-EOF &&
198200
start
199201
symref-verify refs/heads/symref refs/heads/main
200202
symref-delete refs/heads/symrefd refs/heads/main
203+
symref-create refs/heads/symrefc refs/heads/main
201204
prepare
202205
commit
203206
EOF

t/t5605-clone-local.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ test_expect_success REFFILES 'local clone from repo with corrupt refs fails grac
163163
echo a >corrupt/.git/refs/heads/topic &&
164164
165165
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
167167
'
168168

169169
test_done

0 commit comments

Comments
 (0)