Implement check-ignore.
authorJelmer Vernooij <jelmer@jelmer.uk>
Thu, 13 Jul 2017 00:27:20 +0000 (00:27 +0000)
committerJelmer Vernooij <jelmer@jelmer.uk>
Thu, 13 Jul 2017 00:27:20 +0000 (00:27 +0000)
NEWS
bin/dulwich
dulwich/porcelain.py
dulwich/tests/test_porcelain.py

diff --git a/NEWS b/NEWS
index 82ab8de9b6058cad2d33e70ba2dbe41684e7d3f2..6a7645e0470eb6cdfb3f4003595b896d05a4ac85 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -20,6 +20,9 @@
     ``dulwich.porcelain.add`` now honors ignores.
     (Jelmer Vernooij, #524)
 
+ * New ``dulwich.porcelain.check_ignore`` command.
+   (Jelmer Vernooij)
+
  DOCUMENTATION
 
   * Clarified docstrings for Client.{send_pack,fetch_pack} implementations.
index b9a2465580647da07c626cae0b3252b17886cbc2..6fc91645e428a66705cdd790078cf44b1603d006 100755 (executable)
@@ -507,6 +507,15 @@ class cmd_remote(Command):
         return cmd_kls(args[1:])
 
 
+class cmd_check_ignore(Command):
+
+    def run(self, args):
+        parser = optparse.OptionParser()
+        options, args = parser.parse_args(args)
+        for path in porcelain.check_ignore('.', args):
+            print(path)
+
+
 class cmd_help(Command):
 
     def run(self, args):
@@ -532,6 +541,7 @@ For a list of supported commands, see 'dulwich help -a'.
 commands = {
     "add": cmd_add,
     "archive": cmd_archive,
+    "check-ignore": cmd_check_ignore,
     "clone": cmd_clone,
     "commit": cmd_commit,
     "commit-tree": cmd_commit_tree,
index 4aff53fa859f7480d64af3f3fb182058bea985c4..e34b0ad51760dd237d1d8381ec5e699003e2674b 100644 (file)
@@ -24,6 +24,7 @@ Currently implemented:
  * archive
  * add
  * branch{_create,_delete,_list}
+ * check-ignore
  * clone
  * commit
  * commit-tree
@@ -1043,3 +1044,26 @@ def remote_add(repo, name, url):
             raise RemoteExists(section)
         c.set(section, b"url", url)
         c.write_to_path()
+
+
+def check_ignore(repo, paths, no_index=False):
+    """Debug gitignore files.
+
+    :param repo: Path to the repository
+    :param paths: List of paths to check for
+    :param no_index: Don't check index
+    :return: List of ignored files
+    """
+    with open_repo_closing(repo) as r:
+        index = r.open_index()
+        ignore_manager = IgnoreFilterManager.from_repo(r)
+        for path in paths:
+            if os.path.isdir(path):
+                continue
+            if os.path.isabs(path):
+                path = os.path.relpath(path, r.path)
+            if (not no_index and
+                    path.encode(sys.getfilesystemencoding()) in index):
+                continue
+            if ignore_manager.is_ignored(path):
+                yield path
index ee4ae3849d6e09079450080902713c8b055c00f7..a448c9bb8f1419ba2154a480a5d5eb92ac9ddcd3 100644 (file)
@@ -982,3 +982,30 @@ class RemoteAddTests(PorcelainTestCase):
             self.repo, 'jelmer', 'git://jelmer.uk/code/dulwich')
         self.assertRaises(porcelain.RemoteExists, porcelain.remote_add,
                           self.repo, 'jelmer', 'git://jelmer.uk/code/dulwich')
+
+
+class CheckIgnoreTests(PorcelainTestCase):
+
+    def test_check_ignored(self):
+        with open(os.path.join(self.repo.path, '.gitignore'), 'w') as f:
+            f.write("foo")
+        with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
+            f.write("BAR")
+        with open(os.path.join(self.repo.path, 'bar'), 'w') as f:
+            f.write("BAR")
+        self.assertEqual(
+            ['foo'],
+            list(porcelain.check_ignore(self.repo, ['foo'])))
+        self.assertEqual([], list(porcelain.check_ignore(self.repo, ['bar'])))
+
+    def test_check_added(self):
+        with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
+            f.write("BAR")
+        self.repo.stage(['foo'])
+        with open(os.path.join(self.repo.path, '.gitignore'), 'w') as f:
+            f.write("foo\n")
+        self.assertEqual(
+            [], list(porcelain.check_ignore(self.repo, ['foo'])))
+        self.assertEqual(
+            ['foo'],
+            list(porcelain.check_ignore(self.repo, ['foo'], no_index=True)))