Patchwork D8906: templater: restructure open_template() a little to prepare for relative paths

login
register
mail settings
Submitter phabricator
Date Aug. 6, 2020, 6:29 p.m.
Message ID <differential-rev-PHID-DREV-ilx5r76ym7dejght24vg-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/47004/
State Superseded
Headers show

Comments

phabricator - Aug. 6, 2020, 6:29 p.m.
martinvonz created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  I found that it was easier to add support for relative paths after
  this restructuring. It also made it easier to explain each case with a
  code comment (which I did).

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D8906

AFFECTED FILES
  mercurial/templater.py

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -1091,18 +1091,25 @@ 
     will be read from the mercurial.templates package instead. The returned path
     will then be the relative path.
     '''
+    # Does the name point directly to a map file?
+    if os.path.isabs(name):
+        return name, open(name, mode='rb')
+
+    # Does the name point to a template in the provided templatepath, or
+    # in mercurial/templates/ if no path was provided?
     if templatepath is None:
         templatepath = templatedir()
-    if templatepath is not None or os.path.isabs(name):
+    if templatepath is not None:
         f = os.path.join(templatepath, name)
         return f, open(f, mode='rb')
-    else:
-        name_parts = pycompat.sysstr(name).split('/')
-        package_name = '.'.join(['mercurial', 'templates'] + name_parts[:-1])
-        return (
-            name,
-            resourceutil.open_resource(package_name, name_parts[-1]),
-        )
+
+    # Otherwise try to read it using the resources API
+    name_parts = pycompat.sysstr(name).split('/')
+    package_name = '.'.join(['mercurial', 'templates'] + name_parts[:-1])
+    return (
+        name,
+        resourceutil.open_resource(package_name, name_parts[-1]),
+    )
 
 
 def try_open_template(name, templatepath=None):