Skip to content

Commit 7e9ff00

Browse files
torvaldsgitster
authored andcommitted
git branch: clean up detached branch handling
Make the 'show detached branch info' a routine of its own. And in the process, avoid the object lookup that is unnecessary if the current branch isn't detached. Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 191d1ac commit 7e9ff00

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

builtin-branch.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ struct ref_item {
191191

192192
struct ref_list {
193193
struct rev_info revs;
194-
int index, alloc, maxwidth, verbose;
194+
int index, alloc, maxwidth, verbose, abbrev;
195195
struct ref_item *list;
196196
struct commit_list *with_commit;
197197
int kinds;
@@ -418,15 +418,34 @@ static int calc_maxwidth(struct ref_list *refs)
418418
return w;
419419
}
420420

421+
422+
static void show_detached(struct ref_list *ref_list)
423+
{
424+
struct commit *head_commit = lookup_commit_reference_gently(head_sha1, 1);
425+
426+
if (head_commit && is_descendant_of(head_commit, ref_list->with_commit)) {
427+
struct ref_item item;
428+
item.name = xstrdup("(no branch)");
429+
item.len = strlen(item.name);
430+
item.kind = REF_LOCAL_BRANCH;
431+
item.dest = NULL;
432+
item.commit = head_commit;
433+
if (item.len > ref_list->maxwidth)
434+
ref_list->maxwidth = item.len;
435+
print_ref_item(&item, ref_list->maxwidth, ref_list->verbose, ref_list->abbrev, 1, "");
436+
free(item.name);
437+
}
438+
}
439+
421440
static void print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit)
422441
{
423442
int i;
424443
struct ref_list ref_list;
425-
struct commit *head_commit = lookup_commit_reference_gently(head_sha1, 1);
426444

427445
memset(&ref_list, 0, sizeof(ref_list));
428446
ref_list.kinds = kinds;
429447
ref_list.verbose = verbose;
448+
ref_list.abbrev = abbrev;
430449
ref_list.with_commit = with_commit;
431450
if (merge_filter != NO_FILTER)
432451
init_revisions(&ref_list.revs, NULL);
@@ -446,19 +465,8 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev, str
446465
qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
447466

448467
detached = (detached && (kinds & REF_LOCAL_BRANCH));
449-
if (detached && head_commit &&
450-
is_descendant_of(head_commit, with_commit)) {
451-
struct ref_item item;
452-
item.name = xstrdup("(no branch)");
453-
item.len = strlen(item.name);
454-
item.kind = REF_LOCAL_BRANCH;
455-
item.dest = NULL;
456-
item.commit = head_commit;
457-
if (item.len > ref_list.maxwidth)
458-
ref_list.maxwidth = item.len;
459-
print_ref_item(&item, ref_list.maxwidth, verbose, abbrev, 1, "");
460-
free(item.name);
461-
}
468+
if (detached)
469+
show_detached(&ref_list);
462470

463471
for (i = 0; i < ref_list.index; i++) {
464472
int current = !detached &&

0 commit comments

Comments
 (0)