All of lore.kernel.org
 help / color / mirror / Atom feed
From: "René Scharfe" <rene.scharfe@lsrfire.ath.cx>
To: Junio C Hamano <junkio@cox.net>
Cc: Git Mailing List <git@vger.kernel.org>
Subject: [PATCH 2/5] pretty describe: factor out describe_commit() from describe()
Date: Sun, 04 Nov 2007 12:48:31 +0100	[thread overview]
Message-ID: <472DB18F.9000302@lsrfire.ath.cx> (raw)

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 <rene.scharfe@lsrfire.ath.cx>
---
 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

                 reply	other threads:[~2007-11-04 11:48 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=472DB18F.9000302@lsrfire.ath.cx \
    --to=rene.scharfe@lsrfire.ath.cx \
    --cc=git@vger.kernel.org \
    --cc=junkio@cox.net \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.