Rev 5207: (doxxx) 'ignore' command avoids putting duplicate entry into in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Wed May 5 09:14:12 BST 2010


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

------------------------------------------------------------
revno: 5207 [merge]
revision-id: pqm at pqm.ubuntu.com-20100505081410-kuy3vpsv2hmxk1kq
parent: pqm at pqm.ubuntu.com-20100505040408-cnykrpfzu689ohgs
parent: parth.malwankar at gmail.com-20100505063106-90j6wjvuzjhhtcex
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Wed 2010-05-05 09:14:10 +0100
message:
  (doxxx) 'ignore' command avoids putting duplicate entry into
  	.bzrignore
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/ignores.py              ignores.py-20060712153832-2von9l0t7p43ixsv-1
  bzrlib/tests/test_ignores.py   test_ignores.py-20060712172354-vqq9ln0t8di27v53-1
=== modified file 'NEWS'
--- a/NEWS	2010-05-04 22:02:05 +0000
+++ b/NEWS	2010-05-05 06:31:06 +0000
@@ -44,6 +44,9 @@
   better with sudo.
   (Martin <gzlist at googlemail.com>, Parth Malwankar, #376388)
 
+* ``bzr ignore`` will no longer add duplicate patterns to .bzrignore.
+  (Gordon Tyler, #572092)
+
 * ``bzr log --exclude-common-ancestry -r X..Y`` displays the revisions that
   are part of Y ancestry but not part of X ancestry (aka the graph
   difference).

=== modified file 'bzrlib/ignores.py'
--- a/bzrlib/ignores.py	2010-04-30 07:54:33 +0000
+++ b/bzrlib/ignores.py	2010-05-03 04:51:58 +0000
@@ -17,6 +17,8 @@
 """Lists of ignore files, etc."""
 
 import errno
+import os
+from cStringIO import StringIO
 
 import bzrlib
 from bzrlib import (
@@ -181,28 +183,44 @@
     The ignore file will be automatically added under version control.
 
     :param tree: Working tree to update the ignore list.
+    :param name_pattern_list: List of ignore patterns.
+    :return: None
     """
+    # read in the existing ignores set
     ifn = tree.abspath(bzrlib.IGNORE_FILENAME)
     if tree.has_filename(ifn):
-        f = open(ifn, 'rt')
+        f = open(ifn, 'rU')
         try:
-            igns = f.read().decode('utf-8')
+            file_contents = f.read()
+            # figure out what kind of line endings are used
+            newline = getattr(f, 'newlines', None)
+            if type(newline) is tuple:
+                newline = newline[0]
+            elif newline is None:
+                newline = os.linesep
         finally:
             f.close()
     else:
-        igns = ""
-
-    # TODO: If the file already uses crlf-style termination, maybe
-    # we should use that for the newly added lines?
-
-    if igns and igns[-1] != '\n':
-        igns += '\n'
-    for name_pattern in name_pattern_list:
-        igns += name_pattern + '\n'
-
+        file_contents = ""
+        newline = os.linesep
+    
+    sio = StringIO(file_contents)
+    try:
+        ignores = parse_ignore_file(sio)
+    finally:
+        sio.close()
+    
+    # write out the updated ignores set
     f = atomicfile.AtomicFile(ifn, 'wb')
     try:
-        f.write(igns.encode('utf-8'))
+        # write the original contents, preserving original line endings
+        f.write(newline.join(file_contents.split('\n')))
+        if len(file_contents) > 0 and not file_contents.endswith('\n'):
+            f.write(newline)
+        for pattern in name_pattern_list:
+            if not pattern in ignores:
+                f.write(pattern.encode('utf-8'))
+                f.write(newline)
         f.commit()
     finally:
         f.close()

=== modified file 'bzrlib/tests/test_ignores.py'
--- a/bzrlib/tests/test_ignores.py	2009-12-26 21:31:56 +0000
+++ b/bzrlib/tests/test_ignores.py	2010-05-03 04:51:58 +0000
@@ -165,27 +165,55 @@
 
 
 class TestTreeIgnores(TestCaseWithTransport):
+    
+    def assertPatternsEquals(self, patterns):
+        contents = open(".bzrignore", 'rU').read().strip().split('\n')
+        self.assertEquals(sorted(patterns), sorted(contents))
 
     def test_new_file(self):
         tree = self.make_branch_and_tree(".")
         ignores.tree_ignores_add_patterns(tree, ["myentry"])
         self.assertTrue(tree.has_filename(".bzrignore"))
-        self.assertEquals("myentry\n",
-                          open(".bzrignore", 'r').read())
+        self.assertPatternsEquals(["myentry"])
 
     def test_add_to_existing(self):
         tree = self.make_branch_and_tree(".")
         self.build_tree_contents([('.bzrignore', "myentry1\n")])
         tree.add([".bzrignore"])
         ignores.tree_ignores_add_patterns(tree, ["myentry2", "foo"])
-        self.assertEquals("myentry1\nmyentry2\nfoo\n",
-                          open(".bzrignore", 'r').read())
+        self.assertPatternsEquals(["myentry1", "myentry2", "foo"])
 
     def test_adds_ending_newline(self):
         tree = self.make_branch_and_tree(".")
         self.build_tree_contents([('.bzrignore', "myentry1")])
         tree.add([".bzrignore"])
         ignores.tree_ignores_add_patterns(tree, ["myentry2"])
-        self.assertEquals("myentry1\nmyentry2\n",
-                          open(".bzrignore", 'r').read())
-
+        self.assertPatternsEquals(["myentry1", "myentry2"])
+        text = open(".bzrignore", 'r').read()
+        self.assertTrue(text.endswith('\r\n') or
+                        text.endswith('\n') or
+                        text.endswith('\r'))
+
+    def test_does_not_add_dupe(self):
+        tree = self.make_branch_and_tree(".")
+        self.build_tree_contents([('.bzrignore', "myentry\n")])
+        tree.add([".bzrignore"])
+        ignores.tree_ignores_add_patterns(tree, ["myentry"])
+        self.assertPatternsEquals(["myentry"])
+
+    def test_non_ascii(self):
+        tree = self.make_branch_and_tree(".")
+        self.build_tree_contents([('.bzrignore',
+                                   u"myentry\u1234\n".encode('utf-8'))])
+        tree.add([".bzrignore"])
+        ignores.tree_ignores_add_patterns(tree, [u"myentry\u5678"])
+        self.assertPatternsEquals([u"myentry\u1234".encode('utf-8'),
+                                   u"myentry\u5678".encode('utf-8')])
+
+    def test_crlf(self):
+        tree = self.make_branch_and_tree(".")
+        self.build_tree_contents([('.bzrignore', "myentry1\r\n")])
+        tree.add([".bzrignore"])
+        ignores.tree_ignores_add_patterns(tree, ["myentry2", "foo"])
+        self.assertEquals(open('.bzrignore', 'rb').read(), 'myentry1\r\nmyentry2\r\nfoo\r\n')
+        self.assertPatternsEquals(["myentry1", "myentry2", "foo"])




More information about the bazaar-commits mailing list