Skip to content

Commit 9963746

Browse files
ferdinandybgitster
authored andcommitted
refs: add create_only option to refs_update_symref_extended
Allow the caller to specify that it only wants to update the symref if it does not already exist. Silently ignore the error from the transaction API if the symref already exists. Signed-off-by: Bence Ferdinandy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ed2f6f8 commit 9963746

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

builtin/remote.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
14761476
goto cleanup;
14771477
}
14781478
was_detached = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
1479-
"remote set-head", &b_local_head);
1479+
"remote set-head", &b_local_head, 0);
14801480
if (was_detached == -1) {
14811481
result |= error(_("Could not set up %s"), b_head.buf);
14821482
goto cleanup;

refs.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,26 +2116,38 @@ int peel_iterated_oid(struct repository *r, const struct object_id *base, struct
21162116
int refs_update_symref(struct ref_store *refs, const char *ref,
21172117
const char *target, const char *logmsg)
21182118
{
2119-
return refs_update_symref_extended(refs, ref, target, logmsg, NULL);
2119+
return refs_update_symref_extended(refs, ref, target, logmsg, NULL, 0);
21202120
}
21212121

21222122
int refs_update_symref_extended(struct ref_store *refs, const char *ref,
21232123
const char *target, const char *logmsg,
2124-
struct strbuf *referent)
2124+
struct strbuf *referent, int create_only)
21252125
{
21262126
struct ref_transaction *transaction;
21272127
struct strbuf err = STRBUF_INIT;
2128-
int ret = 0;
2128+
int ret = 0, prepret = 0;
21292129

21302130
transaction = ref_store_transaction_begin(refs, &err);
2131-
if (!transaction ||
2132-
ref_transaction_update(transaction, ref, NULL, NULL,
2133-
target, NULL, REF_NO_DEREF,
2134-
logmsg, &err) ||
2135-
ref_transaction_prepare(transaction, &err)) {
2131+
if (!transaction) {
2132+
error_return:
21362133
ret = error("%s", err.buf);
21372134
goto cleanup;
21382135
}
2136+
if (create_only) {
2137+
if (ref_transaction_create(transaction, ref, NULL, target,
2138+
REF_NO_DEREF, logmsg, &err))
2139+
goto error_return;
2140+
prepret = ref_transaction_prepare(transaction, &err);
2141+
if (prepret && prepret != TRANSACTION_CREATE_EXISTS)
2142+
goto error_return;
2143+
} else {
2144+
if (ref_transaction_update(transaction, ref, NULL, NULL,
2145+
target, NULL, REF_NO_DEREF,
2146+
logmsg, &err) ||
2147+
ref_transaction_prepare(transaction, &err))
2148+
goto error_return;
2149+
}
2150+
21392151
if (referent && refs_read_symbolic_ref(refs, ref, referent) == NOT_A_SYMREF) {
21402152
struct object_id oid;
21412153
if (!refs_read_ref(refs, ref, &oid)) {
@@ -2144,8 +2156,11 @@ int refs_update_symref_extended(struct ref_store *refs, const char *ref,
21442156
}
21452157
}
21462158

2159+
if (prepret == TRANSACTION_CREATE_EXISTS)
2160+
goto cleanup;
2161+
21472162
if (ref_transaction_commit(transaction, &err))
2148-
ret = error("%s", err.buf);
2163+
goto error_return;
21492164

21502165
cleanup:
21512166
strbuf_release(&err);

refs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ int refs_update_symref(struct ref_store *refs, const char *refname,
586586

587587
int refs_update_symref_extended(struct ref_store *refs, const char *refname,
588588
const char *target, const char *logmsg,
589-
struct strbuf *referent);
589+
struct strbuf *referent, int create_only);
590590

591591
enum action_on_err {
592592
UPDATE_REFS_MSG_ON_ERR,

0 commit comments

Comments
 (0)