# HG changeset patch
# User Siddharth Agarwal <sid0@fb.com>
# Date 1393977919 28800
# Node ID 4cddcb768cf4b13050f796a1977fb6cf63f6bd27
# Parent  439e57b724b699e4dd34ab150032bd98d93d0e3c
git_handler.fetch: actually return number of heads added or removed

The return value as implemented in git_handler.fetch was pretty bogus. It used
to return the number of values that changed in the 'refs/heads/' namespace,
regardless of whether multiple values in there point to the same Mercurial
commit, or whether particular heads were even imported. Fix all of that by
using the actual heads in the changelog, just like vanilla Mercurial.

The test output changes demonstrate examples where the code was buggy.

diff --git a/hggit/git_handler.py b/hggit/git_handler.py
--- a/hggit/git_handler.py
+++ b/hggit/git_handler.py
@@ -203,8 +203,10 @@
         remote_name = self.remote_name(remote)
 
         oldrefs = self.git.get_refs()
+        oldheads = self.repo.changelog.heads()
+        imported = 0
         if refs:
-            self.import_git_objects(remote_name, refs)
+            imported = self.import_git_objects(remote_name, refs)
             self.import_tags(refs)
             self.update_hg_bookmarks(refs)
             if remote_name:
@@ -223,13 +225,24 @@
             rn = remote_name or 'default'
             return 'refs/remotes/' + rn + ref[10:]
 
-        modheads = [refs[k] for k in refs if k.startswith('refs/heads/')
-                    and not k.endswith('^{}')
-                    and refs[k] != oldrefs.get(remoteref(k))]
-
         self.save_map()
 
-        return len(modheads)
+        if imported == 0:
+            return 0
+
+        # code taken from localrepo.py:addchangegroup
+        dh = 0
+        if oldheads:
+            heads = self.repo.changelog.heads()
+            dh = len(heads) - len(oldheads)
+            for h in heads:
+                if h not in oldheads and self.repo[h].closesbranch():
+                    dh -= 1
+
+        if dh < 0:
+            return dh - 1
+        else:
+            return dh + 1
 
     def export_commits(self):
         try:
diff --git a/tests/test-outgoing.t b/tests/test-outgoing.t
--- a/tests/test-outgoing.t
+++ b/tests/test-outgoing.t
@@ -97,7 +97,7 @@
   $ hg pull 2>&1 | grep -v 'divergent bookmark'
   pulling from */gitrepo (glob)
   importing git objects into hg
-  (run 'hg update' to get a working copy)
+  (run 'hg heads' to see heads, 'hg merge' to merge)
   $ hg outgoing | sed 's/bookmark:    /tag:         /' | grep -v 'searching for changes'
   comparing with */gitrepo (glob)
   changeset:   1:0564f526fb0f
diff --git a/tests/test-pull.t b/tests/test-pull.t
--- a/tests/test-pull.t
+++ b/tests/test-pull.t
@@ -68,7 +68,7 @@
   $ hg -R hgrepo pull
   pulling from $TESTTMP/gitrepo
   importing git objects into hg
-  (run 'hg update' to get a working copy)
+  (run 'hg heads' to see heads, 'hg merge' to merge)
   $ hg -R hgrepo log --graph
   o  changeset:   2:37c124f2d0a0
   |  bookmark:    master
diff --git a/tests/test-push.t b/tests/test-push.t
--- a/tests/test-push.t
+++ b/tests/test-push.t
@@ -71,7 +71,7 @@
   $ hg pull 2>&1 | grep -v 'divergent bookmark'
   pulling from $TESTTMP/gitrepo
   importing git objects into hg
-  (run 'hg update' to get a working copy)
+  (run 'hg heads' to see heads, 'hg merge' to merge)
 TODO shouldn't need to do this since we're (in theory) pushing master explicitly,
 which should not implicitly also push the not-master ref.
   $ hg book not-master -r default/not-master --force