Finding first appearance of text in Mercurial repository
Asked Answered
H

4

23

I have a Mercurial repository with ~800 changesets and I need to find the first changeset where the word Example appeared. The word appears inside a .php file and not on a commit comment etc.

What is the quickest/easiest way to do that?

Hazelwood answered 8/3, 2012 at 13:59 Comment(0)
W
25

try hg grep Example *.php

hg grep [OPTION]... PATTERN [FILE]...

search for a pattern in specified files and revisions

    Search revisions of files for a regular expression.

    This command behaves differently than Unix grep. It only
    accepts Python/Perl regexps. It searches repository
    history, not the working directory. It always prints the
    revision number in which a match appears.

    By default, grep only prints output for the first
    revision of a file in which it finds a match. To get it
    to print every revision that contains a change in match
    status ("-" for a match that becomes a non-match, or "+"
    for a non-match that becomes a match), use the --all
    flag.

options:

 -0 --print0              end fields with NUL
    --all                 print all revisions that match
 -f --follow              follow changeset history, or file
                          history across copies and renames
 -i --ignore-case         ignore case when matching
 -l --files-with-matches  print only filenames and revisions
                          that match
 -n --line-number         print matching line numbers
 -r --rev                 search in given revision range
 -u --user                list the author (long with -v)
 -d --date                list the date (short with -q)
 -I --include             include names matching the given
                          patterns
 -X --exclude             exclude names matching the given
                          patterns

use "hg -v help grep" to show global options
Winna answered 8/3, 2012 at 14:13 Comment(2)
It searches repository history, not the working directory Note that this is only true if you don't specify a file extension. The example provided will only look in .php files in the current working directory.Loathly
This finds the last occurrence of the pattern, not the first. How do you make this search "backwards" (i.e. forwards), as requested? To be clear, although the doc says it reports "the first revision of a file in which it finds a match," it does so while searching backwards from the present—which is usually what you want. But not in this case, if I understand the OP.Armpit
C
10

The selected answer is incomplete:

hg grep --all --files-with-matches 'PATTERN' [FILES]

is normally what you want.

Chivalric answered 30/6, 2016 at 16:38 Comment(0)
R
1

You would want to use the --diff (--all is deprecated) flag of hg grep. It searches the diffs rather than file contents itself, what this would result in is, you would get all the changesets/revisions where the word Example appeared or removed.

Now to get the first hit you need to pass this in revlog order via the -r flag. That is the revisions will be searched from 0 to tip. ( -r 0:tip )

And for the .php files you would want to pass -I flag which is for file name patterns.

So your command will be :

hg grep --all -r 0:tip "Example" -I "*.php"
Referendum answered 15/8, 2018 at 19:30 Comment(1)
There was a bug earlier in the codebase which produced incorrect results when the revisions were passed in revlog order. I had corrected this behaviour as part of my GSoC project.Referendum
D
0
hg help filesets
...
"grep(regex)"
      File contains the given regular expression.

hg locate "set:grep(Example) and **.php"

or

hg locate "set:**.php and (**Example*)"

Dandiprat answered 8/3, 2012 at 14:34 Comment(2)
hg help locate is telling me that By default, this command searches all directories in the working directory. so no changesets will be searched.Winna
@Winna - fileset give fileset, for which we do log. hg log hg locate ...Dandiprat

© 2022 - 2024 — McMap. All rights reserved.