@@ -443,6 +443,21 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
443
443
free (to_free );
444
444
}
445
445
446
+ static void print_current_branch_name (void )
447
+ {
448
+ int flags ;
449
+ const char * refname = resolve_ref_unsafe ("HEAD" , 0 , NULL , & flags );
450
+ const char * shortname ;
451
+ if (!refname )
452
+ die (_ ("could not resolve HEAD" ));
453
+ else if (!(flags & REF_ISSYMREF ))
454
+ return ;
455
+ else if (skip_prefix (refname , "refs/heads/" , & shortname ))
456
+ puts (shortname );
457
+ else
458
+ die (_ ("HEAD (%s) points outside of refs/heads/" ), refname );
459
+ }
460
+
446
461
static void reject_rebase_or_bisect_branch (const char * target )
447
462
{
448
463
struct worktree * * worktrees = get_worktrees (0 );
@@ -581,6 +596,7 @@ static int edit_branch_description(const char *branch_name)
581
596
int cmd_branch (int argc , const char * * argv , const char * prefix )
582
597
{
583
598
int delete = 0 , rename = 0 , copy = 0 , force = 0 , list = 0 ;
599
+ int show_current = 0 ;
584
600
int reflog = 0 , edit_description = 0 ;
585
601
int quiet = 0 , unset_upstream = 0 ;
586
602
const char * new_upstream = NULL ;
@@ -620,6 +636,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
620
636
OPT_BIT ('c' , "copy" , & copy , N_ ("copy a branch and its reflog" ), 1 ),
621
637
OPT_BIT ('C' , NULL , & copy , N_ ("copy a branch, even if target exists" ), 2 ),
622
638
OPT_BOOL ('l' , "list" , & list , N_ ("list branch names" )),
639
+ OPT_BOOL (0 , "show-current" , & show_current , N_ ("show current branch name" )),
623
640
OPT_BOOL (0 , "create-reflog" , & reflog , N_ ("create the branch's reflog" )),
624
641
OPT_BOOL (0 , "edit-description" , & edit_description ,
625
642
N_ ("edit the description for the branch" )),
@@ -662,14 +679,15 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
662
679
argc = parse_options (argc , argv , prefix , options , builtin_branch_usage ,
663
680
0 );
664
681
665
- if (!delete && !rename && !copy && !edit_description && !new_upstream && !unset_upstream && argc == 0 )
682
+ if (!delete && !rename && !copy && !edit_description && !new_upstream &&
683
+ !show_current && !unset_upstream && argc == 0 )
666
684
list = 1 ;
667
685
668
686
if (filter .with_commit || filter .merge != REF_FILTER_MERGED_NONE || filter .points_at .nr ||
669
687
filter .no_commit )
670
688
list = 1 ;
671
689
672
- if (!!delete + !!rename + !!copy + !!new_upstream +
690
+ if (!!delete + !!rename + !!copy + !!new_upstream + !! show_current +
673
691
list + unset_upstream > 1 )
674
692
usage_with_options (builtin_branch_usage , options );
675
693
@@ -697,6 +715,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
697
715
if (!argc )
698
716
die (_ ("branch name required" ));
699
717
return delete_branches (argc , argv , delete > 1 , filter .kind , quiet );
718
+ } else if (show_current ) {
719
+ print_current_branch_name ();
720
+ return 0 ;
700
721
} else if (list ) {
701
722
/* git branch --local also shows HEAD when it is detached */
702
723
if ((filter .kind & FILTER_REFS_BRANCHES ) && filter .detached )
0 commit comments