changeset 1045:67e11b650e94

handle invalid UTF-8 in commit messages consistently 312f36a425f0 fixed this for replay, but not for stupid mode; we address this by consolidating our means of generating a Mercurial commit message in a single function in 'util.py'.
author Dan Villiom Podlaski Christiansen <dan@cabo.dk>
date Thu, 08 Aug 2013 09:38:55 +0200
parents d741f536f23a
children 36fe4b316a6b
files hgsubversion/replay.py hgsubversion/stupid.py hgsubversion/svnmeta.py hgsubversion/util.py hgsubversion/wrappers.py tests/test_fetch_command.py
diffstat 6 files changed, 26 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/replay.py
+++ b/hgsubversion/replay.py
@@ -51,16 +51,6 @@ def updateexternals(ui, meta, current):
             else:
                 current.delete(path)
 
-
-def _safe_message(msg):
-  if msg:
-      try:
-          msg.decode('utf-8')
-      except UnicodeDecodeError:
-          # ancient svn failed to enforce utf8 encoding
-          return msg.decode('iso-8859-1').encode('utf-8')
-  return msg
-
 def convert_rev(ui, meta, svn, r, tbdelta, firstrun):
     try:
         return _convert_rev(ui, meta, svn, r, tbdelta, firstrun)
@@ -177,11 +167,10 @@ def _convert_rev(ui, meta, svn, r, tbdel
                                       islink=islink, isexec=isexec,
                                       copied=copied)
 
-        message = _safe_message(rev.message)
         meta.mapbranch(extra)
         current_ctx = context.memctx(meta.repo,
                                      parents,
-                                     message or util.default_commit_msg(ui),
+                                     util.getmessage(ui, rev),
                                      files.keys(),
                                      filectxfn,
                                      meta.authors[rev.author],
@@ -217,7 +206,7 @@ def _convert_rev(ui, meta, svn, r, tbdel
 
         current_ctx = context.memctx(meta.repo,
                                      (ha, node.nullid),
-                                     _safe_message(rev.message) or ' ',
+                                     util.getmessage(ui, rev),
                                      [],
                                      del_all_files,
                                      meta.authors[rev.author],
--- a/hgsubversion/stupid.py
+++ b/hgsubversion/stupid.py
@@ -804,7 +804,7 @@ def convert_rev(ui, meta, svn, r, tbdelt
         meta.mapbranch(extra)
         current_ctx = context.memctx(meta.repo,
                                      [parentctx.node(), revlog.nullid],
-                                     r.message or util.default_commit_msg(ui),
+                                     util.getmessage(ui, r),
                                      files_touched,
                                      filectxfn,
                                      meta.authors[r.author],
--- a/hgsubversion/svnmeta.py
+++ b/hgsubversion/svnmeta.py
@@ -573,7 +573,7 @@ class SVNMeta(object):
             revnum, branch = self.get_source_rev(ctx=parentctx)[:2]
         ctx = context.memctx(self.repo,
                              (parentctx.node(), node.nullid),
-                             rev.message or util.default_commit_msg(self.ui),
+                             util.getmessage(self.ui, rev),
                              ['.hgtags', ],
                              hgtagsfn,
                              self.authors[rev.author],
@@ -638,7 +638,7 @@ class SVNMeta(object):
 
             ctx = context.memctx(self.repo,
                                  (parent.node(), node.nullid),
-                                 rev.message or ' ',
+                                 util.getmessage(self.ui, rev),
                                  ['.hgtags'],
                                  fctxfun,
                                  self.authors[rev.author],
@@ -660,7 +660,7 @@ class SVNMeta(object):
         self.mapbranch(extra, True)
         ctx = context.memctx(self.repo,
                              (node, revlog.nullid),
-                             rev.message or util.default_commit_msg(self.ui),
+                             util.getmessage(self.ui, rev),
                              [],
                              lambda x, y, z: None,
                              self.authors[rev.author],
--- a/hgsubversion/util.py
+++ b/hgsubversion/util.py
@@ -220,8 +220,19 @@ def outgoing_common_and_heads(repo, reve
         return ([sourcecx.node()], [sourcerev])
     return ([sourcerev], [sourcerev]) # nothing outgoing
 
-def default_commit_msg(ui):
-    return ui.config('hgsubversion', 'defaultmessage', '')
+def getmessage(ui, rev):
+    msg = rev.message
+
+    if msg:
+        try:
+            msg.decode('utf-8')
+            return msg
+
+        except UnicodeDecodeError:
+            # ancient svn failed to enforce utf8 encoding
+            return msg.decode('iso-8859-1').encode('utf-8')
+    else:
+        return ui.config('hgsubversion', 'defaultmessage', '')
 
 def describe_commit(ui, h, b):
     ui.note(' committed to "%s" as %s\n' % ((b or 'default'), node.short(h)))
--- a/hgsubversion/wrappers.py
+++ b/hgsubversion/wrappers.py
@@ -403,12 +403,8 @@ def pull(repo, source, heads=[], force=F
                 converted = False
                 while not converted:
                     try:
-                        msg = ''
-                        if r.message:
-                            msg = r.message.strip()
-                        if not msg:
-                            msg = util.default_commit_msg(ui)
-                        else:
+                        msg = util.getmessage(ui, r).strip()
+                        if msg:
                             msg = [s.strip() for s in msg.splitlines() if s][0]
                         if getattr(ui, 'termwidth', False):
                             w = ui.termwidth()
--- a/tests/test_fetch_command.py
+++ b/tests/test_fetch_command.py
@@ -191,13 +191,16 @@ class TestBasicRepoLayout(test_util.Test
         self.assertMultiLineEqual(open(fixturepaths[0]).read(),
                                   open(fixturepaths[1]).read())
 
-    def test_invalid_message(self):
-        repo = self._load_fixture_and_fetch('invalid_utf8.tar.gz')
+    def test_invalid_message(self, stupid=False):
+        repo = self._load_fixture_and_fetch('invalid_utf8.tar.gz', stupid=stupid)
         # changelog returns descriptions in local encoding
         desc = encoding.fromlocal(repo[0].description())
         self.assertEqual(desc.decode('utf8'),
                          u'bl\xe5b\xe6rgr\xf8d')
 
+    def test_invalid_message_stupid(self):
+        self.test_invalid_message(True)
+
 
 class TestStupidPull(test_util.TestBase):
     def test_stupid(self):