# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1531541958 -32400
#      Sat Jul 14 13:19:18 2018 +0900
# Node ID 88efb7d6bcb6214079d952a90b0eb14dbfa11c71
# Parent  8eeed92475d5bc34c59114e760da1787e459bc5e
phases: remove excessive optimization from newheads() (issue5939)

This function is intended to compute 'heads(::heads - roots::)', but it
failed because 'heads + parents(roots)' missed sibling branches of the roots.
That's why the public heads slipped down from D to B in the example added by
2a227782e754 "tests: add test demonstrating phase loss when cloning":

  > E    draft
  > |\Z  draft
  > | Y  draft
  > D |  public
  > | X  draft
  > C/   public
  > B    public
  > A    public
  where heads = {E, Z},
        roots = {X}

diff --git a/mercurial/phases.py b/mercurial/phases.py
--- a/mercurial/phases.py
+++ b/mercurial/phases.py
@@ -665,8 +665,7 @@
     * `heads`: define the first subset
     * `roots`: define the second we subtract from the first"""
     repo = repo.unfiltered()
-    revset = repo.set('heads((%ln + parents(%ln)) - (%ln::%ln))',
-                      heads, roots, roots, heads)
+    revset = repo.set('heads(::%ln - (%ln::%ln))', heads, roots, heads)
     return [c.node() for c in revset]
 
 
diff --git a/tests/test-phases-exchange.t b/tests/test-phases-exchange.t
--- a/tests/test-phases-exchange.t
+++ b/tests/test-phases-exchange.t
@@ -1478,7 +1478,8 @@
   $ killdaemons.py
 
 With legacy listkeys over bundle2
-TODO issue 5939: public phase lost on 26805 and f5853
+(issue 5939: public phase was lost on 26805 and f5853 before, due to a bug
+of phase heads computation)
 
   $ hg -R mergetest --config devel.legacy.exchange=phases serve -p $HGPORT -d --pid-file=hg.pid
   $ cat hg.pid >> $DAEMON_PIDS
@@ -1492,9 +1493,9 @@
   new changesets 426bada5c675:bb94757e651a
   test-debug-phase: new rev 0:  x -> 0
   test-debug-phase: new rev 1:  x -> 0
-  test-debug-phase: new rev 2:  x -> 1
+  test-debug-phase: new rev 2:  x -> 0
   test-debug-phase: new rev 3:  x -> 1
-  test-debug-phase: new rev 4:  x -> 1
+  test-debug-phase: new rev 4:  x -> 0
   test-debug-phase: new rev 5:  x -> 1
   test-debug-phase: new rev 6:  x -> 1
   test-debug-phase: new rev 7:  x -> 1
@@ -1506,11 +1507,11 @@
   |/|
   o |  13b7b draft
   | |
-  | o  f5853 draft
+  | o  f5853 public
   | |
   o |  c67c4 draft
   | |
-  | o  26805 draft
+  | o  26805 public
   |/
   o  11247 public
   |
@@ -1519,7 +1520,8 @@
   $ killdaemons.py
 
 Without bundle2
-TODO issue 5939: public phase lost on 26805 and f5853
+(issue 5939: public phase was lost on 26805 and f5853 before, due to a bug
+of phase heads computation)
 
   $ hg -R mergetest serve -p $HGPORT -d --pid-file=hg.pid
   $ cat hg.pid >> $DAEMON_PIDS
@@ -1533,9 +1535,9 @@
   new changesets 426bada5c675:bb94757e651a
   test-debug-phase: new rev 0:  x -> 0
   test-debug-phase: new rev 1:  x -> 0
-  test-debug-phase: new rev 2:  x -> 1
+  test-debug-phase: new rev 2:  x -> 0
   test-debug-phase: new rev 3:  x -> 1
-  test-debug-phase: new rev 4:  x -> 1
+  test-debug-phase: new rev 4:  x -> 0
   test-debug-phase: new rev 5:  x -> 1
   test-debug-phase: new rev 6:  x -> 1
   test-debug-phase: new rev 7:  x -> 1
@@ -1547,11 +1549,11 @@
   |/|
   o |  13b7b draft
   | |
-  | o  f5853 draft
+  | o  f5853 public
   | |
   o |  c67c4 draft
   | |
-  | o  26805 draft
+  | o  26805 public
   |/
   o  11247 public
   |