diff extensions/kwarchive.py @ 51:3807a60ee0dd

Shorten path urls by default when expanding keywords. Providen an option "--no-shorten-path" to disable shortening and print urls as-is.
author Franz Glasner <hg@dom66.de>
date Sun, 19 Nov 2017 16:53:58 +0100
parents 94ae433383a2
children d06311d805af
line wrap: on
line diff
--- a/extensions/kwarchive.py	Wed Nov 15 09:34:15 2017 +0100
+++ b/extensions/kwarchive.py	Sun Nov 19 16:53:58 2017 +0100
@@ -45,6 +45,14 @@
 testedwith = "4.3.2"
 
 
+SHORTENED_HG_SCHEMES = {
+    "ssh": "hg+ssh",
+    "http": "hg+http",
+    "https": "hg+https",
+    "file": "file",
+}
+
+
 cmdtable = {}
 
 command = cmdutil.command(cmdtable)
@@ -54,10 +62,11 @@
     [('', 'no-decode', None, _('do not pass files through decoders')),
      ('p', 'prefix', '', _('directory prefix for files in archive'),
      _('PREFIX')),
-     ('r', 'rev', '', _('revision to distribute'), _('REV')),
-     ('t', 'type', '', _('type of distribution to create'), _('TYPE')),
-     ('', 'path', 'default', _('the canonical repository to use'), _('PATH')),
-     ('', "kwconfig", '', _('an alternate pattern configuration configuration file'), _('PATTERNCONFIG')),
+      ('r', 'rev', '', _('revision to distribute'), _('REV')),
+      ('t', 'type', '', _('type of distribution to create'), _('TYPE')),
+      ('', 'path', 'default', _('the canonical repository to use'), _('PATH')),
+      ('', "kwconfig", '', _('an alternate pattern configuration configuration file'), _('PATTERNCONFIG')),
+      ('', "no-shorten-path", None, _("don't shorten the path urls")),
     ] + cmdutil.subrepoopts + cmdutil.walkopts,
     _('[OPTION]... DEST'))
 def kwarchive(ui, repo, dest, **opts):
@@ -103,6 +112,9 @@
     repository root root.  If no path is given then ``default`` is
     assumed.
 
+    Full path URLs are printed somewhat shortened by default. To use
+    them as-is use the --no-shorten-path option.
+
     Returns 0 on success.
 
     '''
@@ -143,6 +155,7 @@
                 ac,
                 archival.tidyprefix(dest, kind, prefix),
                 hgpath=opts.get("path"),
+                shorten_path_url=not opts.get("no_shorten_path"),
                 kwconfig=opts.get("kwconfig")))
 
     archival.archive(repo, dest, node, kind, not opts.get('no_decode'),
@@ -159,6 +172,7 @@
 @command('kwprint',
     [('r', 'rev', '', _('revision to distribute'), _('REV')),
      ('', 'path', 'default', _('the canonical repository to use'), _('PATH')),
+     ('', "no-shorten-path", None, _("don't shorten the path urls")),
     ] + cmdutil.subrepoopts + cmdutil.walkopts,
     _('[OPTION]...'))
 def kwprint(ui, repo, **opts):
@@ -172,6 +186,9 @@
     repository root root.  If no path is given then ``default`` is
     assumed.
 
+    Full path URLs are printed somewhat shortened by default. To use
+    them as-is use the --no-shorten-path option.
+
     Returns 0 on success.
 
     '''
@@ -185,7 +202,9 @@
     prefix = cmdutil.makefilename(repo, "", node)
 
     keywords = make_node_keywords(
-        ui, repo, ctx, prefix, hgpath=opts.get("path"))
+        ui, repo, ctx, prefix,
+        hgpath=opts.get("path"),
+        shorten_path_url=not opts.get("no_shorten_path"))
     for key in sorted(keywords.keys()):
         ui.write("$%s: %s $\n" % (key, keywords[key]))
 
@@ -205,8 +224,11 @@
 
 def make_keyword_filter(ui, repo, ctx, archive_class, prefix,
                         hgpath="default",
+                        shorten_path_url=True,
                         kwconfig=""):
-    keywords = make_node_keywords(ui, repo, ctx, prefix, hgpath=hgpath)
+    keywords = make_node_keywords(ui, repo, ctx, prefix,
+                                  hgpath=hgpath,
+                                  shorten_path_url=shorten_path_url)
     kwconfigdata = kwconfigname = None
     if kwconfig:
         kwconfigdata = open(kwconfig, "rb").read()
@@ -272,25 +294,45 @@
     return _filter
 
 
-def make_node_keywords(ui, repo, ctx, prefix, hgpath="default"):
+def make_node_keywords(ui, repo, ctx, prefix,
+                       hgpath="default",
+                       shorten_path_url=True):
     """Make all the node-specific (i.e. file-path independent) keywords
 
     """
     if hgpath:
         if hgpath == '.':
-            path_url = repo.root
+            path_uri = repo.root
         else:
             try:
-                path_url = util.hidepassword(ui.paths[hgpath].loc)
+                if shorten_path_url:
+                    path_url = util.url(ui.paths[hgpath].loc)
+                    path_url.scheme = SHORTENED_HG_SCHEMES.get(path_url.scheme, "hg")
+                    path_url.user = None
+                    path_url.passwd = None
+                    path_url.host = stripped_hostname(path_url.host)
+                    path_uri = bytes(path_url)
+                else:
+                    path_uri = util.hidepassword(ui.paths[hgpath].loc)
             except LookupError:
                 raise error.Abort(_('repository %s not found') % hgpath)
     else:
-        path_url = repo.root
+        path_uri = repo.root
     keywords = {
-        "HGpath": path_url,
+        "HGpath": path_uri,
         "HGrevision": ctx.hex(),
         "Revision": templatefilters.short(ctx.hex()),
         "Author": templatefilters.person(ctx.user()),
         "Date": templatefilters.isodatesec(ctx.date()),
     }
     return keywords
+
+
+def stripped_hostname(hostname):
+    """Return `hostname` without any domain information"""
+    if not hostname:
+        return hostname
+    idx = hostname.find('.')
+    if idx < 0:
+        return hostname
+    return hostname[:idx]