comparison mercurial/commands.py @ 3087:eeaf9bcdfa25

Move revision parsing into cmdutil.
author Brendan Cully <brendan@kublai.com>
date Thu, 14 Sep 2006 11:19:35 -0700
parents e270cbd4aa20
children 25857e00af8e
comparison
equal deleted inserted replaced
3086:e7fc04dc6349 3087:eeaf9bcdfa25
106 else: 106 else:
107 start = repo.changelog.rev(p) 107 start = repo.changelog.rev(p)
108 defrange = '%s:0' % start 108 defrange = '%s:0' % start
109 else: 109 else:
110 defrange = 'tip:0' 110 defrange = 'tip:0'
111 revs = map(int, revrange(ui, repo, opts['rev'] or [defrange])) 111 revs = map(int, cmdutil.revrange(ui, repo, opts['rev'] or [defrange]))
112 wanted = {} 112 wanted = {}
113 slowpath = anypats 113 slowpath = anypats
114 fncache = {} 114 fncache = {}
115 115
116 chcache = {} 116 chcache = {}
249 fns = fncache.get(rev) or filter(matchfn, getchange(rev)[3]) 249 fns = fncache.get(rev) or filter(matchfn, getchange(rev)[3])
250 yield 'add', rev, fns 250 yield 'add', rev, fns
251 for rev in nrevs: 251 for rev in nrevs:
252 yield 'iter', rev, None 252 yield 'iter', rev, None
253 return iterate(), getchange, matchfn 253 return iterate(), getchange, matchfn
254
255 revrangesep = ':'
256
257 def revfix(repo, val, defval):
258 '''turn user-level id of changeset into rev number.
259 user-level id can be tag, changeset, rev number, or negative rev
260 number relative to number of revs (-1 is tip, etc).'''
261 if not val:
262 return defval
263 try:
264 num = int(val)
265 if str(num) != val:
266 raise ValueError
267 if num < 0:
268 num += repo.changelog.count()
269 if num < 0:
270 num = 0
271 elif num >= repo.changelog.count():
272 raise ValueError
273 except ValueError:
274 try:
275 num = repo.changelog.rev(repo.lookup(val))
276 except KeyError:
277 raise util.Abort(_('invalid revision identifier %s') % val)
278 return num
279
280 def revpair(ui, repo, revs):
281 '''return pair of nodes, given list of revisions. second item can
282 be None, meaning use working dir.'''
283 if not revs:
284 return repo.dirstate.parents()[0], None
285 end = None
286 if len(revs) == 1:
287 start = revs[0]
288 if revrangesep in start:
289 start, end = start.split(revrangesep, 1)
290 start = revfix(repo, start, 0)
291 end = revfix(repo, end, repo.changelog.count() - 1)
292 else:
293 start = revfix(repo, start, None)
294 elif len(revs) == 2:
295 if revrangesep in revs[0] or revrangesep in revs[1]:
296 raise util.Abort(_('too many revisions specified'))
297 start = revfix(repo, revs[0], None)
298 end = revfix(repo, revs[1], None)
299 else:
300 raise util.Abort(_('too many revisions specified'))
301 if end is not None: end = repo.lookup(str(end))
302 return repo.lookup(str(start)), end
303
304 def revrange(ui, repo, revs):
305 """Yield revision as strings from a list of revision specifications."""
306 seen = {}
307 for spec in revs:
308 if revrangesep in spec:
309 start, end = spec.split(revrangesep, 1)
310 start = revfix(repo, start, 0)
311 end = revfix(repo, end, repo.changelog.count() - 1)
312 step = start > end and -1 or 1
313 for rev in xrange(start, end+step, step):
314 if rev in seen:
315 continue
316 seen[rev] = 1
317 yield str(rev)
318 else:
319 rev = revfix(repo, spec, None)
320 if rev in seen:
321 continue
322 seen[rev] = 1
323 yield str(rev)
324 254
325 def write_bundle(cg, filename=None, compress=True): 255 def write_bundle(cg, filename=None, compress=True):
326 """Write a bundle file and return its filename. 256 """Write a bundle file and return its filename.
327 257
328 Existing files will not be overwritten. 258 Existing files will not be overwritten.
1342 1272
1343 Without the -a option, diff will avoid generating diffs of files 1273 Without the -a option, diff will avoid generating diffs of files
1344 it detects as binary. With -a, diff will generate a diff anyway, 1274 it detects as binary. With -a, diff will generate a diff anyway,
1345 probably with undesirable results. 1275 probably with undesirable results.
1346 """ 1276 """
1347 node1, node2 = revpair(ui, repo, opts['rev']) 1277 node1, node2 = cmdutil.revpair(ui, repo, opts['rev'])
1348 1278
1349 fns, matchfn, anypats = cmdutil.matchpats(repo, pats, opts) 1279 fns, matchfn, anypats = cmdutil.matchpats(repo, pats, opts)
1350 1280
1351 patch.diff(repo, node1, node2, fns, match=matchfn, 1281 patch.diff(repo, node1, node2, fns, match=matchfn,
1352 opts=patch.diffopts(ui, opts)) 1282 opts=patch.diffopts(ui, opts))
1378 With the --switch-parent option, the diff will be against the second 1308 With the --switch-parent option, the diff will be against the second
1379 parent. It can be useful to review a merge. 1309 parent. It can be useful to review a merge.
1380 """ 1310 """
1381 if not changesets: 1311 if not changesets:
1382 raise util.Abort(_("export requires at least one changeset")) 1312 raise util.Abort(_("export requires at least one changeset"))
1383 revs = list(revrange(ui, repo, changesets)) 1313 revs = list(cmdutil.revrange(ui, repo, changesets))
1384 if len(revs) > 1: 1314 if len(revs) > 1:
1385 ui.note(_('exporting patches:\n')) 1315 ui.note(_('exporting patches:\n'))
1386 else: 1316 else:
1387 ui.note(_('exporting patch:\n')) 1317 ui.note(_('exporting patch:\n'))
1388 patch.export(repo, map(repo.lookup, revs), template=opts['output'], 1318 patch.export(repo, map(repo.lookup, revs), template=opts['output'],