Jelajahi Sumber

Implement check-ignore.

Jelmer Vernooij 7 tahun lalu
induk
melakukan
6e1f3a58f8
4 mengubah file dengan 64 tambahan dan 0 penghapusan
  1. 3 0
      NEWS
  2. 10 0
      bin/dulwich
  3. 24 0
      dulwich/porcelain.py
  4. 27 0
      dulwich/tests/test_porcelain.py

+ 3 - 0
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.

+ 10 - 0
bin/dulwich

@@ -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,

+ 24 - 0
dulwich/porcelain.py

@@ -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

+ 27 - 0
dulwich/tests/test_porcelain.py

@@ -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)))