Skip to content

Commit 60855a5

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]>
1 parent 8fd79a7 commit 60855a5

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

builtin/show-branch.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "argv-array.h"
88
#include "parse-options.h"
99
#include "dir.h"
10+
#include "commit-slab.h"
1011

1112
static const char* show_branch_usage[] = {
1213
N_("git show-branch [-a | --all] [-r | --remotes] [--topo-order | --date-order]\n"
@@ -59,15 +60,27 @@ struct commit_name {
5960
int generation; /* how many parents away from head_name */
6061
};
6162

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+
6272
/* Name the commit as nth generation ancestor of head_name;
6373
* we count only the first-parent relationship for naming purposes.
6474
*/
6575
static void name_commit(struct commit *commit, const char *head_name, int nth)
6676
{
6777
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+
}
7184
name->head_name = head_name;
7285
name->generation = nth;
7386
}
@@ -79,8 +92,8 @@ static void name_commit(struct commit *commit, const char *head_name, int nth)
7992
*/
8093
static void name_parent(struct commit *commit, struct commit *parent)
8194
{
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);
8497
if (!commit_name)
8598
return;
8699
if (!parent_name ||
@@ -94,12 +107,12 @@ static int name_first_parent_chain(struct commit *c)
94107
int i = 0;
95108
while (c) {
96109
struct commit *p;
97-
if (!c->util)
110+
if (!commit_to_name(c))
98111
break;
99112
if (!c->parents)
100113
break;
101114
p = c->parents->item;
102-
if (!p->util) {
115+
if (!commit_to_name(p)) {
103116
name_parent(c, p);
104117
i++;
105118
}
@@ -122,7 +135,7 @@ static void name_commits(struct commit_list *list,
122135
/* First give names to the given heads */
123136
for (cl = list; cl; cl = cl->next) {
124137
c = cl->item;
125-
if (c->util)
138+
if (commit_to_name(c))
126139
continue;
127140
for (i = 0; i < num_rev; i++) {
128141
if (rev[i] == c) {
@@ -148,17 +161,17 @@ static void name_commits(struct commit_list *list,
148161
struct commit_name *n;
149162
int nth;
150163
c = cl->item;
151-
if (!c->util)
164+
if (!commit_to_name(c))
152165
continue;
153-
n = c->util;
166+
n = commit_to_name(c);
154167
parents = c->parents;
155168
nth = 0;
156169
while (parents) {
157170
struct commit *p = parents->item;
158171
struct strbuf newname = STRBUF_INIT;
159172
parents = parents->next;
160173
nth++;
161-
if (p->util)
174+
if (commit_to_name(p))
162175
continue;
163176
switch (n->generation) {
164177
case 0:
@@ -271,7 +284,7 @@ static void show_one_commit(struct commit *commit, int no_name)
271284
{
272285
struct strbuf pretty = STRBUF_INIT;
273286
const char *pretty_str = "(unavailable)";
274-
struct commit_name *name = commit->util;
287+
struct commit_name *name = commit_to_name(commit);
275288

276289
if (commit->object.parsed) {
277290
pp_commit_easy(CMIT_FMT_ONELINE, commit, &pretty);
@@ -660,6 +673,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
660673
OPT_END()
661674
};
662675

676+
init_commit_name_slab(&name_slab);
677+
663678
git_config(git_show_branch_config, NULL);
664679

665680
/* If nothing is specified, try the default first */

0 commit comments

Comments
 (0)