| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -77,14 +77,6 @@ | |||
| 77 | 77 | "lineCount": 1 | |
| 78 | 78 | } | |
| 79 | 79 | }, | |
| 80 | - { | ||
| 81 | - "code": "reportAssignmentType", | ||
| 82 | - "range": { | ||
| 83 | - "startColumn": 38, | ||
| 84 | - "endColumn": 76, | ||
| 85 | - "lineCount": 1 | ||
| 86 | - } | ||
| 87 | - }, | ||
| 88 | 80 | { | |
| 89 | 81 | "code": "reportArgumentType", | |
| 90 | 82 | "range": { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,6 +11,7 @@ | |||
| 11 | 11 | ||
| 12 | 12 | from git.cmd import Git, handle_process_output | |
| 13 | 13 | from git.compat import defenc | |
| 14 | + from git.objects.base import IndexObject | ||
| 14 | 15 | from git.objects.blob import Blob | |
| 15 | 16 | from git.objects.util import mode_str_to_int | |
| 16 | 17 | from git.util import finalize_process, hex_to_bin | |
@@ -35,7 +36,6 @@ | |||
| 35 | 36 | if TYPE_CHECKING: | |
| 36 | 37 | from subprocess import Popen | |
| 37 | 38 | ||
| 38 | - from git.objects.base import IndexObject | ||
| 39 | 39 | from git.objects.commit import Commit | |
| 40 | 40 | from git.objects.tree import Tree | |
| 41 | 41 | from git.repo.base import Repo | |
@@ -378,6 +378,10 @@ class Diff: | |||
| 378 | 378 | Diffs keep information about the changed blob objects, the file mode, renames, | |
| 379 | 379 | deletions and new files. | |
| 380 | 380 | ||
| 381 | + For submodule changes, ``a_blob`` and ``b_blob`` are | ||
| 382 | + :class:`~git.objects.base.IndexObject` instances whose SHAs refer to commits in | ||
| 383 | + the submodule repository. | ||
| 384 | + | ||
| 381 | 385 | There are a few cases where ``None`` has to be expected as member variable value: | |
| 382 | 386 | ||
| 383 | 387 | New File:: | |
@@ -481,17 +485,22 @@ def __init__( | |||
| 481 | 485 | repo = submodule.module() | |
| 482 | 486 | break | |
| 483 | 487 | ||
| 488 | + # Gitlinks reference commits; generic index objects preserve their path and mode. | ||
| 484 | 489 | self.a_blob: Union["IndexObject", None] | |
| 485 | 490 | if a_blob_id is None or a_blob_id == self.NULL_HEX_SHA: | |
| 486 | 491 | self.a_blob = None | |
| 487 | 492 | else: | |
| 488 | - self.a_blob = Blob(repo, hex_to_bin(a_blob_id), mode=self.a_mode, path=self.a_path) | ||
| 493 | + self.a_blob = (IndexObject if self.a_mode == 0o160000 else Blob)( | ||
| 494 | + repo, hex_to_bin(a_blob_id), mode=self.a_mode, path=self.a_path | ||
| 495 | + ) | ||
| 489 | 496 | ||
| 490 | 497 | self.b_blob: Union["IndexObject", None] | |
| 491 | 498 | if b_blob_id is None or b_blob_id == self.NULL_HEX_SHA: | |
| 492 | 499 | self.b_blob = None | |
| 493 | 500 | else: | |
| 494 | - self.b_blob = Blob(repo, hex_to_bin(b_blob_id), mode=self.b_mode, path=self.b_path) | ||
| 501 | + self.b_blob = (IndexObject if self.b_mode == 0o160000 else Blob)( | ||
| 502 | + repo, hex_to_bin(b_blob_id), mode=self.b_mode, path=self.b_path | ||
| 503 | + ) | ||
| 495 | 504 | ||
| 496 | 505 | self.new_file: bool = new_file | |
| 497 | 506 | self.deleted_file: bool = deleted_file | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -310,7 +310,7 @@ def merge_tree( | |||
| 310 | 310 | return self | |
| 311 | 311 | ||
| 312 | 312 | @classmethod | |
| 313 | - def new(cls, repo: "Repo", *tree_sha: Union[str, Tree]) -> "IndexFile": | ||
| 313 | + def new(cls, repo: "Repo", *tree_sha: Union[str, bytes, Tree]) -> "IndexFile": | ||
| 314 | 314 | """Merge the given treeish revisions into a new index which is returned. | |
| 315 | 315 | ||
| 316 | 316 | This method behaves like ``git-read-tree --aggressive`` when doing the merge. | |
@@ -326,7 +326,9 @@ def new(cls, repo: "Repo", *tree_sha: Union[str, Tree]) -> "IndexFile": | |||
| 326 | 326 | If you intend to write such a merged Index, supply an alternate | |
| 327 | 327 | ``file_path`` to its :meth:`write` method. | |
| 328 | 328 | """ | |
| 329 | - tree_sha_bytes: List[bytes] = [to_bin_sha(str(t)) for t in tree_sha] | ||
| 329 | + tree_sha_bytes: List[bytes] = [ | ||
| 330 | + to_bin_sha(t if isinstance(t, bytes) else str(t).encode("ascii")) for t in tree_sha | ||
| 331 | + ] | ||
| 330 | 332 | base_entries = aggressive_tree_merge(repo.odb, tree_sha_bytes) | |
| 331 | 333 | ||
| 332 | 334 | inst = cls(repo) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ | |||
| 9 | 9 | ||
| 10 | 10 | import gitdb.typ as dbtyp | |
| 11 | 11 | ||
| 12 | + from git.compat import force_text | ||
| 12 | 13 | from git.exc import WorkTreeRepositoryUnsupported | |
| 13 | 14 | from git.util import LazyMixin, bin_to_hex, join_path_native, stream_copy | |
| 14 | 15 | ||
@@ -107,6 +108,10 @@ def __init__(self, repo: "Repo", binsha: bytes) -> None: | |||
| 107 | 108 | ||
| 108 | 109 | :param binsha: | |
| 109 | 110 | 20 byte SHA1 | |
| 111 | + | ||
| 112 | + :note: | ||
| 113 | + Object data is loaded lazily. Loading uncached :attr:`size` metadata | ||
| 114 | + raises :exc:`ValueError` if `binsha` refers to a different object type. | ||
| 110 | 115 | """ | |
| 111 | 116 | super().__init__() | |
| 112 | 117 | self.repo = repo | |
@@ -155,6 +160,9 @@ def _set_cache_(self, attr: str) -> None: | |||
| 155 | 160 | """Retrieve object information.""" | |
| 156 | 161 | if attr == "size": | |
| 157 | 162 | oinfo = self.repo.odb.info(self.binsha) | |
| 163 | + typename = force_text(oinfo.type, "ascii") | ||
| 164 | + if self.type is not None and typename != self.type: | ||
| 165 | + raise ValueError("Object %s is a %s, not a %s" % (self.hexsha, typename, self.type)) | ||
| 158 | 166 | self.size = oinfo.size # type: int | |
| 159 | 167 | else: | |
| 160 | 168 | super()._set_cache_(attr) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,11 +4,15 @@ | |||
| 4 | 4 | # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/ | |
| 5 | 5 | ||
| 6 | 6 | import gc | |
| 7 | + from io import BytesIO | ||
| 7 | 8 | import os | |
| 8 | 9 | import os.path as osp | |
| 9 | 10 | import sys | |
| 10 | 11 | import tempfile | |
| 11 | 12 | from unittest import skipIf | |
| 13 | + from unittest.mock import patch | ||
| 14 | + | ||
| 15 | + from gitdb import OInfo | ||
| 12 | 16 | ||
| 13 | 17 | from git import Repo | |
| 14 | 18 | from git.objects import Blob, Commit, TagObject, Tree | |
@@ -76,6 +80,20 @@ def test_base_object(self): | |||
| 76 | 80 | # Remove the file this way, instead of with a context manager or "finally", | |
| 77 | 81 | # so it is only removed on success, and we can inspect the file on failure. | |
| 78 | 82 | os.remove(tmpfile.name) | |
| 83 | + | ||
| 84 | + for stored_type in (typename, typename.encode("ascii")): | ||
| 85 | + with patch.object(self.rorepo.odb, "info", return_value=OInfo(binsha, stored_type, item.size)): | ||
| 86 | + self.assertEqual(obj_type(self.rorepo, binsha).size, item.size) | ||
| 87 | + for wrong_type in types: | ||
| 88 | + if wrong_type is obj_type: | ||
| 89 | + continue | ||
| 90 | + invalid = wrong_type(self.rorepo, binsha) | ||
| 91 | + with self.assertRaisesRegex(ValueError, f"{hexsha}.*{typename}.*{wrong_type.type}"): | ||
| 92 | + invalid.size | ||
| 93 | + self.assertEqual(invalid.data_stream.read(), data) | ||
| 94 | + ostream = BytesIO() | ||
| 95 | + invalid.stream_data(ostream) | ||
| 96 | + self.assertEqual(ostream.getvalue(), data) | ||
| 79 | 97 | # END for each object type to create | |
| 80 | 98 | ||
| 81 | 99 | # Each has a unique sha. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -348,7 +348,7 @@ def test_diff_submodule(self): | |||
| 348 | 348 | with open(self.submodule_dir + "/subfile", "w") as sub_subfile: | |
| 349 | 349 | sub_subfile.write("") | |
| 350 | 350 | sub.index.add(["subfile"]) | |
| 351 | - sub.index.commit("first commit") | ||
| 351 | + first_commit = sub.index.commit("first commit") | ||
| 352 | 352 | ||
| 353 | 353 | # Init a temp git repo that will incorporate the submodule. | |
| 354 | 354 | repo = Repo.init(self.repo_dir) | |
@@ -364,7 +364,7 @@ def test_diff_submodule(self): | |||
| 364 | 364 | with open(self.repo_dir + "/sub/subfile", "w") as foo_sub_subfile: | |
| 365 | 365 | foo_sub_subfile.write("blub") | |
| 366 | 366 | submodule.module().index.add(["subfile"]) | |
| 367 | - submodule.module().index.commit("changed subfile") | ||
| 367 | + changed_commit = submodule.module().index.commit("changed subfile") | ||
| 368 | 368 | submodule.binsha = submodule.module().head.commit.binsha | |
| 369 | 369 | ||
| 370 | 370 | # Commit submodule updates in parent repo. | |
@@ -373,11 +373,10 @@ def test_diff_submodule(self): | |||
| 373 | 373 | repo.create_tag("2") | |
| 374 | 374 | ||
| 375 | 375 | diff = repo.commit("1").diff(repo.commit("2"))[0] | |
| 376 | - # If diff is unable to find the commit hashes (looks in wrong repo) the | ||
| 377 | - # *_blob.size property will be a string containing exception text, an int | ||
| 378 | - # indicates success. | ||
| 379 | - self.assertIsInstance(diff.a_blob.size, int) | ||
| 380 | - self.assertIsInstance(diff.b_blob.size, int) | ||
| 376 | + # Gitlinks refer to commits in the submodule's object database. | ||
| 377 | + for item, commit in ((diff.a_blob, first_commit), (diff.b_blob, changed_commit)): | ||
| 378 | + self.assertEqual(item.size, commit.size) | ||
| 379 | + self.assertEqual(item.data_stream.read(), commit.data_stream.read()) | ||
| 381 | 380 | ||
| 382 | 381 | def test_diff_rejects_unsafe_output_options(self): | |
| 383 | 382 | commit = self.rorepo.head.commit | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1030,7 +1030,7 @@ def test_index_new(self): | |||
| 1030 | 1030 | H = self.rorepo.tree("25dca42bac17d511b7e2ebdd9d1d679e7626db5f") | |
| 1031 | 1031 | M = self.rorepo.tree("e746f96bcc29238b79118123028ca170adc4ff0f") | |
| 1032 | 1032 | ||
| 1033 | - for args in ((B,), (B, H), (B, H, M)): | ||
| 1033 | + for args in ((B.binsha,), (B.hexsha, H), (B, H.binsha, M.hexsha.encode("ascii"))): | ||
| 1034 | 1034 | index = IndexFile.new(self.rorepo, *args) | |
| 1035 | 1035 | assert isinstance(index, IndexFile) | |
| 1036 | 1036 | # END for each arg tuple | |
| Back | FazBrowse Home | New Git URL |
0 commit comments