Patchwork convert: fix git convert using servers branches

login
register
mail settings
Submitter Durham Goode
Date Aug. 3, 2015, 6:44 p.m.
Message ID <e50c7d78fa48814dde3a.1438627440@dev2000.prn2.facebook.com>
Download mbox | patch
Permalink /patch/10090/
State Accepted
Headers show

Comments

Durham Goode - Aug. 3, 2015, 6:44 p.m.
# HG changeset patch
# User Durham Goode <durham@fb.com>
# Date 1438201263 25200
#      Wed Jul 29 13:21:03 2015 -0700
# Branch stable
# Node ID e50c7d78fa48814dde3a99454766f48994036db0
# Parent  79f0cb97d7537a7c2948f8f9b0a89148825a3a1d
convert: fix git convert using servers branches

The conversion from git to hg was reading the remote branch list directly from
the origin server. If the origin's branch had moved forward since the last git
fetch, it would return a git hash which didn't exist locally, and therefore the
branch was not converted.

This changes it to rely on the local repo's refs/remotes list of branches
instead, so it's completely cut off from the server.
Pierre-Yves David - Aug. 5, 2015, 12:40 a.m.
On 08/03/2015 11:44 AM, Durham Goode wrote:
> # HG changeset patch
> # User Durham Goode <durham@fb.com>
> # Date 1438201263 25200
> #      Wed Jul 29 13:21:03 2015 -0700
> # Branch stable
> # Node ID e50c7d78fa48814dde3a99454766f48994036db0
> # Parent  79f0cb97d7537a7c2948f8f9b0a89148825a3a1d
> convert: fix git convert using servers branches

That patch looks good to me, can I get a second green flag from someone 
who know something about "git show-ref".

[…]

> -                    name = '%s%s' % (reftype, name[prefixlen:])
> +                    name = '%s%s' % (hgprefix, name[len(gitprefix):])
>                       bookmarks[name] = rev
> -            except Exception:
> -                pass
> +        except Exception:
> +            pass

Not sure why this is for, but this is pretty scary.
(I know, not your change)
Siddharth Agarwal - Aug. 5, 2015, 12:58 a.m.
On 8/4/15 5:40 PM, Pierre-Yves David wrote:
>
>
> On 08/03/2015 11:44 AM, Durham Goode wrote:
>> # HG changeset patch
>> # User Durham Goode <durham@fb.com>
>> # Date 1438201263 25200
>> #      Wed Jul 29 13:21:03 2015 -0700
>> # Branch stable
>> # Node ID e50c7d78fa48814dde3a99454766f48994036db0
>> # Parent  79f0cb97d7537a7c2948f8f9b0a89148825a3a1d
>> convert: fix git convert using servers branches
>
> That patch looks good to me, can I get a second green flag from 
> someone who know something about "git show-ref".

Yeah, lgtm

>
> […]
>
>> -                    name = '%s%s' % (reftype, name[prefixlen:])
>> +                    name = '%s%s' % (hgprefix, name[len(gitprefix):])
>>                       bookmarks[name] = rev
>> -            except Exception:
>> -                pass
>> +        except Exception:
>> +            pass
>
> Not sure why this is for, but this is pretty scary.
> (I know, not your change)
>
>
>
Pierre-Yves David - Aug. 5, 2015, 1:11 a.m.
On 08/04/2015 05:58 PM, Siddharth Agarwal wrote:
> On 8/4/15 5:40 PM, Pierre-Yves David wrote:
>>
>>
>> On 08/03/2015 11:44 AM, Durham Goode wrote:
>>> # HG changeset patch
>>> # User Durham Goode <durham@fb.com>
>>> # Date 1438201263 25200
>>> #      Wed Jul 29 13:21:03 2015 -0700
>>> # Branch stable
>>> # Node ID e50c7d78fa48814dde3a99454766f48994036db0
>>> # Parent  79f0cb97d7537a7c2948f8f9b0a89148825a3a1d
>>> convert: fix git convert using servers branches
>>
>> That patch looks good to me, can I get a second green flag from
>> someone who know something about "git show-ref".
>
> Yeah, lgtm

Pushed to the clowncopter.

Patch

diff --git a/hgext/convert/git.py b/hgext/convert/git.py
--- a/hgext/convert/git.py
+++ b/hgext/convert/git.py
@@ -371,28 +371,31 @@  class convert_git(converter_source):
     def getbookmarks(self):
         bookmarks = {}
 
-        # Interesting references in git are prefixed
-        prefix = 'refs/heads/'
-        prefixlen = len(prefix)
+        # Handle local and remote branches
+        remoteprefix = self.ui.config('convert', 'git.remoteprefix', 'remote')
+        reftypes = [
+            # (git prefix, hg prefix)
+            ('refs/remotes/origin/', remoteprefix + '/'),
+            ('refs/heads/', '')
+        ]
 
-        # factor two commands
-        remoteprefix = self.ui.config('convert', 'git.remoteprefix', 'remote')
-        gitcmd = { remoteprefix + '/': 'git ls-remote --heads origin',
-                                   '': 'git show-ref'}
+        exclude = set([
+            'refs/remotes/origin/HEAD',
+        ])
 
-        # Origin heads
-        for reftype in gitcmd:
-            try:
-                fh = self.gitopen(gitcmd[reftype], err=subprocess.PIPE)
-                for line in fh:
-                    line = line.strip()
-                    rev, name = line.split(None, 1)
-                    if not name.startswith(prefix):
+        try:
+            fh = self.gitopen('git show-ref', err=subprocess.PIPE)
+            for line in fh:
+                line = line.strip()
+                rev, name = line.split(None, 1)
+                # Process each type of branch
+                for gitprefix, hgprefix in reftypes:
+                    if not name.startswith(gitprefix) or name in exclude:
                         continue
-                    name = '%s%s' % (reftype, name[prefixlen:])
+                    name = '%s%s' % (hgprefix, name[len(gitprefix):])
                     bookmarks[name] = rev
-            except Exception:
-                pass
+        except Exception:
+            pass
 
         return bookmarks
 
diff --git a/tests/test-convert-git.t b/tests/test-convert-git.t
--- a/tests/test-convert-git.t
+++ b/tests/test-convert-git.t
@@ -666,6 +666,28 @@  convert using a different remote prefix
      master                    0:03bf38caa4c6
      origin/master             0:03bf38caa4c6
 
+Run convert when the remote branches have changed
+(there was an old bug where the local convert read branches from the server)
+
+  $ cd git-repo7
+  $ echo a >> a
+  $ git commit -am "move master forward"
+  [master 0c81947] move master forward
+   Author: nottest <test@example.org>
+   1 file changed, 1 insertion(+)
+  $ cd ..
+  $ rm -rf hg-repo7
+  $ hg convert --config convert.git.remoteprefix=origin git-repo7-client hg-repo7
+  initializing destination hg-repo7 repository
+  scanning source...
+  sorting...
+  converting...
+  0 commit a
+  updating bookmarks
+  $ hg -R hg-repo7 bookmarks
+     master                    0:03bf38caa4c6
+     origin/master             0:03bf38caa4c6
+
 damaged git repository tests:
 In case the hard-coded hashes change, the following commands can be used to
 list the hashes and their corresponding types in the repository: