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>
maint
Nguyễn Thái Ngọc Duy 2018-05-19 07:28:27 +02:00 committed by Junio C Hamano
parent 8fd79a7304
commit 60855a5343
1 changed files with 27 additions and 12 deletions

View File

@ -7,6 +7,7 @@
#include "argv-array.h" #include "argv-array.h"
#include "parse-options.h" #include "parse-options.h"
#include "dir.h" #include "dir.h"
#include "commit-slab.h"


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


define_commit_slab(commit_name_slab, struct commit_name *);
static struct commit_name_slab name_slab;

static struct commit_name *commit_to_name(struct commit *commit)
{
return *commit_name_slab_at(&name_slab, commit);
}


/* Name the commit as nth generation ancestor of head_name; /* Name the commit as nth generation ancestor of head_name;
* we count only the first-parent relationship for naming purposes. * we count only the first-parent relationship for naming purposes.
*/ */
static void name_commit(struct commit *commit, const char *head_name, int nth) static void name_commit(struct commit *commit, const char *head_name, int nth)
{ {
struct commit_name *name; struct commit_name *name;
if (!commit->util)
commit->util = xmalloc(sizeof(struct commit_name)); name = *commit_name_slab_at(&name_slab, commit);
name = commit->util; if (!name) {
name = xmalloc(sizeof(*name));
*commit_name_slab_at(&name_slab, commit) = name;
}
name->head_name = head_name; name->head_name = head_name;
name->generation = nth; name->generation = nth;
} }
@ -79,8 +92,8 @@ static void name_commit(struct commit *commit, const char *head_name, int nth)
*/ */
static void name_parent(struct commit *commit, struct commit *parent) static void name_parent(struct commit *commit, struct commit *parent)
{ {
struct commit_name *commit_name = commit->util; struct commit_name *commit_name = commit_to_name(commit);
struct commit_name *parent_name = parent->util; struct commit_name *parent_name = commit_to_name(parent);
if (!commit_name) if (!commit_name)
return; return;
if (!parent_name || if (!parent_name ||
@ -94,12 +107,12 @@ static int name_first_parent_chain(struct commit *c)
int i = 0; int i = 0;
while (c) { while (c) {
struct commit *p; struct commit *p;
if (!c->util) if (!commit_to_name(c))
break; break;
if (!c->parents) if (!c->parents)
break; break;
p = c->parents->item; p = c->parents->item;
if (!p->util) { if (!commit_to_name(p)) {
name_parent(c, p); name_parent(c, p);
i++; i++;
} }
@ -122,7 +135,7 @@ static void name_commits(struct commit_list *list,
/* First give names to the given heads */ /* First give names to the given heads */
for (cl = list; cl; cl = cl->next) { for (cl = list; cl; cl = cl->next) {
c = cl->item; c = cl->item;
if (c->util) if (commit_to_name(c))
continue; continue;
for (i = 0; i < num_rev; i++) { for (i = 0; i < num_rev; i++) {
if (rev[i] == c) { if (rev[i] == c) {
@ -148,9 +161,9 @@ static void name_commits(struct commit_list *list,
struct commit_name *n; struct commit_name *n;
int nth; int nth;
c = cl->item; c = cl->item;
if (!c->util) if (!commit_to_name(c))
continue; continue;
n = c->util; n = commit_to_name(c);
parents = c->parents; parents = c->parents;
nth = 0; nth = 0;
while (parents) { while (parents) {
@ -158,7 +171,7 @@ static void name_commits(struct commit_list *list,
struct strbuf newname = STRBUF_INIT; struct strbuf newname = STRBUF_INIT;
parents = parents->next; parents = parents->next;
nth++; nth++;
if (p->util) if (commit_to_name(p))
continue; continue;
switch (n->generation) { switch (n->generation) {
case 0: case 0:
@ -271,7 +284,7 @@ static void show_one_commit(struct commit *commit, int no_name)
{ {
struct strbuf pretty = STRBUF_INIT; struct strbuf pretty = STRBUF_INIT;
const char *pretty_str = "(unavailable)"; const char *pretty_str = "(unavailable)";
struct commit_name *name = commit->util; struct commit_name *name = commit_to_name(commit);


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


init_commit_name_slab(&name_slab);

git_config(git_show_branch_config, NULL); git_config(git_show_branch_config, NULL);


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