Skip to content

Commit 23e714d

Browse files
KarthikNayakgitster
authored andcommitted
branch: roll show_detached HEAD into regular ref_list
Remove show_detached() and make detached HEAD to be rolled into regular ref_list by adding REF_DETACHED_HEAD as a kind of branch and supporting the same in append_ref(). This eliminates the need for an extra function and helps in easier porting of branch.c to use ref-filter APIs. Before show_detached() used to check if the HEAD branch satisfies the '--contains' option, now that is taken care by append_ref(). Based-on-patch-by: Jeff King <[email protected]> Mentored-by: Christian Couder <[email protected]> Mentored-by: Matthieu Moy <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2dad24a commit 23e714d

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

builtin/branch.c

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ static const char * const builtin_branch_usage[] = {
2828
NULL
2929
};
3030

31-
#define REF_LOCAL_BRANCH 0x01
32-
#define REF_REMOTE_BRANCH 0x02
31+
#define REF_DETACHED_HEAD 0x01
32+
#define REF_LOCAL_BRANCH 0x02
33+
#define REF_REMOTE_BRANCH 0x04
3334

3435
static const char *head;
3536
static unsigned char head_sha1[20];
@@ -352,8 +353,12 @@ static int append_ref(const char *refname, const struct object_id *oid, int flag
352353
break;
353354
}
354355
}
355-
if (ARRAY_SIZE(ref_kind) <= i)
356-
return 0;
356+
if (ARRAY_SIZE(ref_kind) <= i) {
357+
if (!strcmp(refname, "HEAD"))
358+
kind = REF_DETACHED_HEAD;
359+
else
360+
return 0;
361+
}
357362

358363
/* Don't add types the caller doesn't want */
359364
if ((kind & ref_list->kinds) == 0)
@@ -535,6 +540,8 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
535540
int color;
536541
struct strbuf out = STRBUF_INIT, name = STRBUF_INIT;
537542
const char *prefix = "";
543+
const char *desc = item->name;
544+
char *to_free = NULL;
538545

539546
if (item->ignore)
540547
return;
@@ -547,6 +554,10 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
547554
color = BRANCH_COLOR_REMOTE;
548555
prefix = remote_prefix;
549556
break;
557+
case REF_DETACHED_HEAD:
558+
color = BRANCH_COLOR_CURRENT;
559+
desc = to_free = get_head_description();
560+
break;
550561
default:
551562
color = BRANCH_COLOR_PLAIN;
552563
break;
@@ -558,7 +569,7 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
558569
color = BRANCH_COLOR_CURRENT;
559570
}
560571

561-
strbuf_addf(&name, "%s%s", prefix, item->name);
572+
strbuf_addf(&name, "%s%s", prefix, desc);
562573
if (verbose) {
563574
int utf8_compensation = strlen(name.buf) - utf8_strwidth(name.buf);
564575
strbuf_addf(&out, "%c %s%-*s%s", c, branch_get_color(color),
@@ -581,6 +592,7 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
581592
}
582593
strbuf_release(&name);
583594
strbuf_release(&out);
595+
free(to_free);
584596
}
585597

586598
static int calc_maxwidth(struct ref_list *refs, int remote_bonus)
@@ -601,22 +613,6 @@ static int calc_maxwidth(struct ref_list *refs, int remote_bonus)
601613
return max;
602614
}
603615

604-
static void show_detached(struct ref_list *ref_list, int maxwidth)
605-
{
606-
struct commit *head_commit = lookup_commit_reference_gently(head_sha1, 1);
607-
608-
if (head_commit && is_descendant_of(head_commit, ref_list->with_commit)) {
609-
struct ref_item item;
610-
item.name = get_head_description();
611-
item.kind = REF_LOCAL_BRANCH;
612-
item.dest = NULL;
613-
item.commit = head_commit;
614-
item.ignore = 0;
615-
print_ref_item(&item, maxwidth, ref_list->verbose, ref_list->abbrev, 1, "");
616-
free(item.name);
617-
}
618-
}
619-
620616
static int print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit, const char **pattern)
621617
{
622618
int i;
@@ -643,7 +639,14 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
643639
cb.ref_list = &ref_list;
644640
cb.pattern = pattern;
645641
cb.ret = 0;
642+
/*
643+
* First we obtain all regular branch refs and if the HEAD is
644+
* detached then we insert that ref to the end of the ref_fist
645+
* so that it can be printed and removed first.
646+
*/
646647
for_each_rawref(append_ref, &cb);
648+
if (detached)
649+
head_ref(append_ref, &cb);
647650
/*
648651
* The following implementation is currently duplicated in ref-filter. It
649652
* will eventually be removed when we port branch.c to use ref-filter APIs.
@@ -681,14 +684,12 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
681684

682685
qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
683686

684-
detached = (detached && (kinds & REF_LOCAL_BRANCH));
685-
if (detached && match_patterns(pattern, "HEAD"))
686-
show_detached(&ref_list, maxwidth);
687-
688687
for (i = 0; i < ref_list.index; i++) {
689-
int current = !detached &&
690-
(ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
688+
int current = !detached && (ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
691689
!strcmp(ref_list.list[i].name, head);
690+
/* If detached the first ref_item is the current ref */
691+
if (detached && i == 0)
692+
current = 1;
692693
print_ref_item(&ref_list.list[i], maxwidth, verbose,
693694
abbrev, current, remote_prefix);
694695
}
@@ -914,7 +915,11 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
914915
die(_("branch name required"));
915916
return delete_branches(argc, argv, delete > 1, kinds, quiet);
916917
} else if (list) {
917-
int ret = print_ref_list(kinds, detached, verbose, abbrev,
918+
int ret;
919+
/* git branch --local also shows HEAD when it is detached */
920+
if (kinds & REF_LOCAL_BRANCH)
921+
kinds |= REF_DETACHED_HEAD;
922+
ret = print_ref_list(kinds, detached, verbose, abbrev,
918923
with_commit, argv);
919924
print_columns(&output, colopts, NULL);
920925
string_list_clear(&output, 0);

0 commit comments

Comments
 (0)