@@ -520,13 +520,6 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
520520 const char * interpreted_newname = NULL ;
521521 int recovery = 0 ;
522522
523- if (!oldname ) {
524- if (copy )
525- die (_ ("cannot copy the current branch while not on any." ));
526- else
527- die (_ ("cannot rename the current branch while not on any." ));
528- }
529-
530523 if (strbuf_check_branch_ref (& oldref , oldname )) {
531524 /*
532525 * Bad name --- this could be an attempt to rename a
@@ -827,24 +820,19 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
827820 strbuf_release (& buf );
828821
829822 return ret ;
830- } else if (copy ) {
831- if (!argc )
832- die (_ ("branch name required" ));
833- else if (argc == 1 )
834- copy_or_rename_branch (head , argv [0 ], 1 , copy > 1 );
835- else if (argc == 2 )
836- copy_or_rename_branch (argv [0 ], argv [1 ], 1 , copy > 1 );
837- else
838- die (_ ("too many branches for a copy operation" ));
839- } else if (rename ) {
823+ } else if (copy || rename ) {
840824 if (!argc )
841825 die (_ ("branch name required" ));
826+ else if ((argc == 1 ) && filter .detached )
827+ die (copy ? _ ("cannot copy the current branch while not on any." )
828+ : _ ("cannot rename the current branch while not on any." ));
842829 else if (argc == 1 )
843- copy_or_rename_branch (head , argv [0 ], 0 , rename > 1 );
830+ copy_or_rename_branch (head , argv [0 ], copy , copy + rename > 1 );
844831 else if (argc == 2 )
845- copy_or_rename_branch (argv [0 ], argv [1 ], 0 , rename > 1 );
832+ copy_or_rename_branch (argv [0 ], argv [1 ], copy , copy + rename > 1 );
846833 else
847- die (_ ("too many arguments for a rename operation" ));
834+ die (copy ? _ ("too many branches for a copy operation" )
835+ : _ ("too many arguments for a rename operation" ));
848836 } else if (new_upstream ) {
849837 struct branch * branch ;
850838 struct strbuf buf = STRBUF_INIT ;
0 commit comments