Rev 3533: (Jelmer) Move add ignore functionality out of cmd_ignore. in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Tue Jul 8 18:25:12 BST 2008


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

------------------------------------------------------------
revno: 3533
revision-id:pqm at pqm.ubuntu.com-20080708172503-gl2dtaz3v7fyc2sl
parent: pqm at pqm.ubuntu.com-20080708170938-gt3lvh2xejc4w8vf
parent: jelmer at samba.org-20080708163456-xb2f4tg7kn0cjhdc
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Tue 2008-07-08 18:25:03 +0100
message:
  (Jelmer) Move add ignore functionality out of cmd_ignore.
modified:
  bzrlib/builtins.py             builtins.py-20050830033751-fc01482b9ca23183
  bzrlib/ignores.py              ignores.py-20060712153832-2von9l0t7p43ixsv-1
  bzrlib/tests/test_ignores.py   test_ignores.py-20060712172354-vqq9ln0t8di27v53-1
    ------------------------------------------------------------
    revno: 3528.2.3
    revision-id:jelmer at samba.org-20080708163456-xb2f4tg7kn0cjhdc
    parent: jelmer at samba.org-20080708162615-4qydeb7fgnv79ei6
    committer: Jelmer Vernooij <jelmer at samba.org>
    branch nick: ignorelib
    timestamp: Tue 2008-07-08 18:34:56 +0200
    message:
      use constant for ignore file filename.
    modified:
      bzrlib/ignores.py              ignores.py-20060712153832-2von9l0t7p43ixsv-1
    ------------------------------------------------------------
    revno: 3528.2.2
    revision-id:jelmer at samba.org-20080708162615-4qydeb7fgnv79ei6
    parent: jelmer at samba.org-20080707215404-09t83ot6mv02jr6w
    parent: pqm at pqm.ubuntu.com-20080708161604-6lecb3dmsyzewuoi
    committer: Jelmer Vernooij <jelmer at samba.org>
    branch nick: ignorelib
    timestamp: Tue 2008-07-08 18:26:15 +0200
    message:
      Merge bzr.dev
    added:
      contrib/bash/bzrbashprompt.sh  bzrbashprompt.sh-20080414112733-b78chl4ubylc6775-1
    modified:
      bzrlib/smart/request.py        request.py-20061108095550-gunadhxmzkdjfeek-1
      bzrlib/tests/__init__.py       selftest.py-20050531073622-8d0e3c8845c97a64
      bzrlib/tests/test_smart.py     test_smart.py-20061122024551-ol0l0o0oofsu9b3t-2
    ------------------------------------------------------------
    revno: 3528.2.1
    revision-id:jelmer at samba.org-20080707215404-09t83ot6mv02jr6w
    parent: pqm at pqm.ubuntu.com-20080707151531-ra9moj8zrk9tiw2a
    committer: Jelmer Vernooij <jelmer at samba.org>
    branch nick: ignorelib
    timestamp: Mon 2008-07-07 23:54:04 +0200
    message:
      Move functionality to add ignores to the ignore file into a separate function.
    modified:
      bzrlib/builtins.py             builtins.py-20050830033751-fc01482b9ca23183
      bzrlib/ignores.py              ignores.py-20060712153832-2von9l0t7p43ixsv-1
      bzrlib/tests/test_ignores.py   test_ignores.py-20060712172354-vqq9ln0t8di27v53-1
=== modified file 'bzrlib/builtins.py'
--- a/bzrlib/builtins.py	2008-07-04 03:29:14 +0000
+++ b/bzrlib/builtins.py	2008-07-07 21:54:04 +0000
@@ -1976,7 +1976,7 @@
         ]
     
     def run(self, name_pattern_list=None, old_default_rules=None):
-        from bzrlib.atomicfile import AtomicFile
+        from bzrlib import ignores
         if old_default_rules is not None:
             # dump the rules and exit
             for pattern in ignores.OLD_DEFAULTS:
@@ -1993,34 +1993,7 @@
                 raise errors.BzrCommandError(
                     "NAME_PATTERN should not be an absolute path")
         tree, relpath = WorkingTree.open_containing(u'.')
-        ifn = tree.abspath('.bzrignore')
-        if os.path.exists(ifn):
-            f = open(ifn, 'rt')
-            try:
-                igns = f.read().decode('utf-8')
-            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'
-
-        f = AtomicFile(ifn, 'wb')
-        try:
-            f.write(igns.encode('utf-8'))
-            f.commit()
-        finally:
-            f.close()
-
-        if not tree.path2id('.bzrignore'):
-            tree.add(['.bzrignore'])
-
+        ignores.tree_ignores_add_patterns(tree, name_pattern_list)
         ignored = globbing.Globster(name_pattern_list)
         matches = []
         tree.lock_read()

=== modified file 'bzrlib/ignores.py'
--- a/bzrlib/ignores.py	2007-03-07 14:49:00 +0000
+++ b/bzrlib/ignores.py	2008-07-08 16:34:56 +0000
@@ -18,7 +18,9 @@
 
 import errno
 
+import bzrlib
 from bzrlib import (
+    atomicfile,
     config,
     globbing,
     )
@@ -205,3 +207,38 @@
 def get_runtime_ignores():
     """Get the current set of runtime ignores."""
     return _runtime_ignores
+
+
+def tree_ignores_add_patterns(tree, name_pattern_list):
+    """Retrieve a list of ignores from the ignore file in a tree.
+
+    :param tree: Tree to retrieve the ignore list from.
+    :return: 
+    """
+    ifn = tree.abspath(bzrlib.IGNORE_FILENAME)
+    if tree.has_filename(ifn):
+        f = open(ifn, 'rt')
+        try:
+            igns = f.read().decode('utf-8')
+        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'
+
+    f = atomicfile.AtomicFile(ifn, 'wb')
+    try:
+        f.write(igns.encode('utf-8'))
+        f.commit()
+    finally:
+        f.close()
+
+    if not tree.path2id('.bzrignore'):
+        tree.add(['.bzrignore'])

=== modified file 'bzrlib/tests/test_ignores.py'
--- a/bzrlib/tests/test_ignores.py	2007-03-07 14:49:00 +0000
+++ b/bzrlib/tests/test_ignores.py	2008-07-07 21:54:04 +0000
@@ -19,7 +19,7 @@
 from cStringIO import StringIO
 
 from bzrlib import config, errors, ignores
-from bzrlib.tests import TestCase, TestCaseInTempDir
+from bzrlib.tests import TestCase, TestCaseInTempDir, TestCaseWithTransport
 
 
 class TestParseIgnoreFile(TestCase):
@@ -150,3 +150,30 @@
 
         ignores.add_runtime_ignores(['bar'])
         self.assertEqual(set(['foo', 'bar']), ignores.get_runtime_ignores())
+
+
+class TestTreeIgnores(TestCaseWithTransport):
+
+    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())
+
+    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())
+
+    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())
+




More information about the bazaar-commits mailing list