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

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Fri, 31 Aug 2012 21:38:54 +0700

"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 <[email-masked]>
---
 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