show-branch: use commit-slab for commit-name instead of commit->util

It's done so that commit->util can be removed. See more explanation in the commit that removes commit->util. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed May 19, 2018 at 07:28 UTC 60855a5343d4fb7b232ee7ed1e0aee18f312b9ec
1 file changed +27 -12
builtin/show-branch.c
+27 -12
@@ -7,6 +7,7 @@
7 #include "argv-array.h"
8 #include "parse-options.h"
9 #include "dir.h"
10 +#include "commit-slab.h"
11
12 static const char* show_branch_usage[] = {
13 N_("git show-branch [-a | --all] [-r | --remotes] [--topo-order | --date-order]\n"
@@ -59,15 +60,27 @@ struct commit_name {
60 int generation; /* how many parents away from head_name */
61 };
62
63 +define_commit_slab(commit_name_slab, struct commit_name *);
64 +static struct commit_name_slab name_slab;
65 +
66 +static struct commit_name *commit_to_name(struct commit *commit)
67 +{
68 + return *commit_name_slab_at(&name_slab, commit);
69 +}
70 +
71 +
72 /* Name the commit as nth generation ancestor of head_name;
73 * we count only the first-parent relationship for naming purposes.
74 */
75 static void name_commit(struct commit *commit, const char *head_name, int nth)
76 {
77 struct commit_name *name;
68 - if (!commit->util)
69 - commit->util = xmalloc(sizeof(struct commit_name));
70 - name = commit->util;
78 +
79 + name = *commit_name_slab_at(&name_slab, commit);
80 + if (!name) {
81 + name = xmalloc(sizeof(*name));
82 + *commit_name_slab_at(&name_slab, commit) = name;
83 + }
84 name->head_name = head_name;
85 name->generation = nth;
86 }
@@ -79,8 +92,8 @@ static void name_commit(struct commit *commit, const char *head_name, int nth)
92 */
93 static void name_parent(struct commit *commit, struct commit *parent)
94 {
82 - struct commit_name *commit_name = commit->util;
83 - struct commit_name *parent_name = parent->util;
95 + struct commit_name *commit_name = commit_to_name(commit);
96 + struct commit_name *parent_name = commit_to_name(parent);
97 if (!commit_name)
98 return;
99 if (!parent_name ||
@@ -94,12 +107,12 @@ static int name_first_parent_chain(struct commit *c)
107 int i = 0;
108 while (c) {
109 struct commit *p;
97 - if (!c->util)
110 + if (!commit_to_name(c))
111 break;
112 if (!c->parents)
113 break;
114 p = c->parents->item;
102 - if (!p->util) {
115 + if (!commit_to_name(p)) {
116 name_parent(c, p);
117 i++;
118 }
@@ -122,7 +135,7 @@ static void name_commits(struct commit_list *list,
135 /* First give names to the given heads */
136 for (cl = list; cl; cl = cl->next) {
137 c = cl->item;
125 - if (c->util)
138 + if (commit_to_name(c))
139 continue;
140 for (i = 0; i < num_rev; i++) {
141 if (rev[i] == c) {
@@ -148,9 +161,9 @@ static void name_commits(struct commit_list *list,
161 struct commit_name *n;
162 int nth;
163 c = cl->item;
151 - if (!c->util)
164 + if (!commit_to_name(c))
165 continue;
153 - n = c->util;
166 + n = commit_to_name(c);
167 parents = c->parents;
168 nth = 0;
169 while (parents) {
@@ -158,7 +171,7 @@ static void name_commits(struct commit_list *list,
171 struct strbuf newname = STRBUF_INIT;
172 parents = parents->next;
173 nth++;
161 - if (p->util)
174 + if (commit_to_name(p))
175 continue;
176 switch (n->generation) {
177 case 0:
@@ -271,7 +284,7 @@ static void show_one_commit(struct commit *commit, int no_name)
284 {
285 struct strbuf pretty = STRBUF_INIT;
286 const char *pretty_str = "(unavailable)";
274 - struct commit_name *name = commit->util;
287 + struct commit_name *name = commit_to_name(commit);
288
289 if (commit->object.parsed) {
290 pp_commit_easy(CMIT_FMT_ONELINE, commit, &pretty);
@@ -660,6 +673,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
673 OPT_END()
674 };
675
676 + init_commit_name_slab(&name_slab);
677 +
678 git_config(git_show_branch_config, NULL);
679
680 /* If nothing is specified, try the default first */