Merge branch 'ps/remote-rename-fix'

"git remote rename origin upstream" failed to move origin/HEAD to
upstream/HEAD when origin/HEAD is unborn and performed other
renames extremely inefficiently, which has been corrected.

* ps/remote-rename-fix:
  builtin/remote: only iterate through refs that are to be renamed
  builtin/remote: rework how remote refs get renamed
  builtin/remote: determine whether refs need renaming early on
  builtin/remote: fix sign comparison warnings
  refs: simplify logic when migrating reflog entries
  refs: pass refname when invoking reflog entry callback
This commit is contained in:
Junio C Hamano
2025-08-21 13:46:58 -07:00
19 changed files with 372 additions and 200 deletions

64
refs.c
View File

@@ -1022,7 +1022,6 @@ int is_branch(const char *refname)
}
struct read_ref_at_cb {
const char *refname;
timestamp_t at_time;
int cnt;
int reccnt;
@@ -1052,7 +1051,8 @@ static void set_read_ref_cutoffs(struct read_ref_at_cb *cb,
*cb->cutoff_cnt = cb->reccnt;
}
static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
static int read_ref_at_ent(const char *refname,
struct object_id *ooid, struct object_id *noid,
const char *email UNUSED,
timestamp_t timestamp, int tz,
const char *message, void *cb_data)
@@ -1072,14 +1072,13 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
oidcpy(cb->oid, noid);
if (!oideq(&cb->ooid, noid))
warning(_("log for ref %s has gap after %s"),
cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
}
else if (cb->date == cb->at_time)
oidcpy(cb->oid, noid);
else if (!oideq(noid, cb->oid))
warning(_("log for ref %s unexpectedly ended on %s"),
cb->refname, show_date(cb->date, cb->tz,
DATE_MODE(RFC2822)));
refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
cb->reccnt++;
oidcpy(&cb->ooid, ooid);
oidcpy(&cb->noid, noid);
@@ -1094,7 +1093,8 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
return 0;
}
static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid,
static int read_ref_at_ent_oldest(const char *refname UNUSED,
struct object_id *ooid, struct object_id *noid,
const char *email UNUSED,
timestamp_t timestamp, int tz,
const char *message, void *cb_data)
@@ -1117,7 +1117,6 @@ int read_ref_at(struct ref_store *refs, const char *refname,
struct read_ref_at_cb cb;
memset(&cb, 0, sizeof(cb));
cb.refname = refname;
cb.at_time = at_time;
cb.cnt = cnt;
cb.msg = msg;
@@ -1840,7 +1839,13 @@ int refs_for_each_namespaced_ref(struct ref_store *refs,
int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
{
return do_for_each_ref(refs, "", NULL, fn, 0,
return refs_for_each_rawref_in(refs, "", fn, cb_data);
}
int refs_for_each_rawref_in(struct ref_store *refs, const char *prefix,
each_ref_fn fn, void *cb_data)
{
return do_for_each_ref(refs, prefix, NULL, fn, 0,
DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
}
@@ -2942,6 +2947,7 @@ struct migration_data {
struct ref_transaction *transaction;
struct strbuf *errbuf;
struct strbuf sb, name, mail;
uint64_t index;
};
static int migrate_one_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
@@ -2974,22 +2980,14 @@ 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, *name, *mail;
};
static int migrate_one_reflog_entry(struct object_id *old_oid,
static int migrate_one_reflog_entry(const char *refname,
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;
struct migration_data *data = cb_data;
struct ident_split ident;
const char *date;
int ret;
@@ -2997,17 +2995,17 @@ static int migrate_one_reflog_entry(struct object_id *old_oid,
if (split_ident_line(&ident, committer, strlen(committer)) < 0)
return -1;
strbuf_reset(data->name);
strbuf_add(data->name, ident.name_begin, ident.name_end - ident.name_begin);
strbuf_reset(data->mail);
strbuf_add(data->mail, ident.mail_begin, ident.mail_end - ident.mail_begin);
strbuf_reset(&data->name);
strbuf_add(&data->name, ident.name_begin, ident.name_end - ident.name_begin);
strbuf_reset(&data->mail);
strbuf_add(&data->mail, ident.mail_begin, ident.mail_end - ident.mail_begin);
date = show_date(timestamp, tz, DATE_MODE(NORMAL));
strbuf_reset(data->sb);
strbuf_addstr(data->sb, fmt_ident(data->name->buf, data->mail->buf, WANT_BLANK_IDENT, date, 0));
strbuf_reset(&data->sb);
strbuf_addstr(&data->sb, fmt_ident(data->name.buf, data->mail.buf, WANT_BLANK_IDENT, date, 0));
ret = ref_transaction_update_reflog(data->transaction, data->refname,
new_oid, old_oid, data->sb->buf,
ret = ref_transaction_update_reflog(data->transaction, refname,
new_oid, old_oid, data->sb.buf,
msg, data->index++, data->errbuf);
return ret;
}
@@ -3015,18 +3013,8 @@ static int migrate_one_reflog_entry(struct object_id *old_oid,
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,
.name = &migration_data->name,
.mail = &migration_data->mail,
};
return refs_for_each_reflog_ent(migration_data->old_refs, refname,
migrate_one_reflog_entry, &data);
migrate_one_reflog_entry, migration_data);
}
static int move_files(const char *from_path, const char *to_path, struct strbuf *errbuf)