comparison mercurial/utils/urlutil.py @ 47186:26b3953ba1b0

urlutil: extract `chain_path` in a function This will no longer modify `path` inplace so it does not make much sense as a method. Differential Revision: https://phab.mercurial-scm.org/D10446
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Thu, 15 Apr 2021 17:15:43 +0200
parents a2632ce1f15b
children 7531cc34713c
comparison
equal deleted inserted replaced
47185:a2632ce1f15b 47186:26b3953ba1b0
609 continue 609 continue
610 loc, sub_opts = ui.configsuboptions(b'paths', name) 610 loc, sub_opts = ui.configsuboptions(b'paths', name)
611 self[name] = path(ui, name, rawloc=loc, suboptions=sub_opts) 611 self[name] = path(ui, name, rawloc=loc, suboptions=sub_opts)
612 612
613 for name, p in sorted(self.items()): 613 for name, p in sorted(self.items()):
614 p.chain_path(ui, self) 614 self[name] = _chain_path(p, ui, self)
615 615
616 def getpath(self, ui, name, default=None): 616 def getpath(self, ui, name, default=None):
617 """Return a ``path`` from a string, falling back to default. 617 """Return a ``path`` from a string, falling back to default.
618 618
619 ``name`` can be a named path or locations. Locations are filesystem 619 ``name`` can be a named path or locations. Locations are filesystem
702 @pathsuboption(b'pushrev', b'pushrev') 702 @pathsuboption(b'pushrev', b'pushrev')
703 def pushrevpathoption(ui, path, value): 703 def pushrevpathoption(ui, path, value):
704 return value 704 return value
705 705
706 706
707 def _chain_path(path, ui, paths):
708 """return the result of "path://" logic applied on a given path"""
709 if path.url.scheme == b'path':
710 assert path.url.path is None
711 subpath = paths.get(path.url.host)
712 if subpath is None:
713 m = _(b'cannot use `%s`, "%s" is not a known path')
714 m %= (path.rawloc, path.url.host)
715 raise error.Abort(m)
716 if subpath.raw_url.scheme == b'path':
717 m = _(b'cannot use `%s`, "%s" is also defined as a `path://`')
718 m %= (path.rawloc, path.url.host)
719 raise error.Abort(m)
720 path.url = subpath.url
721 path.rawloc = subpath.rawloc
722 path.loc = subpath.loc
723 if path.branch is None:
724 path.branch = subpath.branch
725 else:
726 base = path.rawloc.rsplit(b'#', 1)[0]
727 path.rawloc = b'%s#%s' % (base, path.branch)
728 suboptions = subpath._all_sub_opts.copy()
729 suboptions.update(path._own_sub_opts)
730 path._apply_suboptions(ui, suboptions)
731 return path
732
733
707 class path(object): 734 class path(object):
708 """Represents an individual path and its configuration.""" 735 """Represents an individual path and its configuration."""
709 736
710 def __init__(self, ui=None, name=None, rawloc=None, suboptions=None): 737 def __init__(self, ui=None, name=None, rawloc=None, suboptions=None):
711 """Construct a path from its config options. 738 """Construct a path from its config options.
753 self._own_sub_opts = suboptions.copy() 780 self._own_sub_opts = suboptions.copy()
754 sub_opts.update(suboptions) 781 sub_opts.update(suboptions)
755 self._all_sub_opts = sub_opts.copy() 782 self._all_sub_opts = sub_opts.copy()
756 783
757 self._apply_suboptions(ui, sub_opts) 784 self._apply_suboptions(ui, sub_opts)
758
759 def chain_path(self, ui, paths):
760 if self.url.scheme == b'path':
761 assert self.url.path is None
762 try:
763 subpath = paths[self.url.host]
764 except KeyError:
765 m = _(b'cannot use `%s`, "%s" is not a known path')
766 m %= (self.rawloc, self.url.host)
767 raise error.Abort(m)
768 if subpath.raw_url.scheme == b'path':
769 m = _(b'cannot use `%s`, "%s" is also defined as a `path://`')
770 m %= (self.rawloc, self.url.host)
771 raise error.Abort(m)
772 self.url = subpath.url
773 self.rawloc = subpath.rawloc
774 self.loc = subpath.loc
775 if self.branch is None:
776 self.branch = subpath.branch
777 else:
778 base = self.rawloc.rsplit(b'#', 1)[0]
779 self.rawloc = b'%s#%s' % (base, self.branch)
780 suboptions = subpath._all_sub_opts.copy()
781 suboptions.update(self._own_sub_opts)
782 self._apply_suboptions(ui, suboptions)
783 785
784 def copy(self): 786 def copy(self):
785 """make a copy of this path object""" 787 """make a copy of this path object"""
786 new = self.__class__() 788 new = self.__class__()
787 for k, v in self.__dict__.items(): 789 for k, v in self.__dict__.items():