comparison hgext/infinitepush/fileindexapi.py @ 37187:03ff17a4bf53

infinitepush: move the extension to core from fb-hgext This patch moves the infinitepush extension from fb-hgext to core. The extension is used to store incoming bundles during a push in bundlestore rather than applying them to the revlog. The extension was copied from the repository revision at f27f094e91553d3cae5167c0b1c42ae940f888d5 and following changes were made: * added `from __future__ import absolute_import` where missing * fixed module imports to follow the core style * minor fixes for test-check-code.t * registered the configs * adding the testedwith value to match core's convention * removed double newlines to make test-check-commit.t happy * added one line doc about extension and marked it as experimental Only one test file test-infinitepush-bundlestore.t is moved to core and following changes are made to file: * remove dependency of library.sh * split the tests into two tests i.e. test-infinitepush.t and test-infinitepush-bundlestore.t * removed testing related to other facebook's extensions pushrebase, inhibit, fbamend library-infinitepush.sh is also copied from fb-hgext from the same revision and following changes are made: * change the path to infinitepush extension as it's in core with this patch * removed sql handling from the file as we are not testing that initially Currently at this revision, test-check-module-imports.t does not pass as there is import of a module from fb/hgext in one the of the file which will be removed in the next patch. This extension right now has a lot of things which we don't require in core like `--to`, `--create` flags to `hg bookmark`, logic related to remotenames extension and another facebook's extensions, custom bundle2parts which can be prevented by using bookmarks bundle part and also logic related to sql store which is probably we don't want initially. The next patches in this series will remove all the unwanted and unrequired things from the extension and will make this a nice one. The end goal is to have a very lighweight extension with no or very less wrapping on the client side. Differential Revision: https://phab.mercurial-scm.org/D2096
author Pulkit Goyal <7895pulkit@gmail.com>
date Fri, 09 Feb 2018 13:39:15 +0530
parents
children 0d6c12668691
comparison
equal deleted inserted replaced
37186:6d43b39fbaa0 37187:03ff17a4bf53
1 # Infinite push
2 #
3 # Copyright 2016 Facebook, Inc.
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
7 """
8 [infinitepush]
9 # Server-side option. Used only if indextype=disk.
10 # Filesystem path to the index store
11 indexpath = PATH
12 """
13
14 from __future__ import absolute_import
15
16 import os
17
18 from mercurial import util
19
20 from . import indexapi
21
22 class fileindexapi(indexapi.indexapi):
23 def __init__(self, repo):
24 super(fileindexapi, self).__init__()
25 self._repo = repo
26 root = repo.ui.config('infinitepush', 'indexpath')
27 if not root:
28 root = os.path.join('scratchbranches', 'index')
29
30 self._nodemap = os.path.join(root, 'nodemap')
31 self._bookmarkmap = os.path.join(root, 'bookmarkmap')
32 self._metadatamap = os.path.join(root, 'nodemetadatamap')
33 self._lock = None
34
35 def __enter__(self):
36 self._lock = self._repo.wlock()
37 return self
38
39 def __exit__(self, exc_type, exc_val, exc_tb):
40 if self._lock:
41 self._lock.__exit__(exc_type, exc_val, exc_tb)
42
43 def addbundle(self, bundleid, nodesctx):
44 for node in nodesctx:
45 nodepath = os.path.join(self._nodemap, node.hex())
46 self._write(nodepath, bundleid)
47
48 def addbookmark(self, bookmark, node):
49 bookmarkpath = os.path.join(self._bookmarkmap, bookmark)
50 self._write(bookmarkpath, node)
51
52 def addmanybookmarks(self, bookmarks):
53 for bookmark, node in bookmarks.items():
54 self.addbookmark(bookmark, node)
55
56 def deletebookmarks(self, patterns):
57 for pattern in patterns:
58 for bookmark, _ in self._listbookmarks(pattern):
59 bookmarkpath = os.path.join(self._bookmarkmap, bookmark)
60 self._delete(bookmarkpath)
61
62 def getbundle(self, node):
63 nodepath = os.path.join(self._nodemap, node)
64 return self._read(nodepath)
65
66 def getnode(self, bookmark):
67 bookmarkpath = os.path.join(self._bookmarkmap, bookmark)
68 return self._read(bookmarkpath)
69
70 def getbookmarks(self, query):
71 return dict(self._listbookmarks(query))
72
73 def saveoptionaljsonmetadata(self, node, jsonmetadata):
74 vfs = self._repo.vfs
75 vfs.write(os.path.join(self._metadatamap, node), jsonmetadata)
76
77 def _listbookmarks(self, pattern):
78 if pattern.endswith('*'):
79 pattern = 're:^' + pattern[:-1] + '.*'
80 kind, pat, matcher = util.stringmatcher(pattern)
81 prefixlen = len(self._bookmarkmap) + 1
82 for dirpath, _, books in self._repo.vfs.walk(self._bookmarkmap):
83 for book in books:
84 bookmark = os.path.join(dirpath, book)[prefixlen:]
85 if not matcher(bookmark):
86 continue
87 yield bookmark, self._read(os.path.join(dirpath, book))
88
89 def _write(self, path, value):
90 vfs = self._repo.vfs
91 dirname = vfs.dirname(path)
92 if not vfs.exists(dirname):
93 vfs.makedirs(dirname)
94
95 vfs.write(path, value)
96
97 def _read(self, path):
98 vfs = self._repo.vfs
99 if not vfs.exists(path):
100 return None
101 return vfs.read(path)
102
103 def _delete(self, path):
104 vfs = self._repo.vfs
105 if not vfs.exists(path):
106 return
107 return vfs.unlink(path)