[PATCH 5 of 9 hglib] client: add grep command
Idan Kamara
idankk86 at gmail.com
Fri Aug 19 12:15:26 CDT 2011
# HG changeset patch
# User Idan Kamara <idankk86 at gmail.com>
# Date 1313773693 -10800
# Node ID 9bd819da245aa135591d29426f000af997fc5010
# Parent 5833f6ac0929e53cc3cd085ecf8d19ebb688007e
client: add grep command
diff -r 5833f6ac0929 -r 9bd819da245a hglib/client.py
--- a/hglib/client.py Fri Aug 19 20:08:13 2011 +0300
+++ b/hglib/client.py Fri Aug 19 20:08:13 2011 +0300
@@ -396,6 +396,44 @@
return bool(eh)
+ def grep(self, pattern, files=[], all=False, text=False, follow=False,
+ ignorecase=False, fileswithmatches=False, line=False, user=False,
+ date=False, include=None, exclude=None):
+ """
+ search for a pattern in specified files and revisions
+
+ yields (filename, revision, [line, [match status, [user, [date, [match]]]]])
+ per match depending on the given options.
+ """
+ if not isinstance(files, list):
+ files = [files]
+
+ args = cmdbuilder('grep', *[pattern] + files, all=all, a=text, f=follow,
+ i=ignorecase, l=fileswithmatches, n=line, u=user, d=date,
+ I=include, X=exclude)
+ args.append('-0')
+
+ def eh(ret, out, err):
+ if ret != 1:
+ raise error.CommandError(args, ret, out, err)
+ return ''
+
+ out = self.rawcommand(args, eh=eh).split('\0')
+
+ fieldcount = 3
+ if user:
+ fieldcount += 1
+ if date:
+ fieldcount += 1
+ if line:
+ fieldcount += 1
+ if all:
+ fieldcount += 1
+ if fileswithmatches:
+ fieldcount -= 1
+
+ return util.grouper(fieldcount, out)
+
def diff(self, files=[], revs=[], change=None, text=False,
git=False, nodates=False, showfunction=False, reverse=False,
ignoreallspace=False, ignorespacechange=False, ignoreblanklines=False,
diff -r 5833f6ac0929 -r 9bd819da245a tests/test-grep.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-grep.py Fri Aug 19 20:08:13 2011 +0300
@@ -0,0 +1,38 @@
+import common
+
+class test_grep(common.basetest):
+ def test_basic(self):
+ self.append('a', 'a\n')
+ self.append('b', 'ab\n')
+ self.client.commit('first', addremove=True)
+
+ # no match
+ self.assertEquals(list(self.client.grep('c')), [])
+
+ self.assertEquals(list(self.client.grep('a')),
+ [('a', '0', 'a'), ('b', '0', 'ab')])
+ self.assertEquals(list(self.client.grep('a', 'a')), [('a', '0', 'a')])
+
+ self.assertEquals(list(self.client.grep('b')), [('b', '0', 'ab')])
+
+ def test_options(self):
+ self.append('a', 'a\n')
+ self.append('b', 'ab\n')
+ rev, node = self.client.commit('first', addremove=True)
+
+ self.assertEquals([('a', '0', '+', 'a'), ('b', '0', '+', 'ab')],
+ list(self.client.grep('a', all=True)))
+
+ self.assertEquals([('a', '0'), ('b', '0')],
+ list(self.client.grep('a', fileswithmatches=True)))
+
+ self.assertEquals([('a', '0', '1', 'a'), ('b', '0', '1', 'ab')],
+ list(self.client.grep('a', line=True)))
+
+ self.assertEquals([('a', '0', 'test', 'a'), ('b', '0', 'test', 'ab')],
+ list(self.client.grep('a', user=True)))
+
+ self.assertEquals([('a', '0', '1', '+', 'test'),
+ ('b', '0', '1', '+', 'test')],
+ list(self.client.grep('a', all=True, user=True, line=True,
+ fileswithmatches=True)))
More information about the Mercurial-devel
mailing list