From: "Alexandr Miloslavskiy via GitGitGadget" <gitgitgadget@gmail.com> To: git@vger.kernel.org Cc: Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com>, Junio C Hamano <gitster@pobox.com>, Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com> Subject: [PATCH 1/5] parse_branchname_arg(): fix dash_dash_pos, drop argcount Date: Thu, 28 Nov 2019 19:32:14 +0000 Message-ID: <771947d01f759a72b9f8999e929bf1f7765327aa.1574969538.git.gitgitgadget@gmail.com> (raw) In-Reply-To: <pull.479.git.1574969538.gitgitgadget@gmail.com> From: Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com> `dash_dash_pos` was only calculated under `opts->accept_pathspec`. This is unexpected to readers and made it harder to reason about the code. Fix this by restoring the expected meaning. Simplify the code by dropping `argcount` and useless `argc` / `argv` manipulations. This should not change behavior in any way. Signed-off-by: Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com> --- builtin/checkout.c | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/builtin/checkout.c b/builtin/checkout.c index 3634a3dac1..655b389756 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -1121,7 +1121,6 @@ static int parse_branchname_arg(int argc, const char **argv, int *dwim_remotes_matched) { const char **new_branch = &opts->new_branch; - int argcount = 0; const char *arg; int dash_dash_pos; int has_dash_dash = 0; @@ -1180,17 +1179,21 @@ static int parse_branchname_arg(int argc, const char **argv, arg = argv[0]; dash_dash_pos = -1; for (i = 0; i < argc; i++) { - if (opts->accept_pathspec && !strcmp(argv[i], "--")) { + if (!strcmp(argv[i], "--")) { dash_dash_pos = i; break; } } - if (dash_dash_pos == 0) - return 1; /* case (2) */ - else if (dash_dash_pos == 1) - has_dash_dash = 1; /* case (3) or (1) */ - else if (dash_dash_pos >= 2) - die(_("only one reference expected, %d given."), dash_dash_pos); + + if (opts->accept_pathspec) { + if (dash_dash_pos == 0) + return 1; /* case (2) */ + else if (dash_dash_pos == 1) + has_dash_dash = 1; /* case (3) or (1) */ + else if (dash_dash_pos >= 2) + die(_("only one reference expected, %d given."), dash_dash_pos); + } + opts->count_checkout_paths = !opts->quiet && !has_dash_dash; if (!strcmp(arg, "-")) @@ -1241,15 +1244,10 @@ static int parse_branchname_arg(int argc, const char **argv, if (!recover_with_dwim) { if (has_dash_dash) die(_("invalid reference: %s"), arg); - return argcount; + return 0; } } - /* we can't end up being in (2) anymore, eat the argument */ - argcount++; - argv++; - argc--; - setup_new_branch_info_and_source_tree(new_branch_info, opts, rev, arg); if (!opts->source_tree) /* case (1): want a tree */ @@ -1262,15 +1260,11 @@ static int parse_branchname_arg(int argc, const char **argv, * even if there happen to be a file called 'branch'; * it would be extremely annoying. */ - if (argc) + if (argc > 1) verify_non_filename(opts->prefix, arg); - } else if (opts->accept_pathspec) { - argcount++; - argv++; - argc--; } - return argcount; + return (dash_dash_pos == 1) ? 2 : 1; } static int switch_unborn_to_new_branch(const struct checkout_opts *opts) -- gitgitgadget
next prev parent reply other threads:[~2019-11-28 19:32 UTC|newest] Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-11-28 19:32 [PATCH 0/5] parse_branchname_arg(): make code easier to understand Alexandr Miloslavskiy via GitGitGadget 2019-11-28 19:32 ` Alexandr Miloslavskiy via GitGitGadget [this message] 2019-12-18 18:52 ` [PATCH 1/5] parse_branchname_arg(): fix dash_dash_pos, drop argcount Junio C Hamano 2019-12-19 18:03 ` Alexandr Miloslavskiy 2019-11-28 19:32 ` [PATCH 2/5] parse_branchname_arg(): introduce expect_commit_only Alexandr Miloslavskiy via GitGitGadget 2019-12-18 19:18 ` Junio C Hamano 2019-12-19 18:03 ` Alexandr Miloslavskiy 2019-11-28 19:32 ` [PATCH 3/5] parse_branchname_arg(): update code comments Alexandr Miloslavskiy via GitGitGadget 2019-11-28 19:32 ` [PATCH 4/5] parse_branchname_arg(): refactor the decision making Alexandr Miloslavskiy via GitGitGadget 2019-11-28 19:32 ` [PATCH 5/5] t2024: cover more cases Alexandr Miloslavskiy via GitGitGadget
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style List information: http://vger.kernel.org/majordomo-info.html * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=771947d01f759a72b9f8999e929bf1f7765327aa.1574969538.git.gitgitgadget@gmail.com \ --to=gitgitgadget@gmail.com \ --cc=alexandr.miloslavskiy@syntevo.com \ --cc=git@vger.kernel.org \ --cc=gitster@pobox.com \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
git@vger.kernel.org list mirror (unofficial, one of many) This inbox may be cloned and mirrored by anyone: git clone --mirror https://public-inbox.org/git git clone --mirror http://ou63pmih66umazou.onion/git git clone --mirror http://czquwvybam4bgbro.onion/git git clone --mirror http://hjrcffqmbrq6wope.onion/git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V1 git git/ https://public-inbox.org/git \ git@vger.kernel.org public-inbox-index git Example config snippet for mirrors. Newsgroups are available over NNTP: nntp://news.public-inbox.org/inbox.comp.version-control.git nntp://ou63pmih66umazou.onion/inbox.comp.version-control.git nntp://czquwvybam4bgbro.onion/inbox.comp.version-control.git nntp://hjrcffqmbrq6wope.onion/inbox.comp.version-control.git nntp://news.gmane.io/gmane.comp.version-control.git note: .onion URLs require Tor: https://www.torproject.org/ code repositories for the project(s) associated with this inbox: https://80x24.org/mirrors/git.git AGPL code for this site: git clone https://public-inbox.org/public-inbox.git