hgext/largefiles/proto.py
author Matt Harbison <matt_harbison@yahoo.com>
Sun, 05 Jan 2025 22:26:16 -0500
changeset 52644 e627cc25b6f3
parent 52640 24ee91ba9aa8
permissions -rw-r--r--
pyupgrade: rewrite `yield` statements in a loop to `yield from` This is the `legacy` fixer in `pyupgrade`, with the `yield` statement yielding loop commented back in. This seems to help pytype in some cases, and hurt it in others. But that can be manually fixed later. Note that it's possibly buggy in that it aggressively changed `import-checker.py` to `yield from 'fcntl', 'grp', 'pwd', 'select', 'termios': # Unix only`, which is invalid syntax. Possibly it needed help from the token fixer that I've disabled locally (because that wants to make a bunch of unrelated changes). Just change those few places to yield from a list, to avoid having to constantly revert that.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     1
# Copyright 2011 Fog Creek Software
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     2
#
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     3
# This software may be used and distributed according to the terms of the
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     4
# GNU General Public License version 2 or any later version.
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     5
51859
f4733654f144 typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents: 50782
diff changeset
     6
from __future__ import annotations
f4733654f144 typing: add `from __future__ import annotations` to most files
Matt Harbison <matt_harbison@yahoo.com>
parents: 50782
diff changeset
     7
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     8
import os
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
     9
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    10
from mercurial.i18n import _
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    11
29312
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    12
from mercurial import (
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    13
    error,
41062
0a7f582f6f1f largefiles: port wrapped functions to exthelper
Matt Harbison <matt_harbison@yahoo.com>
parents: 37614
diff changeset
    14
    exthelper,
29312
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    15
    httppeer,
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    16
    util,
36074
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36070
diff changeset
    17
    wireprototypes,
37614
a81d02ea65db wireproto: move version 1 peer functionality to standalone module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37484
diff changeset
    18
    wireprotov1peer,
41062
0a7f582f6f1f largefiles: port wrapped functions to exthelper
Matt Harbison <matt_harbison@yahoo.com>
parents: 37614
diff changeset
    19
    wireprotov1server,
29312
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    20
)
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    21
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    22
from . import lfutil
29312
29139be0ccc7 py3: make largefiles/proto.py use absolute_import
liscju <piotr.listkiewicz@gmail.com>
parents: 28883
diff changeset
    23
28883
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28576
diff changeset
    24
urlerr = util.urlerr
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28576
diff changeset
    25
urlreq = util.urlreq
032c4c2f802a pycompat: switch to util.urlreq/util.urlerr for py3 compat
timeless <timeless@mozdev.org>
parents: 28576
diff changeset
    26
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    27
LARGEFILES_REQUIRED_MSG = (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    28
    b'\nThis repository uses the largefiles extension.'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    29
    b'\n\nPlease enable it in your Mercurial config '
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    30
    b'file.\n'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    31
)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    32
41062
0a7f582f6f1f largefiles: port wrapped functions to exthelper
Matt Harbison <matt_harbison@yahoo.com>
parents: 37614
diff changeset
    33
eh = exthelper.exthelper()
0a7f582f6f1f largefiles: port wrapped functions to exthelper
Matt Harbison <matt_harbison@yahoo.com>
parents: 37614
diff changeset
    34
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    35
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    36
def putlfile(repo, proto, sha):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    37
    """Server command for putting a largefile into a repository's local store
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    38
    and into the user cache."""
36067
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    39
    with proto.mayberedirectstdio() as output:
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    40
        path = lfutil.storepath(repo, sha)
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    41
        util.makedirs(os.path.dirname(path))
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    42
        tmpfp = util.atomictempfile(path, createmode=repo.store.createmode)
16594
5516fdf3fe24 largefiles: in putlfile, ensure tempfile's directory exists prior to creation
hlian
parents: 16247
diff changeset
    43
36067
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    44
        try:
37414
2d965bfeb8f6 wireproto: allow direct stream processing for unbundle
Joerg Sonnenberger <joerg@bec.de>
parents: 37293
diff changeset
    45
            for p in proto.getpayload():
2d965bfeb8f6 wireproto: allow direct stream processing for unbundle
Joerg Sonnenberger <joerg@bec.de>
parents: 37293
diff changeset
    46
                tmpfp.write(p)
36067
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    47
            tmpfp._fp.seek(0)
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    48
            if sha != lfutil.hexsha1(tmpfp._fp):
52640
24ee91ba9aa8 pyupgrade: drop usage of py3 aliases for `OSError`
Matt Harbison <matt_harbison@yahoo.com>
parents: 52563
diff changeset
    49
                raise OSError(0, _(b'largefile contents do not match hash'))
36067
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    50
            tmpfp.close()
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    51
            lfutil.linktousercache(repo, sha)
52640
24ee91ba9aa8 pyupgrade: drop usage of py3 aliases for `OSError`
Matt Harbison <matt_harbison@yahoo.com>
parents: 52563
diff changeset
    52
        except OSError as e:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    53
            repo.ui.warn(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    54
                _(b'largefiles: failed to put %s into store: %s\n')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    55
                % (sha, e.strerror)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    56
            )
37293
d5d665f6615a wireproto: stop aliasing wire protocol types (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36656
diff changeset
    57
            return wireprototypes.pushres(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    58
                1, output.getvalue() if output else b''
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    59
            )
36067
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    60
        finally:
caca3ac2ac04 wireproto: use maybecapturestdio() for push responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35750
diff changeset
    61
            tmpfp.discard()
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    62
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    63
    return wireprototypes.pushres(0, output.getvalue() if output else b'')
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    64
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    65
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    66
def getlfile(repo, proto, sha):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    67
    """Server command for retrieving a largefile from the repository-local
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    68
    cache or user cache."""
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    69
    filename = lfutil.findfile(repo, sha)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    70
    if not filename:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    71
        raise error.Abort(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    72
            _(b'requested largefile %s not present in cache') % sha
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    73
        )
52562
5dcf40af7c2d largefiles: add a TODO about fixing a file descriptor leak
Matt Harbison <matt_harbison@yahoo.com>
parents: 52389
diff changeset
    74
5dcf40af7c2d largefiles: add a TODO about fixing a file descriptor leak
Matt Harbison <matt_harbison@yahoo.com>
parents: 52389
diff changeset
    75
    # TODO: fix the fd leak here
52389
aa3261b40492 largefiles: stop using the `pycompat.open()` shim
Matt Harbison <matt_harbison@yahoo.com>
parents: 51859
diff changeset
    76
    f = open(filename, 'rb')
52563
30f81efce00b largefiles: use `stat_result.st_size` instead of a magic tuple index
Matt Harbison <matt_harbison@yahoo.com>
parents: 52562
diff changeset
    77
    length = os.fstat(f.fileno()).st_size
15252
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15224
diff changeset
    78
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15224
diff changeset
    79
    # Since we can't set an HTTP content-length header here, and
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15224
diff changeset
    80
    # Mercurial core provides no way to give the length of a streamres
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15224
diff changeset
    81
    # (and reading the entire file into RAM would be ill-advised), we
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15224
diff changeset
    82
    # just send the length on the first line of the response, like the
6e809bb4f969 largefiles: improve comments, internal docstrings
Greg Ward <greg@gerg.ca>
parents: 15224
diff changeset
    83
    # ssh proto does for string responses.
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    84
    def generator():
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    85
        yield b'%d\n' % length
52644
e627cc25b6f3 pyupgrade: rewrite `yield` statements in a loop to `yield from`
Matt Harbison <matt_harbison@yahoo.com>
parents: 52640
diff changeset
    86
        yield from util.filechunkiter(f)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    87
37293
d5d665f6615a wireproto: stop aliasing wire protocol types (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36656
diff changeset
    88
    return wireprototypes.streamreslegacy(gen=generator())
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    89
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
    90
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    91
def statlfile(repo, proto, sha):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    92
    """Server command for checking if a largefile is present - returns '2\n' if
28576
33bd95443e7f largefiles: add some docstrings
Mads Kiilerich <madski@unity3d.com>
parents: 26825
diff changeset
    93
    the largefile is missing, '0\n' if it seems to be in good condition.
18488
a977b42df8b3 largefiles: don't verify largefile hashes on servers when processing statlfile
Mads Kiilerich <madski@unity3d.com>
parents: 18298
diff changeset
    94
a977b42df8b3 largefiles: don't verify largefile hashes on servers when processing statlfile
Mads Kiilerich <madski@unity3d.com>
parents: 18298
diff changeset
    95
    The value 1 is reserved for mismatched checksum, but that is too expensive
a977b42df8b3 largefiles: don't verify largefile hashes on servers when processing statlfile
Mads Kiilerich <madski@unity3d.com>
parents: 18298
diff changeset
    96
    to be verified on every stat and must be caught be running 'hg verify'
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    97
    server side."""
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    98
    filename = lfutil.findfile(repo, sha)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
    99
    if not filename:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   100
        return wireprototypes.bytesresponse(b'2\n')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   101
    return wireprototypes.bytesresponse(b'0\n')
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   102
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   103
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   104
def wirereposetup(ui, repo):
46200
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   105
    orig_commandexecutor = repo.commandexecutor
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   106
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   107
    class lfileswirerepository(repo.__class__):
46200
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   108
        def commandexecutor(self):
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   109
            executor = orig_commandexecutor()
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   110
            if self.capable(b'largefiles'):
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   111
                orig_callcommand = executor.callcommand
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   112
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   113
                class lfscommandexecutor(executor.__class__):
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   114
                    def callcommand(self, command, args):
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   115
                        if command == b'heads':
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   116
                            command = b'lheads'
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   117
                        return orig_callcommand(command, args)
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   118
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   119
                executor.__class__ = lfscommandexecutor
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   120
            return executor
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   121
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   122
        @wireprotov1peer.batchable
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   123
        def lheads(self):
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   124
            return self.heads.batchable(self)
bd31462a86a2 largefiles: redo heads interception
Joerg Sonnenberger <joerg@bec.de>
parents: 45942
diff changeset
   125
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   126
        def putlfile(self, sha, fd):
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   127
            # unfortunately, httprepository._callpush tries to convert its
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   128
            # input file-like into a bundle before sending it, so we can't use
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   129
            # it ...
17192
1ac628cd7113 peer: introduce real peer classes
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 17127
diff changeset
   130
            if issubclass(self.__class__, httppeer.httppeer):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   131
                res = self._call(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   132
                    b'putlfile',
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   133
                    data=fd,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   134
                    sha=sha,
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43085
diff changeset
   135
                    headers={'content-type': 'application/mercurial-0.1'},
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   136
                )
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   137
                try:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   138
                    d, output = res.split(b'\n', 1)
15778
f15c646bffc7 largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents: 15391
diff changeset
   139
                    for l in output.splitlines(True):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   140
                        self.ui.warn(_(b'remote: '), l)  # assume l ends with \n
15778
f15c646bffc7 largefiles: display remote errors from putlfile (issue3123) (issue3149)
Kevin Gessner <kevin@fogcreek.com>
parents: 15391
diff changeset
   141
                    return int(d)
26825
78539633acf3 largefiles: don't mute and obfuscate http errors when putlfile fails
Mads Kiilerich <madski@unity3d.com>
parents: 26587
diff changeset
   142
                except ValueError:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   143
                    self.ui.warn(_(b'unexpected putlfile response: %r\n') % res)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   144
                    return 1
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   145
            # ... but we can't use sshrepository._call because the data=
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   146
            # argument won't get sent, and _callpush does exactly what we want
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   147
            # in this case: send the data straight through
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   148
            else:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   149
                try:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   150
                    ret, output = self._callpush(b"putlfile", fd, sha=sha)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   151
                    if ret == b"":
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   152
                        raise error.ResponseError(
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   153
                            _(b'putlfile failed:'), output
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   154
                        )
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   155
                    return int(ret)
52640
24ee91ba9aa8 pyupgrade: drop usage of py3 aliases for `OSError`
Matt Harbison <matt_harbison@yahoo.com>
parents: 52563
diff changeset
   156
                except OSError:
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   157
                    return 1
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   158
                except ValueError:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   159
                    raise error.ResponseError(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   160
                        _(b'putlfile failed (unexpected response):'), ret
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   161
                    )
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   162
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   163
        def getlfile(self, sha):
19004
6614e5e24e66 largefiles: move protocol conversion into getlfile and make it an iterable
Mads Kiilerich <madski@unity3d.com>
parents: 18922
diff changeset
   164
            """returns an iterable with the chunks of the file with sha sha"""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   165
            stream = self._callstream(b"getlfile", sha=sha)
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   166
            length = stream.readline()
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   167
            try:
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   168
                length = int(length)
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   169
            except ValueError:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   170
                self._abort(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   171
                    error.ResponseError(_(b"unexpected response:"), length)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   172
                )
19004
6614e5e24e66 largefiles: move protocol conversion into getlfile and make it an iterable
Mads Kiilerich <madski@unity3d.com>
parents: 18922
diff changeset
   173
19005
1b84047e7d16 largefiles: drop limitreader, use filechunkiter limit
Mads Kiilerich <madski@unity3d.com>
parents: 19004
diff changeset
   174
            # SSH streams will block if reading more than length
30181
7356e6b1f5b8 util: increase filechunkiter size to 128k
Mads Kiilerich <madski@unity3d.com>
parents: 29312
diff changeset
   175
            for chunk in util.filechunkiter(stream, limit=length):
19004
6614e5e24e66 largefiles: move protocol conversion into getlfile and make it an iterable
Mads Kiilerich <madski@unity3d.com>
parents: 18922
diff changeset
   176
                yield chunk
19006
0b3b84222a2d largefiles: getlfile must hit end of HTTP chunked streams to reuse connections
Mads Kiilerich <madski@unity3d.com>
parents: 19005
diff changeset
   177
            # HTTP streams must hit the end to process the last empty
0b3b84222a2d largefiles: getlfile must hit end of HTTP chunked streams to reuse connections
Mads Kiilerich <madski@unity3d.com>
parents: 19005
diff changeset
   178
            # chunk of Chunked-Encoding so the connection can be reused.
0b3b84222a2d largefiles: getlfile must hit end of HTTP chunked streams to reuse connections
Mads Kiilerich <madski@unity3d.com>
parents: 19005
diff changeset
   179
            if issubclass(self.__class__, httppeer.httppeer):
0b3b84222a2d largefiles: getlfile must hit end of HTTP chunked streams to reuse connections
Mads Kiilerich <madski@unity3d.com>
parents: 19005
diff changeset
   180
                chunk = stream.read(1)
0b3b84222a2d largefiles: getlfile must hit end of HTTP chunked streams to reuse connections
Mads Kiilerich <madski@unity3d.com>
parents: 19005
diff changeset
   181
                if chunk:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   182
                    self._abort(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   183
                        error.ResponseError(_(b"unexpected response:"), chunk)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   184
                    )
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   185
37614
a81d02ea65db wireproto: move version 1 peer functionality to standalone module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37484
diff changeset
   186
        @wireprotov1peer.batchable
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   187
        def statlfile(self, sha):
47873
c424ff4807e6 wireprotov1peer: update all rpcs to use the new batchable scheme
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 46232
diff changeset
   188
            def decode(d):
c424ff4807e6 wireprotov1peer: update all rpcs to use the new batchable scheme
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 46232
diff changeset
   189
                try:
c424ff4807e6 wireprotov1peer: update all rpcs to use the new batchable scheme
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 46232
diff changeset
   190
                    return int(d)
c424ff4807e6 wireprotov1peer: update all rpcs to use the new batchable scheme
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 46232
diff changeset
   191
                except (ValueError, urlerr.httperror):
c424ff4807e6 wireprotov1peer: update all rpcs to use the new batchable scheme
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 46232
diff changeset
   192
                    # If the server returns anything but an integer followed by a
c424ff4807e6 wireprotov1peer: update all rpcs to use the new batchable scheme
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 46232
diff changeset
   193
                    # newline, newline, it's not speaking our language; if we get
c424ff4807e6 wireprotov1peer: update all rpcs to use the new batchable scheme
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 46232
diff changeset
   194
                    # an HTTP error, we can't be sure the largefile is present;
c424ff4807e6 wireprotov1peer: update all rpcs to use the new batchable scheme
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 46232
diff changeset
   195
                    # either way, consider it missing.
c424ff4807e6 wireprotov1peer: update all rpcs to use the new batchable scheme
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 46232
diff changeset
   196
                    return 2
c424ff4807e6 wireprotov1peer: update all rpcs to use the new batchable scheme
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 46232
diff changeset
   197
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   198
            result = {b'sha': sha}
47873
c424ff4807e6 wireprotov1peer: update all rpcs to use the new batchable scheme
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 46232
diff changeset
   199
            return result, decode
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   200
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   201
    repo.__class__ = lfileswirerepository
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   202
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   203
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   204
# advertise the largefiles=serve capability
50782
bf92386f76fd wrapfunction: use sysstr instead of bytes as argument in "largefiles"
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48875
diff changeset
   205
@eh.wrapfunction(wireprotov1server, '_capabilities')
35507
95a9be56c3bb largefiles: modernize how capabilities are added to the wire protocol
Matt Harbison <matt_harbison@yahoo.com>
parents: 35348
diff changeset
   206
def _capabilities(orig, repo, proto):
95a9be56c3bb largefiles: modernize how capabilities are added to the wire protocol
Matt Harbison <matt_harbison@yahoo.com>
parents: 35348
diff changeset
   207
    '''announce largefile server capability'''
95a9be56c3bb largefiles: modernize how capabilities are added to the wire protocol
Matt Harbison <matt_harbison@yahoo.com>
parents: 35348
diff changeset
   208
    caps = orig(repo, proto)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   209
    caps.append(b'largefiles=serve')
35507
95a9be56c3bb largefiles: modernize how capabilities are added to the wire protocol
Matt Harbison <matt_harbison@yahoo.com>
parents: 35348
diff changeset
   210
    return caps
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   211
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41062
diff changeset
   212
37484
c22fd3c4c23e largefiles: wrap heads command handler more directly
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37414
diff changeset
   213
def heads(orig, repo, proto):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   214
    """Wrap server command - largefile capable clients will know to call
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   215
    lheads instead"""
15168
cfccd3bee7b3 hgext: add largefiles extension
various
parents:
diff changeset
   216
    if lfutil.islfilesrepo(repo):
37293
d5d665f6615a wireproto: stop aliasing wire protocol types (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36656
diff changeset
   217
        return wireprototypes.ooberror(LARGEFILES_REQUIRED_MSG)
37484
c22fd3c4c23e largefiles: wrap heads command handler more directly
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37414
diff changeset
   218
c22fd3c4c23e largefiles: wrap heads command handler more directly
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37414
diff changeset
   219
    return orig(repo, proto)