[PATCH on-crew] patch._applydiff: resolve prefix with respect to the cwd

Siddharth Agarwal sid0 at fb.com
Thu Mar 19 18:29:59 UTC 2015


# HG changeset patch
# User Siddharth Agarwal <sid0 at fb.com>
# Date 1426785485 25200
#      Thu Mar 19 10:18:05 2015 -0700
# Node ID 4decbedd708b2c3787a08415c7affe5265616aa2
# Parent  7262bbef2ba80ce3ccfc726a19c0e33a419aff04
patch._applydiff: resolve prefix with respect to the cwd

This has several advantages compared to resolving it relative to the root:

- '--prefix .' works as expected.
- consistent with upcoming 'hg diff' option to produce relative patches

(I made sure to put in the (glob) annotations this time!)

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -4066,7 +4066,7 @@
     ('', 'exact', None,
      _('apply patch to the nodes from which it was generated')),
     ('', 'prefix', '',
-     _('apply patch to directory relative to the root'), _('DIR')),
+     _('apply patch to subdirectory'), _('DIR')),
     ('', 'import-branch', None,
      _('use any branch information in patch (implied by --exact)'))] +
     commitopts + commitopts2 + similarityopts,
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -17,6 +17,7 @@
 from node import hex, short
 import cStringIO
 import base85, mdiff, scmutil, util, diffhelpers, copies, encoding, error
+import pathutil
 
 gitre = re.compile('diff --git a/(.*) b/(.*)')
 tabsplitter = re.compile(r'(\t+|[^\t]+)')
@@ -1795,8 +1796,10 @@
                eolmode='strict'):
 
     if prefix:
-        # clean up double slashes, lack of trailing slashes, etc
-        prefix = util.normpath(prefix) + '/'
+        prefix = pathutil.canonpath(backend.repo.root, backend.repo.getcwd(),
+                                    prefix)
+        if prefix != '':
+            prefix += '/'
     def pstrip(p):
         return pathtransform(p, strip - 1, prefix)[1]
 
diff --git a/tests/test-import-git.t b/tests/test-import-git.t
--- a/tests/test-import-git.t
+++ b/tests/test-import-git.t
@@ -626,6 +626,33 @@
   adding dir/d
   adding dir/dir2/b
   adding dir/dir2/c
+
+prefix '.' is the same as no prefix
+  $ hg import --no-commit --prefix . - <<EOF
+  > diff --git a/dir/a b/dir/a
+  > --- /dev/null
+  > +++ b/dir/a
+  > @@ -0,0 +1 @@
+  > +aaaa
+  > diff --git a/dir/d b/dir/d
+  > --- a/dir/d
+  > +++ b/dir/d
+  > @@ -1,1 +1,2 @@
+  >  d
+  > +dddd
+  > EOF
+  applying patch from stdin
+  $ cat dir/a
+  aaaa
+  $ cat dir/d
+  d
+  dddd
+  $ hg revert -aC
+  forgetting dir/a (glob)
+  reverting dir/d (glob)
+  $ rm dir/a
+
+prefix with default strip
   $ hg import --no-commit --prefix dir/ - <<EOF
   > diff --git a/a b/a
   > --- /dev/null
@@ -649,10 +676,10 @@
   forgetting dir/a
   reverting dir/d
   $ rm dir/a
-(test that prefixes are relative to the root)
+(test that prefixes are relative to the cwd)
   $ mkdir tmpdir
   $ cd tmpdir
-  $ hg import --no-commit -p2 --prefix dir/ - <<EOF
+  $ hg import --no-commit -p2 --prefix ../dir/ - <<EOF
   > diff --git a/foo/a b/foo/a
   > new file mode 100644
   > --- /dev/null


More information about the Mercurial-devel mailing list