##// END OF EJS Templates
grep: add --follow support.
Brendan Cully -
r2870:8eaaf132 default
parent child Browse files
Show More
@@ -1649,6 +1649,7 b' def grep(ui, repo, pattern, *pats, **opt'
1649 return self.line == other.line
1649 return self.line == other.line
1650
1650
1651 matches = {}
1651 matches = {}
1652 copies = {}
1652 def grepbody(fn, rev, body):
1653 def grepbody(fn, rev, body):
1653 matches[rev].setdefault(fn, [])
1654 matches[rev].setdefault(fn, [])
1654 m = matches[rev][fn]
1655 m = matches[rev][fn]
@@ -1709,10 +1710,12 b' def grep(ui, repo, pattern, *pats, **opt'
1709 changeiter, getchange, matchfn = walkchangerevs(ui, repo, pats, opts)
1710 changeiter, getchange, matchfn = walkchangerevs(ui, repo, pats, opts)
1710 count = 0
1711 count = 0
1711 incrementing = False
1712 incrementing = False
1713 follow = opts.get('follow')
1712 for st, rev, fns in changeiter:
1714 for st, rev, fns in changeiter:
1713 if st == 'window':
1715 if st == 'window':
1714 incrementing = rev
1716 incrementing = rev
1715 matches.clear()
1717 matches.clear()
1718 copies.clear()
1716 elif st == 'add':
1719 elif st == 'add':
1717 change = repo.changelog.read(repo.lookup(str(rev)))
1720 change = repo.changelog.read(repo.lookup(str(rev)))
1718 mf = repo.manifest.read(change[0])
1721 mf = repo.manifest.read(change[0])
@@ -1721,22 +1724,34 b' def grep(ui, repo, pattern, *pats, **opt'
1721 if fn in skip:
1724 if fn in skip:
1722 continue
1725 continue
1723 fstate.setdefault(fn, {})
1726 fstate.setdefault(fn, {})
1727 copies.setdefault(rev, {})
1724 try:
1728 try:
1725 grepbody(fn, rev, getfile(fn).read(mf[fn]))
1729 grepbody(fn, rev, getfile(fn).read(mf[fn]))
1730 if follow:
1731 copied = getfile(fn).renamed(mf[fn])
1732 if copied:
1733 copies[rev][fn] = copied[0]
1726 except KeyError:
1734 except KeyError:
1727 pass
1735 pass
1728 elif st == 'iter':
1736 elif st == 'iter':
1729 states = matches[rev].items()
1737 states = matches[rev].items()
1730 states.sort()
1738 states.sort()
1731 for fn, m in states:
1739 for fn, m in states:
1740 copy = copies[rev].get(fn)
1732 if fn in skip:
1741 if fn in skip:
1742 if copy:
1743 skip[copy] = True
1733 continue
1744 continue
1734 if incrementing or not opts['all'] or fstate[fn]:
1745 if incrementing or not opts['all'] or fstate[fn]:
1735 pos, neg = display(fn, rev, m, fstate[fn])
1746 pos, neg = display(fn, rev, m, fstate[fn])
1736 count += pos + neg
1747 count += pos + neg
1737 if pos and not opts['all']:
1748 if pos and not opts['all']:
1738 skip[fn] = True
1749 skip[fn] = True
1750 if copy:
1751 skip[copy] = True
1739 fstate[fn] = m
1752 fstate[fn] = m
1753 if copy:
1754 fstate[copy] = m
1740 prev[fn] = rev
1755 prev[fn] = rev
1741
1756
1742 if not incrementing:
1757 if not incrementing:
@@ -1745,7 +1760,8 b' def grep(ui, repo, pattern, *pats, **opt'
1745 for fn, state in fstate:
1760 for fn, state in fstate:
1746 if fn in skip:
1761 if fn in skip:
1747 continue
1762 continue
1748 display(fn, rev, {}, state)
1763 if fn not in copies[prev[fn]]:
1764 display(fn, rev, {}, state)
1749 return (count == 0 and 1) or 0
1765 return (count == 0 and 1) or 0
1750
1766
1751 def heads(ui, repo, **opts):
1767 def heads(ui, repo, **opts):
@@ -3107,6 +3123,8 b' table = {'
3107 (grep,
3123 (grep,
3108 [('0', 'print0', None, _('end fields with NUL')),
3124 [('0', 'print0', None, _('end fields with NUL')),
3109 ('', 'all', None, _('print all revisions that match')),
3125 ('', 'all', None, _('print all revisions that match')),
3126 ('f', 'follow', None,
3127 _('follow changeset history, or file history across copies and renames')),
3110 ('i', 'ignore-case', None, _('ignore case when matching')),
3128 ('i', 'ignore-case', None, _('ignore case when matching')),
3111 ('l', 'files-with-matches', None,
3129 ('l', 'files-with-matches', None,
3112 _('print only filenames and revs that match')),
3130 _('print only filenames and revs that match')),
@@ -20,3 +20,11 b" hg commit -m 4 -u spam -d '4 0'"
20 hg grep port port
20 hg grep port port
21 hg grep --all -nu port port
21 hg grep --all -nu port port
22 hg grep import port
22 hg grep import port
23
24 hg cp port port2
25 hg commit -m 4 -u spam -d '5 0'
26 echo '% follow'
27 hg grep -f 'import$' port2
28 echo deport >> port2
29 hg commit -m 5 -u eggs -d '6 0'
30 hg grep -f --all -nu port port2
@@ -11,3 +11,15 b' port:2:3:+:spam:import/export'
11 port:1:2:+:eggs:export
11 port:1:2:+:eggs:export
12 port:0:1:+:eggs:import
12 port:0:1:+:eggs:import
13 port:4:import/export
13 port:4:import/export
14 % follow
15 port:0:import
16 port2:6:4:+:eggs:deport
17 port:4:4:-:spam:import/export
18 port:3:4:+:eggs:import/export
19 port:2:1:-:spam:import
20 port:2:2:-:spam:export
21 port:2:1:+:spam:export
22 port:2:2:+:spam:vaportight
23 port:2:3:+:spam:import/export
24 port:1:2:+:eggs:export
25 port:0:1:+:eggs:import
General Comments 0
You need to be logged in to leave comments. Login now