{"thread":{"id":"10641","subject":"[PATCH 2/5] pretty describe: factor out describe_commit() from describe()","startedAt":"2007-11-04T11:48:31Z","lastAt":"2007-11-04T11:48:31Z","messageCount":1,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":5},"messages":[{"id":"58252","messageId":"472DB18F.9000302@lsrfire.ath.cx","threadId":"10641","inReplyTo":null,"subject":"[PATCH 2/5] pretty describe: factor out describe_commit() from describe()","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2007-11-04T11:48:31Z","receivedAt":"2007-11-04T11:48:31Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Factor out a new function, describe_commit(), out of describe().  All\narguments are passed as parameters, not globals.  Also move the clamping\nof max_candidates from cmd_describe() into the new function, which makes\nit ready to be used in a library.\n\nSigned-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\n builtin-describe.c |   64 +++++++++++++++++++++++++++++++++------------------\n 1 files changed, 41 insertions(+), 23 deletions(-)\n\ndiff --git a/builtin-describe.c b/builtin-describe.c\nindex 82c25c3..e68a3d0 100644\n--- a/builtin-describe.c\n+++ b/builtin-describe.c\n@@ -116,28 +116,26 @@ static unsigned long finish_depth_computation(\n \treturn seen_commits;\n }\n \n-static void describe(const char *arg, int last_one)\n+char *describe_commit(struct commit *cmit, int max_candidates, int debug, int *depthp)\n {\n-\tunsigned char sha1[20];\n-\tstruct commit *cmit, *gave_up_on = NULL;\n+\tstruct commit *gave_up_on = NULL;\n \tstruct commit_list *list;\n \tstruct possible_tag all_matches[MAX_TAGS];\n \tunsigned int match_cnt = 0, annotated_cnt = 0, cur_match;\n \tunsigned long seen_commits = 0;\n \n-\tif (get_sha1(arg, sha1))\n-\t\tdie(\"Not a valid object name %s\", arg);\n-\tcmit = lookup_commit_reference(sha1);\n-\tif (!cmit)\n-\t\tdie(\"%s is not a valid '%s' object\", arg, commit_type);\n-\n \tif (cmit->name) {\n-\t\tprintf(\"%s\\n\", all ? cmit->name : cmit->name + 5);\n-\t\treturn;\n+\t\t*depthp = 0;\n+\t\treturn cmit->name;\n \t}\n \n \tif (debug)\n-\t\tfprintf(stderr, \"searching to describe %s\\n\", arg);\n+\t\tfprintf(stderr, \"searching...\\n\");\n+\n+\tif (max_candidates < 1)\n+\t\tmax_candidates = 1;\n+\telse if (max_candidates > MAX_TAGS)\n+\t\tmax_candidates = MAX_TAGS;\n \n \tlist = NULL;\n \tcmit->name_flags = SEEN;\n@@ -149,7 +147,7 @@ static void describe(const char *arg, int last_one)\n \t\tif (c->name) {\n \t\t\tif (match_cnt < max_candidates) {\n \t\t\t\tstruct possible_tag *t = &all_matches[match_cnt++];\n-\t\t\t\tt->name = all ? c->name : c->name + 5;\n+\t\t\t\tt->name = c->name;\n \t\t\t\tt->prio = c->name_prio;\n \t\t\t\tt->depth = seen_commits - 1;\n \t\t\t\tt->flag_within = 1u << match_cnt;\n@@ -184,8 +182,11 @@ static void describe(const char *arg, int last_one)\n \t\t}\n \t}\n \n-\tif (!match_cnt)\n-\t\tdie(\"cannot describe '%s'\", sha1_to_hex(cmit->object.sha1));\n+\tif (!match_cnt) {\n+\t\tfree_commit_list(list);\n+\t\t*depthp = -1;\n+\t\treturn NULL;\n+\t}\n \n \tqsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);\n \n@@ -211,11 +212,32 @@ static void describe(const char *arg, int last_one)\n \t\t\t\tsha1_to_hex(gave_up_on->object.sha1));\n \t\t}\n \t}\n-\tif (abbrev == 0)\n-\t\tprintf(\"%s\\n\", all_matches[0].name);\n+\n+\t*depthp = all_matches[0].depth;\n+\treturn all_matches[0].name;\n+}\n+\n+static void describe(const char *arg, int last_one)\n+{\n+\tunsigned char sha1[20];\n+\tstruct commit *cmit;\n+\tchar *name;\n+\tint depth = 0;\n+\n+\tif (get_sha1(arg, sha1))\n+\t\tdie(\"Not a valid object name %s\", arg);\n+\tcmit = lookup_commit_reference(sha1);\n+\tif (!cmit)\n+\t\tdie(\"%s is not a valid '%s' object\", arg, commit_type);\n+\n+\tname = describe_commit(cmit, max_candidates, debug, &depth);\n+\tif (!name)\n+\t\tdie(\"cannot describe '%s'\", sha1_to_hex(cmit->object.sha1));\n+\n+\tif (abbrev == 0 || depth == 0)\n+\t\tprintf(\"%s\\n\", all ? name : name + 5);\n \telse\n-\t\tprintf(\"%s-%d-g%s\\n\", all_matches[0].name,\n-\t\t       all_matches[0].depth,\n+\t\tprintf(\"%s-%d-g%s\\n\", all ? name : name + 5, depth,\n \t\t       find_unique_abbrev(cmit->object.sha1, abbrev));\n \n \tif (!last_one)\n@@ -237,10 +259,6 @@ int cmd_describe(int argc, const char **argv, const char *prefix)\n \t};\n \n \targc = parse_options(argc, argv, options, describe_usage, 0);\n-\tif (max_candidates < 1)\n-\t\tmax_candidates = 1;\n-\telse if (max_candidates > MAX_TAGS)\n-\t\tmax_candidates = MAX_TAGS;\n \n \tsave_commit_buffer = 0;\n \n-- \n1.5.3.5.529.ge3d6d\n"}]}