Patchwork paths: move path validation logic to its own function

login
register
mail settings
Submitter Durham Goode
Date Aug. 26, 2015, 4:36 a.m.
Message ID <f1776bd34bbe096e356a.1440563764@dev2000.prn2.facebook.com>
Download mbox | patch
Permalink /patch/10281/
State Accepted
Headers show

Comments

Durham Goode - Aug. 26, 2015, 4:36 a.m.
# HG changeset patch
# User Durham Goode <durham@fb.com>
# Date 1440470016 25200
#      Mon Aug 24 19:33:36 2015 -0700
# Node ID f1776bd34bbe096e356a25e8255669fb7340db1b
# Parent  05e7f57c74ac5b556b49870af86f61aa0c54babb
paths: move path validation logic to its own function

Hard coding the '.hg' path in the paths class made it difficult for the hggit
extension to pull from gitrepos.

This patch moves the logic out to it's own function so extensions can add
additional checks to what is a valid path (i.e. a git repo is valid when hggit
is enabled).
Augie Fackler - Aug. 27, 2015, 3:17 p.m.
On Tue, Aug 25, 2015 at 09:36:04PM -0700, Durham Goode wrote:
> # HG changeset patch
> # User Durham Goode <durham@fb.com>
> # Date 1440470016 25200
> #      Mon Aug 24 19:33:36 2015 -0700
> # Node ID f1776bd34bbe096e356a25e8255669fb7340db1b
> # Parent  05e7f57c74ac5b556b49870af86f61aa0c54babb
> paths: move path validation logic to its own function

Queued this, thanks!

>
> Hard coding the '.hg' path in the paths class made it difficult for the hggit
> extension to pull from gitrepos.
>
> This patch moves the logic out to it's own function so extensions can add
> additional checks to what is a valid path (i.e. a git repo is valid when hggit
> is enabled).
>
> diff --git a/mercurial/ui.py b/mercurial/ui.py
> --- a/mercurial/ui.py
> +++ b/mercurial/ui.py
> @@ -1068,11 +1068,17 @@ class path(object):
>
>          # When given a raw location but not a symbolic name, validate the
>          # location is valid.
> -        if (not name and not u.scheme
> -            and not os.path.isdir(os.path.join(str(u), '.hg'))):
> +        if not name and not u.scheme and not self._isvalidlocalpath(self.loc):
>              raise ValueError('location is not a URL or path to a local '
>                               'repo: %s' % rawloc)
>
> +    def _isvalidlocalpath(self, path):
> +        """Returns True if the given path is a potentially valid repository.
> +        This is its own function so that extensions can change the definition of
> +        'valid' in this case (like when pulling from a git repo into a hg
> +        one)."""
> +        return os.path.isdir(os.path.join(path, '.hg'))
> +
>      @property
>      def pushloc(self):
>          return self._pushloc or self.loc
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> https://selenic.com/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -1068,11 +1068,17 @@  class path(object):
 
         # When given a raw location but not a symbolic name, validate the
         # location is valid.
-        if (not name and not u.scheme
-            and not os.path.isdir(os.path.join(str(u), '.hg'))):
+        if not name and not u.scheme and not self._isvalidlocalpath(self.loc):
             raise ValueError('location is not a URL or path to a local '
                              'repo: %s' % rawloc)
 
+    def _isvalidlocalpath(self, path):
+        """Returns True if the given path is a potentially valid repository.
+        This is its own function so that extensions can change the definition of
+        'valid' in this case (like when pulling from a git repo into a hg
+        one)."""
+        return os.path.isdir(os.path.join(path, '.hg'))
+
     @property
     def pushloc(self):
         return self._pushloc or self.loc