How to view revision history for Mercurial file? - mercurial

For a given file in a Mercurial repository, how can you see the revision history?
And how can you diff two revisions of the file?
Ideally doing all this with visual tools (we use ExamDiff to do some other diffs).
I'd say this is basic source control functionality but I can't seem to figure out how to do this with Mercurial.

hg log file
hg diff -r 10 -r 20 file

The hgk extension gives you hg view file command that shows a visual history, from which you can diff/vdiff arbitrary pair of revisions.
TortoiseHg gives you thg log file command that does the same thing but looks better.

For readability
hg diff -r revision1:revision2 file
Where revision1 and revision2 can be a tag, changeset etc.

If you use TortoiseHg:
Windows users can use Windows Explorer and view the revision history by right-clicking on the file.
For Linux users, you can do it within TortoiseHg but it took me a while to figure out how. You need to right-click on the desired file and select "File History". However, for some mysterious reason, the file needs to be unaltered. Furthermore, to find the desired file there are two options:
In ### revision set query### one can type:
file("**<myfile>")
The double ** are necessary to search directories recursively. This gives you immediately an list of all repositories in which the desired file was changed.
Alternatively, next to the ### filter text ### click first on the question mark sign and select "clean" to see all files in the repository. Then inside the ### filter text ### box you can narrow down the number of files shown.
Alternatively, Linux users can do it from a terminal as suggested by Geoffrey Zheng above:
thg log file

Related

What does thg drag_move do? How is it different from hg add and hg remove?

As a user of TortoiseHG, I have been wondering how the thg drag_move command works which mimics (or implements) the functionality behind the "HG Move versioned item(s) here..." context menu entry.
The help entry obtained via thg -v help drag_move gives:
thg drag_move SOURCE... DEST
move the selected files to the desired directory
As far as I understand, it must execute the following sequence (or similar) under the hood:
hg forget SOURCE
mv SOURCE DEST
hg add DEST
But if I do that manually (outside of TortoiseHG), it (naturally) doesn't associate DEST with SOURCE, it rather treats them as individual, unrelated changes.
If I use the GUI or the thg drag_move command, it keeps the history of these entries the same, at least within TortoiseHG I can still see SOURCE's history when I look at DEST's history after the move.
Is this a TortoiseHG extension to Mercurial or is there a hg option to achieve the same "natively"?
TLDR: You can use hg move OLD NEW to achieve the same functionality as thg drag_move.
Thg is using hg move under the hood when it's doing drag_move (see the run.py and quickop.py sources for details).
Hg move is the right way of renaming or moving a file in Mercurial because it's tracking the operation, as it says in his help message (hg help mv):
rename files; equivalent of copy + remove
Mark dest as copies of sources; mark sources for deletion. If dest is a
directory, copies are put in that directory. If dest is a file, there can
only be one source.

Hg: How to move files (to subfolder) without losing history?

How to move files (to subfolder) without losing their history?
In my Mercurial repository (I mean the folder with the .hg in it) I have MyProject/ folder with all project files. Now I need to create src/ folder inside and move all files to it (from MyProject/ to MyProject/src/). How can I do it without losing all history?
Since you have a "tortoisehg" tag, I figured I'd explain the way I do this using the GUI.
Usually, I just rename/move files in my IDE, or from windows explorer, then when I go to commit, THG will show a bunch of (?) unknown files and (R) removed files. Just right click on any of the files and choose "Detect Renames...", then click the "Find Renames" button.
You might have to adjust the "Min Similarity" slider until you get all the files you want and only the files you want, but it's usually very straightforward.
hg mv
does do the right thing, but hg log does not list entries past the move unless you
give it the -f option. See this question for more info
Why 'hg mv' (mercurial) doesn't move a file's history by default?
After you do this, you likely want to add the -f option to hg log
to the hgrc file for the repo.
.hg/hgrc
[defaults]
log = -f
In Windows with Tortoise HG installed, there is a windows shell extension that handles this very nicely.
In Windows Explorer, simply right-click and drag the file(s) you wish to move into the destination folder. You are then presented with a pop-up that give you these choices:
HG Move versioned item(s) here
HG Copy versioned item(s) here
Use hg mv to move your files and then use hg log -f (follow) to see history including renames.

Copied file sharing same mutual history in Mercurial (TortoiseHG)

In Mercurial (preferably via TortoiseHG interface) is it possible to copy a committed file, and for the new destination file to share the history for the source file?
I'm pretty sure this is not possible, but I'm asking in the hope that it's just really well hidden in the documentation.
I have a file with a fairly long history of changes, but I now need to have have a 2nd copy of this file within the project which will contain it's own individual changes. However, it would be exceedingly useful to be able to view the history of this copied file and see all the changes that took place on the original before the copy took place (as well as those that have taken place since the copy).
Update
Thanks to #Inspired for their answer. As I said in my comment to them, I was at fault...
I just realised why it wasn't working: I didn't copy the file through either the command line or THG, but through Visual Studio (2010) using VisualHG. VHG must copy the file and then add it, rather than using the mercurial copy function. So I need to remember to use mercurial directly in the future!
Or to be more precise, my guess is that VisualHG cannot override the default functionality of Visual Studio, and therefore can only work with the copied file, rather than run the mercurial copy command. This is purely my guess.
From command line, you can issue hg copy <old name> <new name>. Mercurial does save the history log when you perform copy or rename (hg cp / hg mv).
In TortoiseHG the copy is a bit tricky: you have to copy the file manually (e.g. via cp), then in THG in Commit mode right-click at the left low pane with the unversioned new file and select Detect renames. Uncheck Only consider deleted files, let TortoiseHG find similar files and click the source file when it finishes. TortoiseHG will record the new file as a copy.
I haven't found a simpler way.
When viewing history from command line, you need to specify -f (or --follow) when you are viewing the history: hg log -f <filename>. TortoiseHG already does that.

Mercurial with KDiff3 -- can it directly expand all folders?

With Mercurial, every time a
hg vdiff
which invokes kdiff3, there are 2 steps that need to be done:
1) close the pop up that says how many files are diff'ing
2) go to Directory -> Unfold all Subdirs
Is there a way to skip the step 1 and step 2 and have it automatically expand all folders and show the files?
There are 2 check-boxes available on kdiff3 Version 0.9.98 under Settings -> Configure KDiff -> Directory: "Unfold all subdirectories on load" and "Skip directory report status".
Hrm, that'll be entirely up to kdiff3, but if you can find a command line option that starts kdiff3 out in expanded mode you'll can tweak the vdiff string in the configuration lines for the ExtdiffExtension, which is the feature someone has configured for you. That can be configured in any hgrc which can be per-repo (.hg/hgrc), per-user (~/.hgrc), or system-wide (/etc/mercurial'hgrc). If you don't recall setting it up it's probably in the system-wide location.
If you want to see exactly where as setting is coming from in a recent version of Mercurial you can use the command:
hg --debug showconfig
look for kdiff3 there and you'll find exactly what file and line to tweak.
It doesn't seem possible, as it's not in kdiff3's command line options, and there's no config item for it as seen from kdiff3 --confighelp.
I guess you can hack the source. It's Qt so it can't be hard, can it?
(BTW Beyond Compare has -expandall option that does exactly that. I don't want to sound like I get commission from Scooter Software, but BC is literally beyond any diff/merge tool that I've seen. There's one thing that kdiff3 can but BC cannot, though: 3-directory comparison.)

How to commit only a part of the changes made to a file?

I want to commit separately different parts of the same file.
I want to commit line 2 first with the message (changeset 1) and the 4th line with the message (changeset 2). How do I do it?
I am using Mercurial Distributed SCM (version 3.5.2+20151001)
You can do this with the interactive option to commit.
First add the following to your ~/.hgrc file:
[ui]
interface = curses
Then use:
hg commit -i
This will tell commit to allow you to interactively select what files or (by drilling into the file) select sub file changes.
You can use this multiple times, selecting individual changes in the files.
Note: without the addition to your .hgrc, hg commit -i will ask you for each file and not allow you to drill into and select individual file changes.
The interactive option is also being implemented in other mercurial commands such as restore (you can select what changes are to be restored) and the new experimental amend command. It is very powerful and easy to use.