Skip to content

Commit 6e0a2ca

Browse files
chooglengitster
authored andcommitted
builtin/branch: consolidate action-picking logic in cmd_branch()
Consolidate the logic for deciding when to create a new branch in cmd_branch(), and save the result for reuse. Besides making the function more explicit, this allows us to validate options that can only be used when creating a branch. Such an option does not exist yet, but one will be introduced in a subsequent commit. Helped-by: Jonathan Tan <[email protected]> Signed-off-by: Glen Choo <[email protected]> Reviewed-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3f3e760 commit 6e0a2ca

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

builtin/branch.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -616,14 +616,15 @@ static int edit_branch_description(const char *branch_name)
616616

617617
int cmd_branch(int argc, const char **argv, const char *prefix)
618618
{
619-
int delete = 0, rename = 0, copy = 0, force = 0, list = 0;
620-
int show_current = 0;
621-
int reflog = 0, edit_description = 0;
622-
int quiet = 0, unset_upstream = 0;
619+
/* possible actions */
620+
int delete = 0, rename = 0, copy = 0, list = 0,
621+
unset_upstream = 0, show_current = 0, edit_description = 0;
623622
const char *new_upstream = NULL;
623+
int noncreate_actions = 0;
624+
/* possible options */
625+
int reflog = 0, quiet = 0, icase = 0, force = 0;
624626
enum branch_track track;
625627
struct ref_filter filter;
626-
int icase = 0;
627628
static struct ref_sorting *sorting;
628629
struct string_list sorting_options = STRING_LIST_INIT_DUP;
629630
struct ref_format format = REF_FORMAT_INIT;
@@ -708,8 +709,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
708709
filter.reachable_from || filter.unreachable_from || filter.points_at.nr)
709710
list = 1;
710711

711-
if (!!delete + !!rename + !!copy + !!new_upstream + !!show_current +
712-
list + edit_description + unset_upstream > 1)
712+
noncreate_actions = !!delete + !!rename + !!copy + !!new_upstream +
713+
!!show_current + !!list + !!edit_description +
714+
!!unset_upstream;
715+
if (noncreate_actions > 1)
713716
usage_with_options(builtin_branch_usage, options);
714717

715718
if (filter.abbrev == -1)
@@ -849,7 +852,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
849852
strbuf_addf(&buf, "branch.%s.merge", branch->name);
850853
git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
851854
strbuf_release(&buf);
852-
} else if (argc > 0 && argc <= 2) {
855+
} else if (!noncreate_actions && argc > 0 && argc <= 2) {
853856
if (filter.kind != FILTER_REFS_BRANCHES)
854857
die(_("The -a, and -r, options to 'git branch' do not take a branch name.\n"
855858
"Did you mean to use: -a|-r --list <pattern>?"));

0 commit comments

Comments
 (0)