Use hg grep --diff PATTERN [FILE]...
to search in diffs, and specify a template to print the changeset specifically.
This answer is no more than an update of @krtek's excellent answer. It contains a few examples more, and it uses the modern --diff
flag rather than the --all
flag, which was deprecated since krtek answered. Both flags do the same thing: instruct hg grep
to search all diffs for the given pattern.
Here are three example invocations and outputs. (The script to create the example repository is at the end of this post.)
Ordinary grep
$ hg grep --diff Foo
myfile:2:+:Dim Foo as integer
myfile:1:-:Dim Foo as integer
myfile:0:+:Dim Foo as integer
Use a template to extract commits' revision numbers and hashes
$ hg grep --diff Foo -T '{rev}:{node|short}\n'
2:2c425adcc0b1
1:5a46fef3be7c
0:b8d4a0cc48a5
See all available data/templating fields
$ hg grep --diff Foo -T json
[
{
"change": "+",
"date": [1662367225, -7200],
"lineno": 1,
"node": "2c425adcc0b1fdcdea5ab8c0a864ce0af4a80478",
"path": "myfile",
"rev": 2,
"texts": [{"matched": false, "text": "Dim "}, {"matched": true, "text": "Foo"}, {"matched": false, "text": " as integer"}],
"user": "Lady MacBeth <[email protected]>"
},
// ...
]
Commands to set up the example repository, used in the examples above:
# Create the repository
hg init
touch myfile
hg add myfile
# Create 3 commits
echo "Dim Foo as integer" > myfile
hg commit -m "1"
echo "Tomorrow, and tomorrow, and tomorrow" > myfile
hg commit -m "2"
echo "Dim Foo as integer" > myfile
hg commit -m "3"