Mercurial > python-hglib
changeset 56:9bd819da245a
client: add grep command
author | Idan Kamara <idankk86@gmail.com> |
---|---|
date | Fri, 19 Aug 2011 20:08:13 +0300 |
parents | 5833f6ac0929 |
children | 2657fd6fef04 |
files | hglib/client.py tests/test-grep.py |
diffstat | 2 files changed, 76 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- 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,
--- /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)))