[PATCH 11 of 12 RFC v2] hgcmdshell: a simple shell that connects to a repo and listens for hg commands

Idan Kamara idankk86 at gmail.com
Wed Jun 15 16:09:23 CDT 2011


# HG changeset patch
# User Idan Kamara <idankk86 at gmail.com>
# Date 1307111694 -10800
# Node ID f7caf168e0aa9374d50bcfc4d406eb8793ab801e
# Parent  06f8e17e7af4b21df2ee211cf8b65ef348f0e323
hgcmdshell: a simple shell that connects to a repo and listens for hg commands

diff -r 06f8e17e7af4 -r f7caf168e0aa contrib/hgcmdshell.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contrib/hgcmdshell.py	Fri Jun 03 17:34:54 2011 +0300
@@ -0,0 +1,75 @@
+import sys, os
+import shlex
+import optparse
+
+from hglib import hglib
+
+def getcmd(prefix=None):
+    if prefix:
+        prompt = '%s >> ' % str(prefix)
+    else:
+        prompt = '>> '
+
+    sys.stdout.write(prompt)
+
+    try:
+        inp = raw_input()
+        return shlex.split(inp)
+    except EOFError:
+        return None
+
+def findrepo(p):
+    while not os.path.isdir(os.path.join(p, ".hg")):
+        oldp, p = p, os.path.dirname(p)
+        if p == oldp:
+            return None
+
+    return p
+
+def main(options, args):
+    path = findrepo(args and args[0] or os.getcwd())
+
+    cfgs = None
+    if options.debug:
+        cfgs = ['cmdserver.log=-']
+
+    client = hglib.connect(path, sys.stdin, sys.stdout, sys.stderr, cfgs)
+
+    if options.cmd:
+        for i in range(100):
+            client.runcommand(shlex.split(options.cmd))
+    else:
+        cmd = getcmd()
+
+        while cmd and cmd[0] and cmd[0] != 'q':
+            if cmd[0] == 'status()':
+                print client.status()
+                ret = 0
+            if cmd[0] == 'getencoding()':
+                print client.encoding
+                ret = 0
+            else:
+                ret = client.runcommand(cmd)
+            cmd = getcmd(ret)
+
+    return client.close()
+
+def parseargs():
+    parser = optparse.OptionParser("%prog [options] "
+                                   "[repo to connect to, defaults to cwd]")
+
+    parser.add_option("-c", "--cmd", type="string",
+                      help="run command 100 times and exit")
+    parser.add_option("-d", "--debug", action="store_true",
+                      help="start the server with debug log")
+    parser.add_option("--with-hg", type="string", metavar="HG",
+                      help="use the specified HG")
+    (options, args) = parser.parse_args()
+
+    if options.with_hg:
+        hglib.HGPATH = options.with_hg
+
+    return (options, args)
+
+if __name__ == '__main__':
+    sys.exit(main(*parseargs()))


More information about the Mercurial-devel mailing list