##// END OF EJS Templates
git: skeleton of a new extension to _directly_ operate on git repos...
git: skeleton of a new extension to _directly_ operate on git repos This is based in part of work I did years ago in hgit, but it's mostly new code since I'm using pygit2 instead of dulwich and the hg storage interfaces have improved. Some cleanup of old hgit code by Pulkit, which I greatly appreciate. test-git-interop.t does not cover a whole lot of cases, but it passes. It includes status, diff, making a new commit, and `hg annotate` working on the git repository. This is _not_ (yet) production quality code: this is an experiment. Known technical debt lurking in this implementation: * Writing bookmarks just totally ignores transactions. * The way progress is threaded down into the gitstore is awful. * Ideally we'd find a way to incrementally reindex DAGs. I'm not sure how to do that efficiently, so we might need a "known only fast-forwards" mode on the DAG indexer for use on `hg commit` and friends. * We don't even _try_ to do anything reasonable for `hg pull` or `hg push`. * Mercurial need an interface for the changelog type. Tests currently require git 2.24 as far as I'm aware: `git status` has some changed output that I didn't try and handle in a compatible way. This patch has produced some interesting cleanups, most recently on the manifest type. I expect continuing down this road will produce other meritorious cleanups throughout our code. Differential Revision: https://phab.mercurial-scm.org/D6734

File last commit:

r44514:006e7821 default
r44961:ad718271 default
Show More
store.py
186 lines | 5.8 KiB | text/x-python | PythonLexer
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 # This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
# based on bundleheads extension by Gregory Szorc <gps@mozilla.com>
from __future__ import absolute_import
import abc
import os
import subprocess
import tempfile
Gregory Szorc
py3: manually import pycompat.open into files that need it...
r43355 from mercurial.pycompat import open
Matt Harbison
py3: convert arguments, cwd and env to native strings when spawning subprocess...
r39851 from mercurial import (
Mark Thomas
py3: fix infinitepush extension tests...
r40288 node,
Matt Harbison
py3: convert arguments, cwd and env to native strings when spawning subprocess...
r39851 pycompat,
)
Augie Fackler
hgext: replace references to hashlib.sha1 with hashutil.sha1...
r44519 from mercurial.utils import (
hashutil,
procutil,
)
Matt Harbison
py3: convert arguments, cwd and env to native strings when spawning subprocess...
r39851
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 NamedTemporaryFile = tempfile.NamedTemporaryFile
Augie Fackler
formatting: blacken the codebase...
r43346
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 class BundleWriteException(Exception):
pass
Augie Fackler
formatting: blacken the codebase...
r43346
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 class BundleReadException(Exception):
pass
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
pytype: don't warn us about ignored-on-py3 metaclasses...
r43775 class abstractbundlestore(object): # pytype: disable=ignored-metaclass
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 """Defines the interface for bundle stores.
A bundle store is an entity that stores raw bundle data. It is a simple
key-value store. However, the keys are chosen by the store. The keys can
be any Python object understood by the corresponding bundle index (see
``abstractbundleindex`` below).
"""
Augie Fackler
formatting: blacken the codebase...
r43346
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 __metaclass__ = abc.ABCMeta
@abc.abstractmethod
def write(self, data):
"""Write bundle data to the store.
This function receives the raw data to be written as a str.
Throws BundleWriteException
The key of the written data MUST be returned.
"""
@abc.abstractmethod
def read(self, key):
"""Obtain bundle data for a key.
Returns None if the bundle isn't known.
Throws BundleReadException
The returned object should be a file object supporting read()
and close().
"""
Augie Fackler
formatting: blacken the codebase...
r43346
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 class filebundlestore(object):
"""bundle store in filesystem
meant for storing bundles somewhere on disk and on network filesystems
"""
Augie Fackler
formatting: blacken the codebase...
r43346
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 def __init__(self, ui, repo):
self.ui = ui
self.repo = repo
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 self.storepath = ui.configpath(b'scratchbranch', b'storepath')
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 if not self.storepath:
Augie Fackler
formatting: blacken the codebase...
r43346 self.storepath = self.repo.vfs.join(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b"scratchbranches", b"filebundlestore"
Augie Fackler
formatting: blacken the codebase...
r43346 )
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 if not os.path.exists(self.storepath):
os.makedirs(self.storepath)
def _dirpath(self, hashvalue):
"""First two bytes of the hash are the name of the upper
level directory, next two bytes are the name of the
next level directory"""
return os.path.join(self.storepath, hashvalue[0:2], hashvalue[2:4])
def _filepath(self, filename):
return os.path.join(self._dirpath(filename), filename)
def write(self, data):
Augie Fackler
hgext: replace references to hashlib.sha1 with hashutil.sha1...
r44519 filename = node.hex(hashutil.sha1(data).digest())
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 dirpath = self._dirpath(filename)
if not os.path.exists(dirpath):
os.makedirs(dirpath)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 with open(self._filepath(filename), b'wb') as f:
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 f.write(data)
return filename
def read(self, key):
try:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 with open(self._filepath(key), b'rb') as f:
Yuya Nishihara
inifinitepush: fix filebundlestore to close file
r37814 return f.read()
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 except IOError:
return None
Augie Fackler
formatting: blacken the codebase...
r43346
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 class externalbundlestore(abstractbundlestore):
def __init__(self, put_binary, put_args, get_binary, get_args):
"""
`put_binary` - path to binary file which uploads bundle to external
storage and prints key to stdout
`put_args` - format string with additional args to `put_binary`
{filename} replacement field can be used.
`get_binary` - path to binary file which accepts filename and key
(in that order), downloads bundle from store and saves it to file
`get_args` - format string with additional args to `get_binary`.
{filename} and {handle} replacement field can be used.
"""
self.put_args = put_args
self.get_args = get_args
self.put_binary = put_binary
self.get_binary = get_binary
def _call_binary(self, args):
p = subprocess.Popen(
Matt Harbison
py3: convert arguments, cwd and env to native strings when spawning subprocess...
r39851 pycompat.rapply(procutil.tonativestr, args),
Augie Fackler
formatting: blacken the codebase...
r43346 stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=True,
)
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 stdout, stderr = p.communicate()
returncode = p.returncode
return returncode, stdout, stderr
def write(self, data):
# Won't work on windows because you can't open file second time without
# closing it
Yuya Nishihara
py3: wrap tempfile.NamedTemporaryFile() to return bytes fp.name...
r38184 # TODO: rewrite without str.format() and replace NamedTemporaryFile()
# with pycompat.namedtempfile()
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 with NamedTemporaryFile() as temp:
temp.write(data)
temp.flush()
temp.seek(0)
Augie Fackler
formatting: blacken the codebase...
r43346 formatted_args = [
arg.format(filename=temp.name) for arg in self.put_args
]
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 returncode, stdout, stderr = self._call_binary(
Augie Fackler
formatting: blacken the codebase...
r43346 [self.put_binary] + formatted_args
)
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204
if returncode != 0:
raise BundleWriteException(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'Failed to upload to external store: %s' % stderr
Augie Fackler
formatting: blacken the codebase...
r43346 )
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 stdout_lines = stdout.splitlines()
if len(stdout_lines) == 1:
return stdout_lines[0]
else:
raise BundleWriteException(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'Bad output from %s: %s' % (self.put_binary, stdout)
Augie Fackler
formatting: blacken the codebase...
r43346 )
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204
def read(self, handle):
# Won't work on windows because you can't open file second time without
# closing it
Yuya Nishihara
py3: wrap tempfile.NamedTemporaryFile() to return bytes fp.name...
r38184 # TODO: rewrite without str.format() and replace NamedTemporaryFile()
# with pycompat.namedtempfile()
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 with NamedTemporaryFile() as temp:
Augie Fackler
formatting: blacken the codebase...
r43346 formatted_args = [
arg.format(filename=temp.name, handle=handle)
for arg in self.get_args
]
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 returncode, stdout, stderr = self._call_binary(
Augie Fackler
formatting: blacken the codebase...
r43346 [self.get_binary] + formatted_args
)
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204
if returncode != 0:
raise BundleReadException(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'Failed to download from external store: %s' % stderr
Augie Fackler
formatting: blacken the codebase...
r43346 )
Pulkit Goyal
infinitepush: move the extension to core from fb-hgext...
r37204 return temp.read()