Diffing only modified files in Mercurial - mercurial

In Mercurial it's possible to hg status only the modified/added/removed files by doing:
hg st -m
hg st -a
hg st -r
Is it possible to obtain the same behaviour for the diff command? From the man page, it seems not.

One option would be to use something like this:
hg status -mar --no-status | xargs hg diff
The --no-status flag insures that just the file name is sent to STDOUT.

Related

In hg how do I pull all remotes/paths in one command?

I'm looking for the equivalent of git fetch --all. I currently have three different remote paths configured for my repository. hg paths shows all three. How do I do hg pull --all to fetch the new change sets from all remote repositories?
I figured out how to do this with an alias. In your ~/.hgrc add:
[alias]
pullall = !$HG paths | cut -f 1 -d ' ' | xargs -n 1 $HG pull
Then you can run hg pullall to fetch all the remotes.

Mercurial: diffs in a particular changeset?

This is almost exactly a duplicate of Examining a single changeset in Mercurial, and without doubt a duplicate of another question I can't find on SO through Google alone.
I'm looking back through a Mercurial repo, and I want to see what exactly changed between two revisions (let's say 2580 and 2581):
hg log -v -r 2581
gives me all the files that changed.
How can I also see the diffs of these files?
Thanks.
Revision 2580 isn't necessasrily the parent revision of 2581. It's easy to check if it is, of course, but easier yet is to just do:
hg log -p -r 2581
That compares 2581 to its (first) parent revision no matter what it is, and most clearly encompasses the answer to the question "what the hell did 2581 do?"
Try hg diff -r 2580 -r 2581.
hg diff -r 2580 -r 2581
This is a wrong example. The revision 2580 can be in another branch and you get diff between two branches.
Use
hg log -p -r 2581
or hg diff -c 2581
The difference between them in the first lines. Hg log also show information about changeset (parent, author, date, ...)
I prefer second variant hg diff -c ... because it can store to patch files.
hg diff -c 2581 > revision_2581.patch
Another solution is to use revset notation which IMO is a better solution as you can use it in more places consistently (ie you don't need to know about diff -c and log -p ).
hg diff -r 'last(ancestors(2581),2)'
Yes that is rather verbose compared to -c (for diff) and -p (for log).
However mercurial allows you to create revset aliases
In your .hgrc:
[revsetalias]
next(s) = descendants(s, 1)
prev(s) = last(ancestors(s),2)
Now you can do
hg diff -r 'prev(2581)'
hg log -r 'prev(2581)'

In Mercurial, is it exactly the same doing "hg vdiff -r -2:-3" and "hg vdiff -c -2"?

Because I used to always do
hg vdiff -r -2:-3
hg vdiff -r 5213:5212
are all the hg commands exactly the same using those, vs using
hg vdiff -c -2
hg vdiff -r 5213
?
The -c option means changes introduces by this revision, so hg foobar -c 23 is the same as hg foobar -r22:23 if 22 is the first parent of 23. When 22 and 23 are not in a parent0-child relationship, you get the diff of two random changesets.
Use hg help revisions to see all the various ways you can specify revisions, which should work with all subcommands.

Showing renames in hg status?

I know that Mercurial can track renames of files, but how do I get it to show me renames instead of adds/removes when I do hg status? For instance, instead of:
A bin/extract-csv-column.pl
A bin/find-mirna-binding.pl
A bin/xls2csv-separate-sheets.pl
A lib/Text/CSV/Euclid.pm
R src/extract-csv-column.pl
R src/find-mirna-binding.pl
R src/modules/Text/CSV/Euclid.pm
R src/xls2csv-separate-sheets.pl
I want some indication that four files have been moved.
I think I read somewhere that the output is like this to preserve backward-compatibility with something-or-other, but I'm not worried about that.
There are several ways to do this.
Before you commit, you can use hg diff --git to show what was renamed:
$ hg diff --git
diff --git a/theTest.txt b/aTest.txt
rename from theTest.txt
rename to aTest.txt
Note that this only works if you used hg mv, hg rename, or mv and hg addremove --similarity 100.
After you commit, you can still use hg diff, but you'll have to specify the change using -r:
$ hg diff -r 0 -r 1 --git
diff --git a/test.txt b/theTest.txt
rename from test.txt
rename to theTest.txt
For both hg status and hg log, use the -C command-line flag to see the source that a file was copied from.
$ hg status -C
A aTest.txt
theTest.txt
R theTest.txt
The line just below aTest.txt indicates the source it was copied from (theTest.txt).
$ hg log -v -C
changeset: 1:4d7b42489d9f
tag: tip
user: jhurne
date: Tue Apr 20 20:57:07 2010 -0400
files: test.txt theTest.txt
copies: theTest.txt (test.txt)
description:
Renamed test.txt
You can see the files that were affected (test.txt and theTest.txt), and that "theTest.txt" was copied from test.txt.
You can find out how many files have been renamed with hg summary. If you want to see the actual files that were renamed, the fastest way I've found is to do:
hg st -a -C
This will output something like this:
A <path\to\renamed\file>
<path\copied\from>
A <path\to\added\file>
A <path\to\renamed\file>
<path\copied\from>
Since hg status considers a rename to be a copy and a remove, your renamed files will list a copied from file. Files that were added but not renamed will not list a copied from file.

In mercurial, how do I apply a reverse-patch to a particular file?

Related to Mercurial: Merging one file between branches in one repo , I'm trying to perform a backout operation on a single file, even though that file was one of many participants in the revision being backed out.
HG being the changeset-oriented tool that it is, it doesn't want to operate on files.
Closest I could find was to use hg export to create a diff, hand-edit the diff, and then hg import to patch the file in reverse order.
..but then I hit this annoying situation where http://hgbook.red-bean.com/read/finding-and-fixing-mistakes.html claims that there is a --reverse option to hg patch when there is not.
So the closest thing I can think of is to generate a hand-edited patch as above, and then using vanilla patch -R to apply a reverse patch.
The hg backout command would seem to be useful here, but is actually a red herring.
There has GOT to be a better way, no?
You can do it using just the -I (include names matching the given patterns) argument for backout with a single line:
hg backout --merge -I thefiletorevert -m 'message' OFFENDINGREVISIONID
Example Script:
hg init testrepo
cd testrepo
echo -e "line1\n\nline3" > file1
echo -e "line1\n\nline3" > file2
hg commit -A -m 'changes to two files'
perl -pi -e 's/line1/line 1/' file1
perl -pi -e 's/line1/line 1/' file2
hg commit -m 'put spaces in line1'
perl -pi -e 's/line3/line 3/' file1
perl -pi -e 's/line3/line 3/' file2
hg commit -m 'put spaces in line3'
hg backout --merge -I file1 -m 'remove spaces from line1' 1
Sample output:
adding file1
adding file2
reverting file1
created new head
changeset 3:6d354f1ad4c5 backs out changeset 1:906bbeaca6a3
merging with changeset 3:6d354f1ad4c5
merging file1
0 files updated, 1 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)
Resulting File Contents:
file1:line1
file1:line 3
file2:line 1
file2:line 3
notice that file1 is missing it's space in line one after the backout of the middle changeset, and the verbose log shows only one file changed in the backout:
$ hg log -v -r tip
changeset: 3:6d354f1ad4c5
tag: tip
parent: 1:906bbeaca6a3
user: Ry4an Brase <ry4an#mini>
date: Mon Sep 14 12:17:23 2009 -0500
files: file1
description:
remove spaces from line1
Here's what I would do: Use a fresh clone of the tip revision.
hg backout --merge -r revision_where_the_change_happened
to merge the reversed changes into the working copy.
Now copy the file in question to your regular working copy and commit
hg commit -m "Reversed the changes to file.h made in revision bla"
and throw away the clone you created above.
This way, mercurial doesn't know that there is a connection between revision_where_the_change_happened and this commit. If you want mercurial to remember this, instead do a
hg revert {all files except the one in question}
after merging the backout commit into the working copy and before commiting. For the second way, you don't need to work on a clone, because you want to keep the backout commit.
I would guess that the choice of which way you use depends on how big a part of the changeset the particular file change was.
Use the revert command.
hg revert -r1 file
This should revert the contents of file to the version in revision 1.
You can then further edit it and commit as normal.