changeset 703:4cddcb768cf4

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.
author Siddharth Agarwal <sid0@fb.com>
date Tue, 04 Mar 2014 16:05:19 -0800
parents 439e57b724b6
children 373f854ff58f
files hggit/git_handler.py tests/test-outgoing.t tests/test-pull.t tests/test-push.t
diffstat 4 files changed, 22 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/hggit/git_handler.py	Tue Mar 04 16:51:43 2014 -0800
+++ b/hggit/git_handler.py	Tue Mar 04 16:05:19 2014 -0800
@@ -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:
--- a/tests/test-outgoing.t	Tue Mar 04 16:51:43 2014 -0800
+++ b/tests/test-outgoing.t	Tue Mar 04 16:05:19 2014 -0800
@@ -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
--- a/tests/test-pull.t	Tue Mar 04 16:51:43 2014 -0800
+++ b/tests/test-pull.t	Tue Mar 04 16:05:19 2014 -0800
@@ -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
--- a/tests/test-push.t	Tue Mar 04 16:51:43 2014 -0800
+++ b/tests/test-push.t	Tue Mar 04 16:05:19 2014 -0800
@@ -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