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

[PATCH] checkout: warn users if action is taken in ambiguous situation

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Aug 31, 2012, 14:38 UTC
Message-ID
<1346423934-7003-1-git-send-email-pclouds@gmail.com>

"git checkout foo" (no more arguments) always checks out existing branch "foo" even if "foo" exists on working directory. To avoid confusion to users who do not know this exception, say something along the action.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Personally I disagree with the comment above the top chunk about
 stopping annoying users in this case. I think the annoyance is a good
 sign for them to rename their branches to something more sensible.
 builtin/checkout.c |  6 ++++--
 cache.h            |  2 ++
 setup.c            | 21 +++++++++++++++++----
 3 files changed, 23 insertions(+), 6 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 7d922c6..7bc4e8a 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -899,8 +899,10 @@ 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)
-			verify_non_filename(NULL, arg);
+		const char *action = NULL;
+		if (!argc)
+			action = _("Checked out the new branch.");
+		verify_non_filename_gently(NULL, arg, action);
 	} else {
 		argcount++;
 		argv++;
diff --git a/cache.h b/cache.h
index 95daa69..95f11df 100644
--- a/cache.h
+++ b/cache.h
@@ -404,6 +404,8 @@ extern void verify_filename(const char *prefix,
 			    const char *name,
 			    int diagnose_misspelt_rev);
 extern void verify_non_filename(const char *prefix, const char *name);
+extern void verify_non_filename_gently(const char *prefix, const char *name,
+				       const char *preferred_action);
 extern int path_inside_repo(const char *prefix, const char *path);
 
 #define INIT_DB_QUIET 0x0001
diff --git a/setup.c b/setup.c
index 3a1b2fd..4b776cf 100644
--- a/setup.c
+++ b/setup.c
@@ -133,7 +133,8 @@ void verify_filename(const char *prefix,
  * and we parsed the arg as a refname.  It should not be interpretable
  * as a filename.
  */
-void verify_non_filename(const char *prefix, const char *arg)
+void verify_non_filename_gently(const char *prefix, const char *arg,
+				const char *preferred_action)
 {
 	if (!is_inside_work_tree() || is_inside_git_dir())
 		return;
@@ -141,9 +142,21 @@ void verify_non_filename(const char *prefix, const char *arg)
 		return; /* flag */
 	if (!check_filename(prefix, arg))
 		return;
-	die("ambiguous argument '%s': both revision and filename\n"
-	    "Use '--' to separate paths from revisions, like this:\n"
-	    "'git <command> [<revision>...] -- [<file>...]'", arg);
+	if (!preferred_action)
+		die(_("ambiguous argument '%s': both revision and filename\n"
+		      "Use '--' to separate paths from revisions, like this:\n"
+		      "'git <command> [<revision>...] -- [<file>...]'"), arg);
+	else {
+		warning(_("ambiguous argument '%s': both revision and filename\n"
+			  "Use '--' to separate paths from revisions, like this:\n"
+			  "'git <command> [<revision>...] -- [<file>...]'"), arg);
+		warning("%s", preferred_action);
+	}
+}
+
+void verify_non_filename(const char *prefix, const char *arg)
+{
+	verify_non_filename_gently(prefix, arg, NULL);
 }
 
 /*
-- 
1.7.12.rc2.18.g61b472e
Next: Junio C Hamano
Message 1 of 2 in “checkout: warn users if action is taken in ambiguous situation”
  1. checkout: warn users if action is taken in ambiguous situationNguyễn Thái Ngọc Duy, Aug 31, 2012
  2. Junio C HamanoAug 31, 2012

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.