Skip to content

Commit 1aa68d6

Browse files
author
Junio C Hamano
committed
show-branch: --current includes the current branch.
With this, the command includes the current branch to the list of revs to be shown when it is not given on the command line. This is handy to use in the configuration file like this: [showbranch] default = --current default = heads/* ; primary branches, not topics under ; subdirectories Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent ebedc31 commit 1aa68d6

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

Documentation/git-show-branch.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ git-show-branch - Show branches and their commits.
77

88
SYNOPSIS
99
--------
10-
'git-show-branch [--all] [--heads] [--tags] [--topo-order] [--more=<n> | --list | --independent | --merge-base] [--no-name | --sha1-name] [<rev> | <glob>]...'
10+
[verse]
11+
git-show-branch [--all] [--heads] [--tags] [--topo-order] [--current]
12+
[--more=<n> | --list | --independent | --merge-base]
13+
[--no-name | --sha1-name] [<rev> | <glob>]...
1114

1215
DESCRIPTION
1316
-----------
@@ -38,6 +41,11 @@ OPTIONS
3841
Show all refs under $GIT_DIR/refs, $GIT_DIR/refs/heads,
3942
and $GIT_DIR/refs/tags, respectively.
4043

44+
--current::
45+
With this option, the command includes the current
46+
branch to the list of revs to be shown when it is not
47+
given on the command line.
48+
4149
--topo-order::
4250
By default, the branches and their commits are shown in
4351
reverse chronological order. This option makes them
@@ -134,7 +142,8 @@ it, having the following in the configuration file may help:
134142
------------
135143

136144
With this,`git show-branch` without extra parameters would show
137-
only the primary branches.
145+
only the primary branches. In addition, if you happen to be on
146+
your topic branch, it is shown as well.
138147

139148

140149

show-branch.c

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "refs.h"
66

77
static const char show_branch_usage[] =
8-
"git-show-branch [--all] [--heads] [--tags] [--topo-order] [--more=count | --list | --independent | --merge-base ] [<refs>...]";
8+
"git-show-branch [--current] [--all] [--heads] [--tags] [--topo-order] [--more=count | --list | --independent | --merge-base ] [<refs>...]";
99

1010
static int default_num = 0;
1111
static int default_alloc = 0;
@@ -435,12 +435,12 @@ static void snarf_refs(int head, int tag)
435435
}
436436
}
437437

438-
static int rev_is_head(char *head_path, int headlen,
439-
char *name,
438+
static int rev_is_head(char *head_path, int headlen, char *name,
440439
unsigned char *head_sha1, unsigned char *sha1)
441440
{
442441
int namelen;
443-
if ((!head_path[0]) || memcmp(head_sha1, sha1, 20))
442+
if ((!head_path[0]) ||
443+
(head_sha1 && sha1 && memcmp(head_sha1, sha1, 20)))
444444
return 0;
445445
namelen = strlen(name);
446446
if ((headlen < namelen) ||
@@ -545,6 +545,7 @@ int main(int ac, char **av)
545545
int sha1_name = 0;
546546
int shown_merge_point = 0;
547547
int topo_order = 0;
548+
int with_current_branch = 0;
548549
int head_at = -1;
549550

550551
git_config(git_show_branch_config);
@@ -574,6 +575,8 @@ int main(int ac, char **av)
574575
extra = -1;
575576
else if (!strcmp(arg, "--no-name"))
576577
no_name = 1;
578+
else if (!strcmp(arg, "--current"))
579+
with_current_branch = 1;
577580
else if (!strcmp(arg, "--sha1-name"))
578581
sha1_name = 1;
579582
else if (!strncmp(arg, "--more=", 7))
@@ -605,6 +608,34 @@ int main(int ac, char **av)
605608
ac--; av++;
606609
}
607610

611+
head_path_p = resolve_ref(git_path("HEAD"), head_sha1, 1);
612+
if (head_path_p) {
613+
head_path_len = strlen(head_path_p);
614+
memcpy(head_path, head_path_p, head_path_len + 1);
615+
}
616+
else {
617+
head_path_len = 0;
618+
head_path[0] = 0;
619+
}
620+
621+
if (with_current_branch && head_path_p) {
622+
int has_head = 0;
623+
for (i = 0; !has_head && i < ref_name_cnt; i++) {
624+
/* We are only interested in adding the branch
625+
* HEAD points at.
626+
*/
627+
if (rev_is_head(head_path,
628+
head_path_len,
629+
ref_name[i],
630+
head_sha1, NULL))
631+
has_head++;
632+
}
633+
if (!has_head) {
634+
int pfxlen = strlen(git_path("refs/heads/"));
635+
append_one_rev(head_path + pfxlen);
636+
}
637+
}
638+
608639
if (!ref_name_cnt) {
609640
fprintf(stderr, "No revs to be shown.\n");
610641
exit(0);
@@ -640,16 +671,6 @@ int main(int ac, char **av)
640671
if (0 <= extra)
641672
join_revs(&list, &seen, num_rev, extra);
642673

643-
head_path_p = resolve_ref(git_path("HEAD"), head_sha1, 1);
644-
if (head_path_p) {
645-
head_path_len = strlen(head_path_p);
646-
memcpy(head_path, head_path_p, head_path_len + 1);
647-
}
648-
else {
649-
head_path_len = 0;
650-
head_path[0] = 0;
651-
}
652-
653674
if (merge_base)
654675
return show_merge_base(seen, num_rev);
655676

0 commit comments

Comments
 (0)