Patchwork [V4] update: warn if cwd was deleted

login
register
mail settings
Submitter Stanislau Hlebik
Date Sept. 14, 2016, 3:05 p.m.
Message ID <84ef6a5bc6556f59ed89.1473865512@dev1918.lla1.facebook.com>
Download mbox | patch
Permalink /patch/16622/
State Changes Requested
Headers show

Comments

Stanislau Hlebik - Sept. 14, 2016, 3:05 p.m.
# HG changeset patch
# User Stanislau Hlebik <stash@fb.com>
# Date 1473865427 25200
#      Wed Sep 14 08:03:47 2016 -0700
# Node ID 84ef6a5bc6556f59ed8935a5f716be53a6e17951
# Parent  f148bfa40489269be2e48046734f81065129847a
update: warn if cwd was deleted

During update directories are deleted as soon as they have no entries.
But if current working directory is deleted then it cause problems
in complex commands like 'hg split'. This commit adds a warning
that will help users figure the problem faster.
Pierre-Yves David - Sept. 16, 2016, 3:17 p.m.
On 09/14/2016 05:05 PM, Stanislau Hlebik wrote:
> # HG changeset patch
> # User Stanislau Hlebik <stash@fb.com>
> # Date 1473865427 25200
> #      Wed Sep 14 08:03:47 2016 -0700
> # Node ID 84ef6a5bc6556f59ed8935a5f716be53a6e17951
> # Parent  f148bfa40489269be2e48046734f81065129847a
> update: warn if cwd was deleted
>
> During update directories are deleted as soon as they have no entries.
> But if current working directory is deleted then it cause problems
> in complex commands like 'hg split'. This commit adds a warning
> that will help users figure the problem faster.
>
> diff --git a/mercurial/merge.py b/mercurial/merge.py
> --- a/mercurial/merge.py
> +++ b/mercurial/merge.py
> @@ -1036,6 +1036,10 @@
>      unlink = util.unlinkpath
>      wjoin = repo.wjoin
>      audit = repo.wvfs.audit
> +    try:
> +        cwd = os.getcwd()
> +    except OSError:
> +        cwd = None

We should probably restrict this to actual directory removal error. The 
current code catch a wider familly of error and could lead to incorrect 
error reporting. The actual error code will be on the exception 'errno' 
attribut. See other usage of this in the codebade or Python 
documentation for details.

>      i = 0
>      for f, args, msg in actions:
>          repo.ui.debug(" %s: %s -> r\n" % (f, msg))
> @@ -1053,6 +1057,16 @@
>          i += 1
>      if i > 0:
>          yield i, f
> +    if cwd:
> +        # cwd was present before we started to remove files
> +        # let's check if it is present after we removed them
> +        try:
> +            os.getcwd()
> +        except OSError:
> +            # Print a warning if cwd was deleted
> +            repo.ui.warn(_("current directory was removed "
> +                           "(consider changing to repo root: %s)\n") %
> +                         repo.root)

Same thing apply here. I believe the error catching is too wide.

Also the hint (part in parenthesis) should be on another line to match 
our convention in error reporting.

Cheers,

Patch

diff --git a/mercurial/merge.py b/mercurial/merge.py
--- a/mercurial/merge.py
+++ b/mercurial/merge.py
@@ -1036,6 +1036,10 @@ 
     unlink = util.unlinkpath
     wjoin = repo.wjoin
     audit = repo.wvfs.audit
+    try:
+        cwd = os.getcwd()
+    except OSError:
+        cwd = None
     i = 0
     for f, args, msg in actions:
         repo.ui.debug(" %s: %s -> r\n" % (f, msg))
@@ -1053,6 +1057,16 @@ 
         i += 1
     if i > 0:
         yield i, f
+    if cwd:
+        # cwd was present before we started to remove files
+        # let's check if it is present after we removed them
+        try:
+            os.getcwd()
+        except OSError:
+            # Print a warning if cwd was deleted
+            repo.ui.warn(_("current directory was removed "
+                           "(consider changing to repo root: %s)\n") %
+                         repo.root)
 
 def batchget(repo, mctx, actions):
     """apply gets to the working directory
diff --git a/tests/test-rebase-scenario-global.t b/tests/test-rebase-scenario-global.t
--- a/tests/test-rebase-scenario-global.t
+++ b/tests/test-rebase-scenario-global.t
@@ -758,6 +758,7 @@ 
   $ hg commit -m 'second source with subdir'
   $ hg rebase -b . -d 1 --traceback
   rebasing 2:779a07b1b7a0 "first source commit"
+  current directory was removed (consider changing to repo root: $TESTTMP/cwd-vanish)
   rebasing 3:a7d6f3a00bf3 "second source with subdir" (tip)
   saved backup bundle to $TESTTMP/cwd-vanish/.hg/strip-backup/779a07b1b7a0-853e0073-backup.hg (glob)
 
diff --git a/tests/test-update-names.t b/tests/test-update-names.t
--- a/tests/test-update-names.t
+++ b/tests/test-update-names.t
@@ -72,3 +72,14 @@ 
   $ cd ..
 
 #endif
+
+Test that warning is printed if cwd is deleted during update
+  $ hg init r4 && cd r4
+  $ mkdir dir
+  $ cd dir
+  $ echo a > a
+  $ echo b > b
+  $ hg add a b
+  $ hg ci -m "file and dir"
+  $ hg up -q null
+  current directory was removed (consider changing to repo root: $TESTTMP/r1/r4)