Mercurial > public > mercurial-scm > hg
comparison mercurial/scmutil.py @ 14319:b33f3e35efb0
scmutil: move revsingle/pair/range from cmdutil
This allows users at levels below the command layer to avoid import loops.
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Fri, 13 May 2011 14:06:28 -0500 |
parents | e3649bcca3f6 |
children | 3438417a6657 |
comparison
equal
deleted
inserted
replaced
14318:1f46be4689ed | 14319:b33f3e35efb0 |
---|---|
4 # | 4 # |
5 # This software may be used and distributed according to the terms of the | 5 # This software may be used and distributed according to the terms of the |
6 # GNU General Public License version 2 or any later version. | 6 # GNU General Public License version 2 or any later version. |
7 | 7 |
8 from i18n import _ | 8 from i18n import _ |
9 import util, error, osutil | 9 import util, error, osutil, revset |
10 import os, errno, stat, sys | 10 import os, errno, stat, sys |
11 | 11 |
12 def checkfilename(f): | 12 def checkfilename(f): |
13 '''Check that the filename f is an acceptable filename for a tracked file''' | 13 '''Check that the filename f is an acceptable filename for a tracked file''' |
14 if '\r' in f or '\n' in f: | 14 if '\r' in f or '\n' in f: |
461 userprofile = os.environ.get('USERPROFILE') | 461 userprofile = os.environ.get('USERPROFILE') |
462 if userprofile: | 462 if userprofile: |
463 path.append(os.path.join(userprofile, 'mercurial.ini')) | 463 path.append(os.path.join(userprofile, 'mercurial.ini')) |
464 path.append(os.path.join(userprofile, '.hgrc')) | 464 path.append(os.path.join(userprofile, '.hgrc')) |
465 return path | 465 return path |
466 | |
467 def revsingle(repo, revspec, default='.'): | |
468 if not revspec: | |
469 return repo[default] | |
470 | |
471 l = revrange(repo, [revspec]) | |
472 if len(l) < 1: | |
473 raise util.Abort(_('empty revision set')) | |
474 return repo[l[-1]] | |
475 | |
476 def revpair(repo, revs): | |
477 if not revs: | |
478 return repo.dirstate.p1(), None | |
479 | |
480 l = revrange(repo, revs) | |
481 | |
482 if len(l) == 0: | |
483 return repo.dirstate.p1(), None | |
484 | |
485 if len(l) == 1: | |
486 return repo.lookup(l[0]), None | |
487 | |
488 return repo.lookup(l[0]), repo.lookup(l[-1]) | |
489 | |
490 _revrangesep = ':' | |
491 | |
492 def revrange(repo, revs): | |
493 """Yield revision as strings from a list of revision specifications.""" | |
494 | |
495 def revfix(repo, val, defval): | |
496 if not val and val != 0 and defval is not None: | |
497 return defval | |
498 return repo.changelog.rev(repo.lookup(val)) | |
499 | |
500 seen, l = set(), [] | |
501 for spec in revs: | |
502 # attempt to parse old-style ranges first to deal with | |
503 # things like old-tag which contain query metacharacters | |
504 try: | |
505 if isinstance(spec, int): | |
506 seen.add(spec) | |
507 l.append(spec) | |
508 continue | |
509 | |
510 if _revrangesep in spec: | |
511 start, end = spec.split(_revrangesep, 1) | |
512 start = revfix(repo, start, 0) | |
513 end = revfix(repo, end, len(repo) - 1) | |
514 step = start > end and -1 or 1 | |
515 for rev in xrange(start, end + step, step): | |
516 if rev in seen: | |
517 continue | |
518 seen.add(rev) | |
519 l.append(rev) | |
520 continue | |
521 elif spec and spec in repo: # single unquoted rev | |
522 rev = revfix(repo, spec, None) | |
523 if rev in seen: | |
524 continue | |
525 seen.add(rev) | |
526 l.append(rev) | |
527 continue | |
528 except error.RepoLookupError: | |
529 pass | |
530 | |
531 # fall through to new-style queries if old-style fails | |
532 m = revset.match(repo.ui, spec) | |
533 for r in m(repo, range(len(repo))): | |
534 if r not in seen: | |
535 l.append(r) | |
536 seen.update(l) | |
537 | |
538 return l |