[PATCH 1 of 2] revsets: let p1() and p2() return parents of working dir

Kevin Bullock kbullock+mercurial at ringworld.org
Thu Nov 4 17:09:52 CDT 2010


# HG changeset patch
# User Kevin Bullock <kbullock at ringworld.org>
# Date 1288907943 18000
# Node ID 8b88770f6880f1b8ac0fc3700ab592ff027ef704
# Parent  6eab8f0df2ca1ae50254004535e4dcffbf3b1634
revsets: let p1() and p2() return parents of working dir

This patch makes the 'set' argument to revset functions p1() and p2()
optional. If no argument is given, p1() and p2() return the first or second
parent of the working directory.

If the working directory is not an in-progress merge (no 2nd parent), p2()
returns the empty set. For a checkout of the null changeset, both p1() and
p2() return the empty set.

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -202,9 +202,12 @@
     return [r for r in subset if r == l]
 
 def p1(repo, subset, x):
-    """``p1(set)``
-    First parent of changesets in set.
+    """``p1([set])``
+    First parent of changesets in set, or the working directory.
     """
+    if x is None:
+        return [repo[x].parents()[0].rev()]
+
     ps = set()
     cl = repo.changelog
     for r in getset(repo, range(len(repo)), x):
@@ -212,9 +215,16 @@
     return [r for r in subset if r in ps]
 
 def p2(repo, subset, x):
-    """``p2(set)``
-    Second parent of changesets in set.
+    """``p2([set])``
+    Second parent of changesets in set, or the working directory.
     """
+    if x is None:
+        ps = repo[x].parents()
+        try:
+            return [ps[1].rev()]
+        except IndexError:
+            return []
+
     ps = set()
     cl = repo.changelog
     for r in getset(repo, range(len(repo)), x):
diff --git a/tests/test-revset-dirstate-parents.t b/tests/test-revset-dirstate-parents.t
new file mode 100644
--- /dev/null
+++ b/tests/test-revset-dirstate-parents.t
@@ -0,0 +1,42 @@
+  $ HGENCODING=utf-8
+  $ export HGENCODING
+
+  $ try() {
+  >   hg debugrevspec --debug $@
+  > }
+
+  $ log() {
+  >   hg log --template '{rev}\n' -r "$1"
+  > }
+
+  $ hg init repo
+  $ cd repo
+
+  $ try 'p1()'
+  ('func', ('symbol', 'p1'), None)
+  -1
+  $ try 'p2()'
+  ('func', ('symbol', 'p2'), None)
+
+null revision
+  $ log 'p1()'
+  $ log 'p2()'
+
+working dir with a single parent
+  $ echo a > a
+  $ hg ci -Aqm0
+  $ log 'p1()'
+  0
+  $ log 'p2()'
+
+merge in progress
+  $ echo b > b
+  $ hg ci -Aqm1
+  $ hg up -q 0
+  $ echo c > c
+  $ hg ci -Aqm2
+  $ hg merge -q
+  $ log 'p1()'
+  2
+  $ log 'p2()'
+  1


More information about the Mercurial-devel mailing list