30

I am looking for the mercurial equivalent of the solution to this question:

How do I "git blame" a deleted line?

In short, I am looking at a mercurial commit where a line was added, and in the current revision this line is no longer present, and I want to find when and why it was removed.

Community
  • 1
  • 1
undefined
  • 6,208
  • 3
  • 49
  • 59
  • Not quite a duplicate, but see [this question](http://stackoverflow.com/questions/9725856/in-mercurial-how-can-i-find-changesets-that-contain-a-string). – anton.burger May 03 '12 at 16:49
  • 3
    wonderful, this helped me get from `hg show 9876:tip path/to/file | grep --all "pattern" -U20` to `hg grep -r 9876:tip "pattern" path/to/file`, which was just what I needed. If you give that as an answer I'll accept it. Originally I tried it without `--all`, thinking that it was a substitute for the -r flag, but found that it only lists the first revision unless `--all` is specified. – undefined May 03 '12 at 17:08
  • Thanks for the offer, Brian, but I only pointed you to an existing answer which solved it. Write up a short answer and [give krtek an upvote](http://stackoverflow.com/a/9726648/715075) :) – anton.burger May 04 '12 at 08:48

2 Answers2

34

hg histgrep will let you search a change log for a pattern, such as a deleted string. If you are looking for more than just the first occurrence, be sure to include the --all flag. For me it looked something like this:

hg histgrep --all -r 9876:tip "pattern" path/to/file

Thanks to Anton for the helpful comments and krtek for his related answer.

stigi
  • 6,661
  • 3
  • 40
  • 50
undefined
  • 6,208
  • 3
  • 49
  • 59
  • 1
    `hg grep` has been renamed to `hg histgrep` in the meantime. besides that the answer is still valid. – stigi Sep 15 '15 at 21:17
  • @stigli: It has? I can't find any reference to that, even in latest Mercurial (3.8.3+). – jwd Jun 17 '16 at 21:31
  • 1
    @stigi: `hg histgrep` doesn't appear to exist in hg 4.5.3. `hg grep` still works. – ForeverWintr Feb 12 '19 at 20:50
  • 1
    @stigi Confirming `hg histgrep` is present in my version of Mercurial 4.4.2. Running `hg grep -h` shows "*For the old 'hg grep', which searches through history, see 'histgrep'.*" – Stevoisiak Apr 12 '23 at 17:30
1

hg log -p fileName > fileName.log

Then open the fileName.log file

In there you will find commit the details of each commit along with the diff of that commit. The details of the commit includes the username.

Mark
  • 1,337
  • 23
  • 34