remote-hg: get the correct refs
[git/dscho.git] / git-remote-hg
blob531a5394ea92379011573d74de1dffcf19e030ba
1 #!/usr/bin/env python
3 import sys
6 def get_repo(alias, url):
7 try:
8 from mercurial import hg, ui
9 except ImportError:
10 sys.stderr.write("Mercurial python libraries not installed!\n")
11 sys.exit(1)
13 ui = ui.ui()
14 source, revs, checkout = hg.parseurl(ui.expandpath(url), [])
15 repo = hg.repository(ui, source)
16 repo.alias = alias
17 if not revs:
18 revs = ['tip']
19 repo.revs = revs
20 return repo
23 def do_capabilities(repo, args):
24 """
25 """
27 sys.stderr.write("Capabilities are AWESUM.\n")
29 print # end capabilities
32 def do_list(repo, args):
33 """
34 """
36 from mercurial.node import hex
37 for ref in repo.revs:
38 sys.stderr.write("? %s (%s)\n" % (ref, hex(repo.lookup(ref))))
39 print '? %s' % (ref)
41 print # end list
44 COMMANDS = {
45 'capabilities': do_capabilities,
46 'list': do_list,
50 def main(args):
51 if len(args) < 3:
52 sys.stderr.write("Missing url and stuff.\n")
53 sys.exit(1)
55 alias = args[1]
56 url = args[2]
57 if url.startswith('hg+'):
58 url = url[3:]
59 elif url.startswith('hg::'):
60 url = url[4:]
62 repo = get_repo(alias, url)
64 sys.stderr.write("How can I help u todayz on %s?\n" % args[1:])
65 line = sys.stdin.readline()
67 while (line):
68 cmdline = line.strip().split()
70 if not cmdline:
71 break # Blank line means we're about to quit
73 cmd = cmdline.pop(0)
74 sys.stderr.write("Got command '%s' with args '%s'\n" % (cmd, ' '.join(cmdline)))
76 if cmd not in COMMANDS:
77 sys.stderr.write("Dunno how to handle that, SUCKAH\n")
78 sys.exit(1)
80 func = COMMANDS[cmd]
81 func(repo, cmdline)
82 sys.stdout.flush()
84 line = sys.stdin.readline()
87 if __name__ == '__main__':
88 sys.exit(main(sys.argv))