Forráskód Böngészése

Honor ignores in dulwich.porcelain.add.

Jelmer Vernooij 7 éve
szülő
commit
b1b7dd6902
4 módosított fájl, 25 hozzáadás és 2 törlés
  1. 1 1
      NEWS
  2. 3 1
      dulwich/ignore.py
  3. 9 0
      dulwich/porcelain.py
  4. 12 0
      dulwich/tests/test_porcelain.py

+ 1 - 1
NEWS

@@ -17,7 +17,7 @@
  IMPROVEMENTS
 
   * Add basic support for reading ignore files in ``dulwich.ignore``.
-    Note that this is not yet hooked into the other parts of Dulwich.
+    ``dulwich.porcelain.add`` now honors ignores.
     (Jelmer Vernooij, #524)
 
  DOCUMENTATION

+ 3 - 1
dulwich/ignore.py

@@ -240,6 +240,8 @@ class IgnoreFilterManager(object):
         :return: None if the file is not mentioned, True if it is included,
             False if it is explicitly excluded.
         """
+        if not os.path.isabs(path):
+            path = os.path.join(self._top_path, path)
         dirname = path
         while dirname not in (self._top_path, '/'):
             dirname = os.path.dirname(dirname)
@@ -250,7 +252,7 @@ class IgnoreFilterManager(object):
                 if status is not None:
                     return status
         for ignore_filter in self._global_filters:
-            relpath = os.path.relpath(path, dirname)
+            relpath = os.path.relpath(path, self._top_path)
             status = ignore_filter.is_ignored(relpath)
             if status is not None:
                 return status

+ 9 - 0
dulwich/porcelain.py

@@ -79,6 +79,7 @@ from dulwich.errors import (
     SendPackError,
     UpdateRefsError,
     )
+from dulwich.ignore import IgnoreFilterManager
 from dulwich.index import get_unstaged_changes
 from dulwich.objects import (
     Commit,
@@ -312,8 +313,11 @@ def add(repo=".", paths=None):
 
     :param repo: Repository for the files
     :param paths: Paths to add.  No value passed stages all modified files.
+    :return: Tuple with set of added files and ignored files
     """
+    ignored = set()
     with open_repo_closing(repo) as r:
+        ignore_manager = IgnoreFilterManager.from_repo(r)
         if not paths:
             paths = list(
                 get_untracked_paths(os.getcwd(), r.path, r.open_index()))
@@ -322,6 +326,10 @@ def add(repo=".", paths=None):
         if not isinstance(paths, list):
             paths = [paths]
         for p in paths:
+            if ignore_manager.is_ignored(p):
+                ignored.add(p)
+                continue
+
             # FIXME: Support patterns, directories.
             if os.path.isabs(p) and p.startswith(repo.path):
                 relpath = os.path.relpath(p, repo.path)
@@ -329,6 +337,7 @@ def add(repo=".", paths=None):
                 relpath = p
             relpaths.append(relpath)
         r.stage(relpaths)
+    return (relpaths, ignored)
 
 
 def rm(repo=".", paths=None):

+ 12 - 0
dulwich/tests/test_porcelain.py

@@ -276,6 +276,18 @@ class AddTests(PorcelainTestCase):
         porcelain.add(self.repo.path, paths=["foo"])
         self.assertIn(b"foo", self.repo.open_index())
 
+    def test_add_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")
+        (added, ignored) = porcelain.add(self.repo.path, paths=["foo", "bar"])
+        self.assertIn(b"bar", self.repo.open_index())
+        self.assertEqual(set(['bar']), set(added))
+        self.assertEqual(set(['foo']), ignored)
+
     def test_add_file_absolute_path(self):
         # Absolute paths are (not yet) supported
         with open(os.path.join(self.repo.path, 'foo'), 'w') as f: