{"thread":{"id":"10642","subject":"[PATCH 3/5] pretty describe: move library functions to the new file describe.c","startedAt":"2007-11-04T11:48:41Z","lastAt":"2007-11-04T14:56:00Z","messageCount":3,"participants":["René Scharfe","Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":5},"messages":[{"id":"58254","messageId":"472DB199.2040305@lsrfire.ath.cx","threadId":"10642","inReplyTo":null,"subject":"[PATCH 3/5] pretty describe: move library functions to the new file describe.c","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2007-11-04T11:48:41Z","receivedAt":"2007-11-04T11:48:41Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"load_commit_names(), describe_commit() et. al. are moved out to their\nown file and properly exported.  There are no code changes inside the\nfunctions.\n\nSigned-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\n Makefile           |    2 +-\n builtin-describe.c |  202 ---------------------------------------------------\n cache.h            |    5 ++\n describe.c         |  204 ++++++++++++++++++++++++++++++++++++++++++++++++++++\n 4 files changed, 210 insertions(+), 203 deletions(-)\n create mode 100644 describe.c\n\ndiff --git a/Makefile b/Makefile\nindex 042f79e..afdd847 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -312,7 +312,7 @@ LIB_OBJS = \\\n \talloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \\\n \tcolor.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \\\n \tconvert.o attr.o decorate.o progress.o mailmap.o symlinks.o remote.o \\\n-\ttransport.o bundle.o walker.o parse-options.o\n+\ttransport.o bundle.o walker.o parse-options.o describe.o\n \n BUILTIN_OBJS = \\\n \tbuiltin-add.o \\\ndiff --git a/builtin-describe.c b/builtin-describe.c\nindex e68a3d0..fcd93f4 100644\n--- a/builtin-describe.c\n+++ b/builtin-describe.c\n@@ -1,14 +1,9 @@\n #include \"cache.h\"\n #include \"commit.h\"\n-#include \"tag.h\"\n-#include \"refs.h\"\n #include \"builtin.h\"\n #include \"exec_cmd.h\"\n #include \"parse-options.h\"\n \n-#define SEEN\t\t(1u<<0)\n-#define MAX_TAGS\t(FLAG_BITS - 1)\n-\n static const char * const describe_usage[] = {\n \t\"git-describe [options] <committish>*\",\n \tNULL\n@@ -20,203 +15,6 @@ static int tags;\t/* But allow any tags if --tags is specified */\n static int abbrev = DEFAULT_ABBREV;\n static int max_candidates = 10;\n \n-static const char *prio_names[] = {\n-\t\"head\", \"lightweight\", \"annotated\",\n-};\n-\n-static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)\n-{\n-\tstruct commit *commit = lookup_commit_reference_gently(sha1, 1);\n-\tstruct object *object;\n-\tint min_prio = *((int *)cb_data);\n-\tint prio;\n-\n-\tif (!commit)\n-\t\treturn 0;\n-\tobject = parse_object(sha1);\n-\n-\tif (!prefixcmp(path, \"refs/tags/\")) {\n-\t\tif (object->type == OBJ_TAG)\n-\t\t\tprio = 2;\n-\t\telse\n-\t\t\tprio = 1;\n-\t}\n-\telse\n-\t\tprio = 0;\n-\tif (prio < min_prio)\n-\t\treturn 0;\n-\n-\tif (commit->name) {\n-\t\tif (commit->name_prio >= prio)\n-\t\t\treturn 0;\n-\t\tfree(commit->name);\n-\t}\n-\tcommit->name = xstrdup(path + 5);\n-\tcommit->name_prio = prio;\n-\n-\treturn 0;\n-}\n-\n-void load_commit_names(int min_prio)\n-{\n-\tfor_each_ref(get_name, &min_prio);\n-}\n-\n-struct possible_tag {\n-\tchar *name;\n-\tint prio; /* annotated tag = 2, tag = 1, head = 0 */\n-\tint depth;\n-\tint found_order;\n-\tunsigned flag_within;\n-};\n-\n-static int compare_pt(const void *a_, const void *b_)\n-{\n-\tstruct possible_tag *a = (struct possible_tag *)a_;\n-\tstruct possible_tag *b = (struct possible_tag *)b_;\n-\tif (a->prio != b->prio)\n-\t\treturn b->prio - a->prio;\n-\tif (a->depth != b->depth)\n-\t\treturn a->depth - b->depth;\n-\tif (a->found_order != b->found_order)\n-\t\treturn a->found_order - b->found_order;\n-\treturn 0;\n-}\n-\n-static unsigned long finish_depth_computation(\n-\tstruct commit_list **list,\n-\tstruct possible_tag *best)\n-{\n-\tunsigned long seen_commits = 0;\n-\twhile (*list) {\n-\t\tstruct commit *c = pop_commit(list);\n-\t\tstruct commit_list *parents = c->parents;\n-\t\tseen_commits++;\n-\t\tif (c->name_flags & best->flag_within) {\n-\t\t\tstruct commit_list *a = *list;\n-\t\t\twhile (a) {\n-\t\t\t\tstruct commit *i = a->item;\n-\t\t\t\tif (!(i->name_flags & best->flag_within))\n-\t\t\t\t\tbreak;\n-\t\t\t\ta = a->next;\n-\t\t\t}\n-\t\t\tif (!a)\n-\t\t\t\tbreak;\n-\t\t} else\n-\t\t\tbest->depth++;\n-\t\twhile (parents) {\n-\t\t\tstruct commit *p = parents->item;\n-\t\t\tparse_commit(p);\n-\t\t\tif (!(p->name_flags & SEEN))\n-\t\t\t\tinsert_by_date(p, list);\n-\t\t\tp->name_flags |= c->name_flags;\n-\t\t\tparents = parents->next;\n-\t\t}\n-\t}\n-\treturn seen_commits;\n-}\n-\n-char *describe_commit(struct commit *cmit, int max_candidates, int debug, int *depthp)\n-{\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 (cmit->name) {\n-\t\t*depthp = 0;\n-\t\treturn cmit->name;\n-\t}\n-\n-\tif (debug)\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-\tcommit_list_insert(cmit, &list);\n-\twhile (list) {\n-\t\tstruct commit *c = pop_commit(&list);\n-\t\tstruct commit_list *parents = c->parents;\n-\t\tseen_commits++;\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 = 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-\t\t\t\tt->found_order = match_cnt;\n-\t\t\t\tc->name_flags |= t->flag_within;\n-\t\t\t\tif (c->name_prio == 2)\n-\t\t\t\t\tannotated_cnt++;\n-\t\t\t}\n-\t\t\telse {\n-\t\t\t\tgave_up_on = c;\n-\t\t\t\tbreak;\n-\t\t\t}\n-\t\t}\n-\t\tfor (cur_match = 0; cur_match < match_cnt; cur_match++) {\n-\t\t\tstruct possible_tag *t = &all_matches[cur_match];\n-\t\t\tif (!(c->name_flags & t->flag_within))\n-\t\t\t\tt->depth++;\n-\t\t}\n-\t\tif (annotated_cnt && !list) {\n-\t\t\tif (debug)\n-\t\t\t\tfprintf(stderr, \"finished search at %s\\n\",\n-\t\t\t\t\tsha1_to_hex(c->object.sha1));\n-\t\t\tbreak;\n-\t\t}\n-\t\twhile (parents) {\n-\t\t\tstruct commit *p = parents->item;\n-\t\t\tparse_commit(p);\n-\t\t\tif (!(p->name_flags & SEEN))\n-\t\t\t\tinsert_by_date(p, &list);\n-\t\t\tp->name_flags |= c->name_flags;\n-\t\t\tparents = parents->next;\n-\t\t}\n-\t}\n-\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-\tif (gave_up_on) {\n-\t\tinsert_by_date(gave_up_on, &list);\n-\t\tseen_commits--;\n-\t}\n-\tseen_commits += finish_depth_computation(&list, &all_matches[0]);\n-\tfree_commit_list(list);\n-\n-\tif (debug) {\n-\t\tfor (cur_match = 0; cur_match < match_cnt; cur_match++) {\n-\t\t\tstruct possible_tag *t = &all_matches[cur_match];\n-\t\t\tfprintf(stderr, \" %-11s %8d %s\\n\",\n-\t\t\t\tprio_names[t->prio], t->depth, t->name);\n-\t\t}\n-\t\tfprintf(stderr, \"traversed %lu commits\\n\", seen_commits);\n-\t\tif (gave_up_on) {\n-\t\t\tfprintf(stderr,\n-\t\t\t\t\"more than %i tags found; listed %i most recent\\n\"\n-\t\t\t\t\"gave up search at %s\\n\",\n-\t\t\t\tmax_candidates, max_candidates,\n-\t\t\t\tsha1_to_hex(gave_up_on->object.sha1));\n-\t\t}\n-\t}\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];\ndiff --git a/cache.h b/cache.h\nindex bfffa05..84423a3 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -602,4 +602,9 @@ extern int diff_auto_refresh_index;\n /* match-trees.c */\n void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, int);\n \n+/* describe.c */\n+struct commit;\n+extern void load_commit_names(int min_prio);\n+extern char *describe_commit(struct commit *cmit, int max_candidates, int debug, int *depthp);\n+\n #endif /* CACHE_H */\ndiff --git a/describe.c b/describe.c\nnew file mode 100644\nindex 0000000..18c7abc\n--- /dev/null\n+++ b/describe.c\n@@ -0,0 +1,204 @@\n+#include \"cache.h\"\n+#include \"commit.h\"\n+#include \"tag.h\"\n+#include \"refs.h\"\n+\n+#define SEEN\t\t(1u<<0)\n+#define MAX_TAGS\t(FLAG_BITS - 1)\n+\n+static const char *prio_names[] = {\n+\t\"head\", \"lightweight\", \"annotated\",\n+};\n+\n+static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)\n+{\n+\tstruct commit *commit = lookup_commit_reference_gently(sha1, 1);\n+\tstruct object *object;\n+\tint min_prio = *((int *)cb_data);\n+\tint prio;\n+\n+\tif (!commit)\n+\t\treturn 0;\n+\tobject = parse_object(sha1);\n+\n+\tif (!prefixcmp(path, \"refs/tags/\")) {\n+\t\tif (object->type == OBJ_TAG)\n+\t\t\tprio = 2;\n+\t\telse\n+\t\t\tprio = 1;\n+\t}\n+\telse\n+\t\tprio = 0;\n+\tif (prio < min_prio)\n+\t\treturn 0;\n+\n+\tif (commit->name) {\n+\t\tif (commit->name_prio >= prio)\n+\t\t\treturn 0;\n+\t\tfree(commit->name);\n+\t}\n+\tcommit->name = xstrdup(path + 5);\n+\tcommit->name_prio = prio;\n+\n+\treturn 0;\n+}\n+\n+void load_commit_names(int min_prio)\n+{\n+\tfor_each_ref(get_name, &min_prio);\n+}\n+\n+struct possible_tag {\n+\tchar *name;\n+\tint prio; /* annotated tag = 2, tag = 1, head = 0 */\n+\tint depth;\n+\tint found_order;\n+\tunsigned flag_within;\n+};\n+\n+static int compare_pt(const void *a_, const void *b_)\n+{\n+\tstruct possible_tag *a = (struct possible_tag *)a_;\n+\tstruct possible_tag *b = (struct possible_tag *)b_;\n+\tif (a->prio != b->prio)\n+\t\treturn b->prio - a->prio;\n+\tif (a->depth != b->depth)\n+\t\treturn a->depth - b->depth;\n+\tif (a->found_order != b->found_order)\n+\t\treturn a->found_order - b->found_order;\n+\treturn 0;\n+}\n+\n+static unsigned long finish_depth_computation(\n+\tstruct commit_list **list,\n+\tstruct possible_tag *best)\n+{\n+\tunsigned long seen_commits = 0;\n+\twhile (*list) {\n+\t\tstruct commit *c = pop_commit(list);\n+\t\tstruct commit_list *parents = c->parents;\n+\t\tseen_commits++;\n+\t\tif (c->name_flags & best->flag_within) {\n+\t\t\tstruct commit_list *a = *list;\n+\t\t\twhile (a) {\n+\t\t\t\tstruct commit *i = a->item;\n+\t\t\t\tif (!(i->name_flags & best->flag_within))\n+\t\t\t\t\tbreak;\n+\t\t\t\ta = a->next;\n+\t\t\t}\n+\t\t\tif (!a)\n+\t\t\t\tbreak;\n+\t\t} else\n+\t\t\tbest->depth++;\n+\t\twhile (parents) {\n+\t\t\tstruct commit *p = parents->item;\n+\t\t\tparse_commit(p);\n+\t\t\tif (!(p->name_flags & SEEN))\n+\t\t\t\tinsert_by_date(p, list);\n+\t\t\tp->name_flags |= c->name_flags;\n+\t\t\tparents = parents->next;\n+\t\t}\n+\t}\n+\treturn seen_commits;\n+}\n+\n+char *describe_commit(struct commit *cmit, int max_candidates, int debug, int *depthp)\n+{\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 (cmit->name) {\n+\t\t*depthp = 0;\n+\t\treturn cmit->name;\n+\t}\n+\n+\tif (debug)\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+\tcommit_list_insert(cmit, &list);\n+\twhile (list) {\n+\t\tstruct commit *c = pop_commit(&list);\n+\t\tstruct commit_list *parents = c->parents;\n+\t\tseen_commits++;\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 = 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+\t\t\t\tt->found_order = match_cnt;\n+\t\t\t\tc->name_flags |= t->flag_within;\n+\t\t\t\tif (c->name_prio == 2)\n+\t\t\t\t\tannotated_cnt++;\n+\t\t\t}\n+\t\t\telse {\n+\t\t\t\tgave_up_on = c;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t\tfor (cur_match = 0; cur_match < match_cnt; cur_match++) {\n+\t\t\tstruct possible_tag *t = &all_matches[cur_match];\n+\t\t\tif (!(c->name_flags & t->flag_within))\n+\t\t\t\tt->depth++;\n+\t\t}\n+\t\tif (annotated_cnt && !list) {\n+\t\t\tif (debug)\n+\t\t\t\tfprintf(stderr, \"finished search at %s\\n\",\n+\t\t\t\t\tsha1_to_hex(c->object.sha1));\n+\t\t\tbreak;\n+\t\t}\n+\t\twhile (parents) {\n+\t\t\tstruct commit *p = parents->item;\n+\t\t\tparse_commit(p);\n+\t\t\tif (!(p->name_flags & SEEN))\n+\t\t\t\tinsert_by_date(p, &list);\n+\t\t\tp->name_flags |= c->name_flags;\n+\t\t\tparents = parents->next;\n+\t\t}\n+\t}\n+\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+\tif (gave_up_on) {\n+\t\tinsert_by_date(gave_up_on, &list);\n+\t\tseen_commits--;\n+\t}\n+\tseen_commits += finish_depth_computation(&list, &all_matches[0]);\n+\tfree_commit_list(list);\n+\n+\tif (debug) {\n+\t\tfor (cur_match = 0; cur_match < match_cnt; cur_match++) {\n+\t\t\tstruct possible_tag *t = &all_matches[cur_match];\n+\t\t\tfprintf(stderr, \" %-11s %8d %s\\n\",\n+\t\t\t\tprio_names[t->prio], t->depth, t->name);\n+\t\t}\n+\t\tfprintf(stderr, \"traversed %lu commits\\n\", seen_commits);\n+\t\tif (gave_up_on) {\n+\t\t\tfprintf(stderr,\n+\t\t\t\t\"more than %i tags found; listed %i most recent\\n\"\n+\t\t\t\t\"gave up search at %s\\n\",\n+\t\t\t\tmax_candidates, max_candidates,\n+\t\t\t\tsha1_to_hex(gave_up_on->object.sha1));\n+\t\t}\n+\t}\n+\n+\t*depthp = all_matches[0].depth;\n+\treturn all_matches[0].name;\n+}\n-- \n1.5.3.5.529.ge3d6d\n"},{"id":"58275","messageId":"Pine.LNX.4.64.0711041435540.4362@racer.site","threadId":"10642","inReplyTo":"472DB199.2040305@lsrfire.ath.cx","subject":"Re: [PATCH 3/5] pretty describe: move library functions to the new file describe.c","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2007-11-04T14:36:44Z","receivedAt":"2007-11-04T14:36:44Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Sun, 4 Nov 2007, Ren? Scharfe wrote:\n\n>  Makefile           |    2 +-\n>  builtin-describe.c |  202 ---------------------------------------------------\n>  cache.h            |    5 ++\n>  describe.c         |  204 ++++++++++++++++++++++++++++++++++++++++++++++++++++\n>  4 files changed, 210 insertions(+), 203 deletions(-)\n>  create mode 100644 describe.c\n\nWould not \"format-patch -C -C\" have given a nicer output?\n\nCiao,\nDscho\n"},{"id":"58278","messageId":"472DDD80.2010203@lsrfire.ath.cx","threadId":"10642","inReplyTo":"Pine.LNX.4.64.0711041435540.4362@racer.site","subject":"Re: [PATCH 3/5] pretty describe: move library functions to the new file describe.c","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2007-11-04T14:56:00Z","receivedAt":"2007-11-04T14:56:00Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Johannes Schindelin schrieb:\n> Hi,\n> \n> On Sun, 4 Nov 2007, Ren? Scharfe wrote:\n> \n>>  Makefile           |    2 +-\n>>  builtin-describe.c |  202 ---------------------------------------------------\n>>  cache.h            |    5 ++\n>>  describe.c         |  204 ++++++++++++++++++++++++++++++++++++++++++++++++++++\n>>  4 files changed, 210 insertions(+), 203 deletions(-)\n>>  create mode 100644 describe.c\n> \n> Would not \"format-patch -C -C\" have given a nicer output?\n\nYes, it would have been shorter, but it looks a bit strange, because it's\ndeleting stuff from the new file describe.c (i.e. all the things left in\nbuiltin-describe.c):\n\n Makefile                         |    2 +-\n builtin-describe.c               |  202 --------------------------------------\n cache.h                          |    5 +\n builtin-describe.c => describe.c |   85 ----------------\n 4 files changed, 6 insertions(+), 288 deletions(-)\n copy builtin-describe.c => describe.c (66%)\n\nThat's 367 lines of patch + stat vs. 470 lines for the one I sent.  Will\nuse next time.\n\nThanks,\nRené\n"}]}