comparison svncommand.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 1a5bb173170b
comparison
equal deleted inserted replaced
-1:000000000000 0:f2636cfed115
1 import os
2 import pickle
3 import stat
4
5 from mercurial import hg
6 from mercurial import node
7
8 import svnwrap
9 import hg_delta_editor
10 import util
11 from util import register_subcommand, svn_subcommands
12 # dirty trick to force demandimport to run my decorator anyway.
13 from utility_commands import print_wc_url
14 from fetch_command import fetch_revisions
15 from push_cmd import commit_from_rev
16 # shut up, pyflakes, we must import those
17 __x = [print_wc_url, fetch_revisions, commit_from_rev, ]
18
19 mode755 = (stat.S_IXUSR | stat.S_IXGRP| stat.S_IXOTH | stat.S_IRUSR |
20 stat.S_IRGRP| stat.S_IROTH | stat.S_IWUSR)
21 mode644 = (stat.S_IRUSR | stat.S_IRGRP| stat.S_IROTH | stat.S_IWUSR)
22
23
24 def svncmd(ui, repo, subcommand, *args, **opts):
25 if subcommand not in svn_subcommands:
26 candidates = []
27 for c in svn_subcommands:
28 if c.startswith(subcommand):
29 candidates.append(c)
30 if len(candidates) == 1:
31 subcommand = candidates[0]
32 path = os.path.dirname(repo.path)
33 try:
34 opts['svn_url'] = open(os.path.join(repo.path, 'svn', 'url')).read()
35 return svn_subcommands[subcommand](ui, args=args,
36 hg_repo_path=path,
37 repo=repo,
38 **opts)
39 except TypeError, e:
40 print e
41 print 'Bad arguments for subcommand %s' % subcommand
42 except KeyError, e:
43 print 'Unknown subcommand %s' % subcommand
44
45 @register_subcommand('help')
46 def help_command(ui, args=None, **opts):
47 """Get help on the subsubcommands.
48 """
49 if args and args[0] in svn_subcommands:
50 print svn_subcommands[args[0]].__doc__.strip()
51 return
52 print 'Valid commands:', ' '.join(sorted(svn_subcommands.keys()))
53
54 @register_subcommand('gentags')
55 def generate_hg_tags(ui, hg_repo_path, **opts):
56 """Save tags to .hg/localtags
57 """
58 hg_editor = hg_delta_editor.HgChangeReceiver(hg_repo_path, ui_=ui)
59 f = open(hg_editor.tag_info_file)
60 tag_info = pickle.load(f)
61 f = open(os.path.join(hg_repo_path, '.hg', 'localtags'), 'w')
62 for tag, source in tag_info.iteritems():
63 source_ha = hg_editor.get_parent_revision(source[1]+1, source[0])
64 f.write('%s tag:%s\n' % (node.hex(source_ha), tag))
65
66 @register_subcommand('up')
67 def update(ui, args, repo, clean=False, **opts):
68 """Update to a specified Subversion revision number.
69 """
70 assert len(args) == 1
71 rev = int(args[0])
72 path = os.path.join(repo.path, 'svn', 'rev_map')
73 answers = []
74 for k,v in pickle.load(open(path)).iteritems():
75 if k[0] == rev:
76 answers.append((v, k[1]))
77 if len(answers) == 1:
78 if clean:
79 return hg.clean(repo, answers[0][0])
80 return hg.update(repo, answers[0][0])
81 elif len(answers) == 0:
82 ui.status('Revision %s did not produce an hg revision.\n' % rev)
83 return 1
84 else:
85 ui.status('Non-ambiguous revision!\n')
86 ui.status('\n'.join(['%s on %s' % (node.hex(a[0]), a[1]) for a in
87 answers]+['']))
88 return 1
89
90
91 @register_subcommand('verify_revision')
92 def verify_revision(ui, args, repo, force=False, **opts):
93 """Verify a single converted revision.
94 Note: This wipes your working copy and then exports the corresponding
95 Subversion into your working copy to verify. Use with caution.
96 """
97 assert len(args) == 1
98 if not force:
99 assert repo.status(ignored=True,
100 unknown=True) == ([], [], [], [], [], [], [])
101 rev = int(args[0])
102 wc_path = os.path.dirname(repo.path)
103 svn_url = open(os.path.join(repo.path, 'svn', 'url')).read()
104 svn = svnwrap.SubversionRepo(svn_url)
105 util.wipe_all_files(wc_path)
106 if update(ui, args, repo, clean=True) == 0:
107 util.wipe_all_files(wc_path)
108 br = repo.dirstate.branch()
109 if br == 'default':
110 br = None
111 if br:
112 diff_path = 'branches/%s' % br
113 else:
114 diff_path = 'trunk'
115 svn.fetch_all_files_to_dir(diff_path, rev, wc_path)
116 stat = repo.status(unknown=True)
117 ignored = [s for s in stat[4]
118 if '/.svn/' not in s and not s.startswith('.svn/')]
119 stat = stat[0:4]
120 if stat != ([], [], [], [],) or ignored != []:
121 ui.status('Something is wrong with this revision.\n')
122 return 2
123 else:
124 ui.status('OK.\n')
125 return 0
126 return 1
127
128 @register_subcommand('verify_all_revisions')
129 def verify_all_revisions(ui, args, repo, **opts):
130 """Verify all the converted revisions, optionally starting at a revision.
131
132 Note: This is *extremely* abusive of the Subversion server. It exports every
133 revision of the code one revision at a time.
134 """
135 assert repo.status(ignored=True,
136 unknown=True) == ([], [], [], [], [], [], [])
137 start_rev = 0
138 args = list(args)
139 if args:
140 start_rev = int(args.pop(0))
141 revmap_f = open(os.path.join(repo.path, 'svn', 'rev_map'))
142 revmap = pickle.load(revmap_f)
143 revs = sorted(revmap.keys())
144 for revnum, br in revs:
145 if revnum < start_rev:
146 continue
147 res = verify_revision(ui, [revnum], repo, force=True)
148 if res == 0:
149 print revnum, 'verfied'
150 elif res == 1:
151 print revnum, 'skipped'
152 else:
153 print revnum, 'failed'
154 return 1
155 return 0