|
5 | 5 | #include "refs.h" |
6 | 6 |
|
7 | 7 | 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>...]"; |
9 | 9 |
|
10 | 10 | static int default_num = 0; |
11 | 11 | static int default_alloc = 0; |
@@ -435,12 +435,12 @@ static void snarf_refs(int head, int tag) |
435 | 435 | } |
436 | 436 | } |
437 | 437 |
|
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, |
440 | 439 | unsigned char *head_sha1, unsigned char *sha1) |
441 | 440 | { |
442 | 441 | 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))) |
444 | 444 | return 0; |
445 | 445 | namelen = strlen(name); |
446 | 446 | if ((headlen < namelen) || |
@@ -545,6 +545,7 @@ int main(int ac, char **av) |
545 | 545 | int sha1_name = 0; |
546 | 546 | int shown_merge_point = 0; |
547 | 547 | int topo_order = 0; |
| 548 | + int with_current_branch = 0; |
548 | 549 | int head_at = -1; |
549 | 550 |
|
550 | 551 | git_config(git_show_branch_config); |
@@ -574,6 +575,8 @@ int main(int ac, char **av) |
574 | 575 | extra = -1; |
575 | 576 | else if (!strcmp(arg, "--no-name")) |
576 | 577 | no_name = 1; |
| 578 | + else if (!strcmp(arg, "--current")) |
| 579 | + with_current_branch = 1; |
577 | 580 | else if (!strcmp(arg, "--sha1-name")) |
578 | 581 | sha1_name = 1; |
579 | 582 | else if (!strncmp(arg, "--more=", 7)) |
@@ -605,6 +608,34 @@ int main(int ac, char **av) |
605 | 608 | ac--; av++; |
606 | 609 | } |
607 | 610 |
|
| 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 | + |
608 | 639 | if (!ref_name_cnt) { |
609 | 640 | fprintf(stderr, "No revs to be shown.\n"); |
610 | 641 | exit(0); |
@@ -640,16 +671,6 @@ int main(int ac, char **av) |
640 | 671 | if (0 <= extra) |
641 | 672 | join_revs(&list, &seen, num_rev, extra); |
642 | 673 |
|
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 | | - |
653 | 674 | if (merge_base) |
654 | 675 | return show_merge_base(seen, num_rev); |
655 | 676 |
|
|
0 commit comments