diff --git a/mercurial/revset.py b/mercurial/revset.py
index 24cda1dd45ff35af8acbe96fb5559d01cd333181_bWVyY3VyaWFsL3JldnNldC5weQ==..1cce8112147298dd164612546267d4450e451f9d_bWVyY3VyaWFsL3JldnNldC5weQ== 100644
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -1468,7 +1468,10 @@
         ps = set()
         cl = repo.changelog
         for r in getset(repo, fullreposet(repo), x):
-            ps.update(cl.parentrevs(r))
+            if r is None:
+                ps.update(p.rev() for p in repo[r].parents())
+            else:
+                ps.update(cl.parentrevs(r))
     ps -= set([node.nullrev])
     return subset & ps
 
diff --git a/tests/test-add.t b/tests/test-add.t
index 24cda1dd45ff35af8acbe96fb5559d01cd333181_dGVzdHMvdGVzdC1hZGQudA==..1cce8112147298dd164612546267d4450e451f9d_dGVzdHMvdGVzdC1hZGQudA== 100644
--- a/tests/test-add.t
+++ b/tests/test-add.t
@@ -75,6 +75,13 @@
 
   $ hg ci -m 0 --traceback
 
+  $ hg log -r "heads(. or wdir() & file('**'))"
+  changeset:   0:* (glob)
+  tag:         tip
+  user:        test
+  date:        Thu Jan 01 00:00:00 1970 +0000
+  summary:     0
+  
 should fail
 
   $ hg add a
@@ -99,6 +106,15 @@
   M a
   ? a.orig
 
+wdir doesn't cause a crash, and can be dynamically selected if dirty
+
+  $ hg log -r "heads(. or wdir() & file('**'))"
+  changeset:   2:*+ (glob)
+  parent:      2:* (glob)
+  parent:      1:* (glob)
+  user:        test
+  date:        * (glob)
+  
 should fail
 
   $ hg add a