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

[PATCH v3 12/14] restore-files: take tree-ish from --from option instead

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Nov 29, 2018, 21:58 UTC
Message-ID
<20181129215850.7278-13-pclouds@gmail.com>
In-Reply-To
<20181129215850.7278-1-pclouds@gmail.com>

This is another departure from 'git checkout' syntax, which uses -- to separate ref and pathspec. The observation is restore-files (or "git checkout ,, <pathspec>") is most often used to restore some files from the index. If this is correct, we can simplify it by taking a way the ref, so that we can write

    git restore-files some-file
without worrying about some-file being a ref and whether we need to do
    git restore-files -- some-file

for safety. If the source of the restore comes from a tree, it will be in the form of an option with value, e.g.

    git restore-files --from=this-tree some-file

This is of course longer to type than using "--". But hopefully it will not be used as often, and it is clearly easier to understand.

dwim_new_local_branch is no longer set (or unset) in cmd_restore_files() because it's irrelevant because we don't really care about dwim-ing. With accept_ref being unset, dwim can't happen.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/checkout.c | 41 ++++++++++++++++++++++++++++++++++-------
 1 file changed, 34 insertions(+), 7 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index fbfebba2d9..7ff9951818 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -39,7 +39,7 @@ static const char * const switch_branch_usage[] = {
 };
 
 static const char * const restore_files_usage[] = {
-	N_("git restore-files [<options>] [<branch>] -- <file>..."),
+	N_("git restore-files [<options>] [--from=<branch>] <file>..."),
 	NULL,
 };
 
@@ -56,6 +56,7 @@ struct checkout_opts {
 	int ignore_other_worktrees;
 	int show_progress;
 	int dwim_new_local_branch;
+	int accept_ref;
 	int accept_pathspec;
 	int switch_branch_doing_nothing_not_ok;
 
@@ -75,6 +76,7 @@ struct checkout_opts {
 	int branch_exists;
 	const char *prefix;
 	struct pathspec pathspec;
+	const char *from_treeish;
 	struct tree *source_tree;
 };
 
@@ -1337,6 +1339,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
 {
 	struct branch_info new_branch_info;
 	int dwim_remotes_matched = 0;
+	int parseopt_flags = 0;
 
 	memset(&new_branch_info, 0, sizeof(new_branch_info));
 	opts->overwrite_ignore = 1;
@@ -1347,8 +1350,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
 
 	opts->track = BRANCH_TRACK_UNSPECIFIED;
 
-	argc = parse_options(argc, argv, prefix, options, usagestr,
-			     PARSE_OPT_KEEP_DASHDASH);
+	if (!opts->accept_pathspec && !opts->accept_ref)
+		BUG("make up your mind, you need to take _something_");
+	if (opts->accept_pathspec && opts->accept_ref)
+		parseopt_flags = PARSE_OPT_KEEP_DASHDASH;
+
+	argc = parse_options(argc, argv, prefix, options,
+			     usagestr, parseopt_flags);
 
 	if (opts->show_progress < 0) {
 		if (opts->quiet)
@@ -1402,7 +1410,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
 	 * including "last branch" syntax and DWIM-ery for names of
 	 * remote branches, erroring out for invalid or ambiguous cases.
 	 */
-	if (argc) {
+	if (argc && opts->accept_ref && opts->accept_pathspec) {
 		struct object_id rev;
 		int dwim_ok =
 			!opts->patch_mode &&
@@ -1414,6 +1422,18 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
 					     &dwim_remotes_matched);
 		argv += n;
 		argc -= n;
+	} else if (!opts->accept_ref && opts->from_treeish) {
+		struct object_id rev;
+
+		if (get_oid_mb(opts->from_treeish, &rev))
+			die(_("could not resolve %s"), opts->from_treeish);
+
+		setup_new_branch_info_and_source_tree(&new_branch_info,
+						      opts, &rev,
+						      opts->from_treeish);
+
+		if (!opts->source_tree)
+			die(_("reference is not a tree: %s"), opts->from_treeish);
 	}
 
 	if (argc) {
@@ -1492,6 +1512,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 	memset(&opts, 0, sizeof(opts));
 	opts.dwim_new_local_branch = 1;
 	opts.switch_branch_doing_nothing_not_ok = 0;
+	opts.accept_ref = 1;
 	opts.accept_pathspec = 1;
 	opts.implicit_detach = 1;
 
@@ -1520,6 +1541,7 @@ int cmd_switch_branch(int argc, const char **argv, const char *prefix)
 
 	memset(&opts, 0, sizeof(opts));
 	opts.dwim_new_local_branch = 1;
+	opts.accept_ref = 1;
 	opts.accept_pathspec = 0;
 	opts.switch_branch_doing_nothing_not_ok = 1;
 	opts.implicit_detach = 0;
@@ -1537,14 +1559,19 @@ int cmd_switch_branch(int argc, const char **argv, const char *prefix)
 int cmd_restore_files(int argc, const char **argv, const char *prefix)
 {
 	struct checkout_opts opts;
-	struct option *options = NULL;
+	struct option *options;
+	struct option restore_options[] = {
+		OPT_STRING(0, "from", &opts.from_treeish, "<tree-ish>",
+			   N_("where the checkout from")),
+		OPT_END()
+	};
 	int ret;
 
 	memset(&opts, 0, sizeof(opts));
-	opts.dwim_new_local_branch = 1;
+	opts.accept_ref = 0;
 	opts.accept_pathspec = 1;
 
-	options = parse_options_dup(options);
+	options = parse_options_dup(restore_options);
 	options = add_common_options(&opts, options);
 	options = add_checkout_path_options(&opts, options);
 
-- 
2.20.0.rc1.380.g3eb999425c.dirty
Previous: Duy NguyenNext: Nguyễn Thái Ngọc Duy
Message 65 of 110 in “checkout: print something when checking out paths”
  1. checkout: print something when checking out pathsNguyễn Thái Ngọc Duy, Nov 10, 2018
  2. Junio C HamanoNov 12, 2018
  3. Duy NguyenNov 12, 2018
  4. Junio C HamanoNov 12, 2018
  5. Ævar Arnfjörð BjarmasonNov 19, 2018
  6. Duy NguyenNov 19, 2018
  7. Junio C HamanoNov 20, 2018
  8. [RFC] Introduce two new commands, switch-branch and restore-pathsDuy Nguyen, Nov 20, 2018
  9. Thomas GummererNov 25, 2018
  10. Junio C HamanoNov 26, 2018
  11. Duy NguyenNov 26, 2018
  12. Ævar Arnfjörð BjarmasonNov 26, 2018
  13. Duy NguyenNov 26, 2018
  14. Stefan BellerNov 26, 2018
  15. Junio C HamanoNov 27, 2018
  16. 0/7 Introduce new commands switch-branch and checkout-filesNguyễn Thái Ngọc Duy, Nov 27, 2018
  17. 1/7 parse-options: allow parse_options_concat(NULL, options)Nguyễn Thái Ngọc Duy, Nov 27, 2018
  18. Stefan BellerNov 27, 2018
  19. Duy NguyenNov 28, 2018
  20. Junio C HamanoNov 28, 2018
  21. 2/7 checkout: make "opts" in cmd_checkout() a pointerNguyễn Thái Ngọc Duy, Nov 27, 2018
  22. 5/7 checkout: split options[] array in three piecesNguyễn Thái Ngọc Duy, Nov 27, 2018
  23. Junio C HamanoNov 29, 2018
  24. 6/7 checkout: split into switch-branch and checkout-filesNguyễn Thái Ngọc Duy, Nov 27, 2018
  25. Junio C HamanoNov 28, 2018
  26. Duy NguyenNov 28, 2018
  27. Stefan BellerNov 28, 2018
  28. Duy NguyenNov 28, 2018
  29. Junio C HamanoNov 29, 2018
  30. Stefan XenosNov 28, 2018
  31. Stefan XenosNov 28, 2018
  32. Stefan XenosNov 28, 2018
  33. Junio C HamanoNov 29, 2018
  34. Duy NguyenNov 29, 2018
  35. Duy NguyenNov 29, 2018
  36. Stefan BellerNov 29, 2018
  37. Duy NguyenNov 29, 2018
  38. Stefan XenosNov 29, 2018
  39. 4/7 checkout: move dwim_new_local_branch to checkout_optsNguyễn Thái Ngọc Duy, Nov 27, 2018
  40. Stefan BellerNov 27, 2018
  41. 7/7 Suggest other commands instead of "git checkout"Nguyễn Thái Ngọc Duy, Nov 27, 2018
  42. Junio C HamanoNov 28, 2018
  43. Duy NguyenNov 28, 2018
  44. Junio C HamanoNov 29, 2018
  45. 3/7 checkout: move 'confict_style' to checkout_optsNguyễn Thái Ngọc Duy, Nov 27, 2018
  46. Stefan BellerNov 27, 2018
  47. Duy NguyenNov 28, 2018
  48. Duy NguyenNov 28, 2018
  49. Stefan BellerNov 28, 2018
  50. Duy NguyenNov 29, 2018
  51. Stefan BellerDec 3, 2018
  52. Junio C HamanoNov 30, 2018
  53. 00/14 Introduce new commands switch-branch and restore-filesNguyễn Thái Ngọc Duy, Nov 29, 2018
  54. 01/14 git-checkout.txt: fix one syntax lineNguyễn Thái Ngọc Duy, Nov 29, 2018
  55. 03/14 checkout: factor out some code in parse_branchname_arg()Nguyễn Thái Ngọc Duy, Nov 29, 2018
  56. 04/14 checkout: make "opts" in cmd_checkout() a pointerNguyễn Thái Ngọc Duy, Nov 29, 2018
  57. 05/14 checkout: move 'confict_style' and 'dwim_..' to checkout_optsNguyễn Thái Ngọc Duy, Nov 29, 2018
  58. 06/14 checkout: split options[] array in three piecesNguyễn Thái Ngọc Duy, Nov 29, 2018
  59. 08/14 switch-branch: better names for -b and -BNguyễn Thái Ngọc Duy, Nov 29, 2018
  60. 09/14 switch-branch: stop accepting pathspecNguyễn Thái Ngọc Duy, Nov 29, 2018
  61. 10/14 switch-branch: reject "do nothing" caseNguyễn Thái Ngọc Duy, Nov 29, 2018
  62. 11/14 switch-branch: only allow explicit detached HEADNguyễn Thái Ngọc Duy, Nov 29, 2018
  63. Eckhard MaaßMar 10, 2019
  64. Duy NguyenMar 11, 2019
  65. 12/14 restore-files: take tree-ish from --from option insteadNguyễn Thái Ngọc Duy, Nov 29, 2018
  66. 13/14 restore-files: make pathspec mandatoryNguyễn Thái Ngọc Duy, Nov 29, 2018
  67. 14/14 doc: promote "git switch-branch" and "git restore-files"Nguyễn Thái Ngọc Duy, Nov 29, 2018
  68. 07/14 checkout: split into switch-branch and restore-filesNguyễn Thái Ngọc Duy, Nov 29, 2018
  69. Elijah NewrenDec 4, 2018
  70. Junio C HamanoDec 4, 2018
  71. Duy NguyenDec 4, 2018
  72. Elijah NewrenDec 4, 2018
  73. Duy NguyenDec 4, 2018
  74. Junio C HamanoDec 5, 2018
  75. Elijah NewrenDec 5, 2018
  76. Junio C HamanoDec 5, 2018
  77. Junio C HamanoDec 5, 2018
  78. Elijah NewrenDec 5, 2018
  79. 02/14 git-checkout.txt: split detached head section outNguyễn Thái Ngọc Duy, Nov 29, 2018
  80. Ævar Arnfjörð BjarmasonNov 29, 2018
  81. Ævar Arnfjörð BjarmasonNov 29, 2018
  82. Dan FabulichNov 29, 2018
  83. Dan FabulichNov 30, 2018
  84. Duy NguyenNov 30, 2018
  85. Duy NguyenNov 30, 2018
  86. Junio C HamanoNov 30, 2018
  87. Ævar Arnfjörð BjarmasonNov 30, 2018
  88. Duy NguyenNov 30, 2018
  89. Junio C HamanoNov 30, 2018
  90. Duy NguyenNov 30, 2018
  91. Junio C HamanoNov 30, 2018
  92. Thomas GummererDec 2, 2018
  93. Junio C HamanoDec 2, 2018
  94. Elijah NewrenDec 4, 2018
  95. Duy NguyenDec 4, 2018
  96. Elijah NewrenDec 4, 2018
  97. Duy NguyenDec 4, 2018
  98. Elijah NewrenDec 4, 2018
  99. Duy NguyenDec 4, 2018
  100. Eric SunshineDec 4, 2018
  101. checkout: print something when checking out pathsNguyễn Thái Ngọc Duy, Nov 13, 2018
  102. Junio C HamanoNov 14, 2018
  103. Duy NguyenNov 14, 2018
  104. Junio C HamanoJan 28, 2019
  105. Duy NguyenJan 29, 2019
  106. 0/2 nd/checkout-noisy updatesNguyễn Thái Ngọc Duy, Feb 6, 2019
  107. 1/2 checkout: update count-checkouts messagesNguyễn Thái Ngọc Duy, Feb 6, 2019
  108. 2/2 checkout: count and print -m paths separatelyNguyễn Thái Ngọc Duy, Feb 6, 2019
  109. Stefan XenosNov 28, 2018
  110. Junio C HamanoNov 29, 2018

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.