comparison utility_commands.py @ 199:91db8fc049b0

Add a genignore utility command that generates an hgignore file by scraping svn:ignore properties.
author Augie Fackler <durin42@gmail.com>
date Tue, 24 Feb 2009 14:30:21 -0600
parents 77812f98e250
children a421aca2b0f5
comparison
equal deleted inserted replaced
198:df4611050286 199:91db8fc049b0
1 import os
2
1 import mercurial 3 import mercurial
2 from mercurial import cmdutil 4 from mercurial import cmdutil
3 from mercurial import node 5 from mercurial import node
4 from mercurial import util as mutil 6 from mercurial import util as mutil
5 from hgext import rebase 7 from hgext import rebase
15 ui_=ui) 17 ui_=ui)
16 ui.status(hge.url, '\n') 18 ui.status(hge.url, '\n')
17 print_wc_url = util.register_subcommand('url')(print_wc_url) 19 print_wc_url = util.register_subcommand('url')(print_wc_url)
18 20
19 21
20 def run_svn_info(ui, repo, hg_repo_path, **opts): 22 def find_wc_parent_rev(ui, repo, hge, svn_commit_hashes):
21 """show Subversion details similar to `svn info' 23 """Find the svn parent revision of the repo's dirstate.
22 """ 24 """
23 hge = hg_delta_editor.HgChangeReceiver(hg_repo_path,
24 ui_=ui)
25 svn_commit_hashes = dict(zip(hge.revmap.itervalues(),
26 hge.revmap.iterkeys()))
27 workingctx = repo.parents()[0] 25 workingctx = repo.parents()[0]
28 o_r = util.outgoing_revisions(ui, repo, hge, svn_commit_hashes, workingctx.node()) 26 o_r = util.outgoing_revisions(ui, repo, hge, svn_commit_hashes, workingctx.node())
29 if o_r: 27 if o_r:
30 workingctx = repo[o_r[-1]].parents()[0] 28 workingctx = repo[o_r[-1]].parents()[0]
31 r, br = svn_commit_hashes[workingctx.node()] 29 return workingctx
30
31
32 def generate_ignore(ui, repo, hg_repo_path, force=False, **opts):
33 """generate .hgignore from svn:ignore properties.
34 """
35 ignpath = os.path.join(hg_repo_path, '.hgignore')
36 if not force and os.path.exists(ignpath):
37 raise mutil.Abort('not overwriting existing .hgignore, try --force?')
38 ignorefile = open(ignpath, 'w')
39 ignorefile.write('.hgignore\nsyntax:glob\n')
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 parent = find_wc_parent_rev(ui, repo, hge, svn_commit_hashes)
45 r, br = svn_commit_hashes[parent.node()]
46 if br == None:
47 branchpath = 'trunk'
48 else:
49 branchpath = 'branches/%s' % br
50 url = hge.url
51 if url[-1] == '/':
52 url = url[:-1]
53 svn = svnwrap.SubversionRepo(url)
54 dirs = [''] + [d[0] for d in svn.list_files(branchpath, r) if d[1] == 'd']
55 for dir in dirs:
56 props = svn.list_props('%s/%s/' % (branchpath,dir), r)
57 if 'svn:ignore' in props:
58 lines = props['svn:ignore'].strip().split('\n')
59 for prop in lines:
60 if dir:
61 ignorefile.write('%s/%s\n' % (dir, prop))
62 else:
63 ignorefile.write('%s\n' % prop)
64 generate_ignore = util.register_subcommand('genignore')(generate_ignore)
65
66
67 def run_svn_info(ui, repo, hg_repo_path, **opts):
68 """show Subversion details similar to `svn info'
69 """
70 hge = hg_delta_editor.HgChangeReceiver(hg_repo_path,
71 ui_=ui)
72 svn_commit_hashes = dict(zip(hge.revmap.itervalues(),
73 hge.revmap.iterkeys()))
74 parent = find_wc_parent_rev(ui, repo, hge, svn_commit_hashes)
75 r, br = svn_commit_hashes[parent.node()]
32 if br == None: 76 if br == None:
33 branchpath = '/trunk' 77 branchpath = '/trunk'
34 else: 78 else:
35 branchpath = '/branches/%s' % br 79 branchpath = '/branches/%s' % br
36 url = hge.url 80 url = hge.url
37 if url[-1] == '/': 81 if url[-1] == '/':
38 url = url[:-1] 82 url = url[:-1]
39 url = '%s%s' % (url, branchpath) 83 url = '%s%s' % (url, branchpath)
40 author = '@'.join(workingctx.user().split('@')[:-1]) 84 author = '@'.join(parent.user().split('@')[:-1])
41 # cleverly figure out repo root w/o actually contacting the server 85 # cleverly figure out repo root w/o actually contacting the server
42 subdir = workingctx.extra()['convert_revision'][40:].split('@')[0] 86 subdir = parent.extra()['convert_revision'][40:].split('@')[0]
43 reporoot = url[:len(url)-len(subdir)] 87 reporoot = url[:len(url)-len(subdir)]
44 ui.status('''URL: %(url)s 88 ui.status('''URL: %(url)s
45 Repository Root: %(reporoot)s 89 Repository Root: %(reporoot)s
46 Repository UUID: %(uuid)s 90 Repository UUID: %(uuid)s
47 Revision: %(revision)s 91 Revision: %(revision)s
53 'uuid': open(hge.uuid_file).read(), 97 'uuid': open(hge.uuid_file).read(),
54 'url': url, 98 'url': url,
55 'author': author, 99 'author': author,
56 'revision': r, 100 'revision': r,
57 # TODO I'd like to format this to the user's local TZ if possible 101 # TODO I'd like to format this to the user's local TZ if possible
58 'date': mutil.datestr(workingctx.date(), 102 'date': mutil.datestr(parent.date(),
59 '%Y-%m-%d %H:%M:%S %1%2 (%a, %d %b %Y)') 103 '%Y-%m-%d %H:%M:%S %1%2 (%a, %d %b %Y)')
60 }) 104 })
61 run_svn_info = util.register_subcommand('info')(run_svn_info) 105 run_svn_info = util.register_subcommand('info')(run_svn_info)
62 106
63 107
66 """ 110 """
67 hge = hg_delta_editor.HgChangeReceiver(hg_repo_path, 111 hge = hg_delta_editor.HgChangeReceiver(hg_repo_path,
68 ui_=ui) 112 ui_=ui)
69 svn_commit_hashes = dict(zip(hge.revmap.itervalues(), 113 svn_commit_hashes = dict(zip(hge.revmap.itervalues(),
70 hge.revmap.iterkeys())) 114 hge.revmap.iterkeys()))
71 ha = repo.parents()[0] 115 ha = find_wc_parent_rev(ui, repo, hge, svn_commit_hashes)
72 o_r = util.outgoing_revisions(ui, repo, hge, svn_commit_hashes, ha.node())
73 if o_r:
74 ha = repo[o_r[-1]].parents()[0]
75 if ha.node() != node.nullid: 116 if ha.node() != node.nullid:
76 r, br = svn_commit_hashes[ha.node()] 117 r, br = svn_commit_hashes[ha.node()]
77 ui.status('Working copy parent revision is %s: r%s on %s\n' % 118 ui.status('Working copy parent revision is %s: r%s on %s\n' %
78 (ha, r, br or 'trunk')) 119 (ha, r, br or 'trunk'))
79 else: 120 else: