mercurial/sshserver.py
author Matt Mackall <mpm@selenic.com>
Wed, 20 Sep 2006 16:50:50 -0500
changeset 3135 b1db258e875c
parent 2859 345bac2bc4ec
child 3223 53e843840349
permissions -rw-r--r--
Abstract ancestor algorithm into generic function Make depth calculation non-recursive Add simple shortcut for linear ancestry Convert context to use ancestor function make memoized parents function Convert revlog to use ancestor function
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2399
cc90dcbdf053 fix comment.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2397
diff changeset
     1
# sshserver.py - ssh protocol server support for mercurial
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     2
#
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     3
# Copyright 2005 Matt Mackall <mpm@selenic.com>
2859
345bac2bc4ec update copyrights.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2673
diff changeset
     4
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     5
#
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     6
# This software may be used and distributed according to the terms
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     7
# of the GNU General Public License, incorporated herein by reference.
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     8
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     9
from demandload import demandload
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    10
from i18n import gettext as _
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    11
from node import *
2612
ffb895f16925 add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
    12
demandload(globals(), "os streamclone sys tempfile util")
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    13
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    14
class sshserver(object):
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    15
    def __init__(self, ui, repo):
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    16
        self.ui = ui
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    17
        self.repo = repo
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    18
        self.lock = None
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    19
        self.fin = sys.stdin
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    20
        self.fout = sys.stdout
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    21
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    22
        sys.stdout = sys.stderr
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    23
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    24
        # Prevent insertion/deletion of CRs
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    25
        util.set_binary(self.fin)
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    26
        util.set_binary(self.fout)
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    27
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    28
    def getarg(self):
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    29
        argline = self.fin.readline()[:-1]
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    30
        arg, l = argline.split()
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    31
        val = self.fin.read(int(l))
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    32
        return arg, val
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    33
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    34
    def respond(self, v):
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    35
        self.fout.write("%d\n" % len(v))
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    36
        self.fout.write(v)
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    37
        self.fout.flush()
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    38
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    39
    def serve_forever(self):
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    40
        while self.serve_one(): pass
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    41
        sys.exit(0)
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    42
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    43
    def serve_one(self):
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    44
        cmd = self.fin.readline()[:-1]
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    45
        if cmd:
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    46
            impl = getattr(self, 'do_' + cmd, None)
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    47
            if impl: impl()
2397
e9d402506514 merge change to ssh protocol.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2396
diff changeset
    48
            else: self.respond("")
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    49
        return cmd != ''
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    50
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    51
    def do_heads(self):
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    52
        h = self.repo.heads()
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    53
        self.respond(" ".join(map(hex, h)) + "\n")
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    54
2419
b17eebc911ae Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents: 2399
diff changeset
    55
    def do_hello(self):
b17eebc911ae Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents: 2399
diff changeset
    56
        '''the hello command returns a set of lines describing various
b17eebc911ae Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents: 2399
diff changeset
    57
        interesting things about the server, in an RFC822-like format.
b17eebc911ae Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents: 2399
diff changeset
    58
        Currently the only one defined is "capabilities", which
b17eebc911ae Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents: 2399
diff changeset
    59
        consists of a line in the form:
b17eebc911ae Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents: 2399
diff changeset
    60
b17eebc911ae Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents: 2399
diff changeset
    61
        capabilities: space separated list of tokens
b17eebc911ae Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents: 2399
diff changeset
    62
        '''
b17eebc911ae Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents: 2399
diff changeset
    63
2621
5a5852a417b1 clone: disable stream support on server side by default.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
    64
        caps = ['unbundle']
2622
064aef9162cc rename stream hgrc option to compressed.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2621
diff changeset
    65
        if self.ui.configbool('server', 'uncompressed'):
2621
5a5852a417b1 clone: disable stream support on server side by default.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
    66
            caps.append('stream=%d' % self.repo.revlogversion)
5a5852a417b1 clone: disable stream support on server side by default.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2612
diff changeset
    67
        self.respond("capabilities: %s\n" % (' '.join(caps),))
2419
b17eebc911ae Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents: 2399
diff changeset
    68
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    69
    def do_lock(self):
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
    70
        '''DEPRECATED - allowing remote client to lock repo is not safe'''
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
    71
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    72
        self.lock = self.repo.lock()
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    73
        self.respond("")
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    74
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    75
    def do_unlock(self):
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
    76
        '''DEPRECATED'''
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
    77
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    78
        if self.lock:
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    79
            self.lock.release()
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    80
        self.lock = None
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    81
        self.respond("")
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    82
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    83
    def do_branches(self):
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    84
        arg, nodes = self.getarg()
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    85
        nodes = map(bin, nodes.split(" "))
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    86
        r = []
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    87
        for b in self.repo.branches(nodes):
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    88
            r.append(" ".join(map(hex, b)) + "\n")
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    89
        self.respond("".join(r))
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    90
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    91
    def do_between(self):
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    92
        arg, pairs = self.getarg()
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    93
        pairs = [map(bin, p.split("-")) for p in pairs.split(" ")]
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    94
        r = []
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    95
        for b in self.repo.between(pairs):
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    96
            r.append(" ".join(map(hex, b)) + "\n")
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    97
        self.respond("".join(r))
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    98
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    99
    def do_changegroup(self):
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   100
        nodes = []
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   101
        arg, roots = self.getarg()
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   102
        nodes = map(bin, roots.split(" "))
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   103
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   104
        cg = self.repo.changegroup(nodes, 'serve')
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   105
        while True:
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   106
            d = cg.read(4096)
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   107
            if not d:
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   108
                break
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   109
            self.fout.write(d)
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   110
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   111
        self.fout.flush()
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   112
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   113
    def do_addchangegroup(self):
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   114
        '''DEPRECATED'''
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   115
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   116
        if not self.lock:
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   117
            self.respond("not locked")
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   118
            return
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   119
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   120
        self.respond("")
2673
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2622
diff changeset
   121
        r = self.repo.addchangegroup(self.fin, 'serve', self.client_url())
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   122
        self.respond(str(r))
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   123
2673
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2622
diff changeset
   124
    def client_url(self):
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2622
diff changeset
   125
        client = os.environ.get('SSH_CLIENT', '').split(' ', 1)[0]
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2622
diff changeset
   126
        return 'remote:ssh:' + client
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2622
diff changeset
   127
        
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   128
    def do_unbundle(self):
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   129
        their_heads = self.getarg()[1].split()
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   130
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   131
        def check_heads():
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   132
            heads = map(hex, self.repo.heads())
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   133
            return their_heads == [hex('force')] or their_heads == heads
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   134
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   135
        # fail early if possible
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   136
        if not check_heads():
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   137
            self.respond(_('unsynced changes'))
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   138
            return
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   139
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   140
        self.respond('')
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   141
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   142
        # write bundle data to temporary file because it can be big
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   143
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   144
        try:
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   145
            fd, tempname = tempfile.mkstemp(prefix='hg-unbundle-')
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   146
            fp = os.fdopen(fd, 'wb+')
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   147
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   148
            count = int(self.fin.readline())
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   149
            while count:
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   150
                fp.write(self.fin.read(count))
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   151
                count = int(self.fin.readline())
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   152
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   153
            was_locked = self.lock is not None
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   154
            if not was_locked:
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   155
                self.lock = self.repo.lock()
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   156
            try:
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   157
                if not check_heads():
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   158
                    # someone else committed/pushed/unbundled while we
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   159
                    # were transferring data
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   160
                    self.respond(_('unsynced changes'))
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   161
                    return
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   162
                self.respond('')
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   163
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   164
                # push can proceed
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   165
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   166
                fp.seek(0)
2673
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2622
diff changeset
   167
                r = self.repo.addchangegroup(fp, 'serve', self.client_url())
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   168
                self.respond(str(r))
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   169
            finally:
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   170
                if not was_locked:
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   171
                    self.lock.release()
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   172
                    self.lock = None
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   173
        finally:
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   174
            fp.close()
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   175
            os.unlink(tempname)
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   176
2612
ffb895f16925 add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
   177
    def do_stream_out(self):
ffb895f16925 add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
   178
        streamclone.stream_out(self.repo, self.fout)