From b07bdd3472886cbdd412ee3e7489273522c43133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20Lindstr=C3=B6m?= Date: Fri, 23 May 2014 12:28:43 +0200 Subject: [PATCH 1/3] remote rm: delete remote configuration as the last MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When removing a remote, delete the remote-tracking branches before deleting the remote configuration. This way, if the operation fails or is aborted while deleting the remote-tracking branches, the command can be rerun to complete the operation. Signed-off-by: Jens Lindström Signed-off-by: Junio C Hamano --- builtin/remote.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/builtin/remote.c b/builtin/remote.c index b3ab4cf8f6..84802cd0a1 100644 --- a/builtin/remote.c +++ b/builtin/remote.c @@ -789,10 +789,6 @@ static int rm(int argc, const char **argv) known_remotes.to_delete = remote; for_each_remote(add_known_remote, &known_remotes); - strbuf_addf(&buf, "remote.%s", remote->name); - if (git_config_rename_section(buf.buf, NULL) < 1) - return error(_("Could not remove config section '%s'"), buf.buf); - read_branches(); for (i = 0; i < branch_list.nr; i++) { struct string_list_item *item = branch_list.items + i; @@ -837,6 +833,12 @@ static int rm(int argc, const char **argv) } string_list_clear(&skipped, 0); + if (!result) { + strbuf_addf(&buf, "remote.%s", remote->name); + if (git_config_rename_section(buf.buf, NULL) < 1) + return error(_("Could not remove config section '%s'"), buf.buf); + } + return result; } From c9e768bb77762765f739fffb5c09d0888f3de20e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20Lindstr=C3=B6m?= Date: Fri, 23 May 2014 12:29:45 +0200 Subject: [PATCH 2/3] remote: repack packed-refs once when deleting multiple refs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When 'git remote rm' or 'git remote prune' were used in a repository with many refs, and needed to delete many remote-tracking refs, a lot of time was spent deleting those refs since for each deleted ref, repack_without_refs() was called to rewrite packed-refs without just that deleted ref. To avoid this, call repack_without_refs() first to repack without all the refs that will be deleted, before calling delete_ref() to delete each one completely. The call to repack_without_ref() in delete_ref() then becomes a no-op, since packed-refs already won't contain any of the deleted refs. Signed-off-by: Jens Lindström Signed-off-by: Junio C Hamano --- builtin/remote.c | 20 ++++++++++++++++++-- refs.c | 2 +- refs.h | 2 ++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/builtin/remote.c b/builtin/remote.c index 84802cd0a1..d33abe6598 100644 --- a/builtin/remote.c +++ b/builtin/remote.c @@ -749,15 +749,23 @@ static int mv(int argc, const char **argv) static int remove_branches(struct string_list *branches) { + const char **branch_names; int i, result = 0; + + branch_names = xmalloc(branches->nr * sizeof(*branch_names)); + for (i = 0; i < branches->nr; i++) + branch_names[i] = branches->items[i].string; + result |= repack_without_refs(branch_names, branches->nr); + free(branch_names); + for (i = 0; i < branches->nr; i++) { struct string_list_item *item = branches->items + i; const char *refname = item->string; - unsigned char *sha1 = item->util; - if (delete_ref(refname, sha1, 0)) + if (delete_ref(refname, NULL, 0)) result |= error(_("Could not remove branch %s"), refname); } + return result; } @@ -1305,6 +1313,7 @@ static int prune_remote(const char *remote, int dry_run) { int result = 0, i; struct ref_states states; + const char **delete_refs; const char *dangling_msg = dry_run ? _(" %s will become dangling!") : _(" %s has become dangling!"); @@ -1318,6 +1327,13 @@ static int prune_remote(const char *remote, int dry_run) states.remote->url_nr ? states.remote->url[0] : _("(no URL)")); + + delete_refs = xmalloc(states.stale.nr * sizeof(*delete_refs)); + for (i = 0; i < states.stale.nr; i++) + delete_refs[i] = states.stale.items[i].util; + if (!dry_run) + result |= repack_without_refs(delete_refs, states.stale.nr); + free(delete_refs); } for (i = 0; i < states.stale.nr; i++) { diff --git a/refs.c b/refs.c index 28d5eca8ea..262c1c26cb 100644 --- a/refs.c +++ b/refs.c @@ -2431,7 +2431,7 @@ static int curate_packed_ref_fn(struct ref_entry *entry, void *cb_data) return 0; } -static int repack_without_refs(const char **refnames, int n) +int repack_without_refs(const char **refnames, int n) { struct ref_dir *packed; struct string_list refs_to_delete = STRING_LIST_INIT_DUP; diff --git a/refs.h b/refs.h index 87a1a79ad6..f287c7aa65 100644 --- a/refs.h +++ b/refs.h @@ -132,6 +132,8 @@ extern void rollback_packed_refs(void); */ int pack_refs(unsigned int flags); +extern int repack_without_refs(const char **refnames, int n); + extern int ref_exists(const char *); /* From e6bea66db6229a0975ebba510a1d2827bf09cb7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20Lindstr=C3=B6m?= Date: Fri, 23 May 2014 12:30:25 +0200 Subject: [PATCH 3/3] remote prune: optimize "dangling symref" check/warning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When 'git remote prune' was used to delete many refs in a repository with many refs, a lot of time was spent checking for (now) dangling symbolic refs pointing to the deleted ref, since warn_dangling_symref() was once per deleted ref to check all other refs in the repository. Avoid this using the new warn_dangling_symrefs() function which makes one pass over all refs and checks for all the deleted refs in one go, after they have all been deleted. Signed-off-by: Jens Lindström Signed-off-by: Junio C Hamano --- builtin/remote.c | 7 ++++++- refs.c | 19 ++++++++++++++++++- refs.h | 1 + 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/builtin/remote.c b/builtin/remote.c index d33abe6598..9b3e368983 100644 --- a/builtin/remote.c +++ b/builtin/remote.c @@ -1313,6 +1313,7 @@ static int prune_remote(const char *remote, int dry_run) { int result = 0, i; struct ref_states states; + struct string_list delete_refs_list = STRING_LIST_INIT_NODUP; const char **delete_refs; const char *dangling_msg = dry_run ? _(" %s will become dangling!") @@ -1339,6 +1340,8 @@ static int prune_remote(const char *remote, int dry_run) for (i = 0; i < states.stale.nr; i++) { const char *refname = states.stale.items[i].util; + string_list_insert(&delete_refs_list, refname); + if (!dry_run) result |= delete_ref(refname, NULL, 0); @@ -1348,9 +1351,11 @@ static int prune_remote(const char *remote, int dry_run) else printf_ln(_(" * [pruned] %s"), abbrev_ref(refname, "refs/remotes/")); - warn_dangling_symref(stdout, dangling_msg, refname); } + warn_dangling_symrefs(stdout, dangling_msg, &delete_refs_list); + string_list_clear(&delete_refs_list, 0); + free_remote_ref_states(&states); return result; } diff --git a/refs.c b/refs.c index 262c1c26cb..59fb70087a 100644 --- a/refs.c +++ b/refs.c @@ -1611,6 +1611,7 @@ int peel_ref(const char *refname, unsigned char *sha1) struct warn_if_dangling_data { FILE *fp; const char *refname; + const struct string_list *refnames; const char *msg_fmt; }; @@ -1625,8 +1626,12 @@ static int warn_if_dangling_symref(const char *refname, const unsigned char *sha return 0; resolves_to = resolve_ref_unsafe(refname, junk, 0, NULL); - if (!resolves_to || strcmp(resolves_to, d->refname)) + if (!resolves_to + || (d->refname + ? strcmp(resolves_to, d->refname) + : !string_list_has_string(d->refnames, resolves_to))) { return 0; + } fprintf(d->fp, d->msg_fmt, refname); fputc('\n', d->fp); @@ -1639,6 +1644,18 @@ void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname) data.fp = fp; data.refname = refname; + data.refnames = NULL; + data.msg_fmt = msg_fmt; + for_each_rawref(warn_if_dangling_symref, &data); +} + +void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames) +{ + struct warn_if_dangling_data data; + + data.fp = fp; + data.refname = NULL; + data.refnames = refnames; data.msg_fmt = msg_fmt; for_each_rawref(warn_if_dangling_symref, &data); } diff --git a/refs.h b/refs.h index f287c7aa65..1440acc06c 100644 --- a/refs.h +++ b/refs.h @@ -89,6 +89,7 @@ static inline const char *has_glob_specials(const char *pattern) extern int for_each_rawref(each_ref_fn, void *); extern void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname); +extern void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list* refnames); /* * Lock the packed-refs file for writing. Flags is passed to