branch: refactor width computation
Remove unnecessary variables from ref_list and ref_item which were used for width computation. This is to make ref_item similar to ref-filter's ref_array_item. This will ensure a smooth port of branch.c to use ref-filter APIs in further patches. Previously the maxwidth was computed when inserting the refs into the ref_list. Now, we obtain the entire ref_list and then compute maxwidth. Based-on-patch-by: Jeff King <peff@peff.net> Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
5242860f54
commit
1051e40dba
@ -282,14 +282,14 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
|
|||||||
struct ref_item {
|
struct ref_item {
|
||||||
char *name;
|
char *name;
|
||||||
char *dest;
|
char *dest;
|
||||||
unsigned int kind, width;
|
unsigned int kind;
|
||||||
struct commit *commit;
|
struct commit *commit;
|
||||||
int ignore;
|
int ignore;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ref_list {
|
struct ref_list {
|
||||||
struct rev_info revs;
|
struct rev_info revs;
|
||||||
int index, alloc, maxwidth, verbose, abbrev;
|
int index, alloc, verbose, abbrev;
|
||||||
struct ref_item *list;
|
struct ref_item *list;
|
||||||
struct commit_list *with_commit;
|
struct commit_list *with_commit;
|
||||||
int kinds;
|
int kinds;
|
||||||
@ -386,15 +386,8 @@ static int append_ref(const char *refname, const struct object_id *oid, int flag
|
|||||||
newitem->name = xstrdup(refname);
|
newitem->name = xstrdup(refname);
|
||||||
newitem->kind = kind;
|
newitem->kind = kind;
|
||||||
newitem->commit = commit;
|
newitem->commit = commit;
|
||||||
newitem->width = utf8_strwidth(refname);
|
|
||||||
newitem->dest = resolve_symref(orig_refname, prefix);
|
newitem->dest = resolve_symref(orig_refname, prefix);
|
||||||
newitem->ignore = 0;
|
newitem->ignore = 0;
|
||||||
/* adjust for "remotes/" */
|
|
||||||
if (newitem->kind == REF_REMOTE_BRANCH &&
|
|
||||||
ref_list->kinds != REF_REMOTE_BRANCH)
|
|
||||||
newitem->width += 8;
|
|
||||||
if (newitem->width > ref_list->maxwidth)
|
|
||||||
ref_list->maxwidth = newitem->width;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -505,11 +498,12 @@ static void add_verbose_info(struct strbuf *out, struct ref_item *item,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
|
static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
|
||||||
int abbrev, int current, char *prefix)
|
int abbrev, int current, const char *remote_prefix)
|
||||||
{
|
{
|
||||||
char c;
|
char c;
|
||||||
int color;
|
int color;
|
||||||
struct strbuf out = STRBUF_INIT, name = STRBUF_INIT;
|
struct strbuf out = STRBUF_INIT, name = STRBUF_INIT;
|
||||||
|
const char *prefix = "";
|
||||||
|
|
||||||
if (item->ignore)
|
if (item->ignore)
|
||||||
return;
|
return;
|
||||||
@ -520,6 +514,7 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
|
|||||||
break;
|
break;
|
||||||
case REF_REMOTE_BRANCH:
|
case REF_REMOTE_BRANCH:
|
||||||
color = BRANCH_COLOR_REMOTE;
|
color = BRANCH_COLOR_REMOTE;
|
||||||
|
prefix = remote_prefix;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
color = BRANCH_COLOR_PLAIN;
|
color = BRANCH_COLOR_PLAIN;
|
||||||
@ -557,16 +552,22 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
|
|||||||
strbuf_release(&out);
|
strbuf_release(&out);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int calc_maxwidth(struct ref_list *refs)
|
static int calc_maxwidth(struct ref_list *refs, int remote_bonus)
|
||||||
{
|
{
|
||||||
int i, w = 0;
|
int i, max = 0;
|
||||||
for (i = 0; i < refs->index; i++) {
|
for (i = 0; i < refs->index; i++) {
|
||||||
if (refs->list[i].ignore)
|
struct ref_item *it = &refs->list[i];
|
||||||
|
int w;
|
||||||
|
|
||||||
|
if (it->ignore)
|
||||||
continue;
|
continue;
|
||||||
if (refs->list[i].width > w)
|
w = utf8_strwidth(it->name);
|
||||||
w = refs->list[i].width;
|
if (it->kind == REF_REMOTE_BRANCH)
|
||||||
|
w += remote_bonus;
|
||||||
|
if (w > max)
|
||||||
|
max = w;
|
||||||
}
|
}
|
||||||
return w;
|
return max;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *get_head_description(void)
|
static char *get_head_description(void)
|
||||||
@ -600,21 +601,18 @@ static char *get_head_description(void)
|
|||||||
return strbuf_detach(&desc, NULL);
|
return strbuf_detach(&desc, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void show_detached(struct ref_list *ref_list)
|
static void show_detached(struct ref_list *ref_list, int maxwidth)
|
||||||
{
|
{
|
||||||
struct commit *head_commit = lookup_commit_reference_gently(head_sha1, 1);
|
struct commit *head_commit = lookup_commit_reference_gently(head_sha1, 1);
|
||||||
|
|
||||||
if (head_commit && is_descendant_of(head_commit, ref_list->with_commit)) {
|
if (head_commit && is_descendant_of(head_commit, ref_list->with_commit)) {
|
||||||
struct ref_item item;
|
struct ref_item item;
|
||||||
item.name = get_head_description();
|
item.name = get_head_description();
|
||||||
item.width = utf8_strwidth(item.name);
|
|
||||||
item.kind = REF_LOCAL_BRANCH;
|
item.kind = REF_LOCAL_BRANCH;
|
||||||
item.dest = NULL;
|
item.dest = NULL;
|
||||||
item.commit = head_commit;
|
item.commit = head_commit;
|
||||||
item.ignore = 0;
|
item.ignore = 0;
|
||||||
if (item.width > ref_list->maxwidth)
|
print_ref_item(&item, maxwidth, ref_list->verbose, ref_list->abbrev, 1, "");
|
||||||
ref_list->maxwidth = item.width;
|
|
||||||
print_ref_item(&item, ref_list->maxwidth, ref_list->verbose, ref_list->abbrev, 1, "");
|
|
||||||
free(item.name);
|
free(item.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -624,6 +622,16 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
|
|||||||
int i;
|
int i;
|
||||||
struct append_ref_cb cb;
|
struct append_ref_cb cb;
|
||||||
struct ref_list ref_list;
|
struct ref_list ref_list;
|
||||||
|
int maxwidth = 0;
|
||||||
|
const char *remote_prefix = "";
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If we are listing more than just remote branches,
|
||||||
|
* then remote branches will have a "remotes/" prefix.
|
||||||
|
* We need to account for this in the width.
|
||||||
|
*/
|
||||||
|
if (kinds != REF_REMOTE_BRANCH)
|
||||||
|
remote_prefix = "remotes/";
|
||||||
|
|
||||||
memset(&ref_list, 0, sizeof(ref_list));
|
memset(&ref_list, 0, sizeof(ref_list));
|
||||||
ref_list.kinds = kinds;
|
ref_list.kinds = kinds;
|
||||||
@ -667,26 +675,22 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
|
|||||||
clear_commit_marks(item->commit, ALL_REV_FLAGS);
|
clear_commit_marks(item->commit, ALL_REV_FLAGS);
|
||||||
}
|
}
|
||||||
clear_commit_marks(filter, ALL_REV_FLAGS);
|
clear_commit_marks(filter, ALL_REV_FLAGS);
|
||||||
|
|
||||||
if (verbose)
|
|
||||||
ref_list.maxwidth = calc_maxwidth(&ref_list);
|
|
||||||
}
|
}
|
||||||
|
if (verbose)
|
||||||
|
maxwidth = calc_maxwidth(&ref_list, strlen(remote_prefix));
|
||||||
|
|
||||||
qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
|
qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
|
||||||
|
|
||||||
detached = (detached && (kinds & REF_LOCAL_BRANCH));
|
detached = (detached && (kinds & REF_LOCAL_BRANCH));
|
||||||
if (detached && match_patterns(pattern, "HEAD"))
|
if (detached && match_patterns(pattern, "HEAD"))
|
||||||
show_detached(&ref_list);
|
show_detached(&ref_list, maxwidth);
|
||||||
|
|
||||||
for (i = 0; i < ref_list.index; i++) {
|
for (i = 0; i < ref_list.index; i++) {
|
||||||
int current = !detached &&
|
int current = !detached &&
|
||||||
(ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
|
(ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
|
||||||
!strcmp(ref_list.list[i].name, head);
|
!strcmp(ref_list.list[i].name, head);
|
||||||
char *prefix = (kinds != REF_REMOTE_BRANCH &&
|
print_ref_item(&ref_list.list[i], maxwidth, verbose,
|
||||||
ref_list.list[i].kind == REF_REMOTE_BRANCH)
|
abbrev, current, remote_prefix);
|
||||||
? "remotes/" : "";
|
|
||||||
print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose,
|
|
||||||
abbrev, current, prefix);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free_ref_list(&ref_list);
|
free_ref_list(&ref_list);
|
||||||
|
Loading…
Reference in New Issue
Block a user