diff hggit/git_handler.py @ 764:13a3513f8e67

git_handler: support extracting metadata from Git extra fields Git has a well-hidden notion of extra fields. They aren't accessible from the CLI, but Dulwich can create them. They're a better place to store Mercurial's extra metadata than the commit message. An upcoming patch will switch hg-git to saving the metadata in the Git extra fields. This commit: (a) prepares for that (b) extracts other Git extra fields so that commits from Git with such fields roundtrip correctly
author Siddharth Agarwal <sid0@fb.com>
date Sun, 31 Aug 2014 06:49:18 -0700
parents eb9ebc7ed061
children 70aba6b2fe7b
line wrap: on
line diff
--- a/hggit/git_handler.py
+++ b/hggit/git_handler.py
@@ -680,7 +680,8 @@
         self.ui.debug(_("importing: %s\n") % commit.id)
 
         (strip_message, hg_renames,
-         hg_branch, extra) = self.extract_hg_metadata(commit.message)
+         hg_branch, extra) = self.extract_hg_metadata(
+             commit.message, commit.extra)
 
         gparents = map(self.map_hg_get, commit.parents)
 
@@ -1244,7 +1245,7 @@
             return convert[mode]
         return ''
 
-    def extract_hg_metadata(self, message):
+    def extract_hg_metadata(self, message, git_extra):
         split = message.split("\n--HG--\n", 1)
         renames = {}
         extra = {}
@@ -1266,8 +1267,27 @@
                 if command == 'branch':
                     branch = data
                 if command == 'extra':
-                    before, after = data.split(" : ", 1)
-                    extra[before] = urllib.unquote(after)
+                    k, v = data.split(" : ", 1)
+                    extra[k] = urllib.unquote(v)
+
+        git_fn = 0
+        for field, data in git_extra:
+            if field.startswith('HG:'):
+                command = field[3:]
+                if command == 'rename':
+                    before, after = data.split(':', 1)
+                    renames[urllib.unquote(after)] = urllib.unquote(before)
+                elif command == 'extra':
+                    k, v = data.split(':', 1)
+                    extra[urllib.unquote(k)] = urllib.unquote(v)
+            else:
+                # preserve ordering in Git by using an incrementing integer for
+                # each field. Note that extra metadata in Git is an ordered list
+                # of pairs.
+                hg_field = 'GIT%d-%s' % (git_fn, field)
+                git_fn += 1
+                extra[urllib.quote(hg_field)] = urllib.quote(data)
+
         return (message, renames, branch, extra)
 
     def get_file(self, commit, f):