Dulwich.io dulwich / 917ab6b
Support passing repo as a Repo object to porcelain.clean. Jelmer Vernooń≥ 2 months ago
4 changed file(s) with 20 addition(s) and 32 deletion(s). Raw diff Collapse all Expand all
146146 KS Chan <mrkschan@gmail.com>
147147 egor <egor@sourced.tech>
148148 Antoine Lambert <anlambert@softwareheritage.org>
149 Lane Barlow <lane.barlow@gmail.com>
149150
150151 If you contributed but are missing from this list, please send me an e-mail.
33
44 * Update directory detection for `get_unstaged_changes` for Python 3.
55 (Boris Feld, #684)
6
7 * Add a basic ``porcelain.clean``. (Lane Barlow, #398)
68
79 0.19.11 2019-02-07
810
430430 if target_dir is None:
431431 target_dir = os.getcwd()
432432
433 if not _is_subdir(target_dir, repo):
434 raise ValueError("target_dir must be in the repo's working dir")
435
436 with open_repo_closing(repo) as r:
433 with open_repo_closing(repo) as r:
434 if not _is_subdir(target_dir, r.path):
435 raise ValueError("target_dir must be in the repo's working dir")
436
437437 index = r.open_index()
438438 ignore_manager = IgnoreFilterManager.from_repo(r)
439439
129129
130130
131131 class CleanTests(PorcelainTestCase):
132 def path_in_wd(self, name):
133 """Get path of file in wd
134 """
135 return os.path.join(self.repo.path, name)
136132
137133 def put_files(self, tracked, ignored, untracked, empty_dirs):
138134 """Put the described files in the wd
139135 """
140136 all_files = tracked | ignored | untracked
141137 for file_path in all_files:
142 abs_path = self.path_in_wd(file_path)
138 abs_path = os.path.join(self.repo.path, file_path)
143139 # File may need to be written in a dir that doesn't exist yet, so
144140 # create the parent dir(s) as necessary
145141 parent_dir = os.path.dirname(abs_path)
151147 with open(abs_path, 'w') as f:
152148 f.write('')
153149
154 with open(self.path_in_wd('.gitignore'), 'w') as f:
150 with open(os.path.join(self.repo.path, '.gitignore'), 'w') as f:
155151 f.writelines(ignored)
156152
157153 for dir_path in empty_dirs:
158 os.mkdir(self.path_in_wd('empty_dir'))
159
160 files_to_add = [self.path_in_wd(t) for t in tracked]
154 os.mkdir(os.path.join(self.repo.path, 'empty_dir'))
155
156 files_to_add = [os.path.join(self.repo.path, t) for t in tracked]
161157 porcelain.add(repo=self.repo.path, paths=files_to_add)
162158 porcelain.commit(repo=self.repo.path, message="init commit")
163
164 def clean(self, target_dir):
165 """Clean the target_dir and assert control dir unchanged
166 """
167 controldir = self.repo._controldir
168
169 controldir_before = set(flat_walk_dir(controldir))
170 porcelain.clean(repo=self.repo.path, target_dir=target_dir)
171 controldir_after = set(flat_walk_dir(controldir))
172
173 self.assertEqual(controldir_after, controldir_before)
174159
175160 def assert_wd(self, expected_paths):
176161 """Assert paths of files and dirs in wd are same as expected_paths
177162 """
178 control_dir = self.repo._controldir
179 control_dir_rel = os.path.relpath(control_dir, self.repo.path)
163 control_dir_rel = os.path.relpath(
164 self.repo._controldir, self.repo.path)
180165
181166 # normalize paths to simplify comparison across platforms
182 from os.path import normpath
183167 found_paths = {
184 normpath(p)
168 os.path.normpath(p)
185169 for p in flat_walk_dir(self.repo.path)
186170 if not p.split(os.sep)[0] == control_dir_rel}
187 norm_expected_paths = {normpath(p) for p in expected_paths}
171 norm_expected_paths = {os.path.normpath(p) for p in expected_paths}
188172 self.assertEqual(found_paths, norm_expected_paths)
189173
190174 def test_from_root(self):
202186 empty_dirs={
203187 'empty_dir'})
204188
205 self.clean(self.repo.path)
189 porcelain.clean(repo=self.repo.path, target_dir=self.repo.path)
206190
207191 self.assert_wd({
208192 'tracked_file',
226210 empty_dirs={
227211 'empty_dir'})
228212
229 target_dir = self.path_in_wd('untracked_dir')
230 self.clean(target_dir)
213 porcelain.clean(
214 repo=self.repo,
215 target_dir=os.path.join(self.repo.path, 'untracked_dir'))
231216
232217 self.assert_wd({
233218 'tracked_file',