Patchwork [4,of,5] convert: improve support for unusual .gitmodules

login
register
mail settings
Submitter Durham Goode
Date June 30, 2015, 1:44 a.m.
Message ID <c4c8a8f7781e3eb4d836.1435628644@dev2000.prn2.facebook.com>
Download mbox | patch
Permalink /patch/9824/
State Accepted
Headers show

Comments

Durham Goode - June 30, 2015, 1:44 a.m.
# HG changeset patch
# User Durham Goode <durham@fb.com>
# Date 1435623598 25200
#      Mon Jun 29 17:19:58 2015 -0700
# Node ID c4c8a8f7781e3eb4d836c13e380155e8c5ebbebe
# Parent  3e439040f3a1fe09af1291691f1f788e0e28c48a
convert: improve support for unusual .gitmodules

Previously convert would throw an exception if it encountered a git commit with
a .gitmodules file that was malformed (i.e. was missing, but had submodule
files, or was malformed).

Instead of breaking the convert entirely, let's print error messages and move
on.

Patch

diff --git a/hgext/convert/git.py b/hgext/convert/git.py
--- a/hgext/convert/git.py
+++ b/hgext/convert/git.py
@@ -7,7 +7,7 @@ 
 
 import os
 import subprocess
-from mercurial import util, config
+from mercurial import util, config, error
 from mercurial.node import hex, nullid
 from mercurial.i18n import _
 
@@ -185,9 +185,19 @@  class convert_git(converter_source):
     def retrievegitmodules(self, version):
         modules, ret = self.gitread("git show %s:%s" % (version, '.gitmodules'))
         if ret:
-            raise util.Abort(_('cannot read submodules config file in %s') %
-                             version)
-        self.parsegitmodules(modules)
+            # This can happen if a file is in the repo that has permissions
+            # 160000, but there is no .gitmodules file.
+            self.ui.warn(_("warning: cannot read submodules config file in "
+                           "%s\n") % version)
+            return
+
+        try:
+            self.parsegitmodules(modules)
+        except error.ParseError:
+            self.ui.warn(_("warning: unable to parse .gitmodules in %s\n")
+                         % version)
+            return
+
         for m in self.submodules:
             node, ret = self.gitread("git rev-parse %s:%s" % (version, m.path))
             if ret:
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
@@ -482,6 +482,39 @@  test non-tab whitespace .gitmodules
   $ rm -rf hg-repo6
   $ cd git-repo6
   $ git reset --hard 'HEAD^' > /dev/null
+
+test missing .gitmodules
+
+  $ git submodule add ../git-repo4 >/dev/null 2>/dev/null
+  $ git checkout HEAD .gitmodules
+  $ git rm .gitmodules
+  rm '.gitmodules'
+  $ git commit -m "remove .gitmodules" .gitmodules
+  [master *] remove .gitmodules (glob)
+   Author: nottest <test@example.org>
+   1 file changed, 3 deletions(-)
+   delete mode 100644 .gitmodules
+  $ git commit -m "missing .gitmodules"
+  [master *] missing .gitmodules (glob)
+   Author: nottest <test@example.org>
+   1 file changed, 1 insertion(+)
+   create mode 160000 git-repo4
+  $ cd ..
+  $ hg convert git-repo6 hg-repo6 --traceback
+  fatal: Path '.gitmodules' does not exist in '*' (glob)
+  initializing destination hg-repo6 repository
+  scanning source...
+  sorting...
+  converting...
+  2 addsubmodule
+  1 remove .gitmodules
+  0 missing .gitmodules
+  warning: cannot read submodules config file in * (glob)
+  updating bookmarks
+  $ rm -rf hg-repo6
+  $ cd git-repo6
+  $ rm -rf git-repo4
+  $ git reset --hard 'HEAD^^' > /dev/null
   $ cd ..
 
 test invalid splicemap1