Show More
@@ -0,0 +1,101 | |||||
|
1 | # commands.py - command processing for mercurial | |||
|
2 | # | |||
|
3 | # Copyright 2005 Matt Mackall <mpm@selenic.com> | |||
|
4 | # | |||
|
5 | # This software may be used and distributed according to the terms | |||
|
6 | # of the GNU General Public License, incorporated herein by reference. | |||
|
7 | ||||
|
8 | from demandload import demandload | |||
|
9 | from i18n import gettext as _ | |||
|
10 | from node import * | |||
|
11 | demandload(globals(), "sys util") | |||
|
12 | ||||
|
13 | class sshserver(object): | |||
|
14 | def __init__(self, ui, repo): | |||
|
15 | self.ui = ui | |||
|
16 | self.repo = repo | |||
|
17 | self.lock = None | |||
|
18 | self.fin = sys.stdin | |||
|
19 | self.fout = sys.stdout | |||
|
20 | ||||
|
21 | sys.stdout = sys.stderr | |||
|
22 | ||||
|
23 | # Prevent insertion/deletion of CRs | |||
|
24 | util.set_binary(self.fin) | |||
|
25 | util.set_binary(self.fout) | |||
|
26 | ||||
|
27 | def getarg(self): | |||
|
28 | argline = self.fin.readline()[:-1] | |||
|
29 | arg, l = argline.split() | |||
|
30 | val = self.fin.read(int(l)) | |||
|
31 | return arg, val | |||
|
32 | ||||
|
33 | def respond(self, v): | |||
|
34 | self.fout.write("%d\n" % len(v)) | |||
|
35 | self.fout.write(v) | |||
|
36 | self.fout.flush() | |||
|
37 | ||||
|
38 | def serve_forever(self): | |||
|
39 | while self.serve_one(): pass | |||
|
40 | sys.exit(0) | |||
|
41 | ||||
|
42 | def serve_one(self): | |||
|
43 | cmd = self.fin.readline()[:-1] | |||
|
44 | if cmd: | |||
|
45 | impl = getattr(self, 'do_' + cmd, None) | |||
|
46 | if impl: impl() | |||
|
47 | else: self.respond("") | |||
|
48 | return cmd != '' | |||
|
49 | ||||
|
50 | def do_heads(self): | |||
|
51 | h = self.repo.heads() | |||
|
52 | self.respond(" ".join(map(hex, h)) + "\n") | |||
|
53 | ||||
|
54 | def do_lock(self): | |||
|
55 | self.lock = self.repo.lock() | |||
|
56 | self.respond("") | |||
|
57 | ||||
|
58 | def do_unlock(self): | |||
|
59 | if self.lock: | |||
|
60 | self.lock.release() | |||
|
61 | self.lock = None | |||
|
62 | self.respond("") | |||
|
63 | ||||
|
64 | def do_branches(self): | |||
|
65 | arg, nodes = self.getarg() | |||
|
66 | nodes = map(bin, nodes.split(" ")) | |||
|
67 | r = [] | |||
|
68 | for b in self.repo.branches(nodes): | |||
|
69 | r.append(" ".join(map(hex, b)) + "\n") | |||
|
70 | self.respond("".join(r)) | |||
|
71 | ||||
|
72 | def do_between(self): | |||
|
73 | arg, pairs = self.getarg() | |||
|
74 | pairs = [map(bin, p.split("-")) for p in pairs.split(" ")] | |||
|
75 | r = [] | |||
|
76 | for b in self.repo.between(pairs): | |||
|
77 | r.append(" ".join(map(hex, b)) + "\n") | |||
|
78 | self.respond("".join(r)) | |||
|
79 | ||||
|
80 | def do_changegroup(self): | |||
|
81 | nodes = [] | |||
|
82 | arg, roots = self.getarg() | |||
|
83 | nodes = map(bin, roots.split(" ")) | |||
|
84 | ||||
|
85 | cg = self.repo.changegroup(nodes, 'serve') | |||
|
86 | while True: | |||
|
87 | d = cg.read(4096) | |||
|
88 | if not d: | |||
|
89 | break | |||
|
90 | self.fout.write(d) | |||
|
91 | ||||
|
92 | self.fout.flush() | |||
|
93 | ||||
|
94 | def do_addchangegroup(self): | |||
|
95 | if not self.lock: | |||
|
96 | self.respond("not locked") | |||
|
97 | return | |||
|
98 | ||||
|
99 | self.respond("") | |||
|
100 | r = self.repo.addchangegroup(self.fin, 'serve') | |||
|
101 | self.respond(str(r)) |
@@ -13,7 +13,7 demandload(globals(), "fancyopts ui hg u | |||||
13 | demandload(globals(), "fnmatch mdiff random signal tempfile time") |
|
13 | demandload(globals(), "fnmatch mdiff random signal tempfile time") | |
14 | demandload(globals(), "traceback errno socket version struct atexit sets bz2") |
|
14 | demandload(globals(), "traceback errno socket version struct atexit sets bz2") | |
15 | demandload(globals(), "archival changegroup") |
|
15 | demandload(globals(), "archival changegroup") | |
16 | demandload(globals(), "hgweb.server") |
|
16 | demandload(globals(), "hgweb.server sshserver") | |
17 |
|
17 | |||
18 | class UnknownCommand(Exception): |
|
18 | class UnknownCommand(Exception): | |
19 | """Exception raised if command is not in the command table.""" |
|
19 | """Exception raised if command is not in the command table.""" | |
@@ -2452,76 +2452,8 def serve(ui, repo, **opts): | |||||
2452 | if opts["stdio"]: |
|
2452 | if opts["stdio"]: | |
2453 | if repo is None: |
|
2453 | if repo is None: | |
2454 | raise hg.RepoError(_('no repo found')) |
|
2454 | raise hg.RepoError(_('no repo found')) | |
2455 | fin, fout = sys.stdin, sys.stdout |
|
2455 | s = sshserver.sshserver(ui, repo) | |
2456 | sys.stdout = sys.stderr |
|
2456 | s.serve_forever() | |
2457 |
|
||||
2458 | # Prevent insertion/deletion of CRs |
|
|||
2459 | util.set_binary(fin) |
|
|||
2460 | util.set_binary(fout) |
|
|||
2461 |
|
||||
2462 | def getarg(): |
|
|||
2463 | argline = fin.readline()[:-1] |
|
|||
2464 | arg, l = argline.split() |
|
|||
2465 | val = fin.read(int(l)) |
|
|||
2466 | return arg, val |
|
|||
2467 | def respond(v): |
|
|||
2468 | fout.write("%d\n" % len(v)) |
|
|||
2469 | fout.write(v) |
|
|||
2470 | fout.flush() |
|
|||
2471 |
|
||||
2472 | lock = None |
|
|||
2473 |
|
||||
2474 | while 1: |
|
|||
2475 | cmd = fin.readline()[:-1] |
|
|||
2476 | if cmd == '': |
|
|||
2477 | return |
|
|||
2478 | if cmd == "heads": |
|
|||
2479 | h = repo.heads() |
|
|||
2480 | respond(" ".join(map(hex, h)) + "\n") |
|
|||
2481 | if cmd == "lock": |
|
|||
2482 | lock = repo.lock() |
|
|||
2483 | respond("") |
|
|||
2484 | if cmd == "unlock": |
|
|||
2485 | if lock: |
|
|||
2486 | lock.release() |
|
|||
2487 | lock = None |
|
|||
2488 | respond("") |
|
|||
2489 | elif cmd == "branches": |
|
|||
2490 | arg, nodes = getarg() |
|
|||
2491 | nodes = map(bin, nodes.split(" ")) |
|
|||
2492 | r = [] |
|
|||
2493 | for b in repo.branches(nodes): |
|
|||
2494 | r.append(" ".join(map(hex, b)) + "\n") |
|
|||
2495 | respond("".join(r)) |
|
|||
2496 | elif cmd == "between": |
|
|||
2497 | arg, pairs = getarg() |
|
|||
2498 | pairs = [map(bin, p.split("-")) for p in pairs.split(" ")] |
|
|||
2499 | r = [] |
|
|||
2500 | for b in repo.between(pairs): |
|
|||
2501 | r.append(" ".join(map(hex, b)) + "\n") |
|
|||
2502 | respond("".join(r)) |
|
|||
2503 | elif cmd == "changegroup": |
|
|||
2504 | nodes = [] |
|
|||
2505 | arg, roots = getarg() |
|
|||
2506 | nodes = map(bin, roots.split(" ")) |
|
|||
2507 |
|
||||
2508 | cg = repo.changegroup(nodes, 'serve') |
|
|||
2509 | while 1: |
|
|||
2510 | d = cg.read(4096) |
|
|||
2511 | if not d: |
|
|||
2512 | break |
|
|||
2513 | fout.write(d) |
|
|||
2514 |
|
||||
2515 | fout.flush() |
|
|||
2516 |
|
||||
2517 | elif cmd == "addchangegroup": |
|
|||
2518 | if not lock: |
|
|||
2519 | respond("not locked") |
|
|||
2520 | continue |
|
|||
2521 | respond("") |
|
|||
2522 |
|
||||
2523 | r = repo.addchangegroup(fin, 'serve') |
|
|||
2524 | respond(str(r)) |
|
|||
2525 |
|
2457 | |||
2526 | optlist = ("name templates style address port ipv6" |
|
2458 | optlist = ("name templates style address port ipv6" | |
2527 | " accesslog errorlog webdir_conf") |
|
2459 | " accesslog errorlog webdir_conf") |
General Comments 0
You need to be logged in to leave comments.
Login now