Patchwork [3,of,3,v4,sparse-ext] sparse: treat paths as cwd-relative

login
register
mail settings
Submitter Kostia Balytskyi
Date Aug. 4, 2017, 12:45 p.m.
Message ID <fe26df0128eaefab8b79.1501850759@devvm1416.lla2.facebook.com>
Download mbox | patch
Permalink /patch/22666/
State Accepted
Headers show

Comments

Kostia Balytskyi - Aug. 4, 2017, 12:45 p.m.
# HG changeset patch
# User Kostia Balytskyi <ikostia@fb.com>
# Date 1501850302 25200
#      Fri Aug 04 05:38:22 2017 -0700
# Node ID fe26df0128eaefab8b79f7552da89c762014dab7
# Parent  c0136292871d85675ccb9f386f4ae1913da4eb53
sparse: treat paths as cwd-relative

This commit makes it so sparse treats passed paths as CWD-relative,
not repo-root-realive. This is a more intuitive behavior in my (and some
other FB people's) opinion.

This is breaking change however. My hope here is that since sparse is
experimental, it's ok to introduce BCs.

The reason (glob)s are needed in the test is this: in these two cases we
do not supply path together with slashes, but `os.path.join` adds them, which
means that under Windows they can be backslashes. To demonstrate this behavior,
one could remove the (glob)s and run `./run-tests.py test-sparse.t` from
MinGW's terminal on Windows.
Augie Fackler - Aug. 4, 2017, 9:25 p.m.
On Fri, Aug 04, 2017 at 05:45:59AM -0700, Kostia Balytskyi wrote:
> # HG changeset patch
> # User Kostia Balytskyi <ikostia@fb.com>
> # Date 1501850302 25200
> #      Fri Aug 04 05:38:22 2017 -0700
> # Node ID fe26df0128eaefab8b79f7552da89c762014dab7
> # Parent  c0136292871d85675ccb9f386f4ae1913da4eb53
> sparse: treat paths as cwd-relative

queued, thanks

>
> This commit makes it so sparse treats passed paths as CWD-relative,
> not repo-root-realive. This is a more intuitive behavior in my (and some
> other FB people's) opinion.
>
> This is breaking change however. My hope here is that since sparse is
> experimental, it's ok to introduce BCs.

Yes, that's why we have experimental things.

>
> The reason (glob)s are needed in the test is this: in these two cases we
> do not supply path together with slashes, but `os.path.join` adds them, which
> means that under Windows they can be backslashes. To demonstrate this behavior,
> one could remove the (glob)s and run `./run-tests.py test-sparse.t` from
> MinGW's terminal on Windows.
>
> diff --git a/hgext/sparse.py b/hgext/sparse.py

[...]

>          elif exclude:
> diff --git a/tests/test-sparse.t b/tests/test-sparse.t
> --- a/tests/test-sparse.t
> +++ b/tests/test-sparse.t
> @@ -48,6 +48,31 @@ TODO: See if this can be made to fail th
>    [255]
>  #endif
>
> +Paths should be treated as cwd-relative, not repo-root-relative
> +  $ mkdir subdir && cd subdir
> +  $ hg debugsparse --include path
> +  $ hg debugsparse
> +  [include]
> +  $TESTTMP/myrepo/hide
> +  hide
> +  subdir/path (glob)
> +
> +  $ cd ..
> +  $ echo hello > subdir/file2.ext
> +  $ cd subdir
> +  $ hg debugsparse --include '**.ext'  # let us test globs
> +  $ hg debugsparse --include 'path:abspath'  # and a path: pattern

I'm a little weirded out that it becomes absolute magically, but I
guess it matches `hg files` (h/t to Martin), so I'm fine with it.

> +  $ cd ..
> +  $ hg debugsparse
> +  [include]
> +  $TESTTMP/myrepo/hide
> +  hide
> +  path:abspath
> +  subdir/**.ext
> +  subdir/path (glob)
> +
> +  $ rm -rf subdir
> +
>  Verify commiting while sparse includes other files
>
>    $ echo z > hide
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff --git a/hgext/sparse.py b/hgext/sparse.py
--- a/hgext/sparse.py
+++ b/hgext/sparse.py
@@ -155,7 +155,8 @@  def _clonesparsecmd(orig, ui, repo, *arg
     if include or exclude or enableprofile:
         def clonesparse(orig, self, node, overwrite, *args, **kwargs):
             sparse.updateconfig(self.unfiltered(), pat, {}, include=include,
-                                exclude=exclude, enableprofile=enableprofile)
+                                exclude=exclude, enableprofile=enableprofile,
+                                usereporootpaths=True)
             return orig(self, node, overwrite, *args, **kwargs)
         extensions.wrapfunction(hg, 'updaterepo', clonesparse)
     return orig(ui, repo, *args, **opts)
diff --git a/mercurial/sparse.py b/mercurial/sparse.py
--- a/mercurial/sparse.py
+++ b/mercurial/sparse.py
@@ -17,6 +17,7 @@  from . import (
     error,
     match as matchmod,
     merge as mergemod,
+    pathutil,
     pycompat,
     scmutil,
     util,
@@ -616,7 +617,7 @@  def importfromfiles(repo, opts, paths, f
 
 def updateconfig(repo, pats, opts, include=False, exclude=False, reset=False,
                  delete=False, enableprofile=False, disableprofile=False,
-                 force=False):
+                 force=False, usereporootpaths=False):
     """Perform a sparse config update.
 
     Only one of the actions may be performed.
@@ -639,6 +640,20 @@  def updateconfig(repo, pats, opts, inclu
         if any(os.path.isabs(pat) for pat in pats):
             raise error.Abort(_('paths cannot be absolute'))
 
+        if not usereporootpaths:
+            # let's treat paths as relative to cwd
+            root, cwd = repo.root, repo.getcwd()
+            abspats = []
+            for kindpat in pats:
+                kind, pat = matchmod._patsplit(kindpat, None)
+                if kind in matchmod.cwdrelativepatternkinds or kind is None:
+                    ap = (kind + ':' if kind else '') +\
+                            pathutil.canonpath(root, cwd, pat)
+                    abspats.append(ap)
+                else:
+                    abspats.append(kindpat)
+            pats = abspats
+
         if include:
             newinclude.update(pats)
         elif exclude:
diff --git a/tests/test-sparse.t b/tests/test-sparse.t
--- a/tests/test-sparse.t
+++ b/tests/test-sparse.t
@@ -48,6 +48,31 @@  TODO: See if this can be made to fail th
   [255]
 #endif
 
+Paths should be treated as cwd-relative, not repo-root-relative
+  $ mkdir subdir && cd subdir
+  $ hg debugsparse --include path
+  $ hg debugsparse
+  [include]
+  $TESTTMP/myrepo/hide
+  hide
+  subdir/path (glob)
+  
+  $ cd ..
+  $ echo hello > subdir/file2.ext
+  $ cd subdir
+  $ hg debugsparse --include '**.ext'  # let us test globs
+  $ hg debugsparse --include 'path:abspath'  # and a path: pattern
+  $ cd ..
+  $ hg debugsparse
+  [include]
+  $TESTTMP/myrepo/hide
+  hide
+  path:abspath
+  subdir/**.ext
+  subdir/path (glob)
+  
+  $ rm -rf subdir
+
 Verify commiting while sparse includes other files
 
   $ echo z > hide