summaryrefslogtreecommitdiff
path: root/refs.c
diff options
context:
space:
mode:
Diffstat (limited to 'refs.c')
-rw-r--r--refs.c552
1 files changed, 400 insertions, 152 deletions
diff --git a/refs.c b/refs.c
index 5f729ed412..998d5c3565 100644
--- a/refs.c
+++ b/refs.c
@@ -3,6 +3,7 @@
*/
#define USE_THE_REPOSITORY_VARIABLE
+#define DISABLE_SIGN_COMPARE_WARNINGS
#include "git-compat-util.h"
#include "advice.h"
@@ -30,6 +31,7 @@
#include "date.h"
#include "commit.h"
#include "wildmatch.h"
+#include "ident.h"
/*
* List of all available backends
@@ -318,9 +320,10 @@ int check_refname_format(const char *refname, int flags)
return check_or_sanitize_refname(refname, flags, NULL);
}
-int refs_fsck(struct ref_store *refs, struct fsck_options *o)
+int refs_fsck(struct ref_store *refs, struct fsck_options *o,
+ struct worktree *wt)
{
- return refs->be->fsck(refs, o);
+ return refs->be->fsck(refs, o, wt);
}
void sanitize_refname_component(const char *refname, struct strbuf *out)
@@ -661,7 +664,8 @@ char *repo_default_branch_name(struct repository *r, int quiet)
if (!ret) {
ret = xstrdup("master");
if (!quiet)
- advise(_(default_branch_name_advice), ret);
+ advise_if_enabled(ADVICE_DEFAULT_BRANCH_NAME,
+ _(default_branch_name_advice), ret);
}
full_ref = xstrfmt("refs/heads/%s", ret);
@@ -697,6 +701,53 @@ static char *substitute_branch_name(struct repository *r,
return NULL;
}
+void copy_branchname(struct strbuf *sb, const char *name, unsigned allowed)
+{
+ int len = strlen(name);
+ struct interpret_branch_name_options options = {
+ .allowed = allowed
+ };
+ int used = repo_interpret_branch_name(the_repository, name, len, sb,
+ &options);
+
+ if (used < 0)
+ used = 0;
+ strbuf_add(sb, name + used, len - used);
+}
+
+int check_branch_ref(struct strbuf *sb, const char *name)
+{
+ if (startup_info->have_repository)
+ copy_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
+ else
+ strbuf_addstr(sb, name);
+
+ /*
+ * This splice must be done even if we end up rejecting the
+ * name; builtin/branch.c::copy_or_rename_branch() still wants
+ * to see what the name expanded to so that "branch -m" can be
+ * used as a tool to correct earlier mistakes.
+ */
+ strbuf_splice(sb, 0, 0, "refs/heads/", 11);
+
+ if (*name == '-' ||
+ !strcmp(sb->buf, "refs/heads/HEAD"))
+ return -1;
+
+ return check_refname_format(sb->buf, 0);
+}
+
+int check_tag_ref(struct strbuf *sb, const char *name)
+{
+ if (name[0] == '-' || !strcmp(name, "HEAD"))
+ return -1;
+
+ strbuf_reset(sb);
+ strbuf_addf(sb, "refs/tags/%s", name);
+
+ return check_refname_format(sb->buf, 0);
+}
+
int repo_dwim_ref(struct repository *r, const char *str, int len,
struct object_id *oid, char **ref, int nonfatal_dangling_mark)
{
@@ -918,7 +969,7 @@ int refs_delete_ref(struct ref_store *refs, const char *msg,
struct ref_transaction *transaction;
struct strbuf err = STRBUF_INIT;
- transaction = ref_store_transaction_begin(refs, &err);
+ transaction = ref_store_transaction_begin(refs, 0, &err);
if (!transaction ||
ref_transaction_delete(transaction, refname, old_oid,
NULL, flags, msg, &err) ||
@@ -1116,6 +1167,7 @@ int read_ref_at(struct ref_store *refs, const char *refname,
}
struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
+ unsigned int flags,
struct strbuf *err)
{
struct ref_transaction *tr;
@@ -1123,6 +1175,7 @@ struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
CALLOC_ARRAY(tr, 1);
tr->ref_store = refs;
+ tr->flags = flags;
return tr;
}
@@ -1148,6 +1201,7 @@ void ref_transaction_free(struct ref_transaction *transaction)
for (i = 0; i < transaction->nr; i++) {
free(transaction->updates[i]->msg);
+ free(transaction->updates[i]->committer_info);
free((char *)transaction->updates[i]->new_target);
free((char *)transaction->updates[i]->old_target);
free(transaction->updates[i]);
@@ -1162,6 +1216,7 @@ struct ref_update *ref_transaction_add_update(
const struct object_id *new_oid,
const struct object_id *old_oid,
const char *new_target, const char *old_target,
+ const char *committer_info,
const char *msg)
{
struct ref_update *update;
@@ -1186,11 +1241,44 @@ struct ref_update *ref_transaction_add_update(
oidcpy(&update->new_oid, new_oid);
if ((flags & REF_HAVE_OLD) && old_oid)
oidcpy(&update->old_oid, old_oid);
+ if (!(flags & REF_SKIP_CREATE_REFLOG)) {
+ update->committer_info = xstrdup_or_null(committer_info);
+ update->msg = normalize_reflog_message(msg);
+ }
- update->msg = normalize_reflog_message(msg);
return update;
}
+static int transaction_refname_valid(const char *refname,
+ const struct object_id *new_oid,
+ unsigned int flags, struct strbuf *err)
+{
+ if (flags & REF_SKIP_REFNAME_VERIFICATION)
+ return 1;
+
+ if (is_pseudo_ref(refname)) {
+ const char *refusal_msg;
+ if (flags & REF_LOG_ONLY)
+ refusal_msg = _("refusing to update reflog for pseudoref '%s'");
+ else
+ refusal_msg = _("refusing to update pseudoref '%s'");
+ strbuf_addf(err, refusal_msg, refname);
+ return 0;
+ } else if ((new_oid && !is_null_oid(new_oid)) ?
+ check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
+ !refname_is_safe(refname)) {
+ const char *refusal_msg;
+ if (flags & REF_LOG_ONLY)
+ refusal_msg = _("refusing to update reflog with bad name '%s'");
+ else
+ refusal_msg = _("refusing to update ref with bad name '%s'");
+ strbuf_addf(err, refusal_msg, refname);
+ return 0;
+ }
+
+ return 1;
+}
+
int ref_transaction_update(struct ref_transaction *transaction,
const char *refname,
const struct object_id *new_oid,
@@ -1208,21 +1296,8 @@ int ref_transaction_update(struct ref_transaction *transaction,
return -1;
}
- if (!(flags & REF_SKIP_REFNAME_VERIFICATION) &&
- ((new_oid && !is_null_oid(new_oid)) ?
- check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
- !refname_is_safe(refname))) {
- strbuf_addf(err, _("refusing to update ref with bad name '%s'"),
- refname);
- return -1;
- }
-
- if (!(flags & REF_SKIP_REFNAME_VERIFICATION) &&
- is_pseudo_ref(refname)) {
- strbuf_addf(err, _("refusing to update pseudoref '%s'"),
- refname);
+ if (!transaction_refname_valid(refname, new_oid, flags, err))
return -1;
- }
if (flags & ~REF_TRANSACTION_UPDATE_ALLOWED_FLAGS)
BUG("illegal flags 0x%x passed to ref_transaction_update()", flags);
@@ -1239,7 +1314,53 @@ int ref_transaction_update(struct ref_transaction *transaction,
ref_transaction_add_update(transaction, refname, flags,
new_oid, old_oid, new_target,
- old_target, msg);
+ old_target, NULL, msg);
+
+ return 0;
+}
+
+/*
+ * Similar to`ref_transaction_update`, but this function is only for adding
+ * a reflog update. Supports providing custom committer information. The index
+ * field can be utiltized to order updates as desired. When not used, the
+ * updates default to being ordered by refname.
+ */
+static int ref_transaction_update_reflog(struct ref_transaction *transaction,
+ const char *refname,
+ const struct object_id *new_oid,
+ const struct object_id *old_oid,
+ const char *committer_info,
+ unsigned int flags,
+ const char *msg,
+ uint64_t index,
+ struct strbuf *err)
+{
+ struct ref_update *update;
+
+ assert(err);
+
+ flags |= REF_LOG_ONLY | REF_FORCE_CREATE_REFLOG | REF_NO_DEREF;
+
+ if (!transaction_refname_valid(refname, new_oid, flags, err))
+ return -1;
+
+ update = ref_transaction_add_update(transaction, refname, flags,
+ new_oid, old_oid, NULL, NULL,
+ committer_info, msg);
+ /*
+ * While we do set the old_oid value, we unset the flag to skip
+ * old_oid verification which only makes sense for refs.
+ */
+ update->flags &= ~REF_HAVE_OLD;
+ update->index = index;
+
+ /*
+ * Reference backends may need to know the max index to optimize
+ * their writes. So we store the max_index on the transaction level.
+ */
+ if (index > transaction->max_index)
+ transaction->max_index = index;
+
return 0;
}
@@ -1257,7 +1378,7 @@ int ref_transaction_create(struct ref_transaction *transaction,
return 1;
}
return ref_transaction_update(transaction, refname, new_oid,
- null_oid(), new_target, NULL, flags,
+ null_oid(the_hash_algo), new_target, NULL, flags,
msg, err);
}
@@ -1276,7 +1397,7 @@ int ref_transaction_delete(struct ref_transaction *transaction,
if (old_target && !(flags & REF_NO_DEREF))
BUG("delete cannot operate on symrefs with deref mode");
return ref_transaction_update(transaction, refname,
- null_oid(), old_oid,
+ null_oid(the_hash_algo), old_oid,
NULL, old_target, flags,
msg, err);
}
@@ -1309,7 +1430,7 @@ int refs_update_ref(struct ref_store *refs, const char *msg,
struct strbuf err = STRBUF_INIT;
int ret = 0;
- t = ref_store_transaction_begin(refs, &err);
+ t = ref_store_transaction_begin(refs, 0, &err);
if (!t ||
ref_transaction_update(t, refname, new_oid, old_oid, NULL, NULL,
flags, msg, &err) ||
@@ -1579,6 +1700,24 @@ struct ref_iterator *refs_ref_iterator_begin(
enum do_for_each_ref_flags flags)
{
struct ref_iterator *iter;
+ struct strvec normalized_exclude_patterns = STRVEC_INIT;
+
+ if (exclude_patterns) {
+ for (size_t i = 0; exclude_patterns[i]; i++) {
+ const char *pattern = exclude_patterns[i];
+ size_t len = strlen(pattern);
+ if (!len)
+ continue;
+
+ if (pattern[len - 1] == '/')
+ strvec_push(&normalized_exclude_patterns, pattern);
+ else
+ strvec_pushf(&normalized_exclude_patterns, "%s/",
+ pattern);
+ }
+
+ exclude_patterns = normalized_exclude_patterns.v;
+ }
if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) {
static int ref_paranoia = -1;
@@ -1599,6 +1738,8 @@ struct ref_iterator *refs_ref_iterator_begin(
if (trim)
iter = prefix_ref_iterator_begin(iter, "", trim);
+ strvec_clear(&normalized_exclude_patterns);
+
return iter;
}
@@ -1788,7 +1929,7 @@ static int refs_read_special_head(struct ref_store *ref_store,
}
result = parse_loose_ref_contents(ref_store->repo->hash_algo, content.buf,
- oid, referent, type, failure_errno);
+ oid, referent, type, NULL, failure_errno);
done:
strbuf_release(&full_path);
@@ -2034,13 +2175,13 @@ struct ref_store *repo_get_submodule_ref_store(struct repository *repo,
if (!is_nonbare_repository_dir(&submodule_sb))
goto done;
- if (submodule_to_gitdir(&submodule_sb, submodule))
+ if (submodule_to_gitdir(repo, &submodule_sb, submodule))
goto done;
subrepo = xmalloc(sizeof(*subrepo));
if (repo_submodule_init(subrepo, repo, submodule,
- null_oid())) {
+ null_oid(the_hash_algo))) {
free(subrepo);
goto done;
}
@@ -2072,8 +2213,8 @@ struct ref_store *get_worktree_ref_store(const struct worktree *wt)
if (wt->id) {
struct strbuf common_path = STRBUF_INIT;
- strbuf_git_common_path(&common_path, wt->repo,
- "worktrees/%s", wt->id);
+ repo_common_path_append(wt->repo, &common_path,
+ "worktrees/%s", wt->id);
refs = ref_store_init(wt->repo, wt->repo->ref_storage_format,
common_path.buf, REF_STORE_ALL_CAPS);
strbuf_release(&common_path);
@@ -2116,19 +2257,53 @@ int peel_iterated_oid(struct repository *r, const struct object_id *base, struct
int refs_update_symref(struct ref_store *refs, const char *ref,
const char *target, const char *logmsg)
{
+ return refs_update_symref_extended(refs, ref, target, logmsg, NULL, 0);
+}
+
+int refs_update_symref_extended(struct ref_store *refs, const char *ref,
+ const char *target, const char *logmsg,
+ struct strbuf *referent, int create_only)
+{
struct ref_transaction *transaction;
struct strbuf err = STRBUF_INIT;
- int ret = 0;
+ int ret = 0, prepret = 0;
- transaction = ref_store_transaction_begin(refs, &err);
- if (!transaction ||
- ref_transaction_update(transaction, ref, NULL, NULL,
- target, NULL, REF_NO_DEREF,
- logmsg, &err) ||
- ref_transaction_commit(transaction, &err)) {
+ transaction = ref_store_transaction_begin(refs, 0, &err);
+ if (!transaction) {
+ error_return:
ret = error("%s", err.buf);
+ goto cleanup;
+ }
+ if (create_only) {
+ if (ref_transaction_create(transaction, ref, NULL, target,
+ REF_NO_DEREF, logmsg, &err))
+ goto error_return;
+ prepret = ref_transaction_prepare(transaction, &err);
+ if (prepret && prepret != TRANSACTION_CREATE_EXISTS)
+ goto error_return;
+ } else {
+ if (ref_transaction_update(transaction, ref, NULL, NULL,
+ target, NULL, REF_NO_DEREF,
+ logmsg, &err) ||
+ ref_transaction_prepare(transaction, &err))
+ goto error_return;
+ }
+
+ if (referent && refs_read_symbolic_ref(refs, ref, referent) == NOT_A_SYMREF) {
+ struct object_id oid;
+ if (!refs_read_ref(refs, ref, &oid)) {
+ strbuf_addstr(referent, oid_to_hex(&oid));
+ ret = NOT_A_SYMREF;
+ }
}
+ if (prepret == TRANSACTION_CREATE_EXISTS)
+ goto cleanup;
+
+ if (ref_transaction_commit(transaction, &err))
+ goto error_return;
+
+cleanup:
strbuf_release(&err);
if (transaction)
ref_transaction_free(transaction);
@@ -2185,17 +2360,20 @@ static int run_transaction_hook(struct ref_transaction *transaction,
for (i = 0; i < transaction->nr; i++) {
struct ref_update *update = transaction->updates[i];
+ if (update->flags & REF_LOG_ONLY)
+ continue;
+
strbuf_reset(&buf);
if (!(update->flags & REF_HAVE_OLD))
- strbuf_addf(&buf, "%s ", oid_to_hex(null_oid()));
+ strbuf_addf(&buf, "%s ", oid_to_hex(null_oid(the_hash_algo)));
else if (update->old_target)
strbuf_addf(&buf, "ref:%s ", update->old_target);
else
strbuf_addf(&buf, "%s ", oid_to_hex(&update->old_oid));
if (!(update->flags & REF_HAVE_NEW))
- strbuf_addf(&buf, "%s ", oid_to_hex(null_oid()));
+ strbuf_addf(&buf, "%s ", oid_to_hex(null_oid(the_hash_algo)));
else if (update->new_target)
strbuf_addf(&buf, "ref:%s ", update->new_target);
else
@@ -2313,25 +2491,23 @@ int ref_transaction_commit(struct ref_transaction *transaction,
}
ret = refs->be->transaction_finish(refs, transaction, err);
- if (!ret)
+ if (!ret && !(transaction->flags & REF_TRANSACTION_FLAG_INITIAL))
run_transaction_hook(transaction, "committed");
return ret;
}
-int refs_verify_refname_available(struct ref_store *refs,
- const char *refname,
- const struct string_list *extras,
- const struct string_list *skip,
- struct strbuf *err)
+int refs_verify_refnames_available(struct ref_store *refs,
+ const struct string_list *refnames,
+ const struct string_list *extras,
+ const struct string_list *skip,
+ unsigned int initial_transaction,
+ struct strbuf *err)
{
- const char *slash;
- const char *extra_refname;
struct strbuf dirname = STRBUF_INIT;
struct strbuf referent = STRBUF_INIT;
- struct object_id oid;
- unsigned int type;
- struct ref_iterator *iter;
- int ok;
+ struct string_list_item *item;
+ struct ref_iterator *iter = NULL;
+ struct strset dirnames;
int ret = -1;
/*
@@ -2341,80 +2517,130 @@ int refs_verify_refname_available(struct ref_store *refs,
assert(err);
- strbuf_grow(&dirname, strlen(refname) + 1);
- for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
- /*
- * Just saying "Is a directory" when we e.g. can't
- * lock some multi-level ref isn't very informative,
- * the user won't be told *what* is a directory, so
- * let's not use strerror() below.
- */
- int ignore_errno;
- /* Expand dirname to the new prefix, not including the trailing slash: */
- strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len);
+ strset_init(&dirnames);
+
+ for_each_string_list_item(item, refnames) {
+ const char *refname = item->string;
+ const char *extra_refname;
+ struct object_id oid;
+ unsigned int type;
+ const char *slash;
+
+ strbuf_reset(&dirname);
+
+ for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
+ /*
+ * Just saying "Is a directory" when we e.g. can't
+ * lock some multi-level ref isn't very informative,
+ * the user won't be told *what* is a directory, so
+ * let's not use strerror() below.
+ */
+ int ignore_errno;
+
+ /* Expand dirname to the new prefix, not including the trailing slash: */
+ strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len);
+
+ /*
+ * We are still at a leading dir of the refname (e.g.,
+ * "refs/foo"; if there is a reference with that name,
+ * it is a conflict, *unless* it is in skip.
+ */
+ if (skip && string_list_has_string(skip, dirname.buf))
+ continue;
+
+ /*
+ * If we've already seen the directory we don't need to
+ * process it again. Skip it to avoid checking checking
+ * common prefixes like "refs/heads/" repeatedly.
+ */
+ if (!strset_add(&dirnames, dirname.buf))
+ continue;
+
+ if (!initial_transaction &&
+ !refs_read_raw_ref(refs, dirname.buf, &oid, &referent,
+ &type, &ignore_errno)) {
+ strbuf_addf(err, _("'%s' exists; cannot create '%s'"),
+ dirname.buf, refname);
+ goto cleanup;
+ }
+
+ if (extras && string_list_has_string(extras, dirname.buf)) {
+ strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"),
+ refname, dirname.buf);
+ goto cleanup;
+ }
+ }
/*
- * We are still at a leading dir of the refname (e.g.,
- * "refs/foo"; if there is a reference with that name,
- * it is a conflict, *unless* it is in skip.
+ * We are at the leaf of our refname (e.g., "refs/foo/bar").
+ * There is no point in searching for a reference with that
+ * name, because a refname isn't considered to conflict with
+ * itself. But we still need to check for references whose
+ * names are in the "refs/foo/bar/" namespace, because they
+ * *do* conflict.
*/
- if (skip && string_list_has_string(skip, dirname.buf))
- continue;
+ strbuf_addstr(&dirname, refname + dirname.len);
+ strbuf_addch(&dirname, '/');
- if (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent,
- &type, &ignore_errno)) {
- strbuf_addf(err, _("'%s' exists; cannot create '%s'"),
- dirname.buf, refname);
- goto cleanup;
+ if (!initial_transaction) {
+ int ok;
+
+ if (!iter) {
+ iter = refs_ref_iterator_begin(refs, dirname.buf, NULL, 0,
+ DO_FOR_EACH_INCLUDE_BROKEN);
+ } else if (ref_iterator_seek(iter, dirname.buf) < 0) {
+ goto cleanup;
+ }
+
+ while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
+ if (skip &&
+ string_list_has_string(skip, iter->refname))
+ continue;
+
+ strbuf_addf(err, _("'%s' exists; cannot create '%s'"),
+ iter->refname, refname);
+ goto cleanup;
+ }
+
+ if (ok != ITER_DONE)
+ BUG("error while iterating over references");
}
- if (extras && string_list_has_string(extras, dirname.buf)) {
+ extra_refname = find_descendant_ref(dirname.buf, extras, skip);
+ if (extra_refname) {
strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"),
- refname, dirname.buf);
+ refname, extra_refname);
goto cleanup;
}
}
- /*
- * We are at the leaf of our refname (e.g., "refs/foo/bar").
- * There is no point in searching for a reference with that
- * name, because a refname isn't considered to conflict with
- * itself. But we still need to check for references whose
- * names are in the "refs/foo/bar/" namespace, because they
- * *do* conflict.
- */
- strbuf_addstr(&dirname, refname + dirname.len);
- strbuf_addch(&dirname, '/');
-
- iter = refs_ref_iterator_begin(refs, dirname.buf, NULL, 0,
- DO_FOR_EACH_INCLUDE_BROKEN);
- while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
- if (skip &&
- string_list_has_string(skip, iter->refname))
- continue;
-
- strbuf_addf(err, _("'%s' exists; cannot create '%s'"),
- iter->refname, refname);
- ref_iterator_abort(iter);
- goto cleanup;
- }
-
- if (ok != ITER_DONE)
- BUG("error while iterating over references");
-
- extra_refname = find_descendant_ref(dirname.buf, extras, skip);
- if (extra_refname)
- strbuf_addf(err, _("cannot process '%s' and '%s' at the same time"),
- refname, extra_refname);
- else
- ret = 0;
+ ret = 0;
cleanup:
strbuf_release(&referent);
strbuf_release(&dirname);
+ strset_clear(&dirnames);
+ ref_iterator_free(iter);
return ret;
}
+int refs_verify_refname_available(struct ref_store *refs,
+ const char *refname,
+ const struct string_list *extras,
+ const struct string_list *skip,
+ unsigned int initial_transaction,
+ struct strbuf *err)
+{
+ struct string_list_item item = { .string = (char *) refname };
+ struct string_list refnames = {
+ .items = &item,
+ .nr = 1,
+ };
+
+ return refs_verify_refnames_available(refs, &refnames, extras, skip,
+ initial_transaction, err);
+}
+
struct do_for_each_reflog_help {
each_reflog_fn *fn;
void *cb_data;
@@ -2484,14 +2710,6 @@ int refs_reflog_expire(struct ref_store *refs,
cleanup_fn, policy_cb_data);
}
-int initial_ref_transaction_commit(struct ref_transaction *transaction,
- struct strbuf *err)
-{
- struct ref_store *refs = transaction->ref_store;
-
- return refs->be->initial_transaction_commit(refs, transaction, err);
-}
-
void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,
ref_transaction_for_each_queued_update_fn cb,
void *cb_data)
@@ -2527,7 +2745,7 @@ int refs_delete_refs(struct ref_store *refs, const char *logmsg,
* individual updates can't fail, so we can pack all of the
* updates into a single transaction.
*/
- transaction = ref_store_transaction_begin(refs, &err);
+ transaction = ref_store_transaction_begin(refs, 0, &err);
if (!transaction) {
ret = error("%s", err.buf);
goto out;
@@ -2625,6 +2843,7 @@ struct migration_data {
struct ref_store *old_refs;
struct ref_transaction *transaction;
struct strbuf *errbuf;
+ struct strbuf sb;
};
static int migrate_one_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
@@ -2639,7 +2858,7 @@ static int migrate_one_ref(const char *refname, const char *referent UNUSED, con
if (ret < 0)
goto done;
- ret = ref_transaction_update(data->transaction, refname, NULL, null_oid(),
+ ret = ref_transaction_update(data->transaction, refname, NULL, null_oid(the_hash_algo),
symref_target.buf, NULL,
REF_SKIP_CREATE_REFLOG | REF_NO_DEREF, NULL, data->errbuf);
if (ret < 0)
@@ -2657,6 +2876,52 @@ done:
return ret;
}
+struct reflog_migration_data {
+ uint64_t index;
+ const char *refname;
+ struct ref_store *old_refs;
+ struct ref_transaction *transaction;
+ struct strbuf *errbuf;
+ struct strbuf *sb;
+};
+
+static int migrate_one_reflog_entry(struct object_id *old_oid,
+ struct object_id *new_oid,
+ const char *committer,
+ timestamp_t timestamp, int tz,
+ const char *msg, void *cb_data)
+{
+ struct reflog_migration_data *data = cb_data;
+ const char *date;
+ int ret;
+
+ date = show_date(timestamp, tz, DATE_MODE(NORMAL));
+ strbuf_reset(data->sb);
+ /* committer contains name and email */
+ strbuf_addstr(data->sb, fmt_ident("", committer, WANT_BLANK_IDENT, date, 0));
+
+ ret = ref_transaction_update_reflog(data->transaction, data->refname,
+ new_oid, old_oid, data->sb->buf,
+ REF_HAVE_NEW | REF_HAVE_OLD, msg,
+ data->index++, data->errbuf);
+ return ret;
+}
+
+static int migrate_one_reflog(const char *refname, void *cb_data)
+{
+ struct migration_data *migration_data = cb_data;
+ struct reflog_migration_data data = {
+ .refname = refname,
+ .old_refs = migration_data->old_refs,
+ .transaction = migration_data->transaction,
+ .errbuf = migration_data->errbuf,
+ .sb = &migration_data->sb,
+ };
+
+ return refs_for_each_reflog_ent(migration_data->old_refs, refname,
+ migrate_one_reflog_entry, &data);
+}
+
static int move_files(const char *from_path, const char *to_path, struct strbuf *errbuf)
{
struct strbuf from_buf = STRBUF_INIT, to_buf = STRBUF_INIT;
@@ -2723,13 +2988,6 @@ done:
return ret;
}
-static int count_reflogs(const char *reflog UNUSED, void *payload)
-{
- size_t *reflog_count = payload;
- (*reflog_count)++;
- return 0;
-}
-
static int has_worktrees(void)
{
struct worktree **worktrees = get_worktrees();
@@ -2754,8 +3012,9 @@ int repo_migrate_ref_storage_format(struct repository *repo,
struct ref_store *old_refs = NULL, *new_refs = NULL;
struct ref_transaction *transaction = NULL;
struct strbuf new_gitdir = STRBUF_INIT;
- struct migration_data data;
- size_t reflog_count = 0;
+ struct migration_data data = {
+ .sb = STRBUF_INIT,
+ };
int did_migrate_refs = 0;
int ret;
@@ -2768,21 +3027,6 @@ int repo_migrate_ref_storage_format(struct repository *repo,
old_refs = get_main_ref_store(repo);
/*
- * We do not have any interfaces that would allow us to write many
- * reflog entries. Once we have them we can remove this restriction.
- */
- if (refs_for_each_reflog(old_refs, count_reflogs, &reflog_count) < 0) {
- strbuf_addstr(errbuf, "cannot count reflogs");
- ret = -1;
- goto done;
- }
- if (reflog_count) {
- strbuf_addstr(errbuf, "migrating reflogs is not supported yet");
- ret = -1;
- goto done;
- }
-
- /*
* Worktrees complicate the migration because every worktree has a
* separate ref storage. While it should be feasible to implement, this
* is pushed out to a future iteration.
@@ -2807,17 +3051,21 @@ int repo_migrate_ref_storage_format(struct repository *repo,
* This operation is safe as we do not yet modify the main
* repository.
*
- * 3. If we're in dry-run mode then we are done and can hand over the
+ * 3. Enumerate all reflogs and write them into the new ref storage.
+ * This operation is safe as we do not yet modify the main
+ * repository.
+ *
+ * 4. If we're in dry-run mode then we are done and can hand over the
* directory to the caller for inspection. If not, we now start
* with the destructive part.
*
- * 4. Delete the old ref storage from disk. As we have a copy of refs
+ * 5. Delete the old ref storage from disk. As we have a copy of refs
* in the new ref storage it's okay(ish) if we now get interrupted
* as there is an equivalent copy of all refs available.
*
- * 5. Move the new ref storage files into place.
+ * 6. Move the new ref storage files into place.
*
- * 6. Change the repository format to the new ref format.
+ * 7. Change the repository format to the new ref format.
*/
strbuf_addf(&new_gitdir, "%s/%s", old_refs->gitdir, "ref_migration.XXXXXX");
if (!mkdtemp(new_gitdir.buf)) {
@@ -2833,7 +3081,8 @@ int repo_migrate_ref_storage_format(struct repository *repo,
if (ret < 0)
goto done;
- transaction = ref_store_transaction_begin(new_refs, errbuf);
+ transaction = ref_store_transaction_begin(new_refs, REF_TRANSACTION_FLAG_INITIAL,
+ errbuf);
if (!transaction)
goto done;
@@ -2858,13 +3107,12 @@ int repo_migrate_ref_storage_format(struct repository *repo,
if (ret < 0)
goto done;
- /*
- * TODO: we might want to migrate to `initial_ref_transaction_commit()`
- * here, which is more efficient for the files backend because it would
- * write new refs into the packed-refs file directly. At this point,
- * the files backend doesn't handle pseudo-refs and symrefs correctly
- * though, so this requires some more work.
- */
+ if (!(flags & REPO_MIGRATE_REF_STORAGE_FORMAT_SKIP_REFLOG)) {
+ ret = refs_for_each_reflog(old_refs, migrate_one_reflog, &data);
+ if (ret < 0)
+ goto done;
+ }
+
ret = ref_transaction_commit(transaction, errbuf);
if (ret < 0)
goto done;
@@ -2940,6 +3188,7 @@ done:
}
ref_transaction_free(transaction);
strbuf_release(&new_gitdir);
+ strbuf_release(&data.sb);
return ret;
}
@@ -2948,4 +3197,3 @@ int ref_update_expects_existing_old_ref(struct ref_update *update)
return (update->flags & REF_HAVE_OLD) &&
(!is_null_oid(&update->old_oid) || update->old_target);
}
-