git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 15/18] parse_branchname_arg(): update code comments

From
AGAlexandr Miloslavskiy via GitGitGadget <gitgitgadget@gmail.com>
Date
Dec 19, 2019, 18:01 UTC
Message-ID
<7989f0c5cf813010d8034e932ee78959c7b39c95.1576778515.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.490.v3.git.1576778515.gitgitgadget@gmail.com>
From: Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com>
These parts repeat git documentation:
    ... if <something> is A...B <...>
    ... remote named in checkout.defaultRemote ...

Some parts repeat the code below. With next patch, code will be easier to understand, so this is no longer needed.

This is a separate patch to reduce the amount of diffs in next patch.
Signed-off-by: Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com>
---
 builtin/checkout.c | 86 +++++++++++-----------------------------------
 1 file changed, 21 insertions(+), 65 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 63f4bb4da6..95a8e08793 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1158,45 +1158,21 @@ static int parse_branchname_arg(int argc, const char **argv,
 	int i;
 
 	/*
-	 * case 1: git checkout <ref> -- [<paths>]
-	 *
-	 *   <ref> must be a valid tree, everything after the '--' must be
-	 *   a path.
-	 *
-	 * case 2: git checkout -- [<paths>]
-	 *
-	 *   everything after the '--' must be paths.
-	 *
-	 * case 3: git checkout <something> [--]
-	 *
-	 *   (a) If <something> is a commit, that is to
-	 *       switch to the branch or detach HEAD at it.  As a special case,
-	 *       if <something> is A...B (missing A or B means HEAD but you can
-	 *       omit at most one side), and if there is a unique merge base
-	 *       between A and B, A...B names that merge base.
-	 *
-	 *   (b) If <something> is _not_ a commit, either "--" is present
-	 *       or <something> is not a path, no -t or -b was given, and
-	 *       and there is a tracking branch whose name is <something>
-	 *       in one and only one remote (or if the branch exists on the
-	 *       remote named in checkout.defaultRemote), then this is a
-	 *       short-hand to fork local <something> from that
-	 *       remote-tracking branch.
-	 *
-	 *   (c) Otherwise, if "--" is present, treat it like case (1).
-	 *
-	 *   (d) Otherwise :
-	 *       - if it's a reference, treat it like case (1)
-	 *       - else if it's a path, treat it like case (2)
-	 *       - else: fail.
-	 *
-	 * case 4: git checkout <something> <paths>
-	 *
-	 *   The first argument must not be ambiguous.
-	 *   - If it's *only* a reference, treat it like case (1).
-	 *   - If it's only a path, treat it like case (2).
-	 *   - else: fail.
-	 *
+	 * Resolve ambiguity where argv[0] may be <pathspec> or <commit>.
+	 * High-level approach is:
+	 * 1) Use various things to reduce ambiguity, examples:
+	 *    * '--' is present
+	 *    * command doesn't accept <pathspec>
+	 *    * additional options like '-b' were given
+	 * 2) If ambiguous and matches both existing <commit> and existing
+	 *    file, complain. However, in 1-argument 'git checkout <arg>'
+	 *    treat as <commit> to avoid annoying users.
+	 * 3) Otherwise, if it matches some existing <commit>, treat as
+	 *    <commit>.
+	 * 4) Otherwise, if it matches a remote branch, and it's considered
+	 *    reasonable to DWIM to create a local branch from remote branch,
+	 *    do that and proceed with (2)(3).
+	 * 5) Otherwise, let caller proceed with <pathspec> interpretation.
 	 */
 	if (!argc)
 		return 0;
@@ -1218,9 +1194,9 @@ static int parse_branchname_arg(int argc, const char **argv,
 
 	if (opts->accept_pathspec) {
 		if (dash_dash_pos == 0)
-			return 1; /* case (2) */
+			return 1;
 		else if (dash_dash_pos == 1)
-			arg0_cant_be_pathspec = 1; /* case (3) or (1) */
+			arg0_cant_be_pathspec = 1;
 		else if (dash_dash_pos >= 2)
 			die(_("only one reference expected, %d given."), dash_dash_pos);
 	}
@@ -1231,14 +1207,6 @@ static int parse_branchname_arg(int argc, const char **argv,
 		arg = "@{-1}";
 
 	if (get_oid_mb(arg, rev)) {
-		/*
-		 * Either case (3) or (4), with <something> not being
-		 * a commit, or an attempt to use case (1) with an
-		 * invalid ref.
-		 *
-		 * It's likely an error, but we need to find out if
-		 * we should auto-create the branch, case (3).(b).
-		 */
 		int recover_with_dwim = dwim_new_local_branch_ok;
 
 		int could_be_checkout_paths = !arg0_cant_be_pathspec &&
@@ -1247,10 +1215,6 @@ static int parse_branchname_arg(int argc, const char **argv,
 		if (!arg0_cant_be_pathspec && !no_wildcard(arg))
 			recover_with_dwim = 0;
 
-		/*
-		 * Accept "git checkout foo", "git checkout foo --"
-		 * and "git switch foo" as candidates for dwim.
-		 */
 		if (!(argc == 1 && dash_dash_pos == -1) &&
 		    !(argc == 2 && dash_dash_pos == 1) &&
 		    opts->accept_pathspec)
@@ -1262,7 +1226,7 @@ static int parse_branchname_arg(int argc, const char **argv,
 			if (remote) {
 				*new_branch = arg;
 				arg = remote;
-				/* DWIMmed to create local branch, case (3).(b) */
+				/* DWIMmed to create local branch */
 			} else {
 				recover_with_dwim = 0;
 			}
@@ -1277,19 +1241,11 @@ static int parse_branchname_arg(int argc, const char **argv,
 
 	setup_new_branch_info_and_source_tree(new_branch_info, opts, rev, arg);
 
-	if (!opts->source_tree)                   /* case (1): want a tree */
+	if (!opts->source_tree)
 		die(_("reference is not a tree: %s"), arg);
 
-	if (!arg0_cant_be_pathspec) {	/* case (3).(d) -> (1) */
-		/*
-		 * Do not complain the most common case
-		 *	git checkout branch
-		 * even if there happen to be a file called 'branch';
-		 * it would be extremely annoying.
-		 */
-		if (argc > 1)
-			verify_non_filename(opts->prefix, arg);
-	}
+	if (!arg0_cant_be_pathspec && argc > 1)
+		verify_non_filename(opts->prefix, arg);
 
 	return (opts->accept_pathspec && dash_dash_pos == 1) ? 2 : 1;
 }
-- 
gitgitgadget
Previous: Alexandr Miloslavskiy via GitGitGadget
Message 67 of 67 in “Extend --pathspec-from-file to git add, checkout”
  1. 00/16 Extend --pathspec-from-file to git add, checkoutAlexandr Miloslavskiy via GitGitGadget, Dec 12, 2019
  2. 02/16 commit: forbid --pathspec-from-file --allAlexandr Miloslavskiy via GitGitGadget, Dec 12, 2019
  3. Phillip WoodDec 16, 2019
  4. Alexandr MiloslavskiyDec 16, 2019
  5. 01/16 t7107, t7526: directly test parse_pathspec_file()Alexandr Miloslavskiy via GitGitGadget, Dec 12, 2019
  6. 06/16 doc: checkout: fix broken text referenceAlexandr Miloslavskiy via GitGitGadget, Dec 12, 2019
  7. 03/16 cmd_add: prepare for next patchAlexandr Miloslavskiy via GitGitGadget, Dec 12, 2019
  8. 11/16 parse_branchname_arg(): easier to understand variablesAlexandr Miloslavskiy via GitGitGadget, Dec 12, 2019
  9. 08/16 doc: restore: synchronize <pathspec> descriptionAlexandr Miloslavskiy via GitGitGadget, Dec 12, 2019
  10. 10/16 checkout: die() on ambiguous tracking branchesAlexandr Miloslavskiy via GitGitGadget, Dec 12, 2019
  11. 14/16 parse_branchname_arg(): refactor the decision makingAlexandr Miloslavskiy via GitGitGadget, Dec 12, 2019
  12. 15/16 t2024: cover more casesAlexandr Miloslavskiy via GitGitGadget, Dec 12, 2019
  13. 13/16 parse_branchname_arg(): update code commentsAlexandr Miloslavskiy via GitGitGadget, Dec 12, 2019
  14. 09/16 parse_branchname_arg(): extract part as new functionAlexandr Miloslavskiy via GitGitGadget, Dec 12, 2019
  15. 16/16 checkout, restore: support the --pathspec-from-file optionAlexandr Miloslavskiy via GitGitGadget, Dec 12, 2019
  16. 12/16 parse_branchname_arg(): introduce expect_commit_onlyAlexandr Miloslavskiy via GitGitGadget, Dec 12, 2019
  17. 05/16 doc: checkout: remove duplicate synopsisAlexandr Miloslavskiy via GitGitGadget, Dec 12, 2019
  18. 04/16 add: support the --pathspec-from-file optionAlexandr Miloslavskiy via GitGitGadget, Dec 12, 2019
  19. 07/16 doc: checkout: synchronize <pathspec> descriptionAlexandr Miloslavskiy via GitGitGadget, Dec 12, 2019
  20. 00/18 Extend --pathspec-from-file to git add, checkoutAlexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  21. 02/18 t7526: add tests for error conditionsAlexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  22. Junio C HamanoDec 18, 2019
  23. Alexandr MiloslavskiyDec 19, 2019
  24. 04/18 commit: forbid --pathspec-from-file --allAlexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  25. Phillip WoodDec 17, 2019
  26. Junio C HamanoDec 18, 2019
  27. Junio C HamanoDec 18, 2019
  28. Junio C HamanoDec 18, 2019
  29. Alexandr MiloslavskiyDec 19, 2019
  30. 05/18 cmd_add: prepare for next patchAlexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  31. 06/18 add: support the --pathspec-from-file optionAlexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  32. 01/18 t7107, t7526: directly test parse_pathspec_file()Alexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  33. Junio C HamanoDec 18, 2019
  34. Junio C HamanoDec 19, 2019
  35. Alexandr MiloslavskiyDec 19, 2019
  36. 09/18 doc: checkout: synchronize <pathspec> descriptionAlexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  37. 08/18 doc: checkout: fix broken text referenceAlexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  38. 13/18 parse_branchname_arg(): easier to understand variablesAlexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  39. 14/18 parse_branchname_arg(): introduce expect_commit_onlyAlexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  40. 17/18 t2024: cover more casesAlexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  41. 15/18 parse_branchname_arg(): update code commentsAlexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  42. 18/18 checkout, restore: support the --pathspec-from-file optionAlexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  43. 12/18 checkout: die() on ambiguous tracking branchesAlexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  44. 10/18 doc: restore: synchronize <pathspec> descriptionAlexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  45. 11/18 parse_branchname_arg(): extract part as new functionAlexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  46. 07/18 doc: checkout: remove duplicate synopsisAlexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  47. 16/18 parse_branchname_arg(): refactor the decision makingAlexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  48. 03/18 t7107: add tests for error conditionsAlexandr Miloslavskiy via GitGitGadget, Dec 16, 2019
  49. 00/18 Extend --pathspec-from-file to git add, checkoutAlexandr Miloslavskiy via GitGitGadget, Dec 19, 2019
  50. 02/18 t7526: add tests for error conditionsAlexandr Miloslavskiy via GitGitGadget, Dec 19, 2019
  51. 01/18 t7107, t7526: directly test parse_pathspec_file()Alexandr Miloslavskiy via GitGitGadget, Dec 19, 2019
  52. 03/18 t7107: add tests for error conditionsAlexandr Miloslavskiy via GitGitGadget, Dec 19, 2019
  53. 04/18 commit: forbid --pathspec-from-file --allAlexandr Miloslavskiy via GitGitGadget, Dec 19, 2019
  54. 05/18 cmd_add: prepare for next patchAlexandr Miloslavskiy via GitGitGadget, Dec 19, 2019
  55. 07/18 doc: checkout: remove duplicate synopsisAlexandr Miloslavskiy via GitGitGadget, Dec 19, 2019
  56. 08/18 doc: checkout: fix broken text referenceAlexandr Miloslavskiy via GitGitGadget, Dec 19, 2019
  57. 09/18 doc: checkout: synchronize <pathspec> descriptionAlexandr Miloslavskiy via GitGitGadget, Dec 19, 2019
  58. 06/18 add: support the --pathspec-from-file optionAlexandr Miloslavskiy via GitGitGadget, Dec 19, 2019
  59. 11/18 parse_branchname_arg(): extract part as new functionAlexandr Miloslavskiy via GitGitGadget, Dec 19, 2019
  60. 10/18 doc: restore: synchronize <pathspec> descriptionAlexandr Miloslavskiy via GitGitGadget, Dec 19, 2019
  61. 13/18 parse_branchname_arg(): easier to understand variablesAlexandr Miloslavskiy via GitGitGadget, Dec 19, 2019
  62. 17/18 t2024: cover more casesAlexandr Miloslavskiy via GitGitGadget, Dec 19, 2019
  63. 16/18 parse_branchname_arg(): refactor the decision makingAlexandr Miloslavskiy via GitGitGadget, Dec 19, 2019
  64. 14/18 parse_branchname_arg(): simplify argument eatingAlexandr Miloslavskiy via GitGitGadget, Dec 19, 2019
  65. 12/18 checkout: die() on ambiguous tracking branchesAlexandr Miloslavskiy via GitGitGadget, Dec 19, 2019
  66. 18/18 checkout, restore: support the --pathspec-from-file optionAlexandr Miloslavskiy via GitGitGadget, Dec 19, 2019
  67. 15/18 parse_branchname_arg(): update code commentsAlexandr Miloslavskiy via GitGitGadget, Dec 19, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.