Mercurial: open earlier version of file in an editor? - mercurial

Is there any way to quickly view an earlier version of a file (at an earlier revision) in an text editor?
Thanks

With command line Mercurial:
hg cat -r REV -o %s-%r FILE
edit FILE-REV
del FILE-REV
In TortoiseHG Workbench:
Select the desired revision.
Open the Revision Details tab.
Click the document icon (Show all version-controlled files in tree view)
Navigate to the file and right click it.
Select View at Revision.
Or starting TortoiseHG from command line:
thg filelog FILE
Right click the desired revision.
Select View at Revision.

Related

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.

In Mercurial (tortoisehg), how do I view a file's history?

In tortoisehg, how do I view a file's history? Much like as the repo explorer, but instead of viewing the whole repo, I would like to view only a specific file's history.
Thanks!
You can view the changes on a file by the following command:
hg log -f file_path
You can see more options by typing hg log --help
You can use TortoiseHg, right click, view history. I think within the repo explorer/workbench you can go into file view mode and do the same from there

Mercurial: rename then create new file with old name

I'm using TortoiseHg. I have a file A. I used the rename tool to rename this to B. All seems well. Now I want to create a new file named A. When I create a file with that name and go to add it to the repo, it's showing me a diff of my new file with the old A (which is now renamed to B.)
How can I rename A to B and be able to add a new A which is, y'know, new, and not have Hg think I'm modifying the old, now-nonexistent A?
Edit: In fact what I originally did was the right thing, I just wasn't committing the rename properly. (I had committed, but only committed the newly-renamed file, and not the now-nonexistent old file.)
You need to commit after the rename and then create the new file with same name.
$ hg mv A B
$ hg commit -m "moving A→B"
$ touch A
$ hg add A
$ hg commit -m "adding new A"
Suppose you the file a.txt in your repository. To rename it to b.txt and add a different file named a.txt, using TortoiseHg 2.x, do the following:
Right-click a.txt. In the context menu, click TortoiseHg > Rename File.
In the Rename dialog box, enter b.txt into the Destination text box. Click Rename.
Right-click the repository folder. In the context menu, click Hg Commit.
In the Commit window, make that you have two files checked, added b.txt and removed a.txt. Enter the commit description, and click Commit. Close the Commit window.
Create new file named a.txt. Right-click it. In the context menu, click TortoiseHg > Add Files.
In the Add window, make sure that a.txt is checked. Click Add.
Right-click the repository folder. In the context menu, click Hg Commit.
In the Commit window, make that you have a.txt checked. Enter the commit description, and click Commit. Close the Commit window.
Now you have a different a.txt, with a fresh history, and b.txt which remembers it used to be named a.txt.
P.S. This is exactly the same steps as Vaibhav Bajpai posted in his answer. You may have a hard time if you have problems dealing with steps expressed as hg commands.

How to view revision history for Mercurial file?

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

How do I get mercurial to show the diff during `hg com`?

Is there a way to configure hg com so that in the commit message file that pops up in the external editor, instead of just showing which files were changed (in the HG: lines) it actually shows the full diff? I'd rather view the output and compose my commit message simultaneously from the comfort of my text editor as opposed to doing hg diff on the command line separately beforehand.
As of 2016, it's possible to do this with the committemplate configuration option. Adding the following to an hgrc file will include the diff in the editor window inline as you type your commit message.
[committemplate]
changeset = {desc}\n\n
HG: {extramsg}
HG: user: {author}\n{ifeq(p2rev, "-1", "",
"HG: branch merge\n")
}HG: branch '{branch}'\n{if(currentbookmark,
"HG: bookmark '{currentbookmark}'\n") }{subrepos %
"HG: subrepo {subrepo}\n" }
{splitlines(diff()) % 'HG: {line}\n'}
See hg help hgrc and search for committemplate for more information.
Mercurial doesn't have that as a built-in feature, but it's easy to simulate in your editor (as launched by commit).
Here's an example using VIM: https://www.mercurial-scm.org/wiki/DiffsInCommitMessageInVIM
The hgeditor script https://www.mercurial-scm.org/hg/hg-stable/raw-file/tip/hgeditor provides further examples.
The basic jist is:
at editor launch run hg diff redirecting to a temp file
have your editor load both the commit message file and the diff
TortoiseHg does this out of the box: a top panel for the commit message and below that, a left pane listing the affected files and a right pane showing the diffs, one after the other.