# HG changeset patch
# User Dan Villiom Podlaski Christiansen <danchr@gmail.com>
# Date 1624348927 -7200
#      Tue Jun 22 10:02:07 2021 +0200
# Node ID 0e22c438ec3cc8157ecc2d28d5cfcffbd4cbf8ec
# Parent  b7843bf667a757daafed09a50ce6d4318262edf0
pull: also advance draft on no-op pulls

Even if a change doesn't pull any new changes, it should synchronise
phases. So merely setting the phase when committing the change doesn't
suffice.

diff --git a/hggit/git_handler.py b/hggit/git_handler.py
--- a/hggit/git_handler.py
+++ b/hggit/git_handler.py
@@ -1709,6 +1709,8 @@
                     if LOCAL_BRANCH_PREFIX + t[len(remote_name) + 1:] not in refs:
                         del self.git.refs[REMOTE_BRANCH_PREFIX + t]
 
+        all_remote_nodeids = []
+
         for ref_name, sha in refs.items():
             hgsha = self.map_hg_get(sha)
 
@@ -1727,8 +1729,18 @@
                   ref_name.endswith(b'^{}')):
                 self.git.refs[ref_name] = sha
 
-        # ensure that we update phases on push and no-op pulls
-        with self.repo.lock(), self.repo.transaction(b"phase") as tr:
+            if hgsha:
+                all_remote_nodeids.append(bin(hgsha))
+
+        with self.repo.lock(), self.repo.transaction(b"hg-git-phases") as tr:
+            if all_remote_nodeids:
+                # sanity check: ensure that all corresponding commits
+                # are at least draft; this can happen on no-op pulls
+                # where the commit already exists, but is secret
+                phases.advanceboundary(
+                    self.repo, tr, phases.draft, all_remote_nodeids,
+                )
+            # ensure that we update phases on push and no-op pulls
             phases.advanceboundary(
                 self.repo,
                 tr,
diff --git a/tests/test-git-tags.t b/tests/test-git-tags.t
--- a/tests/test-git-tags.t
+++ b/tests/test-git-tags.t
@@ -312,7 +312,7 @@
   $ hg -R hgrepo-2 gexport
   $ rm -rf hgrepo-2
 
-Check that pulling will update phases only — which it doesn't, yet:
+Check that pulling will update phases only:
 
   $ cd hgrepo
   $ hg phase -fs gamma detached
@@ -320,7 +320,6 @@
   pulling from $TESTTMP/gitrepo
   no changes found
   $ hg log -T shorttags -r gamma -r detached
-  4:0eb1ab0073a8 secret beta gamma
-  2:61175962e488 secret detached X
-  $ hg phase -d 'all()'
+  4:0eb1ab0073a8 draft beta gamma
+  2:61175962e488 draft detached X
   $ cd ..
diff --git a/tests/test-pull.t b/tests/test-pull.t
--- a/tests/test-pull.t
+++ b/tests/test-pull.t
@@ -105,6 +105,15 @@
      summary:     add alpha
   
 
+no-op pull should affect phases
+  $ hg -R hgrepo phase -fs beta
+  $ hg -R hgrepo pull -r beta
+  pulling from $TESTTMP/gitrepo
+  no changes found
+  $ hg -R hgrepo phase beta
+  1: draft
+
+
 add another commit and tag to the git repo
   $ cd gitrepo
   $ git checkout -q beta