Patchwork [2,of,2] pathutil: hint if a path is root relative instead of cwd relative (issue4663)

login
register
mail settings
Submitter Matt Harbison
Date May 12, 2015, 2:51 a.m.
Message ID <99f04b658814675825ad.1431399112@Envy>
Download mbox | patch
Permalink /patch/9020/
State Accepted
Delegated to: Pierre-Yves David
Headers show

Comments

Matt Harbison - May 12, 2015, 2:51 a.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1431393973 14400
#      Mon May 11 21:26:13 2015 -0400
# Node ID 99f04b658814675825ad17d58524d1f3ecf8f6b9
# Parent  835b2b100c94079bf2a47adbe3381fa351cbdc9b
pathutil: hint if a path is root relative instead of cwd relative (issue4663)

Given that this path is going to abort, it seems OK to spend the time to do an
alternate lookup to better inform the user.  The path returned by util.pathto()
ends with '/' at least in some cases, so os.path.relpath() is used instead,
which requires python 2.6.
Matt Mackall - May 12, 2015, 9:30 p.m.
On Mon, 2015-05-11 at 22:51 -0400, Matt Harbison wrote:
> # HG changeset patch
> # User Matt Harbison <matt_harbison@yahoo.com>
> # Date 1431393973 14400
> #      Mon May 11 21:26:13 2015 -0400
> # Node ID 99f04b658814675825ad17d58524d1f3ecf8f6b9
> # Parent  835b2b100c94079bf2a47adbe3381fa351cbdc9b
> pathutil: hint if a path is root relative instead of cwd relative (issue4663)

These are queued for default, thanks.

Patch

diff --git a/mercurial/pathutil.py b/mercurial/pathutil.py
--- a/mercurial/pathutil.py
+++ b/mercurial/pathutil.py
@@ -152,7 +152,17 @@ 
                 break
             name = dirname
 
-        raise util.Abort(_("%s not under root '%s'") % (myname, root))
+        # A common mistake is to use -R, but specify a file relative to the repo
+        # instead of cwd.  Detect that case, and provide a hint to the user.
+        hint = None
+        try:
+            canonpath(root, root, myname, auditor)
+            hint = _("consider using '--cwd %s'") % os.path.relpath(root, cwd)
+        except util.Abort:
+            pass
+
+        raise util.Abort(_("%s not under root '%s'") % (myname, root),
+                         hint=hint)
 
 def normasprefix(path):
     '''normalize the specified path as path prefix
diff --git a/tests/test-globalopts.t b/tests/test-globalopts.t
--- a/tests/test-globalopts.t
+++ b/tests/test-globalopts.t
@@ -88,6 +88,7 @@ 
   [255]
   $ hg -R b ann a/a
   abort: a/a not under root '$TESTTMP/b' (glob)
+  (consider using '--cwd b')
   [255]
   $ hg log
   abort: no repository found in '$TESTTMP' (.hg not found)!