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 <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>
Karthik Nayak committed
Sep 23, 2015 at 23:41 UTC
23e714df91cb8d824f6fd0594cd80c8a7dd9b751
1 file changed
+33
-28
builtin/branch.c
+33
-28
@@ -28,8 +28,9 @@ static const char * const builtin_branch_usage[] = {
28
NULL
29
};
30
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
34
35
static const char *head;
36
static unsigned char head_sha1[20];
@@ -352,8 +353,12 @@ static int append_ref(const char *refname, const struct object_id *oid, int flag
353
break;
354
}
355
}
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
+ }
362
363
/* Don't add types the caller doesn't want */
364
if ((kind & ref_list->kinds) == 0)
@@ -535,6 +540,8 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
540
int color;
541
struct strbuf out = STRBUF_INIT, name = STRBUF_INIT;
542
const char *prefix = "";
543
+ const char *desc = item->name;
544
+ char *to_free = NULL;
545
546
if (item->ignore)
547
return;
@@ -547,6 +554,10 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
554
color = BRANCH_COLOR_REMOTE;
555
prefix = remote_prefix;
556
break;
557
+ case REF_DETACHED_HEAD:
558
+ color = BRANCH_COLOR_CURRENT;
559
+ desc = to_free = get_head_description();
560
+ break;
561
default:
562
color = BRANCH_COLOR_PLAIN;
563
break;
@@ -558,7 +569,7 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
569
color = BRANCH_COLOR_CURRENT;
570
}
571
561
- strbuf_addf(&name, "%s%s", prefix, item->name);
572
+ strbuf_addf(&name, "%s%s", prefix, desc);
573
if (verbose) {
574
int utf8_compensation = strlen(name.buf) - utf8_strwidth(name.buf);
575
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,
592
}
593
strbuf_release(&name);
594
strbuf_release(&out);
595
+ free(to_free);
596
}
597
598
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)
613
return max;
614
}
615
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
-
616
static int print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit, const char **pattern)
617
{
618
int i;
@@ -643,7 +639,14 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
639
cb.ref_list = &ref_list;
640
cb.pattern = pattern;
641
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
+ */
647
for_each_rawref(append_ref, &cb);
648
+ if (detached)
649
+ head_ref(append_ref, &cb);
650
/*
651
* The following implementation is currently duplicated in ref-filter. It
652
* 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
684
685
qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
686
684
- detached = (detached && (kinds & REF_LOCAL_BRANCH));
685
- if (detached && match_patterns(pattern, "HEAD"))
686
- show_detached(&ref_list, maxwidth);
687
-
687
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) &&
689
!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;
693
print_ref_item(&ref_list.list[i], maxwidth, verbose,
694
abbrev, current, remote_prefix);
695
}
@@ -914,7 +915,11 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
915
die(_("branch name required"));
916
return delete_branches(argc, argv, delete > 1, kinds, quiet);
917
} 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,
923
with_commit, argv);
924
print_columns(&output, colopts, NULL);
925
string_list_clear(&output, 0);