Subject: [PATCH 2/5] pretty describe: factor out describe_commit() from describe()

From: René Scharfe <[email-masked]>
Date: Sun, 04 Nov 2007 12:48:31 +0100

Factor out a new function, describe_commit(), out of describe().  All
arguments are passed as parameters, not globals.  Also move the clamping
of max_candidates from cmd_describe() into the new function, which makes
it ready to be used in a library.

Signed-off-by: Rene Scharfe <[email-masked]>
---
 builtin-describe.c |   64 +++++++++++++++++++++++++++++++++------------------
 1 files changed, 41 insertions(+), 23 deletions(-)

diff --git a/builtin-describe.c b/builtin-describe.c
index 82c25c3..e68a3d0 100644
--- a/builtin-describe.c
+++ b/builtin-describe.c
@@ -116,28 +116,26 @@ static unsigned long finish_depth_computation(
 	return seen_commits;
 }
 
-static void describe(const char *arg, int last_one)
+char *describe_commit(struct commit *cmit, int max_candidates, int debug, int *depthp)
 {
-	unsigned char sha1[20];
-	struct commit *cmit, *gave_up_on = NULL;
+	struct commit *gave_up_on = NULL;
 	struct commit_list *list;
 	struct possible_tag all_matches[MAX_TAGS];
 	unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
 	unsigned long seen_commits = 0;
 
-	if (get_sha1(arg, sha1))
-		die("Not a valid object name %s", arg);
-	cmit = lookup_commit_reference(sha1);
-	if (!cmit)
-		die("%s is not a valid '%s' object", arg, commit_type);
-
 	if (cmit->name) {
-		printf("%s\n", all ? cmit->name : cmit->name + 5);
-		return;
+		*depthp = 0;
+		return cmit->name;
 	}
 
 	if (debug)
-		fprintf(stderr, "searching to describe %s\n", arg);
+		fprintf(stderr, "searching...\n");
+
+	if (max_candidates < 1)
+		max_candidates = 1;
+	else if (max_candidates > MAX_TAGS)
+		max_candidates = MAX_TAGS;
 
 	list = NULL;
 	cmit->name_flags = SEEN;
@@ -149,7 +147,7 @@ static void describe(const char *arg, int last_one)
 		if (c->name) {
 			if (match_cnt < max_candidates) {
 				struct possible_tag *t = &all_matches[match_cnt++];
-				t->name = all ? c->name : c->name + 5;
+				t->name = c->name;
 				t->prio = c->name_prio;
 				t->depth = seen_commits - 1;
 				t->flag_within = 1u << match_cnt;
@@ -184,8 +182,11 @@ static void describe(const char *arg, int last_one)
 		}
 	}
 
-	if (!match_cnt)
-		die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
+	if (!match_cnt) {
+		free_commit_list(list);
+		*depthp = -1;
+		return NULL;
+	}
 
 	qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
 
@@ -211,11 +212,32 @@ static void describe(const char *arg, int last_one)
 				sha1_to_hex(gave_up_on->object.sha1));
 		}
 	}
-	if (abbrev == 0)
-		printf("%s\n", all_matches[0].name);
+
+	*depthp = all_matches[0].depth;
+	return all_matches[0].name;
+}
+
+static void describe(const char *arg, int last_one)
+{
+	unsigned char sha1[20];
+	struct commit *cmit;
+	char *name;
+	int depth = 0;
+
+	if (get_sha1(arg, sha1))
+		die("Not a valid object name %s", arg);
+	cmit = lookup_commit_reference(sha1);
+	if (!cmit)
+		die("%s is not a valid '%s' object", arg, commit_type);
+
+	name = describe_commit(cmit, max_candidates, debug, &depth);
+	if (!name)
+		die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
+
+	if (abbrev == 0 || depth == 0)
+		printf("%s\n", all ? name : name + 5);
 	else
-		printf("%s-%d-g%s\n", all_matches[0].name,
-		       all_matches[0].depth,
+		printf("%s-%d-g%s\n", all ? name : name + 5, depth,
 		       find_unique_abbrev(cmit->object.sha1, abbrev));
 
 	if (!last_one)
@@ -237,10 +259,6 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
 	};
 
 	argc = parse_options(argc, argv, options, describe_usage, 0);
-	if (max_candidates < 1)
-		max_candidates = 1;
-	else if (max_candidates > MAX_TAGS)
-		max_candidates = MAX_TAGS;
 
 	save_commit_buffer = 0;
 
-- 
1.5.3.5.529.ge3d6d