Dulwich.io dulwich / e5a028d
Fix porcelain.path_to_tree_path for Python 3.5 (#777) Boris Feld authored 18 days ago GitHub committed 18 days ago
3 changed file(s) with 52 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
1818 python-version: pypy3
1919 # path encoding
2020 - os: windows-latest
21 python-version: 3.5
22 # path encoding
23 - os: macos-latest
2124 python-version: 3.5
2225 fail-fast: false
2326
218218 path: A path, absolute or relative to the cwd
219219 Returns: A path formatted for use in e.g. an index
220220 """
221 path = Path(path).resolve()
222 repopath = Path(repopath).resolve()
223 relpath = path.relative_to(repopath)
224 if sys.platform == 'win32':
225 return str(relpath).replace(os.path.sep, '/').encode(tree_encoding)
221 # Pathlib resolve before Python 3.6 could raises FileNotFoundError in case
222 # there is no file matching the path so we reuse the old implementation for
223 # Python 3.5
224 if sys.version_info < (3, 6):
225 if not isinstance(path, bytes):
226 path = os.fsencode(path)
227 if not isinstance(repopath, bytes):
228 repopath = os.fsencode(repopath)
229 treepath = os.path.relpath(path, repopath)
230 if treepath.startswith(b'..'):
231 err_msg = 'Path %r not in repo path (%r)' % (path, repopath)
232 raise ValueError(err_msg)
233 if os.path.sep != '/':
234 treepath = treepath.replace(os.path.sep.encode('ascii'), b'/')
235 return treepath
226236 else:
227 return bytes(relpath)
237 # Resolve might returns a relative path on Windows
238 # https://bugs.python.org/issue38671
239 if sys.platform == 'win32':
240 path = os.path.abspath(path)
241
242 path = Path(path).resolve()
243
244 # Resolve and abspath seems to behave differently regarding symlinks,
245 # as we are doing abspath on the file path, we need to do the same on
246 # the repo path or they might not match
247 if sys.platform == 'win32':
248 repopath = os.path.abspath(repopath)
249
250 repopath = Path(repopath).resolve()
251
252 relpath = path.relative_to(repopath)
253 if sys.platform == 'win32':
254 return str(relpath).replace(os.path.sep, '/').encode(tree_encoding)
255 else:
256 return bytes(relpath)
228257
229258
230259 class DivergedBranches(Error):
511511 paths=["foo"])
512512 finally:
513513 os.chdir(cwd)
514
515 def test_remove_file_removed_on_disk(self):
516 fullpath = os.path.join(self.repo.path, 'foo')
517 with open(fullpath, 'w') as f:
518 f.write("BAR")
519 porcelain.add(self.repo.path, paths=[fullpath])
520 cwd = os.getcwd()
521 try:
522 os.chdir(self.repo.path)
523 os.remove(fullpath)
524 porcelain.remove(self.repo.path, paths=["foo"])
525 finally:
526 os.chdir(cwd)
527 self.assertFalse(os.path.exists(os.path.join(self.repo.path, 'foo')))
514528
515529
516530 class LogTests(PorcelainTestCase):