__init__.py
404 lines
| 13.5 KiB
| text/x-python
|
PythonLexer
Matt Harbison
|
r35097 | # lfs - hash-preserving large file support using Git-LFS protocol | ||
# | ||||
# Copyright 2017 Facebook, Inc. | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
"""lfs - large file support (EXPERIMENTAL) | ||||
Matt Harbison
|
r35786 | This extension allows large files to be tracked outside of the normal | ||
repository storage and stored on a centralized server, similar to the | ||||
``largefiles`` extension. The ``git-lfs`` protocol is used when | ||||
communicating with the server, so existing git infrastructure can be | ||||
harnessed. Even though the files are stored outside of the repository, | ||||
they are still integrity checked in the same manner as normal files. | ||||
Matt Harbison
|
r35683 | |||
Matt Harbison
|
r35786 | The files stored outside of the repository are downloaded on demand, | ||
which reduces the time to clone, and possibly the local disk usage. | ||||
This changes fundamental workflows in a DVCS, so careful thought | ||||
should be given before deploying it. :hg:`convert` can be used to | ||||
convert LFS repositories to normal repositories that no longer | ||||
require this extension, and do so without changing the commit hashes. | ||||
This allows the extension to be disabled if the centralized workflow | ||||
becomes burdensome. However, the pre and post convert clones will | ||||
not be able to communicate with each other unless the extension is | ||||
enabled on both. | ||||
Matt Harbison
|
r35825 | To start a new repository, or to add LFS files to an existing one, just | ||
create an ``.hglfs`` file as described below in the root directory of | ||||
the repository. Typically, this file should be put under version | ||||
control, so that the settings will propagate to other repositories with | ||||
push and pull. During any commit, Mercurial will consult this file to | ||||
determine if an added or modified file should be stored externally. The | ||||
type of storage depends on the characteristics of the file at each | ||||
commit. A file that is near a size threshold may switch back and forth | ||||
between LFS and normal storage, as needed. | ||||
Matt Harbison
|
r35786 | |||
Alternately, both normal repositories and largefile controlled | ||||
repositories can be converted to LFS by using :hg:`convert` and the | ||||
``lfs.track`` config option described below. The ``.hglfs`` file | ||||
should then be created and added, to control subsequent LFS selection. | ||||
The hashes are also unchanged in this case. The LFS and non-LFS | ||||
repositories can be distinguished because the LFS repository will | ||||
abort any command if this extension is disabled. | ||||
Matt Harbison
|
r35683 | |||
Matt Harbison
|
r35786 | Committed LFS files are held locally, until the repository is pushed. | ||
Prior to pushing the normal repository data, the LFS files that are | ||||
tracked by the outgoing commits are automatically uploaded to the | ||||
configured central server. No LFS files are transferred on | ||||
:hg:`pull` or :hg:`clone`. Instead, the files are downloaded on | ||||
demand as they need to be read, if a cached copy cannot be found | ||||
locally. Both committing and downloading an LFS file will link the | ||||
file to a usercache, to speed up future access. See the `usercache` | ||||
config setting described below. | ||||
Denis Laxalde
|
r43597 | The extension reads its configuration from a versioned ``.hglfs`` | ||
configuration file found in the root of the working directory. The | ||||
``.hglfs`` file uses the same syntax as all other Mercurial | ||||
configuration files. It uses a single section, ``[track]``. | ||||
Matt Harbison
|
r35786 | |||
Denis Laxalde
|
r43597 | The ``[track]`` section specifies which files are stored as LFS (or | ||
not). Each line is keyed by a file pattern, with a predicate value. | ||||
The first file pattern match is used, so put more specific patterns | ||||
first. The available predicates are ``all()``, ``none()``, and | ||||
``size()``. See "hg help filesets.size" for the latter. | ||||
Matt Harbison
|
r35786 | |||
Denis Laxalde
|
r43597 | Example versioned ``.hglfs`` file:: | ||
Matt Harbison
|
r35683 | |||
Denis Laxalde
|
r43597 | [track] | ||
# No Makefile or python file, anywhere, will be LFS | ||||
**Makefile = none() | ||||
**.py = none() | ||||
Matt Harbison
|
r35683 | |||
Denis Laxalde
|
r43597 | **.zip = all() | ||
**.exe = size(">1MB") | ||||
Matt Harbison
|
r35786 | |||
Denis Laxalde
|
r43597 | # Catchall for everything not matched above | ||
** = size(">10MB") | ||||
Matt Harbison
|
r35683 | |||
Matt Harbison
|
r35097 | Configs:: | ||
[lfs] | ||||
# Remote endpoint. Multiple protocols are supported: | ||||
# - http(s)://user:pass@example.com/path | ||||
# git-lfs endpoint | ||||
# - file:///tmp/path | ||||
# local filesystem, usually for testing | ||||
Matt Harbison
|
r37582 | # if unset, lfs will assume the remote repository also handles blob storage | ||
# for http(s) URLs. Otherwise, lfs will prompt to set this when it must | ||||
# use this value. | ||||
Matt Harbison
|
r35097 | # (default: unset) | ||
Matt Harbison
|
r35786 | url = https://example.com/repo.git/info/lfs | ||
Matt Harbison
|
r35097 | |||
Matt Harbison
|
r35636 | # Which files to track in LFS. Path tests are "**.extname" for file | ||
# extensions, and "path:under/some/directory" for path prefix. Both | ||||
Yuya Nishihara
|
r35759 | # are relative to the repository root. | ||
Matt Harbison
|
r35636 | # File size can be tested with the "size()" fileset, and tests can be | ||
# joined with fileset operators. (See "hg help filesets.operators".) | ||||
# | ||||
# Some examples: | ||||
# - all() # everything | ||||
# - none() # nothing | ||||
# - size(">20MB") # larger than 20MB | ||||
# - !**.txt # anything not a *.txt file | ||||
# - **.zip | **.tar.gz | **.7z # some types of compressed files | ||||
Yuya Nishihara
|
r35759 | # - path:bin # files under "bin" in the project root | ||
Matt Harbison
|
r35636 | # - (**.php & size(">2MB")) | (**.js & size(">5MB")) | **.tar.gz | ||
Yuya Nishihara
|
r35759 | # | (path:bin & !path:/bin/README) | size(">1GB") | ||
Matt Harbison
|
r35636 | # (default: none()) | ||
Matt Harbison
|
r35683 | # | ||
# This is ignored if there is a tracked '.hglfs' file, and this setting | ||||
# will eventually be deprecated and removed. | ||||
Matt Harbison
|
r35636 | track = size(">10M") | ||
Matt Harbison
|
r35097 | |||
# how many times to retry before giving up on transferring an object | ||||
retry = 5 | ||||
Matt Harbison
|
r35281 | |||
# the local directory to store lfs files for sharing across local clones. | ||||
# If not set, the cache is located in an OS specific cache location. | ||||
usercache = /path/to/global/cache | ||||
Matt Harbison
|
r35097 | """ | ||
from __future__ import absolute_import | ||||
Matt Harbison
|
r40304 | import sys | ||
Matt Harbison
|
r35098 | from mercurial.i18n import _ | ||
Matt Harbison
|
r35097 | from mercurial import ( | ||
Matt Harbison
|
r35683 | config, | ||
Matt Harbison
|
r41315 | context, | ||
Matt Harbison
|
r35683 | error, | ||
Matt Harbison
|
r35097 | exchange, | ||
extensions, | ||||
Matt Harbison
|
r41078 | exthelper, | ||
Matt Harbison
|
r35097 | filelog, | ||
Yuya Nishihara
|
r38841 | filesetlang, | ||
Matt Harbison
|
r35167 | localrepo, | ||
Matt Harbison
|
r35636 | minifileset, | ||
Matt Harbison
|
r35520 | node, | ||
Matt Harbison
|
r35675 | pycompat, | ||
Matt Harbison
|
r35097 | revlog, | ||
scmutil, | ||||
Yuya Nishihara
|
r36939 | templateutil, | ||
Matt Harbison
|
r35749 | util, | ||
Matt Harbison
|
r35097 | ) | ||
Augie Fackler
|
r43346 | from mercurial.interfaces import repository | ||
Pulkit Goyal
|
r43078 | |||
Matt Harbison
|
r35097 | from . import ( | ||
blobstore, | ||||
Matt Harbison
|
r37165 | wireprotolfsserver, | ||
Matt Harbison
|
r35097 | wrapper, | ||
) | ||||
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for | ||||
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should | ||||
# be specifying the version(s) of Mercurial they are tested with, or | ||||
# leave the attribute unspecified. | ||||
Augie Fackler
|
r43347 | testedwith = b'ships-with-hg-core' | ||
Matt Harbison
|
r35097 | |||
Matt Harbison
|
r41078 | eh = exthelper.exthelper() | ||
eh.merge(wrapper.eh) | ||||
eh.merge(wireprotolfsserver.eh) | ||||
Matt Harbison
|
r35099 | |||
Matt Harbison
|
r41078 | cmdtable = eh.cmdtable | ||
configtable = eh.configtable | ||||
extsetup = eh.finalextsetup | ||||
uisetup = eh.finaluisetup | ||||
Matt Harbison
|
r41100 | filesetpredicate = eh.filesetpredicate | ||
Matt Harbison
|
r41078 | reposetup = eh.finalreposetup | ||
Matt Harbison
|
r41099 | templatekeyword = eh.templatekeyword | ||
Matt Harbison
|
r41078 | |||
Augie Fackler
|
r43346 | eh.configitem( | ||
Augie Fackler
|
r43347 | b'experimental', b'lfs.serve', default=True, | ||
Matt Harbison
|
r37265 | ) | ||
Augie Fackler
|
r43346 | eh.configitem( | ||
Augie Fackler
|
r43347 | b'experimental', b'lfs.user-agent', default=None, | ||
Matt Harbison
|
r35456 | ) | ||
Augie Fackler
|
r43346 | eh.configitem( | ||
Augie Fackler
|
r43347 | b'experimental', b'lfs.disableusercache', default=False, | ||
Matt Harbison
|
r37580 | ) | ||
Augie Fackler
|
r43346 | eh.configitem( | ||
Augie Fackler
|
r43347 | b'experimental', b'lfs.worker-enable', default=False, | ||
Matt Harbison
|
r35750 | ) | ||
Matt Harbison
|
r35456 | |||
Augie Fackler
|
r43346 | eh.configitem( | ||
Augie Fackler
|
r43347 | b'lfs', b'url', default=None, | ||
Matt Harbison
|
r35099 | ) | ||
Augie Fackler
|
r43346 | eh.configitem( | ||
Augie Fackler
|
r43347 | b'lfs', b'usercache', default=None, | ||
Matt Harbison
|
r35281 | ) | ||
Matt Harbison
|
r35636 | # Deprecated | ||
Augie Fackler
|
r43346 | eh.configitem( | ||
Augie Fackler
|
r43347 | b'lfs', b'threshold', default=None, | ||
Matt Harbison
|
r35099 | ) | ||
Augie Fackler
|
r43346 | eh.configitem( | ||
Augie Fackler
|
r43347 | b'lfs', b'track', default=b'none()', | ||
Matt Harbison
|
r35636 | ) | ||
Augie Fackler
|
r43346 | eh.configitem( | ||
Augie Fackler
|
r43347 | b'lfs', b'retry', default=5, | ||
Matt Harbison
|
r35099 | ) | ||
Matt Harbison
|
r35097 | |||
Matt Harbison
|
r40304 | lfsprocessor = ( | ||
wrapper.readfromstore, | ||||
wrapper.writetostore, | ||||
wrapper.bypasscheckhash, | ||||
) | ||||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r35167 | def featuresetup(ui, supported): | ||
# don't die on seeing a repo with the lfs requirement | ||||
Augie Fackler
|
r43347 | supported |= {b'lfs'} | ||
Matt Harbison
|
r35167 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r41078 | @eh.uisetup | ||
def _uisetup(ui): | ||||
Gregory Szorc
|
r37153 | localrepo.featuresetupfuncs.add(featuresetup) | ||
Matt Harbison
|
r35167 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r41078 | @eh.reposetup | ||
def _reposetup(ui, repo): | ||||
Matt Harbison
|
r35097 | # Nothing to do with a remote repo | ||
if not repo.local(): | ||||
return | ||||
repo.svfs.lfslocalblobstore = blobstore.local(repo) | ||||
repo.svfs.lfsremoteblobstore = blobstore.remote(repo) | ||||
Matt Harbison
|
r35683 | class lfsrepo(repo.__class__): | ||
@localrepo.unfilteredmethod | ||||
Valentin Gatien-Baron
|
r42839 | def commitctx(self, ctx, error=False, origctx=None): | ||
Augie Fackler
|
r43347 | repo.svfs.options[b'lfstrack'] = _trackedmatcher(self) | ||
Valentin Gatien-Baron
|
r42839 | return super(lfsrepo, self).commitctx(ctx, error, origctx=origctx) | ||
Matt Harbison
|
r35683 | |||
repo.__class__ = lfsrepo | ||||
Augie Fackler
|
r43347 | if b'lfs' not in repo.requirements: | ||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r35167 | def checkrequireslfs(ui, repo, **kwargs): | ||
Augie Fackler
|
r43347 | if b'lfs' in repo.requirements: | ||
Matt Harbison
|
r40184 | return 0 | ||
Augie Fackler
|
r43906 | last = kwargs.get('node_last') | ||
Matt Harbison
|
r40184 | _bin = node.bin | ||
if last: | ||||
Augie Fackler
|
r43906 | s = repo.set(b'%n:%n', _bin(kwargs['node']), _bin(last)) | ||
Matt Harbison
|
r40184 | else: | ||
Augie Fackler
|
r43906 | s = repo.set(b'%n', _bin(kwargs['node'])) | ||
Martin von Zweigbergk
|
r41266 | match = repo._storenarrowmatch | ||
Matt Harbison
|
r35520 | for ctx in s: | ||
Matt Harbison
|
r35167 | # TODO: is there a way to just walk the files in the commit? | ||
Augie Fackler
|
r43346 | if any( | ||
ctx[f].islfs() for f in ctx.files() if f in ctx and match(f) | ||||
): | ||||
Augie Fackler
|
r43347 | repo.requirements.add(b'lfs') | ||
Gregory Szorc
|
r39887 | repo.features.add(repository.REPO_FEATURE_LFS) | ||
Matt Harbison
|
r35167 | repo._writerequirements() | ||
Augie Fackler
|
r43347 | repo.prepushoutgoinghooks.add(b'lfs', wrapper.prepush) | ||
Matt Harbison
|
r35520 | break | ||
Matt Harbison
|
r35167 | |||
Augie Fackler
|
r43347 | ui.setconfig(b'hooks', b'commit.lfs', checkrequireslfs, b'lfs') | ||
ui.setconfig( | ||||
b'hooks', b'pretxnchangegroup.lfs', checkrequireslfs, b'lfs' | ||||
) | ||||
Matt Harbison
|
r35753 | else: | ||
Augie Fackler
|
r43347 | repo.prepushoutgoinghooks.add(b'lfs', wrapper.prepush) | ||
Matt Harbison
|
r35167 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r35898 | def _trackedmatcher(repo): | ||
Matt Harbison
|
r35682 | """Return a function (path, size) -> bool indicating whether or not to | ||
track a given file with lfs.""" | ||||
Augie Fackler
|
r43347 | if not repo.wvfs.exists(b'.hglfs'): | ||
Matt Harbison
|
r35825 | # No '.hglfs' in wdir. Fallback to config for now. | ||
Augie Fackler
|
r43347 | trackspec = repo.ui.config(b'lfs', b'track') | ||
Matt Harbison
|
r35683 | |||
Matt Harbison
|
r35825 | # deprecated config: lfs.threshold | ||
Augie Fackler
|
r43347 | threshold = repo.ui.configbytes(b'lfs', b'threshold') | ||
Matt Harbison
|
r35825 | if threshold: | ||
Yuya Nishihara
|
r38841 | filesetlang.parse(trackspec) # make sure syntax errors are confined | ||
Augie Fackler
|
r43347 | trackspec = b"(%s) | size('>%d')" % (trackspec, threshold) | ||
Matt Harbison
|
r35683 | |||
Matt Harbison
|
r35825 | return minifileset.compile(trackspec) | ||
Matt Harbison
|
r35683 | |||
Augie Fackler
|
r43347 | data = repo.wvfs.tryread(b'.hglfs') | ||
Matt Harbison
|
r35683 | if not data: | ||
return lambda p, s: False | ||||
# Parse errors here will abort with a message that points to the .hglfs file | ||||
# and line number. | ||||
cfg = config.config() | ||||
Augie Fackler
|
r43347 | cfg.parse(b'.hglfs', data) | ||
Matt Harbison
|
r35682 | |||
Matt Harbison
|
r35683 | try: | ||
Augie Fackler
|
r43346 | rules = [ | ||
(minifileset.compile(pattern), minifileset.compile(rule)) | ||||
Augie Fackler
|
r43347 | for pattern, rule in cfg.items(b'track') | ||
Augie Fackler
|
r43346 | ] | ||
Matt Harbison
|
r35683 | except error.ParseError as e: | ||
# The original exception gives no indicator that the error is in the | ||||
# .hglfs file, so add that. | ||||
# TODO: See if the line number of the file can be made available. | ||||
Augie Fackler
|
r43347 | raise error.Abort(_(b'parse error in .hglfs: %s') % e) | ||
Matt Harbison
|
r35683 | |||
def _match(path, size): | ||||
for pat, rule in rules: | ||||
if pat(path, size): | ||||
return rule(path, size) | ||||
return False | ||||
return _match | ||||
Matt Harbison
|
r35682 | |||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r41078 | # Called by remotefilelog | ||
Matt Harbison
|
r35097 | def wrapfilelog(filelog): | ||
wrapfunction = extensions.wrapfunction | ||||
wrapfunction(filelog, 'addrevision', wrapper.filelogaddrevision) | ||||
wrapfunction(filelog, 'renamed', wrapper.filelogrenamed) | ||||
wrapfunction(filelog, 'size', wrapper.filelogsize) | ||||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r43347 | @eh.wrapfunction(localrepo, b'resolverevlogstorevfsoptions') | ||
Matt Harbison
|
r40304 | def _resolverevlogstorevfsoptions(orig, ui, requirements, features): | ||
opts = orig(ui, requirements, features) | ||||
for name, module in extensions.extensions(ui): | ||||
if module is sys.modules[__name__]: | ||||
if revlog.REVIDX_EXTSTORED in opts[b'flagprocessors']: | ||||
Augie Fackler
|
r43346 | msg = ( | ||
_(b"cannot register multiple processors on flag '%#x'.") | ||||
% revlog.REVIDX_EXTSTORED | ||||
) | ||||
Matt Harbison
|
r40304 | raise error.Abort(msg) | ||
opts[b'flagprocessors'][revlog.REVIDX_EXTSTORED] = lfsprocessor | ||||
break | ||||
return opts | ||||
Augie Fackler
|
r43346 | |||
Matt Harbison
|
r41078 | @eh.extsetup | ||
def _extsetup(ui): | ||||
Matt Harbison
|
r35097 | wrapfilelog(filelog.filelog) | ||
Matt Harbison
|
r41315 | context.basefilectx.islfs = wrapper.filectxislfs | ||
Augie Fackler
|
r43347 | scmutil.fileprefetchhooks.add(b'lfs', wrapper._prefetchfiles) | ||
Matt Harbison
|
r35940 | |||
Matt Harbison
|
r35097 | # Make bundle choose changegroup3 instead of changegroup2. This affects | ||
# "hg bundle" command. Note: it does not cover all bundle formats like | ||||
# "packed1". Using "packed1" with lfs will likely cause trouble. | ||||
Augie Fackler
|
r43347 | exchange._bundlespeccontentopts[b"v2"][b"cg.version"] = b"03" | ||
Matt Harbison
|
r35097 | |||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r43347 | @eh.filesetpredicate(b'lfs()') | ||
Matt Harbison
|
r36008 | def lfsfileset(mctx, x): | ||
"""File that uses LFS storage.""" | ||||
# i18n: "lfs" is a keyword | ||||
Augie Fackler
|
r43347 | filesetlang.getargs(x, 0, 0, _(b"lfs takes no arguments")) | ||
Yuya Nishihara
|
r38711 | ctx = mctx.ctx | ||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r38711 | def lfsfilep(f): | ||
return wrapper.pointerfromctx(ctx, f, removed=True) is not None | ||||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r43347 | return mctx.predicate(lfsfilep, predrepr=b'<lfs>') | ||
Matt Harbison
|
r36008 | |||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r43347 | @eh.templatekeyword(b'lfs_files', requires={b'ctx'}) | ||
Yuya Nishihara
|
r36616 | def lfsfiles(context, mapping): | ||
Matt Harbison
|
r36017 | """List of strings. All files modified, added, or removed by this | ||
changeset.""" | ||||
Augie Fackler
|
r43347 | ctx = context.resource(mapping, b'ctx') | ||
Matt Harbison
|
r35675 | |||
Augie Fackler
|
r43346 | pointers = wrapper.pointersfromctx(ctx, removed=True) # {path: pointer} | ||
Matt Harbison
|
r35675 | files = sorted(pointers.keys()) | ||
Matt Harbison
|
r35787 | def pointer(v): | ||
Matt Harbison
|
r35749 | # In the file spec, version is first and the other keys are sorted. | ||
Augie Fackler
|
r43347 | sortkeyfunc = lambda x: (x[0] != b'version', x) | ||
Gregory Szorc
|
r43375 | items = sorted(pycompat.iteritems(pointers[v]), key=sortkeyfunc) | ||
Matt Harbison
|
r35749 | return util.sortdict(items) | ||
Matt Harbison
|
r35675 | makemap = lambda v: { | ||
Augie Fackler
|
r43347 | b'file': v, | ||
b'lfsoid': pointers[v].oid() if pointers[v] else None, | ||||
b'lfspointer': templateutil.hybriddict(pointer(v)), | ||||
Matt Harbison
|
r35675 | } | ||
# TODO: make the separator ', '? | ||||
Augie Fackler
|
r43347 | f = templateutil._showcompatlist(context, mapping, b'lfs_file', files) | ||
Yuya Nishihara
|
r36939 | return templateutil.hybrid(f, files, makemap, pycompat.identity) | ||
Matt Harbison
|
r35097 | |||
Augie Fackler
|
r43346 | |||
@eh.command( | ||||
Augie Fackler
|
r43347 | b'debuglfsupload', | ||
[(b'r', b'rev', [], _(b'upload large files introduced by REV'))], | ||||
Augie Fackler
|
r43346 | ) | ||
Matt Harbison
|
r35097 | def debuglfsupload(ui, repo, **opts): | ||
"""upload lfs blobs added by the working copy parent or given revisions""" | ||||
Augie Fackler
|
r43906 | revs = opts.get('rev', []) | ||
Matt Harbison
|
r35097 | pointers = wrapper.extractpointers(repo, scmutil.revrange(repo, revs)) | ||
wrapper.uploadblobs(repo, pointers) | ||||