diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -2469,7 +2469,10 @@ @util.cachefunc def binary(): flog = getfile(fn) - return util.binary(flog.read(ctx.filenode(fn))) + try: + return util.binary(flog.read(ctx.filenode(fn))) + except AttributeError: + return util.binary(ctx.filectx(fn).data()) fieldnamemap = {'filename': 'file', 'linenumber': 'line_number'} if opts.get('all'): @@ -2478,12 +2481,19 @@ iter = [('', l) for l in states] for change, l in iter: fm.startitem() - fm.data(node=fm.hexfunc(ctx.node())) - cols = [ + fm.data(node=fm.hexfunc(scmutil.binnode(ctx))) + if not bool(opts.get('all')) and not bool(opts.get('rev')): + cols = [ ('filename', fn, True), - ('rev', rev, True), + ('rev', rev, False), ('linenumber', l.linenum, opts.get('line_number')), ] + else: + cols = [ + ('filename', fn, True), + ('rev', rev, True), + ('linenumber', l.linenum, opts.get('line_number')), + ] if opts.get('all'): cols.append(('change', change, True)) cols.extend([ @@ -2568,26 +2578,46 @@ ui.pager('grep') fm = ui.formatter('grep', opts) - for ctx in cmdutil.walkchangerevs(repo, match, opts, prep): - rev = ctx.rev() - parent = ctx.p1().rev() - for fn in sorted(revfiles.get(rev, [])): - states = matches[rev][fn] - copy = copies.get(rev, {}).get(fn) - if fn in skip: - if copy: - skip[copy] = True + # This if part handles the default situation,\ + # when nothing is passed in -r or --all + if not bool(opts.get('rev')) and not bool(opts.get('all')): + rev = scmutil.revsingle(repo, opts.get('rev'), None).node() + m = scmutil.match(repo[rev], pats, opts, default='relglob') + m.bad = lambda x, y: False + ctx = repo[rev] + ds = ctx.repo().dirstate + + for fn in ctx.matches(m): + if rev is None and ds[fn] == 'r': continue - pstates = matches.get(parent, {}).get(copy or fn, []) - if pstates or states: - r = display(fm, fn, ctx, pstates, states) + data = ctx.filectx(fn).data() + states = [] + for lnum, cstart, cend, line in matchlines(data): + states.append(linestate(line, lnum, cstart, cend)) + if states: + r = display(fm, fn, ctx, [], states) found = found or r - if r and not opts.get('all'): - skip[fn] = True + else : + for ctx in cmdutil.walkchangerevs(repo, match, opts, prep): + rev = ctx.rev() + parent = ctx.p1().rev() + for fn in sorted(revfiles.get(rev, [])): + states = matches[rev][fn] + copy = copies.get(rev, {}).get(fn) + if fn in skip: if copy: skip[copy] = True - del matches[rev] - del revfiles[rev] + continue + pstates = matches.get(parent, {}).get(copy or fn, []) + if pstates or states: + r = display(fm, fn, ctx, pstates, states) + found = found or r + if r and not opts.get('all'): + skip[fn] = True + if copy: + skip[copy] = True + del matches[rev] + del revfiles[rev] fm.end() return not found diff --git a/tests/test-grep.t b/tests/test-grep.t --- a/tests/test-grep.t +++ b/tests/test-grep.t @@ -340,4 +340,25 @@ $ hg grep "MaCam" --all binfile.bin:0:+: Binary file matches - $ cd .. +Test that grep searches only on working directory + $ cd .. + $ hg init t5 + $ cd t5 + $ echo "mercurial revsets are awesome" > firstfile + $ hg add firstfile + $ hg commit -m 'adds firstfile' + $ hg rm firstfile + $ hg commit -m 'removes firstfile' + $ echo "mercurial revsets are awesome and makes life easier" > secondfile + $ echo "some generic text" > thirdfile + $ hg add secondfile thirdfile + $ hg commit -m 'adds two new files' + $ hg grep 'revsets' + secondfile:None:mercurial revsets are awesome and makes life easier + $ echo "another generic string" > fourthone + +Search on added but not commit i.e dirty working directory + $ hg add fourthone + $ hg grep "generic" + fourthone:another generic string + thirdfile:some generic text