@@ -62,7 +62,7 @@ With `--stdin`, update-ref reads instructions from standard input and
performs all modifications together. Specify commands of the form:
update SP <ref> SP <new-oid> [SP <old-oid>] LF
- create SP <ref> SP <new-oid> LF
+ create SP <ref> SP (<new-oid> | ref:<new-target>) LF
delete SP <ref> [SP (<old-oid> | ref:<old-target>)] LF
verify SP <ref> [SP (<old-oid> | ref:<old-target>)] LF
option SP <opt> LF
@@ -83,7 +83,7 @@ Alternatively, use `-z` to specify in NUL-terminated format, without
quoting:
update SP <ref> NUL <new-oid> NUL [<old-oid>] NUL
- create SP <ref> NUL <new-oid> NUL
+ create SP <ref> NUL (<new-oid> | ref:<new-target>) NUL
delete SP <ref> NUL [(<old-oid> | ref:<old-target>)] NUL
verify SP <ref> NUL [(<old-oid> | ref:<old-target>)] NUL
option SP <opt> NUL
@@ -113,7 +113,9 @@ update::
create::
Create <ref> with <new-oid> after verifying it does not
- exist. The given <new-oid> may not be zero.
+ exist. The given <new-oid> may not be zero. If instead
+ ref:<new-target> is provided, a symbolic ref is created
+ which targets <new-target>.
delete::
Delete <ref> after verifying it exists with <old-oid>, if given.
@@ -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);
}
@@ -244,6 +244,7 @@ static void parse_cmd_create(struct ref_transaction *transaction,
const char *next, const char *end)
{
struct strbuf err = STRBUF_INIT;
+ struct strbuf new_target = STRBUF_INIT;
char *refname;
struct object_id new_oid;
@@ -251,16 +252,22 @@ static void parse_cmd_create(struct ref_transaction *transaction,
if (!refname)
die("create: missing <ref>");
- if (parse_next_arg(&next, end, &new_oid, NULL, "create", refname, 0))
+ if (parse_next_arg(&next, end, &new_oid, &new_target,
+ "create", refname, PARSE_REFNAME_TARGETS))
die("create %s: missing <new-oid>", refname);
- if (is_null_oid(&new_oid))
+ if (!new_target.len && is_null_oid(&new_oid))
die("create %s: zero <new-oid>", refname);
+ if (new_target.len && !(update_flags & REF_NO_DEREF))
+ die("create %s: cannot create symrefs in deref mode", refname);
+
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_target.len ? NULL : &new_oid ,
+ new_target.len ? new_target.buf : NULL,
update_flags | create_reflog_flag,
msg, &err))
die("%s", err.buf);
@@ -268,6 +275,7 @@ static void parse_cmd_create(struct ref_transaction *transaction,
update_flags = default_flags;
free(refname);
strbuf_release(&err);
+ strbuf_release(&new_target);
}
static void parse_cmd_delete(struct ref_transaction *transaction,
@@ -1303,15 +1303,18 @@ int ref_transaction_update(struct ref_transaction *transaction,
int ref_transaction_create(struct ref_transaction *transaction,
const char *refname,
const struct object_id *new_oid,
+ const char *new_target,
unsigned int flags, const char *msg,
struct strbuf *err)
{
- if (!new_oid || is_null_oid(new_oid)) {
- strbuf_addf(err, "'%s' has a null OID", refname);
+ if ((!new_oid || is_null_oid(new_oid)) && !new_target) {
+ strbuf_addf(err, "'%s' has a null OID or no new target", refname);
return 1;
}
+ if (new_target && !(flags & REF_NO_DEREF))
+ BUG("create cannot operate on symrefs with deref mode");
return ref_transaction_update(transaction, refname, new_oid,
- null_oid(), NULL, NULL, flags,
+ null_oid(), new_target, NULL, flags,
msg, err);
}
@@ -752,6 +752,7 @@ int ref_transaction_update(struct ref_transaction *transaction,
int ref_transaction_create(struct ref_transaction *transaction,
const char *refname,
const struct object_id *new_oid,
+ const char *new_target,
unsigned int flags, const char *msg,
struct strbuf *err);
@@ -2610,6 +2610,27 @@ static int lock_ref_for_update(struct files_ref_store *refs,
}
}
+ if (update->new_target) {
+ if (create_symref_lock(refs, lock, update->refname, update->new_target)) {
+ ret = TRANSACTION_GENERIC_ERROR;
+ goto out;
+ }
+
+ if (close_ref_gently(lock)) {
+ strbuf_addf(err, "couldn't close '%s.lock'",
+ update->refname);
+ ret = TRANSACTION_GENERIC_ERROR;
+ goto out;
+ }
+
+ /*
+ * Once we have created the symref lock, the commit
+ * phase of the transaction only needs to commit the lock.
+ */
+ update->flags |= REF_NEEDS_COMMIT;
+ }
+
+
if ((update->flags & REF_HAVE_NEW) &&
!(update->flags & REF_DELETING) &&
!(update->flags & REF_LOG_ONLY)) {
@@ -2905,6 +2926,18 @@ static int files_transaction_finish(struct ref_store *ref_store,
if (update->flags & REF_NEEDS_COMMIT ||
update->flags & REF_LOG_ONLY) {
+ if (update->new_target) {
+ /*
+ * We want to get the resolved OID for the target, to ensure
+ * that the correct value is added to the reflog.
+ */
+ if (!refs_resolve_ref_unsafe(&refs->base, update->new_target,
+ RESOLVE_REF_READING, &update->new_oid, NULL)) {
+ /* for dangling symrefs we gracefully set the oid to zero */
+ update->new_oid = *null_oid();
+ }
+ }
+
if (files_log_ref_write(refs,
lock->ref_name,
&lock->old_oid,
@@ -2922,6 +2955,15 @@ static int files_transaction_finish(struct ref_store *ref_store,
goto cleanup;
}
}
+
+ /*
+ * We try creating a symlink, if that succeeds we continue to the
+ * next updated. If not, we try and create a regular symref.
+ */
+ if (update->new_target && prefer_symlink_refs)
+ if (!create_ref_symlink(lock, update->new_target))
+ continue;
+
if (update->flags & REF_NEEDS_COMMIT) {
clear_loose_ref_cache(refs);
if (commit_ref(lock)) {
@@ -856,7 +856,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
* There is no need to write the reference deletion
* when the reference in question doesn't exist.
*/
- if (u->flags & REF_HAVE_NEW && !is_null_oid(&u->new_oid)) {
+ if (u->flags & REF_HAVE_NEW && !ref_update_is_null_new_value(u)) {
ret = queue_transaction_update(refs, tx_data, u,
¤t_oid, err);
if (ret)
@@ -1062,7 +1062,7 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data
* - `core.logAllRefUpdates` tells us to create the reflog for
* the given ref.
*/
- if (u->flags & REF_HAVE_NEW && !(u->type & REF_ISSYMREF) && is_null_oid(&u->new_oid)) {
+ if (u->flags & REF_HAVE_NEW && !(u->type & REF_ISSYMREF) && ref_update_is_null_new_value(u)) {
struct reftable_log_record log = {0};
struct reftable_iterator it = {0};
@@ -1104,6 +1104,12 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data
should_write_log(&arg->refs->base, u->refname))) {
struct reftable_log_record *log;
+ if (u->new_target)
+ if (!refs_resolve_ref_unsafe(&arg->refs->base, u->new_target,
+ RESOLVE_REF_READING, &u->new_oid, NULL))
+ /* for dangling symrefs we gracefully set the oid to zero */
+ u->new_oid = *null_oid();
+
ALLOC_GROW(logs, logs_nr + 1, logs_alloc);
log = &logs[logs_nr++];
memset(log, 0, sizeof(*log));
@@ -1120,7 +1126,18 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data
if (u->flags & REF_LOG_ONLY)
continue;
- if (u->flags & REF_HAVE_NEW && ref_update_is_null_new_value(u)) {
+ if (u->flags & REF_HAVE_NEW && u->new_target) {
+ struct reftable_ref_record ref = {
+ .refname = (char *)u->refname,
+ .value_type = REFTABLE_REF_SYMREF,
+ .value.symref = (char *)u->new_target,
+ .update_index = ts,
+ };
+
+ ret = reftable_writer_add_ref(writer, &ref);
+ if (ret < 0)
+ goto done;
+ } else if (u->flags & REF_HAVE_NEW && ref_update_is_null_new_value(u)) {
struct reftable_ref_record ref = {
.refname = (char *)u->refname,
.update_index = ts,
@@ -472,4 +472,36 @@ test_expect_success POSIXPERM 'git reflog expire honors core.sharedRepository' '
esac
'
+test_expect_success SYMLINKS 'symref transaction supports symlinks' '
+ test_when_finished "git symbolic-ref -d TESTSYMREFONE" &&
+ git update-ref refs/heads/new @ &&
+ test_config core.prefersymlinkrefs true &&
+ cat >stdin <<-EOF &&
+ start
+ create TESTSYMREFONE ref:refs/heads/new
+ prepare
+ commit
+ EOF
+ git update-ref --no-deref --stdin <stdin &&
+ test_path_is_symlink .git/TESTSYMREFONE &&
+ test "$(test_readlink .git/TESTSYMREFONE)" = refs/heads/new
+'
+
+test_expect_success 'symref transaction supports false symlink config' '
+ test_when_finished "git symbolic-ref -d TESTSYMREFONE" &&
+ git update-ref refs/heads/new @ &&
+ test_config core.prefersymlinkrefs false &&
+ cat >stdin <<-EOF &&
+ start
+ create TESTSYMREFONE ref:refs/heads/new
+ prepare
+ commit
+ EOF
+ git update-ref --no-deref --stdin <stdin &&
+ test_path_is_file .git/TESTSYMREFONE &&
+ git symbolic-ref TESTSYMREFONE >actual &&
+ echo refs/heads/new >expect &&
+ test_cmp expect actual
+'
+
test_done
@@ -1758,6 +1758,61 @@ do
test_must_fail git rev-parse --verify -q $b
'
+ test_expect_success "stdin ${type} create symref fails without --no-deref" '
+ create_stdin_buf ${type} "create refs/heads/symref" "ref:$a" &&
+ test_must_fail git update-ref --stdin ${type} <stdin 2>err &&
+ grep "fatal: create refs/heads/symref: cannot create symrefs in deref mode" err
+ '
+
+ test_expect_success "stdin ${type} create symref fails with too many arguments" '
+ create_stdin_buf ${type} "create refs/heads/symref" "ref:$a" "ref:$a" >stdin &&
+ test_must_fail git update-ref --stdin ${type} --no-deref <stdin 2>err &&
+ if test "$type" = "-z"
+ then
+ grep "fatal: unknown command: ref:$a" err
+ else
+ grep "fatal: create refs/heads/symref: extra input: ref:$a" err
+ fi
+ '
+
+ test_expect_success "stdin ${type} create symref ref works" '
+ test_when_finished "git symbolic-ref -d refs/heads/symref" &&
+ create_stdin_buf ${type} "create refs/heads/symref" "ref:$a" >stdin &&
+ git update-ref --stdin ${type} --no-deref <stdin &&
+ git symbolic-ref refs/heads/symref >expect &&
+ echo $a >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "stdin ${type} create dangling symref ref works" '
+ test_when_finished "git symbolic-ref -d refs/heads/symref" &&
+ create_stdin_buf ${type} "create refs/heads/symref" "ref:refs/heads/unkown" >stdin &&
+ git update-ref --stdin ${type} --no-deref <stdin &&
+ git symbolic-ref refs/heads/symref >expect &&
+ echo refs/heads/unkown >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "stdin ${type} create symref does not create reflogs by default" '
+ test_when_finished "git symbolic-ref -d refs/symref" &&
+ create_stdin_buf ${type} "create refs/symref" "ref:$a" >stdin &&
+ git update-ref --stdin ${type} --no-deref <stdin &&
+ git symbolic-ref refs/symref >expect &&
+ echo $a >actual &&
+ test_cmp expect actual &&
+ test_must_fail git reflog exists refs/symref
+ '
+
+ test_expect_success "stdin ${type} create symref reflogs with --create-reflog" '
+ test_when_finished "git symbolic-ref -d refs/heads/symref" &&
+ create_stdin_buf ${type} "create refs/heads/symref" "ref:$a" >stdin &&
+ git update-ref --create-reflog --stdin ${type} --no-deref <stdin &&
+ git symbolic-ref refs/heads/symref >expect &&
+ echo $a >actual &&
+ test_cmp expect actual &&
+ git reflog exists refs/heads/symref
+ '
+
done
test_done