Dulwich.io dulwich / fa3ad53
Fix handling of stored encoding in dulwich.porcelain.get_object_by_path on Python 3. Jelmer Vernooij 13 days ago
3 changed file(s) with 40 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
11
22 * Properly handle files that are just executable for the
33 current user. (Jelmer Vernooij, #734)
4
5 * Fix handling of stored encoding in
6 ``dulwich.porcelain.get_object_by_path`` on Python 3.
7 (Jelmer Vernooij)
48
59 0.19.14 2019-11-30
610
522522
523523
524524 def commit_decode(commit, contents, default_encoding=DEFAULT_ENCODING):
525 if commit.encoding is not None:
526 return contents.decode(commit.encoding, "replace")
527 return contents.decode(default_encoding, "replace")
525 if commit.encoding:
526 encoding = commit.encoding.decode('ascii')
527 else:
528 encoding = default_encoding
529 return contents.decode(encoding, "replace")
530
531
532 def commit_encode(commit, contents, default_encoding=DEFAULT_ENCODING):
533 if commit.encoding:
534 encoding = commit.encoding.decode('ascii')
535 else:
536 encoding = default_encoding
537 return contents.encode(encoding)
528538
529539
530540 def print_commit(commit, decode, outstream=sys.stdout):
603613 diffstream,
604614 repo.object_store, base_tree, commit.tree)
605615 diffstream.seek(0)
606 outstream.write(
607 diffstream.getvalue().decode(
608 commit.encoding or DEFAULT_ENCODING, 'replace'))
616 outstream.write(commit_decode(commit, diffstream.getvalue()))
609617
610618
611619 def show_tree(repo, tree, decode, outstream=sys.stdout):
15641572 commit = parse_commit(r, committish)
15651573 base_tree = commit.tree
15661574 if not isinstance(path, bytes):
1567 path = path.encode(commit.encoding or DEFAULT_ENCODING)
1575 path = commit_encode(commit, path)
15681576 (mode, sha) = tree_lookup_path(
15691577 r.object_store.__getitem__,
15701578 base_tree, path)
17691769 os.chdir(cwd)
17701770
17711771
1772 class GetObjectBypathTests(PorcelainTestCase):
1772 class GetObjectByPathTests(PorcelainTestCase):
17731773
17741774 def test_simple(self):
17751775 fullpath = os.path.join(self.repo.path, 'foo')
17831783 self.assertEqual(
17841784 b"BAR",
17851785 porcelain.get_object_by_path(self.repo, 'foo').data)
1786 self.assertEqual(
1787 b"BAR",
1788 porcelain.get_object_by_path(self.repo, b'foo').data)
1789
1790 def test_encoding(self):
1791 fullpath = os.path.join(self.repo.path, 'foo')
1792 with open(fullpath, 'w') as f:
1793 f.write("BAR")
1794 porcelain.add(repo=self.repo.path, paths=[fullpath])
1795 porcelain.commit(
1796 self.repo.path, message=b"Some message",
1797 author=b"Joe <joe@example.com>",
1798 committer=b"Bob <bob@example.com>",
1799 encoding=b"utf-8")
1800 self.assertEqual(
1801 b"BAR",
1802 porcelain.get_object_by_path(self.repo, 'foo').data)
1803 self.assertEqual(
1804 b"BAR",
1805 porcelain.get_object_by_path(self.repo, b'foo').data)
17861806
17871807 def test_missing(self):
17881808 self.assertRaises(