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>
This commit is contained in:
Karthik Nayak
2024-06-07 15:33:02 +02:00
committed by Junio C Hamano
parent 2343720967
commit ed3272720e
9 changed files with 146 additions and 6 deletions

View File

@ -547,7 +547,7 @@ static void write_remote_refs(const struct ref *local_refs)
if (!r->peer_ref)
continue;
if (ref_transaction_create(t, r->peer_ref->name, &r->old_oid,
0, NULL, &err))
NULL, 0, NULL, &err))
die("%s", err.buf);
}

View File

@ -257,7 +257,7 @@ static void parse_cmd_create(struct ref_transaction *transaction,
if (*next != line_termination)
die("create %s: extra input: %s", refname, next);
if (ref_transaction_create(transaction, refname, &new_oid,
if (ref_transaction_create(transaction, refname, &new_oid, NULL,
update_flags | create_reflog_flag,
msg, &err))
die("%s", err.buf);
@ -267,6 +267,35 @@ static void parse_cmd_create(struct ref_transaction *transaction,
strbuf_release(&err);
}
static void parse_cmd_symref_create(struct ref_transaction *transaction,
const char *next, const char *end)
{
struct strbuf err = STRBUF_INIT;
char *refname, *new_target;
refname = parse_refname(&next);
if (!refname)
die("symref-create: missing <ref>");
new_target = parse_next_refname(&next);
if (!new_target)
die("symref-create %s: missing <new-target>", refname);
if (*next != line_termination)
die("symref-create %s: extra input: %s", refname, next);
if (ref_transaction_create(transaction, refname, NULL, new_target,
update_flags | create_reflog_flag,
msg, &err))
die("%s", err.buf);
update_flags = default_flags;
free(refname);
free(new_target);
strbuf_release(&err);
}
static void parse_cmd_delete(struct ref_transaction *transaction,
const char *next, const char *end)
{
@ -473,6 +502,7 @@ static const struct parse_cmd {
{ "create", parse_cmd_create, 2, UPDATE_REFS_OPEN },
{ "delete", parse_cmd_delete, 2, UPDATE_REFS_OPEN },
{ "verify", parse_cmd_verify, 2, UPDATE_REFS_OPEN },
{ "symref-create", parse_cmd_symref_create, 2, UPDATE_REFS_OPEN },
{ "symref-delete", parse_cmd_symref_delete, 2, UPDATE_REFS_OPEN },
{ "symref-verify", parse_cmd_symref_verify, 2, UPDATE_REFS_OPEN },
{ "option", parse_cmd_option, 1, UPDATE_REFS_OPEN },