Browse Source

Support passing repo as a Repo object to porcelain.clean.

Jelmer Vernooij 6 years ago
parent
commit
917ab6b832
4 changed files with 18 additions and 30 deletions
  1. 1 0
      AUTHORS
  2. 2 0
      NEWS
  3. 3 3
      dulwich/porcelain.py
  4. 12 27
      dulwich/tests/test_porcelain.py

+ 1 - 0
AUTHORS

@@ -147,5 +147,6 @@ Boris Feld <lothiraldan@gmail.com>
 KS Chan <mrkschan@gmail.com>
 egor <egor@sourced.tech>
 Antoine Lambert <anlambert@softwareheritage.org>
+Lane Barlow <lane.barlow@gmail.com>
 
 If you contributed but are missing from this list, please send me an e-mail.

+ 2 - 0
NEWS

@@ -5,6 +5,8 @@
  * Update directory detection for `get_unstaged_changes` for Python 3.
    (Boris Feld, #684)
 
+ * Add a basic ``porcelain.clean``. (Lane Barlow, #398)
+
 0.19.11	2019-02-07
 
  IMPROVEMENTS

+ 3 - 3
dulwich/porcelain.py

@@ -431,10 +431,10 @@ def clean(repo=".", target_dir=None):
     if target_dir is None:
         target_dir = os.getcwd()
 
-    if not _is_subdir(target_dir, repo):
-        raise ValueError("target_dir must be in the repo's working dir")
-
     with open_repo_closing(repo) as r:
+        if not _is_subdir(target_dir, r.path):
+            raise ValueError("target_dir must be in the repo's working dir")
+
         index = r.open_index()
         ignore_manager = IgnoreFilterManager.from_repo(r)
 

+ 12 - 27
dulwich/tests/test_porcelain.py

@@ -130,17 +130,13 @@ class CommitTests(PorcelainTestCase):
 
 
 class CleanTests(PorcelainTestCase):
-    def path_in_wd(self, name):
-        """Get path of file in wd
-        """
-        return os.path.join(self.repo.path, name)
 
     def put_files(self, tracked, ignored, untracked, empty_dirs):
         """Put the described files in the wd
         """
         all_files = tracked | ignored | untracked
         for file_path in all_files:
-            abs_path = self.path_in_wd(file_path)
+            abs_path = os.path.join(self.repo.path, file_path)
             # File may need to be written in a dir that doesn't exist yet, so
             # create the parent dir(s) as necessary
             parent_dir = os.path.dirname(abs_path)
@@ -152,40 +148,28 @@ class CleanTests(PorcelainTestCase):
             with open(abs_path, 'w') as f:
                 f.write('')
 
-        with open(self.path_in_wd('.gitignore'), 'w') as f:
+        with open(os.path.join(self.repo.path, '.gitignore'), 'w') as f:
             f.writelines(ignored)
 
         for dir_path in empty_dirs:
-            os.mkdir(self.path_in_wd('empty_dir'))
+            os.mkdir(os.path.join(self.repo.path, 'empty_dir'))
 
-        files_to_add = [self.path_in_wd(t) for t in tracked]
+        files_to_add = [os.path.join(self.repo.path, t) for t in tracked]
         porcelain.add(repo=self.repo.path, paths=files_to_add)
         porcelain.commit(repo=self.repo.path, message="init commit")
 
-    def clean(self, target_dir):
-        """Clean the target_dir and assert control dir unchanged
-        """
-        controldir = self.repo._controldir
-
-        controldir_before = set(flat_walk_dir(controldir))
-        porcelain.clean(repo=self.repo.path, target_dir=target_dir)
-        controldir_after = set(flat_walk_dir(controldir))
-
-        self.assertEqual(controldir_after, controldir_before)
-
     def assert_wd(self, expected_paths):
         """Assert paths of files and dirs in wd are same as expected_paths
         """
-        control_dir = self.repo._controldir
-        control_dir_rel = os.path.relpath(control_dir, self.repo.path)
+        control_dir_rel = os.path.relpath(
+            self.repo._controldir, self.repo.path)
 
         # normalize paths to simplify comparison across platforms
-        from os.path import normpath
         found_paths = {
-            normpath(p)
+            os.path.normpath(p)
             for p in flat_walk_dir(self.repo.path)
             if not p.split(os.sep)[0] == control_dir_rel}
-        norm_expected_paths = {normpath(p) for p in expected_paths}
+        norm_expected_paths = {os.path.normpath(p) for p in expected_paths}
         self.assertEqual(found_paths, norm_expected_paths)
 
     def test_from_root(self):
@@ -203,7 +187,7 @@ class CleanTests(PorcelainTestCase):
             empty_dirs={
                 'empty_dir'})
 
-        self.clean(self.repo.path)
+        porcelain.clean(repo=self.repo.path, target_dir=self.repo.path)
 
         self.assert_wd({
             'tracked_file',
@@ -227,8 +211,9 @@ class CleanTests(PorcelainTestCase):
             empty_dirs={
                 'empty_dir'})
 
-        target_dir = self.path_in_wd('untracked_dir')
-        self.clean(target_dir)
+        porcelain.clean(
+            repo=self.repo,
+            target_dir=os.path.join(self.repo.path, 'untracked_dir'))
 
         self.assert_wd({
             'tracked_file',