mercurial/sshserver.py
author Matt Mackall <mpm@selenic.com>
Wed, 14 Jul 2010 15:25:15 -0500
changeset 11581 4530b3307fb9
parent 11580 69248b5add46
child 11584 1af96b090116
permissions -rw-r--r--
protocol: introduce wireproto.py - add a protocol-independent dispatcher - move most of the basic commands from sshserver to wireproto - dispatch through wireproto first
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
#
4635
63b9d2deed48 Updated copyright notices and add "and others" to "hg version"
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4258
diff changeset
     3
# Copyright 2005-2007 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
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8109
diff changeset
     6
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9742
diff changeset
     7
# GNU General Public License version 2 or any later version.
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     8
3891
6b4127c7d52a Simplify i18n imports
Matt Mackall <mpm@selenic.com>
parents: 3877
diff changeset
     9
from i18n import _
6211
f89fd07fc51d Expand import * to allow Pyflakes to find problems
Joel Rosdahl <joel@rosdahl.net>
parents: 5833
diff changeset
    10
from node import bin, hex
11581
4530b3307fb9 protocol: introduce wireproto.py
Matt Mackall <mpm@selenic.com>
parents: 11580
diff changeset
    11
import streamclone, util, hook, pushkey, wireproto
9713
d193cc97c4e8 hgweb/sshserver: extract capabilities for easier modification
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9198
diff changeset
    12
import os, sys, tempfile, urllib, copy
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):
9713
d193cc97c4e8 hgweb/sshserver: extract capabilities for easier modification
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9198
diff changeset
    15
11369
02a4373ca5cd pushkey: add ssh support
Matt Mackall <mpm@selenic.com>
parents: 10377
diff changeset
    16
    caps = 'unbundle lookup changegroupsubset branchmap pushkey'.split()
9713
d193cc97c4e8 hgweb/sshserver: extract capabilities for easier modification
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9198
diff changeset
    17
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    18
    def __init__(self, ui, repo):
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    19
        self.ui = ui
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    20
        self.repo = repo
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    21
        self.lock = None
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    22
        self.fin = sys.stdin
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    23
        self.fout = sys.stdout
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    24
5833
323b9c55b328 hook: redirect stdout to stderr for ssh and http servers
Matt Mackall <mpm@selenic.com>
parents: 4635
diff changeset
    25
        hook.redirect(True)
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    26
        sys.stdout = sys.stderr
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
        # Prevent insertion/deletion of CRs
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    29
        util.set_binary(self.fin)
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    30
        util.set_binary(self.fout)
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    31
11579
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    32
    def getargs(self, args):
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    33
        data = {}
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    34
        keys = args.split()
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    35
        count = len(keys)
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    36
        for n in xrange(len(keys)):
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    37
            argline = self.fin.readline()[:-1]
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    38
            arg, l = argline.split()
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    39
            val = self.fin.read(int(l))
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    40
            if arg not in keys:
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    41
                raise util.Abort("unexpected parameter %r" % arg)
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    42
            if arg == '*':
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    43
                star = {}
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    44
                for n in xrange(int(l)):
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    45
                    arg, l = argline.split()
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    46
                    val = self.fin.read(int(l))
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    47
                    star[arg] = val
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    48
                data['*'] = star
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    49
            else:
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    50
                data[arg] = val
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    51
        return [data[k] for k in keys]
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    52
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    53
    def getarg(self, name):
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
    54
        return self.getargs(name)[0]
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    55
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    56
    def respond(self, v):
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    57
        self.fout.write("%d\n" % len(v))
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    58
        self.fout.write(v)
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    59
        self.fout.flush()
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    60
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    61
    def serve_forever(self):
8109
496ae1ea4698 switch lock releasing in the core from gc to explicit
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 7875
diff changeset
    62
        try:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    63
            while self.serve_one():
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    64
                pass
8109
496ae1ea4698 switch lock releasing in the core from gc to explicit
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 7875
diff changeset
    65
        finally:
496ae1ea4698 switch lock releasing in the core from gc to explicit
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 7875
diff changeset
    66
            if self.lock is not None:
496ae1ea4698 switch lock releasing in the core from gc to explicit
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 7875
diff changeset
    67
                self.lock.release()
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    68
        sys.exit(0)
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    69
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    70
    def serve_one(self):
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    71
        cmd = self.fin.readline()[:-1]
11581
4530b3307fb9 protocol: introduce wireproto.py
Matt Mackall <mpm@selenic.com>
parents: 11580
diff changeset
    72
        if cmd and not wireproto.dispatch(self.repo, self, cmd):
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    73
            impl = getattr(self, 'do_' + cmd, None)
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    74
            if impl:
11580
69248b5add46 protocol: move most ssh responses to returns
Matt Mackall <mpm@selenic.com>
parents: 11579
diff changeset
    75
                r = impl()
69248b5add46 protocol: move most ssh responses to returns
Matt Mackall <mpm@selenic.com>
parents: 11579
diff changeset
    76
                if r is not None:
69248b5add46 protocol: move most ssh responses to returns
Matt Mackall <mpm@selenic.com>
parents: 11579
diff changeset
    77
                    self.respond(r)
2397
e9d402506514 merge change to ssh protocol.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2396
diff changeset
    78
            else: self.respond("")
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    79
        return cmd != ''
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    80
2419
b17eebc911ae Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents: 2399
diff changeset
    81
    def do_hello(self):
b17eebc911ae Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents: 2399
diff changeset
    82
        '''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
    83
        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
    84
        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
    85
        consists of a line in the form:
b17eebc911ae Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents: 2399
diff changeset
    86
b17eebc911ae Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents: 2399
diff changeset
    87
        capabilities: space separated list of tokens
b17eebc911ae Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents: 2399
diff changeset
    88
        '''
9713
d193cc97c4e8 hgweb/sshserver: extract capabilities for easier modification
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9198
diff changeset
    89
        caps = copy.copy(self.caps)
10377
04e1e6743809 streamclone: allow uncompressed clones by default
Matt Mackall <mpm@selenic.com>
parents: 10282
diff changeset
    90
        if streamclone.allowed(self.repo.ui):
4258
b11a2fb59cf5 revlog: simplify revlog version handling
Matt Mackall <mpm@selenic.com>
parents: 3891
diff changeset
    91
            caps.append('stream=%d' % self.repo.changelog.version)
11580
69248b5add46 protocol: move most ssh responses to returns
Matt Mackall <mpm@selenic.com>
parents: 11579
diff changeset
    92
        return "capabilities: %s\n" % (' '.join(caps),)
2419
b17eebc911ae Initial implementation of hello command for ssh
Matt Mackall <mpm@selenic.com>
parents: 2399
diff changeset
    93
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    94
    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
    95
        '''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
    96
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    97
        self.lock = self.repo.lock()
11580
69248b5add46 protocol: move most ssh responses to returns
Matt Mackall <mpm@selenic.com>
parents: 11579
diff changeset
    98
        return ""
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
    99
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   100
    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
   101
        '''DEPRECATED'''
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   102
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   103
        if self.lock:
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   104
            self.lock.release()
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   105
        self.lock = None
11580
69248b5add46 protocol: move most ssh responses to returns
Matt Mackall <mpm@selenic.com>
parents: 11579
diff changeset
   106
        return ""
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   107
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   108
    def do_changegroup(self):
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   109
        nodes = []
11579
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
   110
        roots = self.getarg('roots')
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   111
        nodes = map(bin, roots.split(" "))
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
        cg = self.repo.changegroup(nodes, 'serve')
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   114
        while True:
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   115
            d = cg.read(4096)
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   116
            if not d:
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   117
                break
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   118
            self.fout.write(d)
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.fout.flush()
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   121
3446
0b450267cf47 Adding changegroupsubset and lookup to ssh protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3223
diff changeset
   122
    def do_changegroupsubset(self):
11579
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
   123
        bases, heads = self.getargs('bases heads')
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
   124
        bases = [bin(n) for n in bases.split(' ')]
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
   125
        heads = [bin(n) for n in heads.split(' ')]
3446
0b450267cf47 Adding changegroupsubset and lookup to ssh protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3223
diff changeset
   126
0b450267cf47 Adding changegroupsubset and lookup to ssh protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3223
diff changeset
   127
        cg = self.repo.changegroupsubset(bases, heads, 'serve')
0b450267cf47 Adding changegroupsubset and lookup to ssh protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3223
diff changeset
   128
        while True:
0b450267cf47 Adding changegroupsubset and lookup to ssh protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3223
diff changeset
   129
            d = cg.read(4096)
0b450267cf47 Adding changegroupsubset and lookup to ssh protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3223
diff changeset
   130
            if not d:
0b450267cf47 Adding changegroupsubset and lookup to ssh protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3223
diff changeset
   131
                break
0b450267cf47 Adding changegroupsubset and lookup to ssh protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3223
diff changeset
   132
            self.fout.write(d)
0b450267cf47 Adding changegroupsubset and lookup to ssh protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3223
diff changeset
   133
0b450267cf47 Adding changegroupsubset and lookup to ssh protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3223
diff changeset
   134
        self.fout.flush()
0b450267cf47 Adding changegroupsubset and lookup to ssh protocol so pull -r and
Eric Hopper <hopper@omnifarious.org>
parents: 3223
diff changeset
   135
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   136
    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
   137
        '''DEPRECATED'''
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   138
2396
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   139
        if not self.lock:
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   140
            self.respond("not locked")
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   141
            return
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   142
8d44649df03b refactor ssh server.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
   143
        self.respond("")
11442
ee1ed6afac21 addchangegroup: pass in lock to release it before changegroup hook is called
Matt Mackall <mpm@selenic.com>
parents: 11369
diff changeset
   144
        r = self.repo.addchangegroup(self.fin, 'serve', self.client_url(),
ee1ed6afac21 addchangegroup: pass in lock to release it before changegroup hook is called
Matt Mackall <mpm@selenic.com>
parents: 11369
diff changeset
   145
                                     lock=self.lock)
11580
69248b5add46 protocol: move most ssh responses to returns
Matt Mackall <mpm@selenic.com>
parents: 11579
diff changeset
   146
        return str(r)
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   147
2673
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2622
diff changeset
   148
    def client_url(self):
109a22f5434a hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2622
diff changeset
   149
        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
   150
        return 'remote:ssh:' + client
3223
53e843840349 Whitespace/Tab cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2859
diff changeset
   151
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   152
    def do_unbundle(self):
11579
c19d7e91cc46 protocol: add ssh getargs
Matt Mackall <mpm@selenic.com>
parents: 11442
diff changeset
   153
        their_heads = self.getarg('heads').split()
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   154
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   155
        def check_heads():
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   156
            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
   157
            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
   158
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   159
        # fail early if possible
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   160
        if not check_heads():
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   161
            self.respond(_('unsynced changes'))
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   162
            return
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
        self.respond('')
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
        # write bundle data to temporary file because it can be big
9742
0c84afa1d622 sshrepo: move mkstemp() out of the try block, we don't use the exception
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9713
diff changeset
   167
        fd, tempname = tempfile.mkstemp(prefix='hg-unbundle-')
0c84afa1d622 sshrepo: move mkstemp() out of the try block, we don't use the exception
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9713
diff changeset
   168
        fp = os.fdopen(fd, 'wb+')
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   169
        try:
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   170
            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
   171
            while count:
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   172
                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
   173
                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
   174
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   175
            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
   176
            if not was_locked:
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   177
                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
   178
            try:
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   179
                if not check_heads():
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   180
                    # 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
   181
                    # were transferring data
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   182
                    self.respond(_('unsynced changes'))
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   183
                    return
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   184
                self.respond('')
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   185
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   186
                # push can proceed
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   187
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   188
                fp.seek(0)
11442
ee1ed6afac21 addchangegroup: pass in lock to release it before changegroup hook is called
Matt Mackall <mpm@selenic.com>
parents: 11369
diff changeset
   189
                r = self.repo.addchangegroup(fp, 'serve', self.client_url(),
ee1ed6afac21 addchangegroup: pass in lock to release it before changegroup hook is called
Matt Mackall <mpm@selenic.com>
parents: 11369
diff changeset
   190
                                             lock=self.lock)
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   191
                self.respond(str(r))
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   192
            finally:
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   193
                if not was_locked:
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   194
                    self.lock.release()
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   195
                    self.lock = None
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   196
        finally:
9742
0c84afa1d622 sshrepo: move mkstemp() out of the try block, we don't use the exception
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9713
diff changeset
   197
            fp.close()
0c84afa1d622 sshrepo: move mkstemp() out of the try block, we don't use the exception
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 9713
diff changeset
   198
            os.unlink(tempname)
2439
e8c4f3d3df8c extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2419
diff changeset
   199
2612
ffb895f16925 add support for streaming clone.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2439
diff changeset
   200
    def do_stream_out(self):
6925
87abfefafe02 make streamclone.stream_out() a generator
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6794
diff changeset
   201
        try:
87abfefafe02 make streamclone.stream_out() a generator
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6794
diff changeset
   202
            for chunk in streamclone.stream_out(self.repo):
87abfefafe02 make streamclone.stream_out() a generator
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6794
diff changeset
   203
                self.fout.write(chunk)
87abfefafe02 make streamclone.stream_out() a generator
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6794
diff changeset
   204
            self.fout.flush()
87abfefafe02 make streamclone.stream_out() a generator
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6794
diff changeset
   205
        except streamclone.StreamException, inst:
87abfefafe02 make streamclone.stream_out() a generator
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6794
diff changeset
   206
            self.fout.write(str(inst))
87abfefafe02 make streamclone.stream_out() a generator
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6794
diff changeset
   207
            self.fout.flush()