From: Ronnie Sahlberg <sahlberg@google.com>
To: git@vger.kernel.org
Cc: Ronnie Sahlberg <sahlberg@google.com>
Subject: [PATCH 04/15] refs.c: use a stringlist for repack_without_refs
Date: Tue, 21 Oct 2014 13:36:49 -0700 [thread overview]
Message-ID: <1413923820-14457-5-git-send-email-sahlberg@google.com> (raw)
In-Reply-To: <1413923820-14457-1-git-send-email-sahlberg@google.com>
Change-Id: I87a1b93c4b4f4a10ad6a9f8e2bb4d53f89b28c4a
Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
builtin/remote.c | 23 ++++++++---------------
refs.c | 42 +++++++++++++++++++++---------------------
refs.h | 2 +-
3 files changed, 30 insertions(+), 37 deletions(-)
diff --git a/builtin/remote.c b/builtin/remote.c
index c25420f..6806251 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -751,7 +751,6 @@ static int mv(int argc, const char **argv)
static int remove_branches(struct string_list *branches)
{
struct strbuf err = STRBUF_INIT;
- const char **branch_names;
int i, result = 0;
if (lock_packed_refs(0)) {
@@ -763,13 +762,9 @@ static int remove_branches(struct string_list *branches)
return -1;
}
- branch_names = xmalloc(branches->nr * sizeof(*branch_names));
- for (i = 0; i < branches->nr; i++)
- branch_names[i] = branches->items[i].string;
- if (repack_without_refs(branch_names, branches->nr, &err))
+ if (repack_without_refs(branches, &err))
result |= error("%s", err.buf);
strbuf_release(&err);
- free(branch_names);
for (i = 0; i < branches->nr; i++) {
struct string_list_item *item = branches->items + i;
@@ -1327,7 +1322,6 @@ 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!")
: _(" %s has become dangling!");
@@ -1335,6 +1329,11 @@ static int prune_remote(const char *remote, int dry_run)
memset(&states, 0, sizeof(states));
get_remote_ref_states(remote, &states, GET_REF_STATES);
+ for (i = 0; i < states.stale.nr; i++) {
+ string_list_insert(&delete_refs_list,
+ states.stale.items[i].util);
+ }
+
if (states.stale.nr) {
printf_ln(_("Pruning %s"), remote);
printf_ln(_("URL: %s"),
@@ -1342,9 +1341,6 @@ static int prune_remote(const char *remote, int dry_run)
? 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) {
struct strbuf err = STRBUF_INIT;
@@ -1353,19 +1349,16 @@ static int prune_remote(const char *remote, int dry_run)
errno, &err);
result |= error("%s", err.buf);
} else
- if (repack_without_refs(delete_refs,
- states.stale.nr, &err))
+ if (repack_without_refs(&delete_refs_list,
+ &err))
result |= error("%s", err.buf);
strbuf_release(&err);
}
- free(delete_refs);
}
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);
diff --git a/refs.c b/refs.c
index 662af9b..b9c8f91 100644
--- a/refs.c
+++ b/refs.c
@@ -2663,31 +2663,32 @@ static int curate_packed_ref_fn(struct ref_entry *entry, void *cb_data)
/*
* Must be called with packed refs already locked (and sorted)
*/
-int repack_without_refs(const char **refnames, int n, struct strbuf *err)
+int repack_without_refs(struct string_list *without, struct strbuf *err)
{
struct ref_dir *packed;
struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
struct string_list_item *ref_to_delete;
- int i, ret, removed = 0;
+ int count, ret, removed = 0;
assert(err);
/* Look for a packed ref */
- for (i = 0; i < n; i++)
- if (get_packed_ref(refnames[i]))
- break;
+ count = 0;
+ for_each_string_list_item(ref_to_delete, without)
+ if (get_packed_ref(ref_to_delete->string))
+ count++;
- /* Avoid processing if we have nothing to do */
- if (i == n) {
+ /* No refname exists in packed refs */
+ if (!count) {
rollback_packed_refs();
- return 0; /* no refname exists in packed refs */
+ return 0;
}
packed = get_packed_refs(&ref_cache);
/* Remove refnames from the cache */
- for (i = 0; i < n; i++)
- if (remove_entry(packed, refnames[i]) != -1)
+ for_each_string_list_item(ref_to_delete, without)
+ if (remove_entry(packed, ref_to_delete->string) != -1)
removed = 1;
if (!removed) {
/*
@@ -3797,12 +3798,13 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,
int transaction_commit(struct transaction *transaction,
struct strbuf *err)
{
- int ret = 0, delnum = 0, i, need_repack = 0;
- const char **delnames;
+ int ret = 0, i, need_repack = 0;
int n = transaction->nr;
struct packed_ref_cache *packed_ref_cache;
struct ref_update **updates = transaction->updates;
struct ref_dir *packed;
+ struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
+ struct string_list_item *ref_to_delete;
assert(err);
@@ -3814,9 +3816,6 @@ int transaction_commit(struct transaction *transaction,
return 0;
}
- /* Allocate work space */
- delnames = xmalloc(sizeof(*delnames) * n);
-
/* Copy, sort, and reject duplicate refs */
qsort(updates, n, sizeof(*updates), ref_update_compare);
if (ref_update_reject_duplicates(updates, n, err)) {
@@ -3908,7 +3907,8 @@ int transaction_commit(struct transaction *transaction,
}
try_remove_empty_parents((char *)update->refname);
if (!(update->flags & REF_ISPRUNING))
- delnames[delnum++] = xstrdup(update->lock->ref_name);
+ string_list_insert(&refs_to_delete,
+ update->lock->ref_name);
unlock_ref(update->lock);
update->lock = NULL;
}
@@ -3925,7 +3925,7 @@ int transaction_commit(struct transaction *transaction,
(update->have_old ?
update->old_sha1 :
NULL),
- NULL,
+ &refs_to_delete,
update->flags,
&update->type);
if (!update->lock) {
@@ -3939,8 +3939,8 @@ int transaction_commit(struct transaction *transaction,
}
/* delete reflog for all deleted refs */
- for (i = 0; i < delnum; i++)
- unlink_or_warn(git_path("logs/%s", delnames[i]));
+ for_each_string_list_item(ref_to_delete, &refs_to_delete)
+ unlink_or_warn(git_path("logs/%s", ref_to_delete->string));
/* Lock all reflog files */
for (i = 0; i < n; i++) {
@@ -4047,7 +4047,7 @@ int transaction_commit(struct transaction *transaction,
}
}
- if (repack_without_refs(delnames, delnum, err))
+ if (repack_without_refs(&refs_to_delete, err))
ret = TRANSACTION_GENERIC_ERROR;
clear_loose_ref_cache(&ref_cache);
@@ -4060,7 +4060,7 @@ cleanup:
for (i = 0; i < n; i++)
if (updates[i]->lock)
unlock_ref(updates[i]->lock);
- free(delnames);
+ string_list_clear(&refs_to_delete, 0);
return ret;
}
diff --git a/refs.h b/refs.h
index 9153e1d..d174380 100644
--- a/refs.h
+++ b/refs.h
@@ -163,7 +163,7 @@ extern void rollback_packed_refs(void);
*/
int pack_refs(unsigned int flags);
-extern int repack_without_refs(const char **refnames, int n,
+extern int repack_without_refs(struct string_list *without,
struct strbuf *err);
extern int ref_exists(const char *);
--
2.1.0.rc2.206.gedb03e5
next prev parent reply other threads:[~2014-10-21 20:37 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-21 20:36 [PATCH 00/15] ref-transaction-rename Ronnie Sahlberg
2014-10-21 20:36 ` [PATCH 01/15] refs.c: allow passing raw git_committer_info as email to _update_reflog Ronnie Sahlberg
2014-10-21 20:36 ` [PATCH 02/15] refs.c: return error instead of dying when locking fails during transaction Ronnie Sahlberg
2014-11-11 10:34 ` Jeff King
2014-11-11 15:42 ` Ronnie Sahlberg
2014-10-21 20:36 ` [PATCH 03/15] refs.c: use packed refs when deleting refs during a transaction Ronnie Sahlberg
2014-10-22 19:48 ` Junio C Hamano
2014-10-21 20:36 ` Ronnie Sahlberg [this message]
2014-10-21 20:36 ` [PATCH 05/15] refs.c: update rename_ref to use " Ronnie Sahlberg
2014-10-28 19:07 ` Junio C Hamano
2014-10-28 19:56 ` Junio C Hamano
2014-10-28 20:56 ` Ronnie Sahlberg
2014-10-28 21:12 ` Junio C Hamano
2014-10-29 17:18 ` Ronnie Sahlberg
2014-10-29 18:43 ` Junio C Hamano
2014-10-30 18:46 ` Ronnie Sahlberg
2014-10-21 20:36 ` [PATCH 06/15] refs.c: rollback the lockfile before we die() in repack_without_refs Ronnie Sahlberg
2014-10-21 20:36 ` [PATCH 07/15] refs.c: move reflog updates into its own function Ronnie Sahlberg
2014-10-21 20:36 ` [PATCH 08/15] refs.c: write updates to packed refs when a transaction has more than one ref Ronnie Sahlberg
2014-10-21 20:36 ` [PATCH 09/15] remote.c: use a transaction for deleting refs Ronnie Sahlberg
2014-10-21 20:36 ` [PATCH 10/15] refs.c: make repack_without_refs static Ronnie Sahlberg
2014-10-21 20:36 ` [PATCH 11/15] refs.c: make the *_packed_refs functions static Ronnie Sahlberg
2014-10-21 20:36 ` [PATCH 12/15] refs.c: replace the onerr argument in update_ref with a strbuf err Ronnie Sahlberg
2014-10-21 20:36 ` [PATCH 13/15] refs.c: make add_packed_ref return an error instead of calling die Ronnie Sahlberg
2014-10-21 20:36 ` [PATCH 14/15] refs.c: make lock_packed_refs take an err argument Ronnie Sahlberg
2014-10-21 20:37 ` [PATCH 15/15] refs.c: add an err argument to pack_refs Ronnie Sahlberg
2014-10-30 19:57 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1413923820-14457-5-git-send-email-sahlberg@google.com \
--to=sahlberg@google.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).