[PATCH v3] update: teach hg to override untracked dir with a tracked file on update

Kostiantyn Balytskyi ikostia at fb.com
Fri Jul 1 11:44:22 EDT 2016


# HG changeset patch
# User Kostia Balytskyi <ikostia at fb.com>
# Date 1467387775 -7200
#      Fri Jul 01 17:42:55 2016 +0200
# Node ID f9db0a6e3bfd6b980b441a699095fa1db0c46400
# Parent  5b71a8d7f7ffc2662ceb58ad8eec806e147ae13d
update: teach hg to override untracked dir with a tracked file on update

This is a fix to an old problem when Mercurial got confused by an
untracked folder with the same name as one of the files in a commit
hg was trying to update to. It is pretty safe to remove this folder if
it is empty. Backing up an empty folder seems to go against Mercurial's
"don't track dirs" philosophy.

diff --git a/mercurial/merge.py b/mercurial/merge.py
--- a/mercurial/merge.py
+++ b/mercurial/merge.py
@@ -1076,15 +1076,14 @@ def batchget(repo, mctx, actions):
                 absf = repo.wjoin(f)
                 orig = scmutil.origpath(ui, repo, absf)
                 try:
-                    # TODO Mercurial has always aborted if an untracked
-                    # directory is replaced by a tracked file, or generally
-                    # with file/directory merges. This needs to be sorted out.
                     if repo.wvfs.isfileorlink(f):
                         util.rename(absf, orig)
                 except OSError as e:
                     if e.errno != errno.ENOENT:
                         raise
 
+            if repo.wvfs.isdir(f):
+                repo.wvfs.removedirs(f)
             wwrite(f, fctx(f).data(), flags, backgroundclose=True)
             if i == 100:
                 yield i, f
diff --git a/tests/test-merge1.t b/tests/test-merge1.t
--- a/tests/test-merge1.t
+++ b/tests/test-merge1.t
@@ -24,11 +24,12 @@
   $ hg update 0
   0 files updated, 0 files merged, 1 files removed, 0 files unresolved
 
-Test interrupted updates by exploiting our non-handling of directory collisions
+Test interrupted updates by having a non-empty dir with the same name as one
+of the files in a commit we're updating to
 
-  $ mkdir b
+  $ mkdir b && touch b/nonempty
   $ hg up
-  abort: *: '$TESTTMP/t/b' (glob)
+  abort: Directory not empty: '$TESTTMP/t/b'
   [255]
   $ hg ci
   abort: last update was interrupted
@@ -38,10 +39,10 @@ Test interrupted updates by exploiting o
   parent: 0:538afb845929 
    commit #0
   branch: default
-  commit: (interrupted update)
+  commit: 1 unknown (interrupted update)
   update: 1 new changesets (update)
   phases: 2 draft
-  $ rmdir b
+  $ rm -r b/nonempty
   $ hg up
   1 files updated, 0 files merged, 0 files removed, 0 files unresolved
   $ hg sum
diff --git a/tests/test-update-renames.t b/tests/test-update-names.t
rename from tests/test-update-renames.t
rename to tests/test-update-names.t
--- a/tests/test-update-renames.t
+++ b/tests/test-update-names.t
@@ -1,8 +1,9 @@
-Test update logic when there are renames
+Test update logic when there are renames or weird same-name cases between dirs
+and files
 
 Update with local changes across a file rename
 
-  $ hg init
+  $ hg init r1 && cd r1
 
   $ echo a > a
   $ hg add a
@@ -24,3 +25,31 @@ Update with local changes across a file 
   0 files updated, 0 files merged, 0 files removed, 1 files unresolved
   use 'hg resolve' to retry unresolved file merges
   [1]
+
+Test update when local untracked directory exists with the same name as a
+tracked file in a commit we are updating to
+  $ hg init r2 && cd r2
+  $ echo root > root && hg ci -Am root  # rev 0
+  adding root
+  $ echo text > name && hg ci -Am "name is a file"  # rev 1
+  adding name
+  $ hg up 0
+  0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  $ mkdir name
+  $ hg up 1
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+
+Test update when local untracked directory exists with some files in it and has
+the same name a tracked file in a commit we are updating to. In future this
+should be updated to give an friendlier error message, but now we should just
+make sure that this does not erase untracked data
+  $ hg up 0
+  0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  $ mkdir name
+  $ echo text > name/file
+  $ hg st
+  ? name/file
+  $ hg up 1
+  abort: Directory not empty: '$TESTTMP/r1/r2/name'
+  [255]
+  $ cd ..


More information about the Mercurial-devel mailing list