# HG changeset patch # User Augie Fackler # Date 1227305723 21600 # Node ID 91ce18fa037541416f7453feb224c2daafa5edad # Parent 1da7aafdd3238226fffc315e49b635194254871c Add a diff command that behaves kind of like svn diff. diff --git a/diff_cmd.py b/diff_cmd.py new file mode 100644 --- /dev/null +++ b/diff_cmd.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +import re + +from mercurial import patch + +import util +import hg_delta_editor + +b_re = re.compile(r'^\+\+\+ b\/([^\n]*)', re.MULTILINE) +a_re = re.compile(r'^--- a\/([^\n]*)', re.MULTILINE) +devnull_re = re.compile(r'^([-+]{3}) /dev/null', re.MULTILINE) +header_re = re.compile(r'^diff --git .* b\/(.*)', re.MULTILINE) +newfile_devnull_re = re.compile(r'^--- /dev/null\n\+\+\+ b/([^\n]*)', + re.MULTILINE) +def filterdiff(diff, base_revision): + diff = newfile_devnull_re.sub(r'--- \1\t(revision 0)' '\n' + r'+++ \1\t(working copy)', + diff) + diff = a_re.sub(r'--- \1'+ ('\t(revision %d)' % base_revision), diff) + diff = b_re.sub(r'+++ \1' + '\t(working copy)', diff) + diff = devnull_re.sub(r'\1 /dev/null' '\t(working copy)', diff) + + diff = header_re.sub(r'Index: \1' + '\n' + ('=' * 67), diff) + return diff + + +@util.register_subcommand('diff') +def diff_command(ui, repo, hg_repo_path, **opts): + """Show a diff of the most recent revision against its parent from svn. + """ + hge = hg_delta_editor.HgChangeReceiver(hg_repo_path, + ui_=ui) + svn_commit_hashes = dict(zip(hge.revmap.itervalues(), + hge.revmap.iterkeys())) + o_r = util.outgoing_revisions(ui, repo, hge, svn_commit_hashes) + parent = repo.parents()[0] + if o_r: + parent = repo[o_r[-1]].parents()[0] + base_rev, _junk = svn_commit_hashes[parent.node()] + it = patch.diff(repo, parent.node(), None, + opts=patch.diffopts(ui, opts)) + ui.write(filterdiff(''.join(it), base_rev)) diff --git a/svncommand.py b/svncommand.py --- a/svncommand.py +++ b/svncommand.py @@ -16,8 +16,9 @@ from util import register_subcommand, sv from utility_commands import print_wc_url from fetch_command import fetch_revisions from push_cmd import commit_from_rev +from diff_cmd import diff_command # shut up, pyflakes, we must import those -__x = [print_wc_url, fetch_revisions, commit_from_rev, ] +__x = [print_wc_url, fetch_revisions, commit_from_rev, diff_command] mode755 = (stat.S_IXUSR | stat.S_IXGRP| stat.S_IXOTH | stat.S_IRUSR | stat.S_IRGRP| stat.S_IROTH | stat.S_IWUSR) @@ -74,7 +75,7 @@ def help_command(ui, args=None, **opts): doc = "No documentation available for %s." % subcommand ui.status(doc.strip(), '\n') return - ui.status('Valid commands: ', ' '.join(sorted(svn_subcommands.keys())), + ui.status('Valid commands: ', ' '.join(sorted(svn_subcommands.keys())), '\n') @register_subcommand('gentags')