Mercurial - how to see the history for a specific line of code
Asked Answered
R

4

8

I have a CSS file with thousands of lines of code. I want to see when a specific line/chunk of code was changed, without going back and reviewing each revision that changed this file (that will take a looooong time!)

Is there a way, using either TortoiseHg, Eclipse with a Mercurial plugin, or command-line, to view the history of a specific piece of code?

Rigney answered 25/11, 2012 at 8:46 Comment(0)
C
21

The correct answer is hg grep (Mercurial grep page).

More deep:

hg grep --all "PATTERN" FILENAME

Sample output:

>hg grep --all "textdomain" functions.php
functions.php:2:-:load_theme_textdomain('fiver', get_template_directory() . '/translation');
functions.php:2:+:load_theme_textdomain('fiver', get_template_directory() . '/languages');
functions.php:1:+:load_theme_textdomain('fiver', get_template_directory() . '/translation');

(in order - filename, revision, action, string in this revision)

Campney answered 25/11, 2012 at 9:52 Comment(1)
I don't think this is quite a correct answer, despite it being accepted. This will search for the occurrence of a certain pattern in a file, throughout history. It will not give you the history of a specific line in a file, unless there just happens to only be one line that matches that pattern throughout history. hg annotate gives the right information, though you still have to manually leapfrog back through history to get the full chain of events.Risley
D
13

You can use:

hg annotate <file>

to find out in which revision line was changed and then use same command with -r <revision> at the end to go backwards through revisions.

Discuss answered 25/11, 2012 at 9:5 Comment(1)
Blame show only: 1. most recent revision of change 2. can't show info for currently deleted stringsCampney
E
1

I don't think there is an option to view a specific part of a file. But to see the differences of the total file over several revisions you can use hg diff:

hg diff -r firstrevisionnumber:otherrevnumber filename

For example, hg diff -r 0:8 screen.css

Or the command hg log screen.css.

Ezequieleziechiele answered 25/11, 2012 at 8:55 Comment(1)
this is good to know, but does not help for this particular situation, as i have no idea of the range of revisions to look in, and we have almost 3,000 revisions in the history so far.Rigney
D
0

Use hg histgrep --all PATTERN FILENAME (used to be hg grep in the older versions, and that doesn't work anymore)

Diatomaceous answered 24/3, 2021 at 4:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.