hgit
258 lines
| 8.0 KiB
| text/plain
|
TextLexer
/ contrib / hgit
|
r267 | #!/usr/bin/env python | ||
# | ||||
# Minimal support for git commands on an hg repository | ||||
# | ||||
# Copyright 2005 Chris Mason <mason@suse.com> | ||||
# | ||||
# This software may be used and distributed according to the terms | ||||
# of the GNU General Public License, incorporated herein by reference. | ||||
import time, sys, signal | ||||
from mercurial import hg, mdiff, fancyopts, commands, ui | ||||
|
r396 | def difftree(args, ui, repo): | ||
|
r267 | def __difftree(repo, files = None, node1 = None, node2 = None): | ||
|
r334 | def date(c): | ||
return time.asctime(time.gmtime(float(c[2].split(' ')[0]))) | ||||
|
r267 | |||
|
r334 | if node2: | ||
change = repo.changelog.read(node2) | ||||
mmap2 = repo.manifest.read(change[0]) | ||||
(c, a, d) = repo.diffrevs(node1, node2) | ||||
def read(f): return repo.file(f).read(mmap2[f]) | ||||
date2 = date(change) | ||||
else: | ||||
date2 = time.asctime() | ||||
(c, a, d, u) = repo.diffdir(repo.root, node1) | ||||
if not node1: | ||||
node1 = repo.dirstate.parents()[0] | ||||
def read(f): return file(os.path.join(repo.root, f)).read() | ||||
|
r267 | |||
|
r334 | change = repo.changelog.read(node1) | ||
mmap = repo.manifest.read(change[0]) | ||||
date1 = date(change) | ||||
empty = "0" * 40; | ||||
|
r267 | |||
|
r334 | if files: | ||
c, a, d = map(lambda x: filterfiles(files, x), (c, a, d)) | ||||
|
r267 | |||
|
r334 | for f in c: | ||
# TODO get file permissions | ||||
|
r406 | print ":100664 100664 %s %s M\t%s\t%s" % (hg.hex(mmap[f]), | ||
|
r334 | hg.hex(mmap2[f]), f, f) | ||
for f in a: | ||||
|
r406 | print ":000000 100664 %s %s N\t%s\t%s" % (empty, hg.hex(mmap2[f]), f, f) | ||
|
r334 | for f in d: | ||
|
r406 | print ":100664 000000 %s %s D\t%s\t%s" % (hg.hex(mmap[f]), empty, f, f) | ||
|
r267 | ## | ||
revs = [] | ||||
if args: | ||||
|
r334 | doptions = {} | ||
opts = [('p', 'patch', None, 'patch'), | ||||
('r', 'recursive', None, 'recursive')] | ||||
args = fancyopts.fancyopts(args, opts, doptions, | ||||
|
r267 | 'hg diff-tree [options] sha1 sha1') | ||
if len(args) < 2: | ||||
|
r334 | help() | ||
|
r267 | sys.exit(1) | ||
revs.append(repo.lookup(args[0])) | ||||
revs.append(repo.lookup(args[1])) | ||||
args = args[2:] | ||||
if doptions['patch']: | ||||
|
r396 | commands.dodiff(ui, repo, "", args, *revs) | ||
|
r267 | else: | ||
|
r334 | __difftree(repo, args, *revs) | ||
|
r267 | |||
def catcommit(repo, n, prefix): | ||||
nlprefix = '\n' + prefix; | ||||
changes = repo.changelog.read(n) | ||||
(p1, p2) = repo.changelog.parents(n) | ||||
(h, h1, h2) = map(hg.hex, (n, p1, p2)) | ||||
(i1, i2) = map(repo.changelog.rev, (p1, p2)) | ||||
print "tree %s" % (h) | ||||
if i1 != -1: print "%sparent %s" % (prefix, h1) | ||||
if i2 != -1: print "%sparent %s" % (prefix, h2) | ||||
date_ar = changes[2].split(' ') | ||||
date = int(float(date_ar[0])) | ||||
print "%sauthor <%s> %s %s" % (prefix, changes[1], date, date_ar[1]) | ||||
print "%scommitter <%s> %s %s" % (prefix, changes[1], date, date_ar[1]) | ||||
print prefix | ||||
if prefix != "": | ||||
|
r334 | print "%s%s" % (prefix, changes[4].replace('\n', nlprefix).strip()) | ||
|
r267 | else: | ||
|
r334 | print changes[4] | ||
|
r267 | |||
def catfile(args, ui, repo): | ||||
doptions = {} | ||||
opts = [('s', 'stdin', None, 'stdin')] | ||||
args = fancyopts.fancyopts(args, opts, doptions, | ||||
|
r334 | 'hg cat-file type sha1') | ||
|
r267 | |||
# 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']: | ||||
|
r334 | try: | ||
(type, r) = raw_input().split(' '); | ||||
prefix = " " | ||||
except EOFError: | ||||
return | ||||
|
r267 | |||
else: | ||||
|
r334 | if len(args) < 2: | ||
help() | ||||
sys.exit(1) | ||||
|
r267 | type = args[0] | ||
|
r334 | r = args[1] | ||
|
r267 | |||
while r: | ||||
|
r334 | if type != "commit": | ||
sys.stderr.write("aborting hg cat-file only understands commits\n") | ||||
sys.exit(1); | ||||
n = repo.changelog.lookup(r) | ||||
catcommit(repo, n, prefix) | ||||
if doptions['stdin']: | ||||
try: | ||||
(type, r) = raw_input().split(' '); | ||||
except EOFError: | ||||
break | ||||
else: | ||||
break | ||||
|
r267 | |||
# git rev-tree is a confusing thing. You can supply a number of | ||||
# commit sha1s on the command line, and it walks the commit history | ||||
# telling you which commits are reachable from the supplied ones via | ||||
# a bitmask based on arg position. | ||||
# you can specify a commit to stop at by starting the sha1 with ^ | ||||
|
r356 | def revtree(args, repo, full="tree", maxnr=0): | ||
|
r267 | # calculate and return the reachability bitmask for sha | ||
def is_reachable(ar, reachable, sha): | ||||
|
r334 | if len(ar) == 0: | ||
return 1 | ||||
mask = 0 | ||||
for i in range(len(ar)): | ||||
if sha in reachable[i]: | ||||
mask |= 1 << i | ||||
|
r267 | |||
|
r334 | return mask | ||
|
r267 | |||
reachable = [] | ||||
stop_sha1 = [] | ||||
want_sha1 = [] | ||||
|
r356 | count = 0 | ||
|
r267 | |||
# figure out which commits they are asking for and which ones they | ||||
# want us to stop on | ||||
for i in range(len(args)): | ||||
if args[i].count('^'): | ||||
|
r334 | s = args[i].split('^')[1] | ||
stop_sha1.append(repo.changelog.lookup(s)) | ||||
want_sha1.append(s) | ||||
elif args[i] != 'HEAD': | ||||
want_sha1.append(args[i]) | ||||
|
r356 | |||
|
r267 | # calculate the graph for the supplied commits | ||
for i in range(len(want_sha1)): | ||||
|
r334 | reachable.append({}); | ||
n = repo.changelog.lookup(want_sha1[i]); | ||||
visit = [n]; | ||||
reachable[i][n] = 1 | ||||
while visit: | ||||
n = visit.pop(0) | ||||
if n in stop_sha1: | ||||
break | ||||
for p in repo.changelog.parents(n): | ||||
if p not in reachable[i]: | ||||
reachable[i][p] = 1 | ||||
visit.append(p) | ||||
if p in stop_sha1: | ||||
break | ||||
|
r356 | |||
|
r267 | # walk the repository looking for commits that are in our | ||
# reachability graph | ||||
|
r356 | for i in range(repo.changelog.count()-1, -1, -1): | ||
|
r334 | n = repo.changelog.node(i) | ||
mask = is_reachable(want_sha1, reachable, n) | ||||
if mask: | ||||
|
r356 | if not full: | ||
print hg.hex(n) | ||||
elif full is "commit": | ||||
print hg.hex(n) | ||||
catcommit(repo, n, ' ') | ||||
else: | ||||
changes = repo.changelog.read(n) | ||||
(p1, p2) = repo.changelog.parents(n) | ||||
(h, h1, h2) = map(hg.hex, (n, p1, p2)) | ||||
(i1, i2) = map(repo.changelog.rev, (p1, p2)) | ||||
|
r267 | |||
|
r356 | date = changes[2].split(' ')[0] | ||
print "%s %s:%s" % (date, h, mask), | ||||
mask = is_reachable(want_sha1, reachable, p1) | ||||
if i1 != -1 and mask > 0: | ||||
print "%s:%s " % (h1, mask), | ||||
mask = is_reachable(want_sha1, reachable, p2) | ||||
if i2 != -1 and mask > 0: | ||||
print "%s:%s " % (h2, mask), | ||||
print "" | ||||
if maxnr and count >= maxnr: | ||||
break | ||||
count += 1 | ||||
|
r267 | |||
# 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 = {} | ||||
|
r356 | opts = [('c', 'commit', None, 'commit'), | ||
('n', 'max-nr', 0, 'max-nr')] | ||||
|
r267 | args = fancyopts.fancyopts(args, opts, doptions, | ||
|
r334 | 'hg rev-list') | ||
|
r356 | if doptions['commit']: | ||
full = "commit" | ||||
else: | ||||
full = None | ||||
for i in range(1, len(args)): | ||||
args[i] = '^' + args[i] | ||||
revtree(args, repo, full, doptions['max-nr']) | ||||
|
r267 | |||
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]\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": | ||||
|
r396 | difftree(args, u, repo) | ||
|
r267 | |||
elif cmd == "cat-file": | ||||
|
r396 | catfile(args, u, repo) | ||
|
r267 | |||
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) | ||||
sys.exit(0) | ||||