Rev 2478: (bialix, r=john, martin) Bugfix #110901: commit message template written in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Thu May 3 10:47:34 BST 2007


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 2478
revision-id: pqm at pqm.ubuntu.com-20070503094732-4ypcrctuxb93rwcv
parent: pqm at pqm.ubuntu.com-20070503064943-wh3ls1tw3q2awye0
parent: bialix at ukr.net-20070501090918-km46cslj820tiyzl
committer: Canonical.com Patch Queue Manager<pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Thu 2007-05-03 10:47:32 +0100
message:
  (bialix,r=john,martin) Bugfix #110901: commit message template written
   with native line-endings; corresponding unit tests added
modified:
  bzrlib/msgeditor.py            msgeditor.py-20050901111708-ef6d8de98f5d8f2f
  bzrlib/tests/__init__.py       selftest.py-20050531073622-8d0e3c8845c97a64
  bzrlib/tests/test_msgeditor.py test_msgeditor.py-20051202041359-920315ec6011ee51
    ------------------------------------------------------------
    revno: 2472.4.2
    merged: bialix at ukr.net-20070501090918-km46cslj820tiyzl
    parent: bialix at ukr.net-20070430195244-pxc5r3x72ckh027e
    committer: Alexander Belchenko <bialix at ukr.net>
    branch nick: win32.fixes
    timestamp: Tue 2007-05-01 12:09:18 +0300
    message:
      unwrapping long lines in tests
    ------------------------------------------------------------
    revno: 2472.4.1
    merged: bialix at ukr.net-20070430195244-pxc5r3x72ckh027e
    parent: pqm at pqm.ubuntu.com-20070430083158-pitv7lbgdu0q8g6h
    committer: Alexander Belchenko <bialix at ukr.net>
    branch nick: win32.fixes
    timestamp: Mon 2007-04-30 22:52:44 +0300
    message:
      Bugfix #110901: commit message template written with native line-endings; corresponding unit tests added
=== modified file 'bzrlib/msgeditor.py'
--- a/bzrlib/msgeditor.py	2007-04-30 01:49:39 +0000
+++ b/bzrlib/msgeditor.py	2007-04-30 19:52:44 +0000
@@ -87,48 +87,33 @@
     This is run if they don't give a message or
     message-containing file on the command line.
 
-    infotext:
-        Text to be displayed at bottom of message for
-        the user's reference; currently similar to
-        'bzr status'.
-
-    ignoreline:
-        The separator to use above the infotext.
-
-    start_message:
-        The text to place above the separator, if any. This will not be
-        removed from the message after the user has edited it.
+    :param infotext:    Text to be displayed at bottom of message
+                        for the user's reference;
+                        currently similar to 'bzr status'.
+
+    :param ignoreline:  The separator to use above the infotext.
+
+    :param start_message:   The text to place above the separator, if any.
+                            This will not be removed from the message
+                            after the user has edited it.
+
+    :return:    commit message or None.
     """
-    import tempfile
-
     msgfilename = None
     try:
-        tmp_fileno, msgfilename = tempfile.mkstemp(prefix='bzr_log.',
-                                                   dir=u'.',
-                                                   text=True)
-        msgfile = os.fdopen(tmp_fileno, 'w')
-        try:
-            if start_message is not None:
-                msgfile.write("%s\n" % start_message.encode(
-                                           bzrlib.user_encoding, 'replace'))
-
-            if infotext is not None and infotext != "":
-                hasinfo = True
-                msgfile.write("\n\n%s\n\n%s" % (ignoreline,
-                              infotext.encode(bzrlib.user_encoding,
-                                                    'replace')))
-            else:
-                hasinfo = False
-        finally:
-            msgfile.close()
-
-        if not _run_editor(msgfilename):
+        msgfilename, hasinfo = _create_temp_file_with_commit_template(
+                                    infotext, ignoreline, start_message)
+
+        if not msgfilename or not _run_editor(msgfilename):
             return None
         
         started = False
         msg = []
         lastline, nlines = 0, 0
-        for line in codecs.open(msgfilename, 'r', bzrlib.user_encoding):
+        # codecs.open() ALWAYS opens file in binary mode but we need text mode
+        # 'rU' mode useful when bzr.exe used on Cygwin (bialix 20070430)
+        f = file(msgfilename, 'rU')
+        for line in codecs.getreader(bzrlib.user_encoding)(f):
             stripped_line = line.strip()
             # strip empty line before the log message starts
             if not started:
@@ -145,7 +130,8 @@
             if stripped_line != "":
                 lastline = nlines
             msg.append(line)
-            
+        f.close()
+
         if len(msg) == 0:
             return ""
         # delete empty lines at the end
@@ -164,6 +150,46 @@
                 warning("failed to unlink %s: %s; ignored", msgfilename, e)
 
 
+def _create_temp_file_with_commit_template(infotext,
+                                           ignoreline=DEFAULT_IGNORE_LINE,
+                                           start_message=None):
+    """Create temp file and write commit template in it.
+
+    :param infotext:    Text to be displayed at bottom of message
+                        for the user's reference;
+                        currently similar to 'bzr status'.
+
+    :param ignoreline:  The separator to use above the infotext.
+
+    :param start_message:   The text to place above the separator, if any.
+                            This will not be removed from the message
+                            after the user has edited it.
+
+    :return:    2-tuple (temp file name, hasinfo)
+    """
+    import tempfile
+    tmp_fileno, msgfilename = tempfile.mkstemp(prefix='bzr_log.',
+                                               dir=u'.',
+                                               text=True)
+    msgfile = os.fdopen(tmp_fileno, 'w')
+    try:
+        if start_message is not None:
+            msgfile.write("%s\n" % start_message.encode(
+                                       bzrlib.user_encoding, 'replace'))
+
+        if infotext is not None and infotext != "":
+            hasinfo = True
+            msgfile.write("\n\n%s\n\n%s" % (ignoreline,
+                          infotext.encode(bzrlib.user_encoding,
+                                                'replace')))
+        else:
+            hasinfo = False
+    finally:
+        msgfile.close()
+
+    return (msgfilename, hasinfo)
+
+
 def make_commit_message_template(working_tree, specific_files):
     """Prepare a template file for a commit into a branch.
 

=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2007-04-26 09:07:38 +0000
+++ b/bzrlib/tests/__init__.py	2007-04-30 19:52:44 +0000
@@ -1964,8 +1964,7 @@
     def assertFileEqual(self, content, path):
         """Fail if path does not contain 'content'."""
         self.failUnlessExists(path)
-        # TODO: jam 20060427 Shouldn't this be 'rb'?
-        f = file(path, 'r')
+        f = file(path, 'rb')
         try:
             s = f.read()
         finally:

=== modified file 'bzrlib/tests/test_msgeditor.py'
--- a/bzrlib/tests/test_msgeditor.py	2007-02-04 21:58:50 +0000
+++ b/bzrlib/tests/test_msgeditor.py	2007-05-01 09:09:18 +0000
@@ -187,3 +187,26 @@
                 del os.environ['EDITOR']
             else:
                 os.environ['EDITOR'] = editor
+
+    def test__create_temp_file_with_commit_template(self):
+        # check that commit template written properly
+        # and has platform native line-endings (CRLF on win32)
+        create_file = bzrlib.msgeditor._create_temp_file_with_commit_template
+        msgfilename, hasinfo = create_file('infotext','----','start message')
+        self.assertNotEqual(None, msgfilename)
+        self.assertTrue(hasinfo)
+        expected = os.linesep.join(['start message',
+                                    '',
+                                    '',
+                                    '----',
+                                    '',
+                                    'infotext'])
+        self.assertFileEqual(expected, msgfilename)
+
+    def test__create_temp_file_with_empty_commit_template(self):
+        # empty file
+        create_file = bzrlib.msgeditor._create_temp_file_with_commit_template
+        msgfilename, hasinfo = create_file('')
+        self.assertNotEqual(None, msgfilename)
+        self.assertFalse(hasinfo)
+        self.assertFileEqual('', msgfilename)




More information about the bazaar-commits mailing list