threads / patch / 14689

patch, 3 partsbuiltin-branch: factor out merge_filter matching

Subject: [PATCH 2/3] builtin-branch: factor out merge_filter matching

## tl;dr

4 messages between Jul 26, 2008 and Jul 26, 2008. Diffs are folded; open one to read it.

replies: 3people: 1as markdown or json

Lars Hjemli· Jul 26, 2008, 10:27 UTC · lore

[PATCH 0/3] builtin-branch --[no-]merged post-optimization fixes

The optimization of --[no-]merged added some code duplication and a possible "bug" for -v output which this series tries to rectify.

Lars Hjemli (3):
  builtin-branch: remove duplicated code
  builtin-branch: factor out merge_filter matching
  builtin-branch: fix -v for --[no-]merged
 builtin-branch.c |   42 ++++++++++++++++++++++++++++++------------
 1 files changed, 30 insertions(+), 12 deletions(-)
Lars Hjemli· Jul 26, 2008, 10:27 UTC · re: Lars Hjemli · lore

[PATCH 1/3] builtin-branch: remove duplicated code

The previous optimization to --[no-]merged ended up with some duplicated code which this patch removes.

Signed-off-by: Lars Hjemli <hjemli@gmail.com>
---
 builtin-branch.c |    9 ++-------
 1 files changed, 2 insertions(+), 7 deletions(-)
Show changes to builtin-branch.c +2 −7
diff --git a/builtin-branch.c b/builtin-branch.c
index 5db8ad8..675a9b1 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -214,7 +214,6 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
 	struct commit *commit;
 	int kind;
 	int len;
-	static struct commit_list branch;
 
 	/* Detect kind */
 	if (!prefixcmp(refname, "refs/heads/")) {
@@ -238,13 +237,9 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
 	if ((kind & ref_list->kinds) == 0)
 		return 0;
 
-	if (merge_filter != NO_FILTER) {
-		branch.item = lookup_commit_reference_gently(sha1, 1);
-		if (!branch.item)
-			die("Unable to lookup tip of branch %s", refname);
+	if (merge_filter != NO_FILTER)
 		add_pending_object(&ref_list->revs,
-				   (struct object *)branch.item, refname);
-	}
+				   (struct object *)commit, refname);
 
 	/* Resize buffer */
 	if (ref_list->index >= ref_list->alloc) {
-- 
1.6.0.rc0.79.gb0320
Lars Hjemli· Jul 26, 2008, 10:27 UTC · re: Lars Hjemli · lore

The logic for checking commits against merge_filter will be reused when we recalculate the maxwidth of refnames.

Signed-off-by: Lars Hjemli <hjemli@gmail.com>
---
 builtin-branch.c |   18 +++++++++++++-----
 1 files changed, 13 insertions(+), 5 deletions(-)
Show changes to builtin-branch.c +13 −5
diff --git a/builtin-branch.c b/builtin-branch.c
index 675a9b1..bff74cf 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -294,6 +294,17 @@ static void fill_tracking_info(char *stat, const char *branch_name)
 		sprintf(stat, "[ahead %d, behind %d] ", ours, theirs);
 }
 
+static int matches_merge_filter(struct commit *commit)
+{
+	int is_merged;
+	
+	if (merge_filter == NO_FILTER)
+		return 1;
+		
+	is_merged = !!(commit->object.flags & UNINTERESTING);
+	return (is_merged == (merge_filter == SHOW_MERGED));
+}
+
 static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
 			   int abbrev, int current)
 {
@@ -301,11 +312,8 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
 	int color;
 	struct commit *commit = item->commit;
 
-	if (merge_filter != NO_FILTER) {
-		int is_merged = !!(item->commit->object.flags & UNINTERESTING);
-		if (is_merged != (merge_filter == SHOW_MERGED))
-			return;
-	}
+	if (!matches_merge_filter(commit))
+		return;
 
 	switch (item->kind) {
 	case REF_LOCAL_BRANCH:
-- 
1.6.0.rc0.79.gb0320
Lars Hjemli· Jul 26, 2008, 10:27 UTC · re: Lars Hjemli · lore

[PATCH 3/3] builtin-branch: fix -v for --[no-]merged

After the optimization to --[no-]merged logic, the calculation of the width of the longest refname to be shown might become inaccurate (since the matching against merge_filter is performed after adding refs to ref_list). This patch forces a recalculation of maxwidth when it might be needed.

Signed-off-by: Lars Hjemli <hjemli@gmail.com>
---
 builtin-branch.c |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)
Show changes to builtin-branch.c +15 −0
diff --git a/builtin-branch.c b/builtin-branch.c
index bff74cf..fed6f5e 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -363,6 +363,19 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
 	}
 }
 
+static int calc_maxwidth(struct ref_list *refs)
+{
+	int i, l, w = 0;
+	for (i = 0; i < refs->index; i++) {
+		if (!matches_merge_filter(refs->list[i].commit))
+			continue;
+		l = strlen(refs->list[i].name);
+		if (l > w)
+			w = l;
+	}
+	return w;
+}
+
 static void print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit)
 {
 	int i;
@@ -383,6 +396,8 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev, str
 				   (struct object *) filter, "");
 		ref_list.revs.limited = 1;
 		prepare_revision_walk(&ref_list.revs);
+		if (verbose)
+			ref_list.maxwidth = calc_maxwidth(&ref_list);
 	}
 
 	qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
-- 
1.6.0.rc0.79.gb0320

← back to recent threads