comparison mercurial/commands.py @ 627:b6c42714d900

Add locate command. # HG changeset patch # User Bryan O'Sullivan <bos@serpentine.com> # Node ID ebf5eba347a17a5c09000b3342caf350cd060a1b # Parent d2994b5298fb20f87dc1d4747635b280db3c0526 Add locate command. Used for finding files with names that match specific patterns, such as "*.c". This patch also introduces localrepository.getcwd, which returns the current directory relative to the repository root.
author Bryan O'Sullivan <bos@serpentine.com>
date Tue, 05 Jul 2005 18:19:01 -0800
parents 978011cf5279
children 8d7f6e68828a
comparison
equal deleted inserted replaced
626:47207ec19c51 627:b6c42714d900
6 # of the GNU General Public License, incorporated herein by reference. 6 # of the GNU General Public License, incorporated herein by reference.
7 7
8 from demandload import * 8 from demandload import *
9 demandload(globals(), "os re sys signal") 9 demandload(globals(), "os re sys signal")
10 demandload(globals(), "fancyopts ui hg util") 10 demandload(globals(), "fancyopts ui hg util")
11 demandload(globals(), "hgweb mdiff random signal time traceback") 11 demandload(globals(), "fnmatch hgweb mdiff random signal time traceback")
12 demandload(globals(), "errno socket version struct") 12 demandload(globals(), "errno socket version struct")
13 13
14 class UnknownCommand(Exception): pass 14 class UnknownCommand(Exception): pass
15 15
16 def filterfiles(filters, files): 16 def filterfiles(filters, files):
629 if source: 629 if source:
630 ui.warn("no longer supported: use \"hg clone\" instead\n") 630 ui.warn("no longer supported: use \"hg clone\" instead\n")
631 sys.exit(1) 631 sys.exit(1)
632 repo = hg.repository(ui, ".", create=1) 632 repo = hg.repository(ui, ".", create=1)
633 633
634 def locate(ui, repo, *pats, **opts):
635 """locate files matching specific patterns"""
636 if [p for p in pats if os.sep in p]:
637 ui.warn("error: patterns may not contain '%s'\n" % os.sep)
638 ui.warn("use '-i <dir>' instead\n")
639 sys.exit(1)
640 def compile(pats, head = '^', tail = os.sep, on_empty = True):
641 if not pats:
642 class c:
643 def match(self, x): return on_empty
644 return c()
645 regexp = r'%s(?:%s)%s' % (
646 head,
647 '|'.join([fnmatch.translate(os.path.normpath(os.path.normcase(p)))[:-1]
648 for p in pats]),
649 tail)
650 print regexp
651 return re.compile(regexp)
652 exclude = compile(opts['exclude'], on_empty = False)
653 include = compile(opts['include'])
654 pat = compile([os.path.normcase(p) for p in pats], head = '', tail = '$')
655 end = '\n'
656 if opts['print0']: end = '\0'
657 if opts['rev']: node = repo.manifest.lookup(opts['rev'])
658 else: node = repo.manifest.tip()
659 manifest = repo.manifest.read(node)
660 cwd = repo.getcwd()
661 cwd_plus = cwd and (cwd + os.sep)
662 found = []
663 for f in manifest:
664 f = os.path.normcase(f)
665 if exclude.match(f) or not(include.match(f) and
666 f.startswith(cwd_plus) and
667 pat.match(os.path.basename(f))): continue
668 if opts['fullpath']: f = os.path.join(repo.root, f)
669 elif cwd: f = f[len(cwd_plus):]
670 found.append(f)
671 found.sort()
672 for f in found: ui.write(f, end)
673
634 def log(ui, repo, f=None, **opts): 674 def log(ui, repo, f=None, **opts):
635 """show the revision history of the repository or a single file""" 675 """show the revision history of the repository or a single file"""
636 if f: 676 if f:
637 files = relpath(repo, [f]) 677 files = relpath(repo, [f])
638 filelog = repo.file(files[0]) 678 filelog = repo.file(files[0])
1035 "import|patch": (import_, 1075 "import|patch": (import_,
1036 [('p', 'strip', 1, 'path strip'), 1076 [('p', 'strip', 1, 'path strip'),
1037 ('b', 'base', "", 'base path')], 1077 ('b', 'base', "", 'base path')],
1038 "hg import [options] <patches>"), 1078 "hg import [options] <patches>"),
1039 "^init": (init, [], 'hg init'), 1079 "^init": (init, [], 'hg init'),
1080 "locate": (locate,
1081 [('0', 'print0', None, 'end records with NUL'),
1082 ('f', 'fullpath', None, 'print complete paths'),
1083 ('i', 'include', [], 'include path in search'),
1084 ('r', 'rev', '', 'revision'),
1085 ('x', 'exclude', [], 'exclude path from search')],
1086 'hg locate [options] [files]'),
1040 "^log|history": (log, 1087 "^log|history": (log,
1041 [('r', 'rev', [], 'revision'), 1088 [('r', 'rev', [], 'revision'),
1042 ('p', 'patch', None, 'show patch')], 1089 ('p', 'patch', None, 'show patch')],
1043 'hg log [-r A] [-r B] [-p] [file]'), 1090 'hg log [-r A] [-r B] [-p] [file]'),
1044 "manifest": (manifest, [], 'hg manifest [rev]'), 1091 "manifest": (manifest, [], 'hg manifest [rev]'),