comparison mercurial/commands.py @ 1145:bd917e1a26dd

grep: change default to printing first matching rev. Printing of every matching rev remains via --every-match/-e switch.
author Bryan O'Sullivan <bos@serpentine.com>
date Mon, 29 Aug 2005 08:24:06 -0700
parents 0cdd73b0767c
children 9061f79c6c6f
comparison
equal deleted inserted replaced
1125:a33a7a543803 1145:bd917e1a26dd
871 871
872 prev = {} 872 prev = {}
873 def display(fn, rev, states, prevstates): 873 def display(fn, rev, states, prevstates):
874 diff = list(sets.Set(states).symmetric_difference(sets.Set(prevstates))) 874 diff = list(sets.Set(states).symmetric_difference(sets.Set(prevstates)))
875 diff.sort(lambda x, y: cmp(x.linenum, y.linenum)) 875 diff.sort(lambda x, y: cmp(x.linenum, y.linenum))
876 counts = {'-': 0, '+': 0}
876 for l in diff: 877 for l in diff:
877 if incrementing: 878 if incrementing or not opts['every_match']:
878 change = ((l in prevstates) and '-') or '+' 879 change = ((l in prevstates) and '-') or '+'
879 r = rev 880 r = rev
880 else: 881 else:
881 change = ((l in states) and '-') or '+' 882 change = ((l in states) and '-') or '+'
882 r = prev[fn] 883 r = prev[fn]
883 ui.write('%s:%s:%s:%s%s\n' % (fn, r, l.linenum, change, l.line)) 884 ui.write('%s:%s:%s:%s%s\n' % (fn, r, l.linenum, change, l.line))
885 counts[change] += 1
886 return counts['+'], counts['-']
884 887
885 fstate = {} 888 fstate = {}
889 skip = {}
886 for st, rev, fns in walkchangerevs(ui, repo, repo.getcwd(), pats, opts): 890 for st, rev, fns in walkchangerevs(ui, repo, repo.getcwd(), pats, opts):
887 if st == 'window': 891 if st == 'window':
888 incrementing = rev 892 incrementing = rev
889 matches.clear() 893 matches.clear()
890 elif st == 'add': 894 elif st == 'add':
891 change = repo.changelog.read(repo.lookup(str(rev))) 895 change = repo.changelog.read(repo.lookup(str(rev)))
892 mf = repo.manifest.read(change[0]) 896 mf = repo.manifest.read(change[0])
893 matches[rev] = {} 897 matches[rev] = {}
894 for fn in fns: 898 for fn in fns:
899 if fn in skip: continue
895 fstate.setdefault(fn, {}) 900 fstate.setdefault(fn, {})
896 try: 901 try:
897 grepbody(fn, rev, getfile(fn).read(mf[fn])) 902 grepbody(fn, rev, getfile(fn).read(mf[fn]))
898 except KeyError: 903 except KeyError:
899 pass 904 pass
900 elif st == 'iter': 905 elif st == 'iter':
901 states = matches[rev].items() 906 states = matches[rev].items()
902 states.sort() 907 states.sort()
903 for fn, m in states: 908 for fn, m in states:
904 if incrementing or fstate[fn]: 909 if fn in skip: continue
905 display(fn, rev, m, fstate[fn]) 910 if incrementing or not opts['every_match'] or fstate[fn]:
911 pos, neg = display(fn, rev, m, fstate[fn])
912 if pos and not opts['every_match']:
913 skip[fn] = True
906 fstate[fn] = m 914 fstate[fn] = m
907 prev[fn] = rev 915 prev[fn] = rev
908 916
909 if not incrementing: 917 if not incrementing:
910 fstate = fstate.items() 918 fstate = fstate.items()
911 fstate.sort() 919 fstate.sort()
912 for fn, state in fstate: 920 for fn, state in fstate:
921 if fn in skip: continue
913 display(fn, rev, {}, state) 922 display(fn, rev, {}, state)
914 923
915 def heads(ui, repo, **opts): 924 def heads(ui, repo, **opts):
916 """show current repository heads""" 925 """show current repository heads"""
917 heads = repo.changelog.heads() 926 heads = repo.changelog.heads()
1563 "grep": 1572 "grep":
1564 (grep, 1573 (grep,
1565 [('0', 'print0', None, 'end filenames with NUL'), 1574 [('0', 'print0', None, 'end filenames with NUL'),
1566 ('I', 'include', [], 'include path in search'), 1575 ('I', 'include', [], 'include path in search'),
1567 ('X', 'exclude', [], 'include path in search'), 1576 ('X', 'exclude', [], 'include path in search'),
1577 ('e', 'every-match', None, 'print every match in file history'),
1568 ('i', 'ignore-case', None, 'ignore case when matching'), 1578 ('i', 'ignore-case', None, 'ignore case when matching'),
1569 ('l', 'files-with-matches', None, 'print names of files with matches'), 1579 ('l', 'files-with-matches', None, 'print names of files with matches'),
1570 ('n', 'line-number', '', 'print line numbers'), 1580 ('n', 'line-number', '', 'print line numbers'),
1571 ('r', 'rev', [], 'search in revision rev')], 1581 ('r', 'rev', [], 'search in revision rev')],
1572 "hg grep [OPTION]... PATTERN [FILE]..."), 1582 "hg grep [OPTION]... PATTERN [FILE]..."),