[PATCH] git-branch: only traverse the requested refs

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Lars Hjemli
Date: Wednesday, October 10, 2007 - 2:54 pm

This avoids looking at every single file below .git/refs when git-branch
is fetching the list of refs to display.

Signed-off-by: Lars Hjemli <hjemli@gmail.com>
---

This patch should make git-branch much more efficient when there exists
many files below .git/refs, but it does require two passes through
.git/packed-refs when -a is specified.

No benchmarking performed...

 builtin-branch.c |   28 +++++++++-------------------
 1 files changed, 9 insertions(+), 19 deletions(-)

diff --git a/builtin-branch.c b/builtin-branch.c
index 3da8b55..466e1e0 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -185,25 +185,8 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
 {
 	struct ref_list *ref_list = (struct ref_list*)(cb_data);
 	struct ref_item *newitem;
-	int kind = REF_UNKNOWN_TYPE;
 	int len;
 
-	/* Detect kind */
-	if (!prefixcmp(refname, "refs/heads/")) {
-		kind = REF_LOCAL_BRANCH;
-		refname += 11;
-	} else if (!prefixcmp(refname, "refs/remotes/")) {
-		kind = REF_REMOTE_BRANCH;
-		refname += 13;
-	} else if (!prefixcmp(refname, "refs/tags/")) {
-		kind = REF_TAG;
-		refname += 10;
-	}
-
-	/* Don't add types the caller doesn't want */
-	if ((kind & ref_list->kinds) == 0)
-		return 0;
-
 	/* Resize buffer */
 	if (ref_list->index >= ref_list->alloc) {
 		ref_list->alloc = alloc_nr(ref_list->alloc);
@@ -214,7 +197,7 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
 	/* Record the new item */
 	newitem = &(ref_list->list[ref_list->index++]);
 	newitem->name = xstrdup(refname);
-	newitem->kind = kind;
+	newitem->kind = ref_list->kinds;
 	hashcpy(newitem->sha1, sha1);
 	len = strlen(newitem->name);
 	if (len > ref_list->maxwidth)
@@ -296,8 +279,15 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev)
 	struct ref_list ref_list;
 
 	memset(&ref_list, 0, sizeof(ref_list));
+	if (kinds & REF_LOCAL_BRANCH) {
+		ref_list.kinds = REF_LOCAL_BRANCH;
+		for_each_branch_ref(append_ref, &ref_list);
+	}
+	if (kinds & REF_REMOTE_BRANCH) {
+		ref_list.kinds = REF_REMOTE_BRANCH;
+		for_each_remote_ref(append_ref, &ref_list);
+	}
 	ref_list.kinds = kinds;
-	for_each_ref(append_ref, &ref_list);
 
 	qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
 
-- 
1.5.3.4.206.g58ba4

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
git branch performance problem?, Han-Wen Nienhuys, (Wed Oct 10, 1:22 pm)
Re: git branch performance problem?, Lars Hjemli, (Wed Oct 10, 1:44 pm)
Re: git branch performance problem?, Han-Wen Nienhuys, (Wed Oct 10, 2:17 pm)
Re: git branch performance problem?, Han-Wen Nienhuys, (Wed Oct 10, 2:24 pm)
Re: git branch performance problem?, Han-Wen Nienhuys, (Wed Oct 10, 2:30 pm)
Re: git branch performance problem?, Lars Hjemli, (Wed Oct 10, 2:34 pm)
Re: git branch performance problem?, J. Bruce Fields, (Wed Oct 10, 2:39 pm)
Re: git branch performance problem?, Lars Hjemli, (Wed Oct 10, 2:45 pm)
Re: git branch performance problem?, Han-Wen Nienhuys, (Wed Oct 10, 2:49 pm)
Re: git branch performance problem?, J. Bruce Fields, (Wed Oct 10, 2:53 pm)
Re: git branch performance problem?, Johannes Schindelin, (Wed Oct 10, 2:53 pm)
[PATCH] git-branch: only traverse the requested refs, Lars Hjemli, (Wed Oct 10, 2:54 pm)
Re: git branch performance problem?, Han-Wen Nienhuys, (Wed Oct 10, 3:01 pm)
Re: Spam: Re: git branch performance problem?, Brandon Casey, (Wed Oct 10, 3:55 pm)
Re: [PATCH] git-branch: only traverse the requested refs, Johannes Schindelin, (Wed Oct 10, 4:00 pm)
Re: git branch performance problem?, Linus Torvalds, (Wed Oct 10, 4:39 pm)
Re: git branch performance problem?, Han-Wen Nienhuys, (Wed Oct 10, 7:26 pm)
Re: git branch performance problem?, Alex Riesen, (Wed Oct 10, 11:41 pm)
Re: Spam: Re: git branch performance problem?, Mike Ralphson, (Thu Oct 11, 2:41 am)
Re: git branch performance problem?, Johannes Schindelin, (Thu Oct 11, 3:46 am)
Re: Spam: Re: git branch performance problem?, Johannes Schindelin, (Thu Oct 11, 3:58 am)
Re: git branch performance problem?, Han-Wen Nienhuys, (Thu Oct 11, 6:11 am)
Re: git branch performance problem?, Linus Torvalds, (Thu Oct 11, 8:16 am)
Re: git branch performance problem?, Salikh Zakirov, (Fri Oct 12, 10:19 am)