[PATCH 3 of 5 RFC] hgcmdshell: a simple shell that connects to a repo and listens for hg commands

Idan Kamara idankk86 at gmail.com
Fri Jun 3 15:04:42 CDT 2011


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

diff -r 15c24865581d -r 629b46ad35e6 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,65 @@
+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())
+    client = hglib.connect(path, sys.stdout, sys.stderr)
+
+    if options.cmd:
+        for i in range(100):
+            client.rawcommand(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
+            else:
+                ret = client.rawcommand(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("--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