Skip to content

Commit 066208a

Browse files
ferdinandybttaylorr
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: Taylor Blau <[email protected]>
1 parent 31d287c commit 066208a

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

builtin/remote.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
14711471
if (!refs_ref_exists(refs, b_remote_head.buf))
14721472
result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
14731473
else if (refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
1474-
"remote set-head", &b_local_head))
1474+
"remote set-head", &b_local_head, 0))
14751475
result |= error(_("Could not setup %s"), b_head.buf);
14761476
else if (opt_a)
14771477
report_set_head_auto(argv[0], head_name, &b_local_head);

refs.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,31 +2116,45 @@ 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)
21402152
refs_read_symbolic_ref(refs, ref, referent);
21412153

2154+
if (prepret == TRANSACTION_CREATE_EXISTS)
2155+
goto cleanup;
21422156
if (ref_transaction_commit(transaction, &err))
2143-
ret = error("%s", err.buf);
2157+
goto error_return;
21442158

21452159
cleanup:
21462160
strbuf_release(&err);

refs.h

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

576576
int refs_update_symref_extended(struct ref_store *refs, const char *refname,
577577
const char *target, const char *logmsg,
578-
struct strbuf *referent);
578+
struct strbuf *referent, int create_only);
579579

580580
enum action_on_err {
581581
UPDATE_REFS_MSG_ON_ERR,

0 commit comments

Comments
 (0)