Patchwork [2,of,3,relative-import] cmdutil.tryimportone: allow importing relative patches into the working dir

login
register
mail settings
Submitter Siddharth Agarwal
Date March 10, 2015, 8:49 p.m.
Message ID <a7cb4e2a12a100353ffe.1426020596@devbig136.prn2.facebook.com>
Download mbox | patch
Permalink /patch/7981/
State Accepted
Headers show

Comments

Siddharth Agarwal - March 10, 2015, 8:49 p.m.
# HG changeset patch
# User Siddharth Agarwal <sid0@fb.com>
# Date 1425950544 25200
#      Mon Mar 09 18:22:24 2015 -0700
# Node ID a7cb4e2a12a100353ffe96ca2ea256b503ebfea4
# Parent  894b02e812027a4cb52821cd8f0a783b29fce2d0
cmdutil.tryimportone: allow importing relative patches into the working dir

This makes hg import --prefix dir/ (without bypass) work.

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -615,6 +615,7 @@ 
 
     update = not opts.get('bypass')
     strip = opts["strip"]
+    prefix = opts["prefix"]
     sim = float(opts.get('similarity') or 0)
     if not tmpname:
         return (None, None, False)
@@ -674,8 +675,8 @@ 
             partial = opts.get('partial', False)
             files = set()
             try:
-                patch.patch(ui, repo, tmpname, strip=strip, files=files,
-                            eolmode=None, similarity=sim / 100.0)
+                patch.patch(ui, repo, tmpname, strip=strip, prefix=prefix,
+                            files=files, eolmode=None, similarity=sim / 100.0)
             except patch.PatchError, e:
                 if not partial:
                     raise util.Abort(str(e))
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -1533,7 +1533,7 @@ 
     backend = repobackend(ui, repo, ctx, store)
     return patchbackend(ui, backend, patchobj, strip, '', files, eolmode)
 
-def patch(ui, repo, patchname, strip=1, files=None, eolmode='strict',
+def patch(ui, repo, patchname, strip=1, prefix='', files=None, eolmode='strict',
           similarity=0):
     """Apply <patchname> to the working directory.
 
@@ -1552,7 +1552,7 @@ 
     if patcher:
         return _externalpatch(ui, repo, patcher, patchname, strip,
                               files, similarity)
-    return internalpatch(ui, repo, patchname, strip, '', files, eolmode,
+    return internalpatch(ui, repo, patchname, strip, prefix, files, eolmode,
                          similarity)
 
 def changedfiles(ui, repo, patchpath, strip=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
@@ -612,12 +612,64 @@ 
     a
   R a
 
-Renames, similarity and git diff
+Prefix with strip, renames, creates etc
 
   $ hg revert -aC
   undeleting a
   forgetting b
   $ rm b
+  $ mkdir -p dir/dir2
+  $ echo b > dir/dir2/b
+  $ echo c > dir/dir2/c
+  $ echo d > dir/d
+  $ hg ci -Am addbcd
+  adding dir/d
+  adding dir/dir2/b
+  adding dir/dir2/c
+(test that prefixes are relative to the root)
+  $ mkdir tmpdir
+  $ cd tmpdir
+  $ hg import --no-commit -p2 --prefix dir/ - <<EOF
+  > diff --git a/foo/a b/foo/a
+  > new file mode 100644
+  > --- /dev/null
+  > +++ b/foo/a
+  > @@ -0,0 +1 @@
+  > +a
+  > diff --git a/foo/dir2/b b/foo/dir2/b2
+  > rename from foo/dir2/b
+  > rename to foo/dir2/b2
+  > diff --git a/foo/dir2/c b/foo/dir2/c
+  > --- a/foo/dir2/c
+  > +++ b/foo/dir2/c
+  > @@ -0,0 +1 @@
+  > +cc
+  > diff --git a/foo/d b/foo/d
+  > deleted file mode 100644
+  > --- a/foo/d
+  > +++ /dev/null
+  > @@ -1,1 +0,0 @@
+  > -d
+  > EOF
+  applying patch from stdin
+  $ hg st --copies
+  M dir/dir2/c
+  A dir/a
+  A dir/dir2/b2
+    dir/dir2/b
+  R dir/d
+  R dir/dir2/b
+  $ cd ..
+
+Renames, similarity and git diff
+
+  $ hg revert -aC
+  forgetting dir/a
+  undeleting dir/d
+  undeleting dir/dir2/b
+  forgetting dir/dir2/b2
+  reverting dir/dir2/c
+  $ rm dir/a dir/dir2/b2
   $ hg import --similarity 90 --no-commit - <<EOF
   > diff --git a/a b/b
   > rename from a
diff --git a/tests/test-import.t b/tests/test-import.t
--- a/tests/test-import.t
+++ b/tests/test-import.t
@@ -670,6 +670,25 @@ 
   $ hg status
   $ cat a
   bb
+
+test --prefix
+
+  $ mkdir -p dir/dir2
+  $ echo b > dir/dir2/b
+  $ hg ci -Am b
+  adding dir/dir2/b
+  $ hg import -p2 --prefix dir - << EOF
+  > foobar
+  > --- drop1/drop2/dir2/b
+  > +++ drop1/drop2/dir2/b
+  > @@ -1,1 +1,1 @@
+  > -b
+  > +cc
+  > EOF
+  applying patch from stdin
+  $ hg status
+  $ cat dir/dir2/b
+  cc
   $ cd ..