#!/usr/bin/python
#
# mercurial - a minimal scalable distributed SCM
# v0.2 "anna karenina"
#
# Copyright 2005 Matt Mackall <mpm@selenic.com>
#
# This software may be used and distributed according to the terms
# of the GNU General Public License, incorporated herein by reference.

import hg, sys, os, binascii

try:
    cmd = sys.argv[1]
    args = sys.argv[2:]
except:
    print "mercurial v0.2"
    print "commands:"
    print " init checkout add remove commit status history log merge"
    sys.exit(1)

ui = hg.ui()
    
if cmd == "init":
    repo = hg.repository(ui, ".", create=1)
    sys.exit(0)
else:
    repo = hg.repository(ui=ui)

if cmd == "checkout":
    node = repo.changelog.tip()
    if len(args): rev = int(args[0])
    repo.checkout(node)

elif cmd == "add":
    repo.add(args)

elif cmd == "remove" :
    repo.remove(args)

elif cmd == "commit":
    if 1:
        repo.commit()
    else:
        import hotshot, hotshot.stats, test.pystone
        prof = hotshot.Profile("/tmp/commit.prof")
        prof.runcall(repo.commit)
        prof.close()
        stats = hotshot.stats.load("/tmp/commit.prof")
        stats.strip_dirs()
        stats.sort_stats('time', 'calls')
        stats.print_stats(20)

elif cmd == "status":
    (c, a, d) = repo.diffdir(repo.root)
    for f in c: print "C", f
    for f in a: print "?", f
    for f in d: print "D", f

elif cmd == "addremove":
    (c, a, d) = repo.diffdir(repo.root)
    repo.add(a)
    repo.remove(d)
    
elif cmd == "history":
    for i in range(repo.changelog.count()):
        n = repo.changelog.node(i)
        changes = repo.changelog.read(n)
        (p1, p2) = repo.changelog.parents(n)
        (h, h1, h2) = map(repo.changelog.hex, (n, p1, p2))
        (i1, i2) = map(repo.changelog.rev, (p1, p2))
        print "rev:      %4d:%s" % (i, h)
        print "parents:  %4d:%s\n          %4d:%s" % (i1, h1, i2, h2)
        print "manifest: %4d:%s" % (repo.manifest.rev(changes[0]),
                                    repo.changelog.hex(changes[0]))
        print "user:", changes[1]
        print "files:", len(changes[3])
        print "description:"
        print changes[4]

elif cmd == "log":
    r = repo.file(args[0])
    for i in range(r.count()):
        n = r.node(i)
        (p1, p2) = r.parents(n)
        (h, h1, h2) = map(r.hex, (n, p1, p2))
        (i1, i2) = map(r.rev, (p1, p2))
        print "rev:      %4d:%s" % (i, h)
        print "parents:  %4d:%s\n          %4d:%s" % (i1, h1, i2, h2)

elif cmd == "merge":
    other = hg.repository(ui, args[0])
    repo.merge(other)
