]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/branch: consolidate action-picking logic in cmd_branch()
authorGlen Choo <chooglen@google.com>
Sat, 29 Jan 2022 00:04:44 +0000 (16:04 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 1 Feb 2022 22:19:02 +0000 (14:19 -0800)
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 <jonathantanmy@google.com>
Signed-off-by: Glen Choo <chooglen@google.com>
Reviewed-by: Jonathan Tan <jonathantanmy@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/branch.c

index 0a49de0281f81e4a53fae45d62ad79f36df981b3..209b1cc442603c70dda0f86876f49986306b6ac8 100644 (file)
@@ -616,14 +616,15 @@ static int edit_branch_description(const char *branch_name)
 
 int cmd_branch(int argc, const char **argv, const char *prefix)
 {
-       int delete = 0, rename = 0, copy = 0, force = 0, list = 0;
-       int show_current = 0;
-       int reflog = 0, edit_description = 0;
-       int quiet = 0, unset_upstream = 0;
+       /* possible actions */
+       int delete = 0, rename = 0, copy = 0, list = 0,
+           unset_upstream = 0, show_current = 0, edit_description = 0;
        const char *new_upstream = NULL;
+       int noncreate_actions = 0;
+       /* possible options */
+       int reflog = 0, quiet = 0, icase = 0, force = 0;
        enum branch_track track;
        struct ref_filter filter;
-       int icase = 0;
        static struct ref_sorting *sorting;
        struct string_list sorting_options = STRING_LIST_INIT_DUP;
        struct ref_format format = REF_FORMAT_INIT;
@@ -708,8 +709,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
            filter.reachable_from || filter.unreachable_from || filter.points_at.nr)
                list = 1;
 
-       if (!!delete + !!rename + !!copy + !!new_upstream + !!show_current +
-           list + edit_description + unset_upstream > 1)
+       noncreate_actions = !!delete + !!rename + !!copy + !!new_upstream +
+                           !!show_current + !!list + !!edit_description +
+                           !!unset_upstream;
+       if (noncreate_actions > 1)
                usage_with_options(builtin_branch_usage, options);
 
        if (filter.abbrev == -1)
@@ -849,7 +852,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
                strbuf_addf(&buf, "branch.%s.merge", branch->name);
                git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
                strbuf_release(&buf);
-       } else if (argc > 0 && argc <= 2) {
+       } else if (!noncreate_actions && argc > 0 && argc <= 2) {
                if (filter.kind != FILTER_REFS_BRANCHES)
                        die(_("The -a, and -r, options to 'git branch' do not take a branch name.\n"
                                  "Did you mean to use: -a|-r --list <pattern>?"));