Bug 1287234 - Make finding remote mozreview repository a little more robust. r?gps draft
authorMike Hommey <mh@glandium.org>
Sat, 16 Jul 2016 09:50:31 +0900
changeset 8914 8e693f0ba1f7efeca7f3334bcde735f62cb10795
parent 8913 e8fc8b042a265d01e9d5a3731df3931e10c9e473
push id1025
push userbmo:mh+mozilla@glandium.org
push dateSat, 16 Jul 2016 01:01:51 +0000
reviewersgps
bugs1287234, 0
Bug 1287234 - Make finding remote mozreview repository a little more robust. r?gps When for some reason the output of git cinnabar hg2git doesn't fit the "one sha1 per line" model, the current code can go horribly wrong by splitting words instead of lines, assuming that anything that is not a 0000000000000000000000000000000000000000 is a valid sha1, assuming the output has the same number of lines as the input and still trying other remotes even when one was already found. This change makes it all more robust. MozReview-Commit-ID: 79iHG2qgInO
git/commands/git-mozreview
--- a/git/commands/git-mozreview
+++ b/git/commands/git-mozreview
@@ -632,26 +632,28 @@ def configure_command(args):
         res = requests.get(url)
         if res.status_code == 200:
             data = res.json()
             hg_shas = sorted(data.keys())
             res, output = get_output(['git', 'cinnabar', 'hg2git'] + hg_shas)
             if res:
                 ui.warn('warning: error trying to resolve Mercurial changesets\n')
             else:
-                git_commits = output.strip().split()
-                for i, node in enumerate(git_commits):
-                    if node == b'0' * 40:
+                is_sha1 = re.compile('[0-9a-f]{40}$')
+                git_commits = (l.strip() for l in output.splitlines())
+                for node, hg_sha in zip(git_commits, hg_shas):
+                    if node == b'0' * 40 or not is_sha1.match(node.lower()):
                         continue
-                    remote_url = data[hg_shas[i]].split()[0]
+                    remote_url = data[hg_sha].split()[0]
                     remote_url = 'hg::%s' % remote_url
                     subprocess.check_call(['git', 'config', 'mozreview.remote',
                                            remote_url])
                     git_config['mozreview.remote'] = remote_url
                     ui.warn('using %s\n' % remote_url)
+                    break
         else:
             ui.warn('warning: unable to fetch list of review repos; '
                     'manual configuration of review remote will be required\n')
 
     # Now install the commit-msg hook.
     source_hook = os.path.join(ROOT, 'git', 'hooks', 'commit-msg-mozreview')
     dest_hook = os.path.join(git_dir, 'hooks', 'commit-msg')
     install_hook = True