~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to grep.py

  • Committer: Parth Malwankar
  • Date: 2010-02-24 13:39:52 UTC
  • mto: (0.44.2 grep) (6531.3.1 merge-grep)
  • mto: This revision was merged to the branch mainline in revision 6555.
  • Revision ID: parth.malwankar@gmail.com-20100224133952-2sc2ebe1rct1pryq
added support for --line-number.
improved tests to check for long as well as short option.

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
    return patternc
41
41
 
42
42
 
43
 
def file_grep(tree, id, relpath, path, patternc, eol_marker, outf):
44
 
    index = 1
 
43
def file_grep(tree, id, relpath, path, patternc, eol_marker, outf, line_number=True):
45
44
    if relpath:
46
45
        path = osutils.normpath(osutils.pathjoin(relpath, path))
47
46
        path = path.replace('\\', '/')
48
47
        path = path.replace(relpath + '/', '', 1)
49
 
    fmt = path + ":%d:%s" + eol_marker
 
48
    fmt_with_n = path + ":%d:%s" + eol_marker
 
49
    fmt_without_n = path + ":%s" + eol_marker
50
50
 
 
51
    index = 1
51
52
    for line in tree.get_file_lines(id):
52
53
        res = patternc.search(line)
53
54
        if res:
54
 
            outf.write( fmt % (index, line.strip()))
 
55
            if line_number:
 
56
                outf.write(fmt_with_n % (index, line.strip()))
 
57
            else:
 
58
                outf.write(fmt_without_n % (line.strip(),))
55
59
        index += 1
56
60
 
57
61