Patchwork [1,of,3] listkeypattern: add pattern parameter to listkeys

login
register
mail settings
Submitter Stanislau Hlebik
Date Aug. 12, 2016, 12:09 p.m.
Message ID <a7c8796d3636837bc90c.1471003748@dev1918.lla1.facebook.com>
Download mbox | patch
Permalink /patch/16260/
State Changes Requested
Headers show

Comments

Stanislau Hlebik - Aug. 12, 2016, 12:09 p.m.
# HG changeset patch
# User Stanislau Hlebik <stash@fb.com>
# Date 1471003467 25200
#      Fri Aug 12 05:04:27 2016 -0700
# Node ID a7c8796d3636837bc90c2bc3712a0da5e44ebe75
# Parent  37b6f0ec6241a62de90737409458cd622e2fac0d
listkeypattern: add pattern parameter to listkeys

This patch adds an ability to list keys by pattern.  This will allow to list
only relevant remote keys.

We want to allow users to push their local bookmarks and commits to the server
so that it will be easy to share them.
This is a two-step project. First step is a selective pull which will be
(probably) mostly implemented in remotenames extension -
https://www.mercurial-scm.org/wiki/SelectivePullPlan.
Second step is infinite push. It will be
used to save certain commits in some other store. If
this project will be implemented then it will be easy to share data + all the
local commits will be automatically backed up.

Of course filtering can be done client-side somewhere in remotenames extension
but sending too many bookmarks over the wire seems unnecessary.

Patch

diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -1907,10 +1907,21 @@ 
         self._afterlock(runhook)
         return ret
 
-    def listkeys(self, namespace):
+    def listkeys(self, namespace, patterns=None):
         self.hook('prelistkeys', throw=True, namespace=namespace)
         self.ui.debug('listing keys for "%s"\n' % namespace)
         values = pushkey.list(self, namespace)
+        if patterns:
+            matchers = []
+            for pattern in patterns:
+                kind, p, matcher = util.stringmatcher(pattern)
+                matchers.append(matcher)
+            filtered = {}
+            for key, value in values.iteritems():
+                if any(m for m in matchers if m(key)):
+                    filtered[key] = value
+            values = filtered
+
         self.hook('listkeys', namespace=namespace, values=values)
         return values