[patch 3/4] Turn hgit into an extension

Chris Mason mason at suse.com
Wed Sep 7 10:03:39 CDT 2005


# HG changeset patch
# User mason at suse.com
Turn hgit into an extension

To make the command names more user friendly, each hgit command starts
with git-.  Currently supported are:

hg git-rev-list
hg git-cat-file
hg git-diff-tree

There's still some work to do here for updating these to the latest git
syntax.

Index: crew/contrib/hgit
===================================================================
--- crew.orig/contrib/hgit	2005-09-07 13:07:20.000000000 +0000
+++ crew/contrib/hgit	2005-09-07 14:43:32.000000000 +0000
@@ -10,8 +10,9 @@
 import time, sys, signal
 from mercurial import hg, mdiff, fancyopts, commands, ui
     
-def difftree(args, ui, repo):
-    def __difftree(repo, files = None, node1 = None, node2 = None):
+def difftree(ui, repo, node1, node2, **opts):
+    """diff trees from two commits"""
+    def __difftree(repo, node1, node2):
         def date(c):
             return time.asctime(time.gmtime(float(c[2].split(' ')[0])))
 
@@ -23,7 +24,7 @@ def difftree(args, ui, repo):
             date2 = date(change)
         else:
             date2 = time.asctime()
-            (c, a, d, u) = repo.diffdir(repo.root, node1)
+            (c, a, d, u) = repo.changes(node1, None)
             if not node1:
                 node1 = repo.dirstate.parents()[0]
             def read(f): return file(os.path.join(repo.root, f)).read()
@@ -33,9 +34,6 @@ def difftree(args, ui, repo):
         date1 = date(change)
         empty = "0" * 40;
 
-        if files:
-            c, a, d = map(lambda x: filterfiles(files, x), (c, a, d))
-
         for f in c:
             # TODO get file permissions
             print ":100664 100664 %s %s M\t%s\t%s" % (hg.hex(mmap[f]), 
@@ -46,23 +44,12 @@ def difftree(args, ui, repo):
             print ":100664 000000 %s %s D\t%s\t%s" % (hg.hex(mmap[f]), empty, f, f)
     ##
 
-    revs = []
-    if args:
-        doptions = {}
-        opts = [('p', 'patch', None, 'patch'),
-                ('r', 'recursive', None, 'recursive')]
-        args = fancyopts.fancyopts(args, opts, doptions)
-
-    if len(args) < 2:
-        help()
-        sys.exit(1)
-    revs.append(repo.lookup(args[0]))
-    revs.append(repo.lookup(args[1]))
-    args = args[2:]
-    if doptions['patch']:
-        commands.dodiff(sys.stdout, ui, repo, args, *revs)
+    node1 = repo.lookup(node1)
+    node2 = repo.lookup(node2)
+    if opts['patch']:
+        commands.dodiff(sys.stdout, ui, repo, node1, node2)
     else:
-        __difftree(repo, args, *revs)
+        __difftree(repo, node1, node2)
 
 def catcommit(repo, n, prefix):
     nlprefix = '\n' + prefix;
@@ -83,17 +70,14 @@ def catcommit(repo, n, prefix):
     else:
         print changes[4]
 
-def catfile(args, ui, repo):
-    doptions = {}
-    opts = [('s', 'stdin', None, 'stdin')]
-    args = fancyopts.fancyopts(args, opts, doptions)
-
+def catfile(ui, repo, type=None, r=None, **opts):
+    """cat a specific revision"""
     # in stdin mode, every line except the commit is prefixed with two
     # spaces.  This way the our caller can find the commit without magic
     # strings
     #
     prefix = ""
-    if doptions['stdin']:
+    if opts['stdin']:
         try:
             (type, r) = raw_input().split(' ');
             prefix = "  "
@@ -101,11 +85,9 @@ def catfile(args, ui, repo):
             return
 
     else:
-        if len(args) < 2:
-            help()
-            sys.exit(1)
-        type = args[0]
-        r = args[1]
+        if not type or not r:
+            ui.warn("cat-file: type or revision not supplied\n")
+            commands.help_(ui, 'cat-file')
 
     while r:
         if type != "commit":
@@ -113,7 +95,7 @@ def catfile(args, ui, repo):
             sys.exit(1);
         n = repo.lookup(r)
         catcommit(repo, n, prefix)
-        if doptions['stdin']:
+        if opts['stdin']:
             try:
                 (type, r) = raw_input().split(' ');
             except EOFError:
@@ -203,53 +185,27 @@ def revtree(args, repo, full="tree", max
 # git rev-list tries to order things by date, and has the ability to stop
 # at a given commit without walking the whole repo.  TODO add the stop
 # parameter
-def revlist(args, repo):
-    doptions = {}
-    opts = [('c', 'commit', None, 'commit'),
-            ('n', 'max-nr', 0, 'max-nr')]
-    args = fancyopts.fancyopts(args, opts, doptions)
-    if doptions['commit']:
+def revlist(ui, repo, *revs, **opts):
+    """print revisions"""
+    if opts['commit']:
         full = "commit"
     else:
         full = None
-    for i in range(1, len(args)):
-        args[i] = '^' + args[i]
-    revtree(args, repo, full, doptions['max-nr'])
-
-def catchterm(*args):
-    raise SignalInterrupt
-
-def help():
-    sys.stderr.write("commands:\n")
-    sys.stderr.write("  hgit cat-file [type] sha1\n")
-    sys.stderr.write("  hgit diff-tree [-p] [-r] sha1 sha1\n")
-    sys.stderr.write("  hgit rev-tree [sha1 ... [^stop sha1]]\n")
-    sys.stderr.write("  hgit rev-list [-c] [sha1 [stop sha1]\n")
-
-cmd = sys.argv[1]
-args = sys.argv[2:]
-u = ui.ui()
-signal.signal(signal.SIGTERM, catchterm)
-repo = hg.repository(ui = u)
-
-if cmd == "diff-tree":
-    difftree(args, u, repo)
-
-elif cmd == "cat-file":
-    catfile(args, u, repo)
-
-elif cmd == "rev-tree":
-    revtree(args, repo)
-
-elif cmd == "rev-list":
-    revlist(args, repo)
-
-elif cmd == "help":
-    help()
-
-else:
-    if cmd: sys.stderr.write("unknown command\n\n")
-    help()
-    sys.exit(1)
+    copy = [x for x in revs]
+    for i in range(1, len(copy)):
+        copy[i] = '^' + copy[i]
+    revtree(copy, repo, full, opts['maxnr'])
+
+cmdtable = {
+    "git-diff-tree": (difftree, [('p', 'patch', None, 'generate patch'),
+                            ('r', 'recursive', None, 'recursive')],
+                            "hg diff-tree [options] node1 node2"),
+    "git-cat-file": (catfile, [('s', 'stdin', None, 'stdin')],
+                 "hg cat-file [options] type file"),
+    "git-rev-list": (revlist, [('c', 'commit', None, 'commit'),
+                           ('n', 'maxnr', 0, 'max-nr')],
+                 "hg rev-list [options] revs"),
+}
 
-sys.exit(0)
+def reposetup(ui, repo):
+    pass

--


More information about the Mercurial mailing list