27

Assuming I have a named branch foo with two commits a, b:

      a       b       c  
------o-------o-------o------- # default
       \          d         e
        ----------o---------o  # branch foo

I want to see the diff between a and e (a not included). I could of course use the revision id, but that's not very practical. In git, one can just do git diff master..foo. How can I do the same in hg ?

David Cournapeau
  • 78,318
  • 8
  • 63
  • 70

4 Answers4

34

You can do it using revsets.

In your specific example I think you could get a list of of just d and e using:

hg log -r "branch('foo') - branch('default')"

where that - is defined as:

"x - y"
      Changesets in x but not in y.

Getting the diff from a to e could be done as:

hg diff -r "ancestor(default, foo)" -r foo

though there's possibly a shorthand for that I'm not seeing.

Ry4an Brase
  • 78,112
  • 7
  • 148
  • 169
  • Thanks, this at least give a way to do it automatically. But I am a bit puzzled by the predicate branch(foo). hg log -r "branch(foo)" does what I want, bug hg diff -r "branch(foo)" does not. Do you know why ? – David Cournapeau Jan 19 '11 at 05:15
  • 1
    Because it just filters all changesets by that predicate. "branch(foo)" would return (d, e) which is relatively meaningless in this context. – Ringding Jan 19 '11 at 07:43
  • 2
    The correct syntax seems to be: `hg log -r "branch('foo') - branch(default)"` – anatoly techtonik Jul 17 '12 at 05:44
  • 1
    If you're on the tip of 'foo', you can do the diff with simply `hg diff -r "ancestor(default, .)"` – Jason R. Coombs Aug 27 '13 at 13:30
12

Another way to do this, useful also for branches that you have already merged to default is:

hg diff -r "max(ancestors(foo) and branch(default)):foo"

Though that can be a pit of a pain, so i'd recommend setting up an alias by adding something like:

[alias]
branchdiff = diff -r "max(ancestors('$1') and branch(default)):'$1'"

To your Mercurial.INI/hgrc which you can then use like this:

hg branchdiff <branch name>

or

hg branchdiff .
Richard
  • 455
  • 5
  • 13
  • It would be great to see an explanation of how exactly HG achieves the desired result with this revset `"max(ancestors(foo) and branch(default)):foo"`. – rszalski Jul 06 '16 at 10:28
1

If you want logs from current branch only:

hg log -b .

Sid Sarasvati
  • 819
  • 9
  • 10
0

diff between branches:

hg diff -r <branchname_1>:<branchname_2>

show current branch's latest commit:

hg log -r .

zhengquan
  • 39
  • 8