From 759062fbfd3e8fa67641258ced38031c1476a2eb Mon Sep 17 00:00:00 2001 From: Siddharth Agarwal Date: Tue, 4 Mar 2014 16:05:19 -0800 Subject: [PATCH] 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. --- hggit/git_handler.py | 25 +++++++++++++++++++------ tests/test-outgoing.t | 2 +- tests/test-pull.t | 2 +- tests/test-push.t | 2 +- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/hggit/git_handler.py b/hggit/git_handler.py index 74a1c54ed8..f1020ffca5 100644 --- a/hggit/git_handler.py +++ b/hggit/git_handler.py @@ -203,8 +203,10 @@ class GitHandler(object): 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 @@ class GitHandler(object): 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 index 82d7f7f9fb..dddea91668 100644 --- a/tests/test-outgoing.t +++ b/tests/test-outgoing.t @@ -97,7 +97,7 @@ let's pull and try again $ 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 index 02ceef957a..90c2f78234 100644 --- a/tests/test-pull.t +++ b/tests/test-pull.t @@ -68,7 +68,7 @@ pull everything else $ 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 index 1b5e9c3dc8..40bcf61a4d 100644 --- a/tests/test-push.t +++ b/tests/test-push.t @@ -71,7 +71,7 @@ this should fail $ 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