git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: mhagger@alum.mit.edu
To: Junio C Hamano <gitster@pobox.com>
Cc: Jeff King <peff@peff.net>, Philip Oakley <philipoakley@iee.org>,
	git@vger.kernel.org, Michael Haggerty <mhagger@alum.mit.edu>
Subject: [PATCH v2 17/17] filter_refs(): simplify logic
Date: Sat, 25 Aug 2012 08:44:27 +0200	[thread overview]
Message-ID: <1345877067-11841-18-git-send-email-mhagger@alum.mit.edu> (raw)
In-Reply-To: <1345877067-11841-1-git-send-email-mhagger@alum.mit.edu>

From: Michael Haggerty <mhagger@alum.mit.edu>

* Build linked list of return values as we go rather than recording
  them in a temporary array and linking them up later.

* Handle ref in a single if...else statement in the main loop, to make
  it clear that each ref has exactly two possible destinies.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 builtin/fetch-pack.c | 56 ++++++++++++++++++----------------------------------
 1 file changed, 19 insertions(+), 37 deletions(-)

diff --git a/builtin/fetch-pack.c b/builtin/fetch-pack.c
index 1bc4599..db77ee6 100644
--- a/builtin/fetch-pack.c
+++ b/builtin/fetch-pack.c
@@ -523,66 +523,48 @@ static void mark_recent_complete_commits(unsigned long cutoff)
 
 static void filter_refs(struct ref **refs, int *nr_heads, char **heads)
 {
-	struct ref **return_refs;
 	struct ref *newlist = NULL;
 	struct ref **newtail = &newlist;
 	struct ref *ref, *next;
-	int head_pos = 0, matched = 0, unmatched = 0;
-
-	if (*nr_heads && !args.fetch_all)
-		return_refs = xcalloc(*nr_heads, sizeof(struct ref *));
-	else
-		return_refs = NULL;
+	int head_pos = 0, unmatched = 0;
 
 	for (ref = *refs; ref; ref = next) {
+		int keep_ref = 0;
 		next = ref->next;
 		if (!memcmp(ref->name, "refs/", 5) &&
 		    check_refname_format(ref->name, 0))
 			; /* trash */
 		else if (args.fetch_all &&
-			 (!args.depth || prefixcmp(ref->name, "refs/tags/") )) {
-			*newtail = ref;
-			ref->next = NULL;
-			newtail = &ref->next;
-			continue;
-		}
-		else {
-			int cmp = -1;
+			   (!args.depth || prefixcmp(ref->name, "refs/tags/")))
+			keep_ref = 1;
+		else
 			while (head_pos < *nr_heads) {
-				cmp = strcmp(ref->name, heads[head_pos]);
-				if (cmp < 0) /* definitely do not have it */
+				int cmp = strcmp(ref->name, heads[head_pos]);
+				if (cmp < 0) { /* definitely do not have it */
 					break;
-				else if (cmp == 0) { /* definitely have it */
-					return_refs[matched++] = ref;
+				} else if (cmp == 0) { /* definitely have it */
 					free(heads[head_pos++]);
+					keep_ref = 1;
 					break;
-				}
-				else { /* might have it; keep looking */
+				} else { /* might have it; keep looking */
 					heads[unmatched++] = heads[head_pos++];
 				}
 			}
-			if (!cmp)
-				continue; /* we will link it later */
-		}
-		free(ref);
-	}
-
-	if (!args.fetch_all) {
-		int i;
 
-		/* copy remaining unmatched heads: */
-		while (head_pos < *nr_heads)
-			heads[unmatched++] = heads[head_pos++];
-		*nr_heads = unmatched;
-
-		for (i = 0; i < matched; i++) {
-			ref = return_refs[i];
+		if (keep_ref) {
 			*newtail = ref;
 			ref->next = NULL;
 			newtail = &ref->next;
+		} else {
+			free(ref);
 		}
-		free(return_refs);
 	}
+
+	/* copy any remaining unmatched heads: */
+	while (head_pos < *nr_heads)
+		heads[unmatched++] = heads[head_pos++];
+	*nr_heads = unmatched;
+
 	*refs = newlist;
 }
 
-- 
1.7.11.3

  parent reply	other threads:[~2012-08-25  6:45 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-25  6:44 [PATCH v2 00/17] Clean up how fetch_pack() handles the heads list mhagger
2012-08-25  6:44 ` [PATCH v2 01/17] t5500: add tests of error output for missing refs mhagger
2012-08-25  6:44 ` [PATCH v2 02/17] Rename static function fetch_pack() to http_fetch_pack() mhagger
2012-08-25  6:44 ` [PATCH v2 03/17] Fix formatting mhagger
2012-08-25  6:44 ` [PATCH v2 04/17] Name local variables more consistently mhagger
2012-08-25  6:44 ` [PATCH v2 05/17] Do not check the same head_pos twice mhagger
2012-08-25  6:44 ` [PATCH v2 06/17] Let fetch_pack() inform caller about number of unique heads mhagger
2012-08-25  6:44 ` [PATCH v2 07/17] Pass nr_heads to do_pack_ref() by reference mhagger
2012-08-25  6:44 ` [PATCH v2 08/17] Pass nr_heads to everything_local() " mhagger
2012-08-25  6:44 ` [PATCH v2 09/17] Pass nr_heads to filter_refs() " mhagger
2012-08-25  6:44 ` [PATCH v2 10/17] Remove ineffective optimization mhagger
2012-08-25  6:44 ` [PATCH v2 11/17] filter_refs(): do not leave gaps in return_refs mhagger
2012-08-25  6:44 ` [PATCH v2 12/17] filter_refs(): compress unmatched refs in heads array mhagger
2012-08-25  6:44 ` [PATCH v2 13/17] cmd_fetch_pack: return early if finish_connect() returns an error mhagger
2012-08-25  6:44 ` [PATCH v2 14/17] Report missing refs even if no existing refs were received mhagger
2012-08-25  6:44 ` [PATCH v2 15/17] cmd_fetch_pack(): simplify computation of return value mhagger
2012-08-25  6:44 ` [PATCH v2 16/17] fetch_pack(): free matching heads mhagger
2012-08-25  6:44 ` mhagger [this message]
2012-08-26 17:20 ` [PATCH v2 00/17] Clean up how fetch_pack() handles the heads list 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=1345877067-11841-18-git-send-email-mhagger@alum.mit.edu \
    --to=mhagger@alum.mit.edu \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    --cc=philipoakley@iee.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).