comparison utility_commands.py @ 0:f2636cfed115

Initial import of hgsubversion into a public repository.
author Augie Fackler <durin42@gmail.com>
date Tue, 30 Sep 2008 11:42:52 -0500
parents
children 05800c403321
comparison
equal deleted inserted replaced
-1:000000000000 0:f2636cfed115
1 from mercurial import cmdutil
2 from mercurial import node
3 from hgext import rebase
4
5 import util
6 import hg_delta_editor
7
8 @util.register_subcommand('url')
9 def print_wc_url(ui, repo, hg_repo_path, **opts):
10 hge = hg_delta_editor.HgChangeReceiver(hg_repo_path,
11 ui_=ui)
12 ui.status(hge.url, '\n')
13
14
15 @util.register_subcommand('parent')
16 def print_parent_revision(ui, repo, hg_repo_path, **opts):
17 """Prints the hg hash and svn revision info for the nearest svn parent of
18 the current revision"""
19 hge = hg_delta_editor.HgChangeReceiver(hg_repo_path,
20 ui_=ui)
21 svn_commit_hashes = dict(zip(hge.revmap.itervalues(),
22 hge.revmap.iterkeys()))
23 ha = repo.parents()[0]
24 o_r = outgoing_revisions(ui, repo, hge, svn_commit_hashes)
25 if o_r:
26 ha = repo[o_r[-1]].parents()[0]
27 if ha.node() != node.nullid:
28 r, br = svn_commit_hashes[ha.node()]
29 ui.status('Working copy parent revision is %s: r%s on %s\n' %
30 (ha, r, br or 'trunk'))
31 else:
32 ui.status('Working copy seems to have no parent svn revision.\n')
33 return 0
34
35
36 @util.register_subcommand('rebase')
37 def rebase_commits(ui, repo, hg_repo_path, **opts):
38 """Rebases the current uncommitted revisions onto the top of the branch.
39 """
40 hge = hg_delta_editor.HgChangeReceiver(hg_repo_path,
41 ui_=ui)
42 svn_commit_hashes = dict(zip(hge.revmap.itervalues(),
43 hge.revmap.iterkeys()))
44 o_r = outgoing_revisions(ui, repo, hge, svn_commit_hashes)
45 if not o_r:
46 ui.status('Nothing to rebase!\n')
47 return 0
48 if len(repo.parents()[0].children()):
49 ui.status('Refusing to rebase non-head commit like a coward\n')
50 return 0
51 parent_rev = repo[o_r[-1]].parents()[0]
52 target_rev = parent_rev
53 p_n = parent_rev.node()
54 exhausted_choices = False
55 while target_rev.children() and not exhausted_choices:
56 for c in target_rev.children():
57 exhausted_choices = True
58 n = c.node()
59 if (n in svn_commit_hashes and
60 svn_commit_hashes[n][1] == svn_commit_hashes[p_n][1]):
61 target_rev = c
62 exhausted_choices = False
63 break
64 if parent_rev == target_rev:
65 ui.status('Already up to date!\n')
66 return 0
67 # TODO this is really hacky, there must be a more direct way
68 return rebase.rebase(ui, repo, dest=node.hex(target_rev.node()),
69 base=node.hex(repo.parents()[0].node()))
70
71
72 @util.register_subcommand('outgoing')
73 def show_outgoing_to_svn(ui, repo, hg_repo_path, **opts):
74 """Commit the current revision and any required parents back to svn.
75 """
76 hge = hg_delta_editor.HgChangeReceiver(hg_repo_path,
77 ui_=ui)
78 svn_commit_hashes = dict(zip(hge.revmap.itervalues(),
79 hge.revmap.iterkeys()))
80 o_r = outgoing_revisions(ui, repo, hge, svn_commit_hashes)
81 if not (o_r and len(o_r)):
82 ui.status('No outgoing changes found.\n')
83 return 0
84 displayer = cmdutil.show_changeset(ui, repo, opts, buffered=False)
85 for rev in reversed(o_r):
86 displayer.show(changenode=rev)
87
88
89 def outgoing_revisions(ui, repo, hg_editor, reverse_map):
90 """Given a repo and an hg_editor, determines outgoing revisions for the
91 current working copy state.
92 """
93 outgoing_rev_hashes = []
94 working_rev = repo.parents()
95 assert len(working_rev) == 1
96 working_rev = working_rev[0]
97 if working_rev.node() in reverse_map:
98 return
99 while (not working_rev.node() in reverse_map
100 and working_rev.node() != node.nullid):
101 outgoing_rev_hashes.append(working_rev.node())
102 working_rev = working_rev.parents()
103 assert len(working_rev) == 1
104 working_rev = working_rev[0]
105 if working_rev.node() != node.nullid:
106 return outgoing_rev_hashes