hgext/lfs/wrapper.py
author Matt Harbison <matt_harbison@yahoo.com>
Thu, 20 Sep 2018 17:27:01 -0700
changeset 40324 6637b079ae45
parent 39950 e1f97179a3f5
child 40368 fad6068249d9
permissions -rw-r--r--
lfs: autoload the extension when cloning from repo with lfs enabled This is based on a patch by Gregory Szorc. I made small adjustments to clean up the messaging when the server has the extension enabled, but the client has it disabled (to prevent autoloading). Additionally, I added a second server capability to distinguish between the server having the extension enabled, and the server having LFS commits. This helps prevent unnecessary requirement propagation- the client shouldn't add a requirement that the server doesn't have, just because the server had the extension loaded. The TODO I had about advertising a capability when the server can natively serve up blobs isn't relevant anymore (we've had 2 releases that support this), so I dropped it. Currently, we lazily add the "lfs" requirement to a repo when we first encounter LFS data. Due to a pretxnchangegroup hook that looks for LFS data, this can happen at the end of clone. Now that we have more control over how repositories are created, we can do better. This commit adds a repo creation option to add the "lfs" requirement. hg.clone() sets this creation option if the remote peer is advertising lfs usage (as opposed to just support needed to push). So, what this change effectively does is have cloned repos automatically inherit the "lfs" requirement. Differential Revision: https://phab.mercurial-scm.org/D5130
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     1
# wrapper.py - methods wrapping core mercurial logic
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     2
#
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     3
# Copyright 2017 Facebook, Inc.
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     4
#
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     7
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     8
from __future__ import absolute_import
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
     9
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    10
import hashlib
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    11
35122
b8e5fb8d2389 lfs: quiesce check-module-import warnings
Matt Harbison <matt_harbison@yahoo.com>
parents: 35121
diff changeset
    12
from mercurial.i18n import _
36633
f6b9f9ddd0de lfs: convert hexdigest to bytes using sysbytes
Augie Fackler <augie@google.com>
parents: 36632
diff changeset
    13
from mercurial.node import bin, hex, nullid, short
35122
b8e5fb8d2389 lfs: quiesce check-module-import warnings
Matt Harbison <matt_harbison@yahoo.com>
parents: 35121
diff changeset
    14
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    15
from mercurial import (
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    16
    error,
39856
1f7b3b980af8 lfs: add repository feature denoting the use of LFS
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39854
diff changeset
    17
    repository,
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    18
    revlog,
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    19
    util,
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    20
)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    21
37087
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36633
diff changeset
    22
from mercurial.utils import (
39883
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39861
diff changeset
    23
    storageutil,
37087
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36633
diff changeset
    24
    stringutil,
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36633
diff changeset
    25
)
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36633
diff changeset
    26
35372
b0ba1539af01 lfs: restore the local blob store after a repo upgrade
Matt Harbison <matt_harbison@yahoo.com>
parents: 35355
diff changeset
    27
from ..largefiles import lfutil
b0ba1539af01 lfs: restore the local blob store after a repo upgrade
Matt Harbison <matt_harbison@yahoo.com>
parents: 35355
diff changeset
    28
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    29
from . import (
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    30
    blobstore,
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    31
    pointer,
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    32
)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    33
39856
1f7b3b980af8 lfs: add repository feature denoting the use of LFS
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39854
diff changeset
    34
def localrepomakefilestorage(orig, requirements, features, **kwargs):
1f7b3b980af8 lfs: add repository feature denoting the use of LFS
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39854
diff changeset
    35
    if b'lfs' in requirements:
1f7b3b980af8 lfs: add repository feature denoting the use of LFS
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39854
diff changeset
    36
        features.add(repository.REPO_FEATURE_LFS)
1f7b3b980af8 lfs: add repository feature denoting the use of LFS
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39854
diff changeset
    37
1f7b3b980af8 lfs: add repository feature denoting the use of LFS
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39854
diff changeset
    38
    return orig(requirements=requirements, features=features, **kwargs)
1f7b3b980af8 lfs: add repository feature denoting the use of LFS
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39854
diff changeset
    39
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    40
def allsupportedversions(orig, ui):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    41
    versions = orig(ui)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    42
    versions.add('03')
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    43
    return versions
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    44
35510
fa865878a849 lfs: show a friendly message when pushing lfs to a server without lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
    45
def _capabilities(orig, repo, proto):
fa865878a849 lfs: show a friendly message when pushing lfs to a server without lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
    46
    '''Wrap server command to announce lfs server capability'''
fa865878a849 lfs: show a friendly message when pushing lfs to a server without lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
    47
    caps = orig(repo, proto)
37851
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
    48
    if util.safehasattr(repo.svfs, 'lfslocalblobstore'):
40324
6637b079ae45 lfs: autoload the extension when cloning from repo with lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 39950
diff changeset
    49
        # Advertise a slightly different capability when lfs is *required*, so
6637b079ae45 lfs: autoload the extension when cloning from repo with lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 39950
diff changeset
    50
        # that the client knows it MUST load the extension.  If lfs is not
6637b079ae45 lfs: autoload the extension when cloning from repo with lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 39950
diff changeset
    51
        # required on the server, there's no reason to autoload the extension
6637b079ae45 lfs: autoload the extension when cloning from repo with lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 39950
diff changeset
    52
        # on the client.
6637b079ae45 lfs: autoload the extension when cloning from repo with lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 39950
diff changeset
    53
        if b'lfs' in repo.requirements:
6637b079ae45 lfs: autoload the extension when cloning from repo with lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 39950
diff changeset
    54
            caps.append('lfs-serve')
6637b079ae45 lfs: autoload the extension when cloning from repo with lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 39950
diff changeset
    55
37851
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
    56
        caps.append('lfs')
35510
fa865878a849 lfs: show a friendly message when pushing lfs to a server without lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
    57
    return caps
fa865878a849 lfs: show a friendly message when pushing lfs to a server without lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
    58
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    59
def bypasscheckhash(self, text):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    60
    return False
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    61
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    62
def readfromstore(self, text):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    63
    """Read filelog content from local blobstore transform for flagprocessor.
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    64
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    65
    Default tranform for flagprocessor, returning contents from blobstore.
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    66
    Returns a 2-typle (text, validatehash) where validatehash is True as the
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    67
    contents of the blobstore should be checked using checkhash.
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    68
    """
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    69
    p = pointer.deserialize(text)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    70
    oid = p.oid()
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    71
    store = self.opener.lfslocalblobstore
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    72
    if not store.has(oid):
35568
ebf14075a5c1 lfs: improve the error message for a missing remote blob
Matt Harbison <matt_harbison@yahoo.com>
parents: 35553
diff changeset
    73
        p.filename = self.filename
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    74
        self.opener.lfsremoteblobstore.readbatch([p], store)
35480
417e8e040102 lfs: verify lfs object content when transferring to and from the remote store
Matt Harbison <matt_harbison@yahoo.com>
parents: 35477
diff changeset
    75
417e8e040102 lfs: verify lfs object content when transferring to and from the remote store
Matt Harbison <matt_harbison@yahoo.com>
parents: 35477
diff changeset
    76
    # The caller will validate the content
417e8e040102 lfs: verify lfs object content when transferring to and from the remote store
Matt Harbison <matt_harbison@yahoo.com>
parents: 35477
diff changeset
    77
    text = store.read(oid, verify=False)
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    78
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    79
    # pack hg filelog metadata
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    80
    hgmeta = {}
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    81
    for k in p.keys():
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    82
        if k.startswith('x-hg-'):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    83
            name = k[len('x-hg-'):]
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    84
            hgmeta[name] = p[k]
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    85
    if hgmeta or text.startswith('\1\n'):
39883
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39861
diff changeset
    86
        text = storageutil.packmeta(hgmeta, text)
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    87
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    88
    return (text, True)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    89
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    90
def writetostore(self, text):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    91
    # hg filelog metadata (includes rename, etc)
39883
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39861
diff changeset
    92
    hgmeta, offset = storageutil.parsemeta(text)
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    93
    if offset and offset > 0:
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    94
        # lfs blob does not contain hg filelog metadata
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    95
        text = text[offset:]
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    96
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
    97
    # git-lfs only supports sha256
36633
f6b9f9ddd0de lfs: convert hexdigest to bytes using sysbytes
Augie Fackler <augie@google.com>
parents: 36632
diff changeset
    98
    oid = hex(hashlib.sha256(text).digest())
35553
a77418095530 lfs: remove the verification option when writing to the local store
Matt Harbison <matt_harbison@yahoo.com>
parents: 35510
diff changeset
    99
    self.opener.lfslocalblobstore.write(oid, text)
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   100
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   101
    # replace contents with metadata
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   102
    longoid = 'sha256:%s' % oid
36632
dcb6fbaa43a8 lfs: use %d to encode int, not str()
Augie Fackler <augie@google.com>
parents: 36180
diff changeset
   103
    metadata = pointer.gitlfspointer(oid=longoid, size='%d' % len(text))
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   104
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   105
    # by default, we expect the content to be binary. however, LFS could also
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   106
    # be used for non-binary content. add a special entry for non-binary data.
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   107
    # this will be used by filectx.isbinary().
37087
f0b6fbea00cf stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36633
diff changeset
   108
    if not stringutil.binary(text):
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   109
        # not hg filelog metadata (affecting commit hash), no "x-hg-" prefix
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   110
        metadata['x-is-binary'] = '0'
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   111
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   112
    # translate hg filelog metadata to lfs metadata with "x-hg-" prefix
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   113
    if hgmeta is not None:
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   114
        for k, v in hgmeta.iteritems():
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   115
            metadata['x-hg-%s' % k] = v
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   116
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   117
    rawtext = metadata.serialize()
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   118
    return (rawtext, False)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   119
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   120
def _islfs(rlog, node=None, rev=None):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   121
    if rev is None:
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   122
        if node is None:
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   123
            # both None - likely working copy content where node is not ready
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   124
            return False
39860
62a532045e71 lfs: access revlog directly
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39858
diff changeset
   125
        rev = rlog._revlog.rev(node)
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   126
    else:
39860
62a532045e71 lfs: access revlog directly
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39858
diff changeset
   127
        node = rlog._revlog.node(rev)
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   128
    if node == nullid:
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   129
        return False
39860
62a532045e71 lfs: access revlog directly
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39858
diff changeset
   130
    flags = rlog._revlog.flags(rev)
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   131
    return bool(flags & revlog.REVIDX_EXTSTORED)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   132
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   133
def filelogaddrevision(orig, self, text, transaction, link, p1, p2,
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   134
                       cachedelta=None, node=None,
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   135
                       flags=revlog.REVIDX_DEFAULT_FLAGS, **kwds):
37851
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   136
    # The matcher isn't available if reposetup() wasn't called.
39860
62a532045e71 lfs: access revlog directly
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39858
diff changeset
   137
    lfstrack = self._revlog.opener.options.get('lfstrack')
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   138
37851
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   139
    if lfstrack:
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   140
        textlen = len(text)
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   141
        # exclude hg rename meta from file size
39883
3e896b51aa5d storageutil: move metadata parsing and packing from revlog (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39861
diff changeset
   142
        meta, offset = storageutil.parsemeta(text)
37851
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   143
        if offset:
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   144
            textlen -= offset
35648
588d02d9208a lfs: always exclude '.hg*' text files
Matt Harbison <matt_harbison@yahoo.com>
parents: 35624
diff changeset
   145
39861
96838b620b9c filelog: store filename directly on revlog instance
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39860
diff changeset
   146
        if lfstrack(self._revlog.filename, textlen):
37851
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   147
            flags |= revlog.REVIDX_EXTSTORED
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   148
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   149
    return orig(self, text, transaction, link, p1, p2, cachedelta=cachedelta,
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   150
                node=node, flags=flags, **kwds)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   151
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   152
def filelogrenamed(orig, self, node):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   153
    if _islfs(self, node):
39860
62a532045e71 lfs: access revlog directly
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39858
diff changeset
   154
        rawtext = self._revlog.revision(node, raw=True)
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   155
        if not rawtext:
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   156
            return False
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   157
        metadata = pointer.deserialize(rawtext)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   158
        if 'x-hg-copy' in metadata and 'x-hg-copyrev' in metadata:
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   159
            return metadata['x-hg-copy'], bin(metadata['x-hg-copyrev'])
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   160
        else:
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   161
            return False
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   162
    return orig(self, node)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   163
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   164
def filelogsize(orig, self, rev):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   165
    if _islfs(self, rev=rev):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   166
        # fast path: use lfs metadata to answer size
39860
62a532045e71 lfs: access revlog directly
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39858
diff changeset
   167
        rawtext = self._revlog.revision(rev, raw=True)
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   168
        metadata = pointer.deserialize(rawtext)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   169
        return int(metadata['size'])
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   170
    return orig(self, rev)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   171
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   172
def filectxcmp(orig, self, fctx):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   173
    """returns True if text is different than fctx"""
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   174
    # some fctx (ex. hg-git) is not based on basefilectx and do not have islfs
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   175
    if self.islfs() and getattr(fctx, 'islfs', lambda: False)():
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   176
        # fast path: check LFS oid
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   177
        p1 = pointer.deserialize(self.rawdata())
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   178
        p2 = pointer.deserialize(fctx.rawdata())
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   179
        return p1.oid() != p2.oid()
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   180
    return orig(self, fctx)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   181
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   182
def filectxisbinary(orig, self):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   183
    if self.islfs():
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   184
        # fast path: use lfs metadata to answer isbinary
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   185
        metadata = pointer.deserialize(self.rawdata())
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   186
        # if lfs metadata says nothing, assume it's binary by default
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   187
        return bool(int(metadata.get('x-is-binary', 1)))
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   188
    return orig(self)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   189
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   190
def filectxislfs(self):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   191
    return _islfs(self.filelog(), self.filenode())
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   192
35663
a985834961f7 lfs: allow the pointer file to be viewed with `hg cat -T '{rawdata}'`
Matt Harbison <matt_harbison@yahoo.com>
parents: 35648
diff changeset
   193
def _updatecatformatter(orig, fm, ctx, matcher, path, decode):
a985834961f7 lfs: allow the pointer file to be viewed with `hg cat -T '{rawdata}'`
Matt Harbison <matt_harbison@yahoo.com>
parents: 35648
diff changeset
   194
    orig(fm, ctx, matcher, path, decode)
a985834961f7 lfs: allow the pointer file to be viewed with `hg cat -T '{rawdata}'`
Matt Harbison <matt_harbison@yahoo.com>
parents: 35648
diff changeset
   195
    fm.data(rawdata=ctx[path].rawdata())
a985834961f7 lfs: allow the pointer file to be viewed with `hg cat -T '{rawdata}'`
Matt Harbison <matt_harbison@yahoo.com>
parents: 35648
diff changeset
   196
35189
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   197
def convertsink(orig, sink):
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   198
    sink = orig(sink)
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   199
    if sink.repotype == 'hg':
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   200
        class lfssink(sink.__class__):
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   201
            def putcommit(self, files, copies, parents, commit, source, revmap,
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   202
                          full, cleanp2):
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   203
                pc = super(lfssink, self).putcommit
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   204
                node = pc(files, copies, parents, commit, source, revmap, full,
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   205
                          cleanp2)
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   206
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   207
                if 'lfs' not in self.repo.requirements:
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   208
                    ctx = self.repo[node]
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   209
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   210
                    # The file list may contain removed files, so check for
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   211
                    # membership before assuming it is in the context.
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   212
                    if any(f in ctx and ctx[f].islfs() for f, n in files):
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   213
                        self.repo.requirements.add('lfs')
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   214
                        self.repo._writerequirements()
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   215
35225
8887a45e3384 lfs: enable the extension locally after converting to an 'lfs' repo
Matt Harbison <matt_harbison@yahoo.com>
parents: 35224
diff changeset
   216
                        # Permanently enable lfs locally
35624
188b1371d1ed lfs: convert EOL of hgrc before appending to bytes IO
Yuya Nishihara <yuya@tcha.org>
parents: 35618
diff changeset
   217
                        self.repo.vfs.append(
188b1371d1ed lfs: convert EOL of hgrc before appending to bytes IO
Yuya Nishihara <yuya@tcha.org>
parents: 35618
diff changeset
   218
                            'hgrc', util.tonativeeol('\n[extensions]\nlfs=\n'))
35225
8887a45e3384 lfs: enable the extension locally after converting to an 'lfs' repo
Matt Harbison <matt_harbison@yahoo.com>
parents: 35224
diff changeset
   219
35189
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   220
                return node
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   221
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   222
        sink.__class__ = lfssink
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   223
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   224
    return sink
f8f939a2926c lfs: add a repo requirement for this extension when converting to lfs
Matt Harbison <matt_harbison@yahoo.com>
parents: 35122
diff changeset
   225
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   226
def vfsinit(orig, self, othervfs):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   227
    orig(self, othervfs)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   228
    # copy lfs related options
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   229
    for k, v in othervfs.options.items():
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   230
        if k.startswith('lfs'):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   231
            self.options[k] = v
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   232
    # also copy lfs blobstores. note: this can run before reposetup, so lfs
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   233
    # blobstore attributes are not always ready at this time.
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   234
    for name in ['lfslocalblobstore', 'lfsremoteblobstore']:
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   235
        if util.safehasattr(othervfs, name):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   236
            setattr(self, name, getattr(othervfs, name))
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   237
37762
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37564
diff changeset
   238
def _prefetchfiles(repo, revs, match):
35962
0b79f99fd7b0 lfs: prefetch lfs blobs when applying merge updates
Matt Harbison <matt_harbison@yahoo.com>
parents: 35961
diff changeset
   239
    """Ensure that required LFS blobs are present, fetching them as a group if
36180
60dd840a7fdb merge: invoke scmutil.fileprefetchhooks() prior to applying updates
Matt Harbison <matt_harbison@yahoo.com>
parents: 36176
diff changeset
   240
    needed."""
37851
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   241
    if not util.safehasattr(repo.svfs, 'lfslocalblobstore'):
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   242
        return
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   243
35962
0b79f99fd7b0 lfs: prefetch lfs blobs when applying merge updates
Matt Harbison <matt_harbison@yahoo.com>
parents: 35961
diff changeset
   244
    pointers = []
37762
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37564
diff changeset
   245
    oids = set()
35962
0b79f99fd7b0 lfs: prefetch lfs blobs when applying merge updates
Matt Harbison <matt_harbison@yahoo.com>
parents: 35961
diff changeset
   246
    localstore = repo.svfs.lfslocalblobstore
0b79f99fd7b0 lfs: prefetch lfs blobs when applying merge updates
Matt Harbison <matt_harbison@yahoo.com>
parents: 35961
diff changeset
   247
37762
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37564
diff changeset
   248
    for rev in revs:
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37564
diff changeset
   249
        ctx = repo[rev]
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37564
diff changeset
   250
        for f in ctx.walk(match):
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37564
diff changeset
   251
            p = pointerfromctx(ctx, f)
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37564
diff changeset
   252
            if p and p.oid() not in oids and not localstore.has(p.oid()):
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37564
diff changeset
   253
                p.filename = f
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37564
diff changeset
   254
                pointers.append(p)
7269b87f817c scmutil: teach the file prefetch hook to handle multiple commits
Matt Harbison <matt_harbison@yahoo.com>
parents: 37564
diff changeset
   255
                oids.add(p.oid())
35962
0b79f99fd7b0 lfs: prefetch lfs blobs when applying merge updates
Matt Harbison <matt_harbison@yahoo.com>
parents: 35961
diff changeset
   256
0b79f99fd7b0 lfs: prefetch lfs blobs when applying merge updates
Matt Harbison <matt_harbison@yahoo.com>
parents: 35961
diff changeset
   257
    if pointers:
37518
092eff6833a7 lfs: infer the blob store URL from paths.default
Matt Harbison <matt_harbison@yahoo.com>
parents: 37442
diff changeset
   258
        # Recalculating the repo store here allows 'paths.default' that is set
092eff6833a7 lfs: infer the blob store URL from paths.default
Matt Harbison <matt_harbison@yahoo.com>
parents: 37442
diff changeset
   259
        # on the repo by a clone command to be used for the update.
092eff6833a7 lfs: infer the blob store URL from paths.default
Matt Harbison <matt_harbison@yahoo.com>
parents: 37442
diff changeset
   260
        blobstore.remote(repo).readbatch(pointers, localstore)
35962
0b79f99fd7b0 lfs: prefetch lfs blobs when applying merge updates
Matt Harbison <matt_harbison@yahoo.com>
parents: 35961
diff changeset
   261
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   262
def _canskipupload(repo):
37851
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   263
    # Skip if this hasn't been passed to reposetup()
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   264
    if not util.safehasattr(repo.svfs, 'lfsremoteblobstore'):
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   265
        return True
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   266
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   267
    # if remotestore is a null store, upload is a no-op and can be skipped
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   268
    return isinstance(repo.svfs.lfsremoteblobstore, blobstore._nullremote)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   269
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   270
def candownload(repo):
37851
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   271
    # Skip if this hasn't been passed to reposetup()
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   272
    if not util.safehasattr(repo.svfs, 'lfsremoteblobstore'):
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   273
        return False
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   274
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   275
    # if remotestore is a null store, downloads will lead to nothing
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   276
    return not isinstance(repo.svfs.lfsremoteblobstore, blobstore._nullremote)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   277
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   278
def uploadblobsfromrevs(repo, revs):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   279
    '''upload lfs blobs introduced by revs
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   280
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   281
    Note: also used by other extensions e. g. infinitepush. avoid renaming.
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   282
    '''
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   283
    if _canskipupload(repo):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   284
        return
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   285
    pointers = extractpointers(repo, revs)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   286
    uploadblobs(repo, pointers)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   287
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   288
def prepush(pushop):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   289
    """Prepush hook.
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   290
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   291
    Read through the revisions to push, looking for filelog entries that can be
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   292
    deserialized into metadata so that we can block the push on their upload to
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   293
    the remote blobstore.
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   294
    """
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   295
    return uploadblobsfromrevs(pushop.repo, pushop.outgoing.missing)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   296
35510
fa865878a849 lfs: show a friendly message when pushing lfs to a server without lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
   297
def push(orig, repo, remote, *args, **kwargs):
37564
31a4ea773369 lfs: infer the blob store URL from an explicit push dest or default-push
Matt Harbison <matt_harbison@yahoo.com>
parents: 37518
diff changeset
   298
    """bail on push if the extension isn't enabled on remote when needed, and
31a4ea773369 lfs: infer the blob store URL from an explicit push dest or default-push
Matt Harbison <matt_harbison@yahoo.com>
parents: 37518
diff changeset
   299
    update the remote store based on the destination path."""
35510
fa865878a849 lfs: show a friendly message when pushing lfs to a server without lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
   300
    if 'lfs' in repo.requirements:
fa865878a849 lfs: show a friendly message when pushing lfs to a server without lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
   301
        # If the remote peer is for a local repo, the requirement tests in the
fa865878a849 lfs: show a friendly message when pushing lfs to a server without lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
   302
        # base class method enforce lfs support.  Otherwise, some revisions in
fa865878a849 lfs: show a friendly message when pushing lfs to a server without lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
   303
        # this repo use lfs, and the remote repo needs the extension loaded.
fa865878a849 lfs: show a friendly message when pushing lfs to a server without lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
   304
        if not remote.local() and not remote.capable('lfs'):
fa865878a849 lfs: show a friendly message when pushing lfs to a server without lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
   305
            # This is a copy of the message in exchange.push() when requirements
fa865878a849 lfs: show a friendly message when pushing lfs to a server without lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
   306
            # are missing between local repos.
fa865878a849 lfs: show a friendly message when pushing lfs to a server without lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
   307
            m = _("required features are not supported in the destination: %s")
fa865878a849 lfs: show a friendly message when pushing lfs to a server without lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
   308
            raise error.Abort(m % 'lfs',
fa865878a849 lfs: show a friendly message when pushing lfs to a server without lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
   309
                              hint=_('enable the lfs extension on the server'))
37564
31a4ea773369 lfs: infer the blob store URL from an explicit push dest or default-push
Matt Harbison <matt_harbison@yahoo.com>
parents: 37518
diff changeset
   310
31a4ea773369 lfs: infer the blob store URL from an explicit push dest or default-push
Matt Harbison <matt_harbison@yahoo.com>
parents: 37518
diff changeset
   311
        # Repositories where this extension is disabled won't have the field.
31a4ea773369 lfs: infer the blob store URL from an explicit push dest or default-push
Matt Harbison <matt_harbison@yahoo.com>
parents: 37518
diff changeset
   312
        # But if there's a requirement, then the extension must be loaded AND
31a4ea773369 lfs: infer the blob store URL from an explicit push dest or default-push
Matt Harbison <matt_harbison@yahoo.com>
parents: 37518
diff changeset
   313
        # there may be blobs to push.
31a4ea773369 lfs: infer the blob store URL from an explicit push dest or default-push
Matt Harbison <matt_harbison@yahoo.com>
parents: 37518
diff changeset
   314
        remotestore = repo.svfs.lfsremoteblobstore
31a4ea773369 lfs: infer the blob store URL from an explicit push dest or default-push
Matt Harbison <matt_harbison@yahoo.com>
parents: 37518
diff changeset
   315
        try:
31a4ea773369 lfs: infer the blob store URL from an explicit push dest or default-push
Matt Harbison <matt_harbison@yahoo.com>
parents: 37518
diff changeset
   316
            repo.svfs.lfsremoteblobstore = blobstore.remote(repo, remote.url())
31a4ea773369 lfs: infer the blob store URL from an explicit push dest or default-push
Matt Harbison <matt_harbison@yahoo.com>
parents: 37518
diff changeset
   317
            return orig(repo, remote, *args, **kwargs)
31a4ea773369 lfs: infer the blob store URL from an explicit push dest or default-push
Matt Harbison <matt_harbison@yahoo.com>
parents: 37518
diff changeset
   318
        finally:
31a4ea773369 lfs: infer the blob store URL from an explicit push dest or default-push
Matt Harbison <matt_harbison@yahoo.com>
parents: 37518
diff changeset
   319
            repo.svfs.lfsremoteblobstore = remotestore
31a4ea773369 lfs: infer the blob store URL from an explicit push dest or default-push
Matt Harbison <matt_harbison@yahoo.com>
parents: 37518
diff changeset
   320
    else:
31a4ea773369 lfs: infer the blob store URL from an explicit push dest or default-push
Matt Harbison <matt_harbison@yahoo.com>
parents: 37518
diff changeset
   321
        return orig(repo, remote, *args, **kwargs)
35510
fa865878a849 lfs: show a friendly message when pushing lfs to a server without lfs enabled
Matt Harbison <matt_harbison@yahoo.com>
parents: 35509
diff changeset
   322
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   323
def writenewbundle(orig, ui, repo, source, filename, bundletype, outgoing,
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   324
                   *args, **kwargs):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   325
    """upload LFS blobs added by outgoing revisions on 'hg bundle'"""
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   326
    uploadblobsfromrevs(repo, outgoing.missing)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   327
    return orig(ui, repo, source, filename, bundletype, outgoing, *args,
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   328
                **kwargs)
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   329
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   330
def extractpointers(repo, revs):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   331
    """return a list of lfs pointers added by given revs"""
35482
5a73a0446afd lfs: use ui.note() and ui.debug() instead of ui.write() and their flags
Matt Harbison <matt_harbison@yahoo.com>
parents: 35480
diff changeset
   332
    repo.ui.debug('lfs: computing set of blobs to upload\n')
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   333
    pointers = {}
39297
37e56607cbb9 lfs: add a progress bar when searching for blobs to upload
Matt Harbison <matt_harbison@yahoo.com>
parents: 37851
diff changeset
   334
39416
b26350d9d7b5 lfs: use a context manager to control the progress bar lifetime
Matt Harbison <matt_harbison@yahoo.com>
parents: 39297
diff changeset
   335
    makeprogress = repo.ui.makeprogress
b26350d9d7b5 lfs: use a context manager to control the progress bar lifetime
Matt Harbison <matt_harbison@yahoo.com>
parents: 39297
diff changeset
   336
    with makeprogress(_('lfs search'), _('changesets'), len(revs)) as progress:
39297
37e56607cbb9 lfs: add a progress bar when searching for blobs to upload
Matt Harbison <matt_harbison@yahoo.com>
parents: 37851
diff changeset
   337
        for r in revs:
37e56607cbb9 lfs: add a progress bar when searching for blobs to upload
Matt Harbison <matt_harbison@yahoo.com>
parents: 37851
diff changeset
   338
            ctx = repo[r]
37e56607cbb9 lfs: add a progress bar when searching for blobs to upload
Matt Harbison <matt_harbison@yahoo.com>
parents: 37851
diff changeset
   339
            for p in pointersfromctx(ctx).values():
37e56607cbb9 lfs: add a progress bar when searching for blobs to upload
Matt Harbison <matt_harbison@yahoo.com>
parents: 37851
diff changeset
   340
                pointers[p.oid()] = p
37e56607cbb9 lfs: add a progress bar when searching for blobs to upload
Matt Harbison <matt_harbison@yahoo.com>
parents: 37851
diff changeset
   341
            progress.increment()
39950
e1f97179a3f5 lfs: explicitly name a key when sorting blob pointers
Matt Harbison <matt_harbison@yahoo.com>
parents: 39883
diff changeset
   342
        return sorted(pointers.values(), key=lambda p: p.oid())
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   343
36037
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   344
def pointerfromctx(ctx, f, removed=False):
35961
47e737d27e01 lfs: factor out a method for extracting the pointer of a single file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35799
diff changeset
   345
    """return a pointer for the named file from the given changectx, or None if
36037
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   346
    the file isn't LFS.
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   347
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   348
    Optionally, the pointer for a file deleted from the context can be returned.
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   349
    Since no such pointer is actually stored, and to distinguish from a non LFS
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   350
    file, this pointer is represented by an empty dict.
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   351
    """
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   352
    _ctx = ctx
35961
47e737d27e01 lfs: factor out a method for extracting the pointer of a single file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35799
diff changeset
   353
    if f not in ctx:
36037
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   354
        if not removed:
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   355
            return None
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   356
        if f in ctx.p1():
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   357
            _ctx = ctx.p1()
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   358
        elif f in ctx.p2():
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   359
            _ctx = ctx.p2()
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   360
        else:
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   361
            return None
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   362
    fctx = _ctx[f]
35961
47e737d27e01 lfs: factor out a method for extracting the pointer of a single file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35799
diff changeset
   363
    if not _islfs(fctx.filelog(), fctx.filenode()):
47e737d27e01 lfs: factor out a method for extracting the pointer of a single file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35799
diff changeset
   364
        return None
47e737d27e01 lfs: factor out a method for extracting the pointer of a single file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35799
diff changeset
   365
    try:
36037
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   366
        p = pointer.deserialize(fctx.rawdata())
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   367
        if ctx == _ctx:
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   368
            return p
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   369
        return {}
35961
47e737d27e01 lfs: factor out a method for extracting the pointer of a single file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35799
diff changeset
   370
    except pointer.InvalidPointer as ex:
47e737d27e01 lfs: factor out a method for extracting the pointer of a single file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35799
diff changeset
   371
        raise error.Abort(_('lfs: corrupted pointer (%s@%s): %s\n')
36037
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   372
                          % (f, short(_ctx.node()), ex))
35961
47e737d27e01 lfs: factor out a method for extracting the pointer of a single file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35799
diff changeset
   373
36037
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   374
def pointersfromctx(ctx, removed=False):
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   375
    """return a dict {path: pointer} for given single changectx.
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   376
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   377
    If ``removed`` == True and the LFS file was removed from ``ctx``, the value
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   378
    stored for the path is an empty dict.
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   379
    """
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   380
    result = {}
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   381
    for f in ctx.files():
36037
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   382
        p = pointerfromctx(ctx, f, removed=removed)
dce43aaaf209 lfs: allow a pointer to be extracted from a context that removes the file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35964
diff changeset
   383
        if p is not None:
35961
47e737d27e01 lfs: factor out a method for extracting the pointer of a single file
Matt Harbison <matt_harbison@yahoo.com>
parents: 35799
diff changeset
   384
            result[f] = p
35121
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   385
    return result
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   386
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   387
def uploadblobs(repo, pointers):
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   388
    """upload given pointers from local blobstore"""
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   389
    if not pointers:
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   390
        return
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   391
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   392
    remoteblob = repo.svfs.lfsremoteblobstore
66c5a8cf2868 lfs: import the Facebook git-lfs client extension
Matt Harbison <matt_harbison@yahoo.com>
parents:
diff changeset
   393
    remoteblob.writebatch(pointers, repo.svfs.lfslocalblobstore)
35355
9eb19b13e92a lfs: allow to run 'debugupgraderepo' on repo with largefiles
Boris Feld <boris.feld@octobus.net>
parents: 35225
diff changeset
   394
35372
b0ba1539af01 lfs: restore the local blob store after a repo upgrade
Matt Harbison <matt_harbison@yahoo.com>
parents: 35355
diff changeset
   395
def upgradefinishdatamigration(orig, ui, srcrepo, dstrepo, requirements):
b0ba1539af01 lfs: restore the local blob store after a repo upgrade
Matt Harbison <matt_harbison@yahoo.com>
parents: 35355
diff changeset
   396
    orig(ui, srcrepo, dstrepo, requirements)
b0ba1539af01 lfs: restore the local blob store after a repo upgrade
Matt Harbison <matt_harbison@yahoo.com>
parents: 35355
diff changeset
   397
37851
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   398
    # Skip if this hasn't been passed to reposetup()
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   399
    if (util.safehasattr(srcrepo.svfs, 'lfslocalblobstore') and
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   400
        util.safehasattr(dstrepo.svfs, 'lfslocalblobstore')):
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   401
        srclfsvfs = srcrepo.svfs.lfslocalblobstore.vfs
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   402
        dstlfsvfs = dstrepo.svfs.lfslocalblobstore.vfs
35372
b0ba1539af01 lfs: restore the local blob store after a repo upgrade
Matt Harbison <matt_harbison@yahoo.com>
parents: 35355
diff changeset
   403
37851
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   404
        for dirpath, dirs, files in srclfsvfs.walk():
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   405
            for oid in files:
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   406
                ui.write(_('copying lfs blob %s\n') % oid)
3790efb388ca lfs: bypass wrapped functions when reposetup() hasn't been called (issue5902)
Matt Harbison <matt_harbison@yahoo.com>
parents: 37762
diff changeset
   407
                lfutil.link(srclfsvfs.join(oid), dstlfsvfs.join(oid))
35372
b0ba1539af01 lfs: restore the local blob store after a repo upgrade
Matt Harbison <matt_harbison@yahoo.com>
parents: 35355
diff changeset
   408
35355
9eb19b13e92a lfs: allow to run 'debugupgraderepo' on repo with largefiles
Boris Feld <boris.feld@octobus.net>
parents: 35225
diff changeset
   409
def upgraderequirements(orig, repo):
9eb19b13e92a lfs: allow to run 'debugupgraderepo' on repo with largefiles
Boris Feld <boris.feld@octobus.net>
parents: 35225
diff changeset
   410
    reqs = orig(repo)
9eb19b13e92a lfs: allow to run 'debugupgraderepo' on repo with largefiles
Boris Feld <boris.feld@octobus.net>
parents: 35225
diff changeset
   411
    if 'lfs' in repo.requirements:
9eb19b13e92a lfs: allow to run 'debugupgraderepo' on repo with largefiles
Boris Feld <boris.feld@octobus.net>
parents: 35225
diff changeset
   412
        reqs.add('lfs')
9eb19b13e92a lfs: allow to run 'debugupgraderepo' on repo with largefiles
Boris Feld <boris.feld@octobus.net>
parents: 35225
diff changeset
   413
    return reqs