changeset 4759:20ec5cc02f18

convert: ove recode method into converter_source
author Brendan Cully <brendan@kublai.com>
date Sun, 01 Jul 2007 12:58:08 -0700
parents a72dd3bfce41
children 07efcce17d28
files hgext/convert/common.py hgext/convert/cvs.py hgext/convert/git.py
diffstat 3 files changed, 19 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/convert/common.py	Sun Jul 01 19:19:42 2007 +0200
+++ b/hgext/convert/common.py	Sun Jul 01 12:58:08 2007 -0700
@@ -44,6 +44,18 @@
         """Return the tags as a dictionary of name: revision"""
         raise NotImplementedError()
 
+    def recode(self, s, encoding=None):
+        if not encoding:
+            encoding = hasattr(self, 'encoding') and self.encoding or 'utf-8'
+            
+        try:
+            return s.decode(encoding).encode("utf-8")
+        except:
+            try:
+                return s.decode("latin-1").encode("utf-8")
+            except:
+                return s.decode(encoding, "replace").encode("utf-8")
+
 class converter_sink(object):
     """Conversion sink (target) interface"""
 
--- a/hgext/convert/cvs.py	Sun Jul 01 19:19:42 2007 +0200
+++ b/hgext/convert/cvs.py	Sun Jul 01 12:58:08 2007 -0700
@@ -235,9 +235,6 @@
         cl.sort()
         return cl
 
-    def recode(self, text):
-        return text.decode(self.encoding, "replace").encode("utf-8")
-
     def getcommit(self, rev):
         return self.changeset[rev]
 
--- a/hgext/convert/git.py	Sun Jul 01 19:19:42 2007 +0200
+++ b/hgext/convert/git.py	Sun Jul 01 12:58:08 2007 -0700
@@ -4,23 +4,16 @@
 
 from common import NoRepo, commit, converter_source
 
-def recode(s):
-    try:
-        return s.decode("utf-8").encode("utf-8")
-    except:
-        try:
-            return s.decode("latin-1").encode("utf-8")
-        except:
-            return s.decode("utf-8", "replace").encode("utf-8")
-
 class convert_git(converter_source):
     def __init__(self, ui, path):
         if os.path.isdir(path + "/.git"):
             path += "/.git"
+        if not os.path.exists(path + "/objects"):
+            raise NoRepo("couldn't open GIT repo %s" % path)
+
         self.path = path
         self.ui = ui
-        if not os.path.exists(path + "/objects"):
-            raise NoRepo("couldn't open GIT repo %s" % path)
+        self.encoding = 'utf-8'
 
     def getheads(self):
         fh = os.popen("GIT_DIR=%s git-rev-parse --verify HEAD" % self.path)
@@ -58,7 +51,7 @@
         c = self.catfile(version, "commit") # read the commit hash
         end = c.find("\n\n")
         message = c[end+2:]
-        message = recode(message)
+        message = self.recode(message)
         l = c[:end].splitlines()
         manifest = l[0].split()[1]
         parents = []
@@ -69,13 +62,13 @@
                 tm, tz = p[-2:]
                 author = " ".join(p[:-2])
                 if author[0] == "<": author = author[1:-1]
-                author = recode(author)
+                author = self.recode(author)
             if n == "committer":
                 p = v.split()
                 tm, tz = p[-2:]
                 committer = " ".join(p[:-2])
                 if committer[0] == "<": committer = committer[1:-1]
-                committer = recode(committer)
+                committer = self.recode(committer)
                 message += "\ncommitter: %s\n" % committer
             if n == "parent": parents.append(v)