| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 44ebbe2 commit 8ff1b66
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -131,6 +131,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable): | |||
| 131 | 131 | """ | |
| 132 | 132 | ||
| 133 | 133 | unsafe_git_checkout_index_options = ["--prefix"] | |
| 134 | + unsafe_git_read_tree_options = ["--index-output"] | ||
| 134 | 135 | ||
| 135 | 136 | __slots__ = ("repo", "version", "entries", "_extension_data", "_file_path") | |
| 136 | 137 | ||
@@ -256,7 +257,12 @@ def write( | |||
| 256 | 257 | ||
| 257 | 258 | @post_clear_cache | |
| 258 | 259 | @default_index | |
| 259 | - def merge_tree(self, rhs: Treeish, base: Union[None, Treeish] = None) -> "IndexFile": | ||
| 260 | + def merge_tree( | ||
| 261 | + self, | ||
| 262 | + rhs: Treeish, | ||
| 263 | + base: Union[None, Treeish] = None, | ||
| 264 | + allow_unsafe_options: bool = False, | ||
| 265 | + ) -> "IndexFile": | ||
| 260 | 266 | """Merge the given `rhs` treeish into the current index, possibly taking | |
| 261 | 267 | a common base treeish into account. | |
| 262 | 268 | ||
@@ -270,6 +276,9 @@ def merge_tree(self, rhs: Treeish, base: Union[None, Treeish] = None) -> "IndexF | |||
| 270 | 276 | Optional treeish reference pointing to the common base of `rhs` and this | |
| 271 | 277 | index which equals lhs. | |
| 272 | 278 | ||
| 279 | + :param allow_unsafe_options: | ||
| 280 | + Allow options that may write to arbitrary paths. | ||
| 281 | + | ||
| 273 | 282 | :return: | |
| 274 | 283 | self (containing the merge and possibly unmerged entries in case of | |
| 275 | 284 | conflicts) | |
@@ -280,6 +289,12 @@ def merge_tree(self, rhs: Treeish, base: Union[None, Treeish] = None) -> "IndexF | |||
| 280 | 289 | yourself, you have to commit the changed index (or make a valid tree from | |
| 281 | 290 | it) and retry with a three-way :meth:`index.from_tree <from_tree>` call. | |
| 282 | 291 | """ | |
| 292 | + if not allow_unsafe_options: | ||
| 293 | + Git.check_unsafe_options( | ||
| 294 | + options=Git._option_candidates([base, rhs]), | ||
| 295 | + unsafe_options=self.unsafe_git_read_tree_options, | ||
| 296 | + ) | ||
| 297 | + | ||
| 283 | 298 | # -i : ignore working tree status | |
| 284 | 299 | # --aggressive : handle more merge cases | |
| 285 | 300 | # -m : do an actual merge | |
@@ -324,7 +339,13 @@ def new(cls, repo: "Repo", *tree_sha: Union[str, Tree]) -> "IndexFile": | |||
| 324 | 339 | return inst | |
| 325 | 340 | ||
| 326 | 341 | @classmethod | |
| 327 | - def from_tree(cls, repo: "Repo", *treeish: Treeish, **kwargs: Any) -> "IndexFile": | ||
| 342 | + def from_tree( | ||
| 343 | + cls, | ||
| 344 | + repo: "Repo", | ||
| 345 | + *treeish: Treeish, | ||
| 346 | + allow_unsafe_options: bool = False, | ||
| 347 | + **kwargs: Any, | ||
| 348 | + ) -> "IndexFile": | ||
| 328 | 349 | R"""Merge the given treeish revisions into a new index which is returned. | |
| 329 | 350 | The original index will remain unaltered. | |
| 330 | 351 | ||
@@ -348,6 +369,9 @@ def from_tree(cls, repo: "Repo", *treeish: Treeish, **kwargs: Any) -> "IndexFile | |||
| 348 | 369 | :param kwargs: | |
| 349 | 370 | Additional arguments passed to :manpage:`git-read-tree(1)`. | |
| 350 | 371 | ||
| 372 | + :param allow_unsafe_options: | ||
| 373 | + Allow options that may write to arbitrary paths. | ||
| 374 | + | ||
| 351 | 375 | :return: | |
| 352 | 376 | New :class:`IndexFile` instance. It will point to a temporary index location | |
| 353 | 377 | which does not exist anymore. If you intend to write such a merged Index, | |
@@ -365,6 +389,12 @@ def from_tree(cls, repo: "Repo", *treeish: Treeish, **kwargs: Any) -> "IndexFile | |||
| 365 | 389 | if len(treeish) == 0 or len(treeish) > 3: | |
| 366 | 390 | raise ValueError("Please specify between 1 and 3 treeish, got %i" % len(treeish)) | |
| 367 | 391 | ||
| 392 | + if not allow_unsafe_options: | ||
| 393 | + Git.check_unsafe_options( | ||
| 394 | + options=Git._option_candidates(treeish, kwargs), | ||
| 395 | + unsafe_options=cls.unsafe_git_read_tree_options, | ||
| 396 | + ) | ||
| 397 | + | ||
| 368 | 398 | arg_list: List[Union[Treeish, str]] = [] | |
| 369 | 399 | # Ignore that the working tree and index possibly are out of date. | |
| 370 | 400 | if len(treeish) > 1: | |
@@ -1414,6 +1444,7 @@ def reset( | |||
| 1414 | 1444 | working_tree: bool = False, | |
| 1415 | 1445 | paths: Union[None, Iterable[PathLike]] = None, | |
| 1416 | 1446 | head: bool = False, | |
| 1447 | + allow_unsafe_options: bool = False, | ||
| 1417 | 1448 | **kwargs: Any, | |
| 1418 | 1449 | ) -> "IndexFile": | |
| 1419 | 1450 | """Reset the index to reflect the tree at the given commit. This will not adjust | |
@@ -1445,6 +1476,9 @@ def reset( | |||
| 1445 | 1476 | The paths need to exist at the commit, otherwise an exception will be | |
| 1446 | 1477 | raised. | |
| 1447 | 1478 | ||
| 1479 | + :param allow_unsafe_options: | ||
| 1480 | + Allow options that may write to arbitrary paths. | ||
| 1481 | + | ||
| 1448 | 1482 | :param kwargs: | |
| 1449 | 1483 | Additional keyword arguments passed to :manpage:`git-reset(1)`. | |
| 1450 | 1484 | ||
@@ -1461,7 +1495,7 @@ def reset( | |||
| 1461 | 1495 | """ | |
| 1462 | 1496 | # What we actually want to do is to merge the tree into our existing index, | |
| 1463 | 1497 | # which is what git-read-tree does. | |
| 1464 | - new_inst = type(self).from_tree(self.repo, commit) | ||
| 1498 | + new_inst = type(self).from_tree(self.repo, commit, allow_unsafe_options=allow_unsafe_options) | ||
| 1465 | 1499 | if not paths: | |
| 1466 | 1500 | self.entries = new_inst.entries | |
| 1467 | 1501 | else: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -327,6 +327,22 @@ def add_bad_blob(): | |||
| 327 | 327 | except Exception as ex: | |
| 328 | 328 | assert "index.lock' could not be obtained" not in str(ex) | |
| 329 | 329 | ||
| 330 | + @with_rw_repo("0.1.6") | ||
| 331 | + def test_read_tree_methods_reject_index_output(self, rw_repo): | ||
| 332 | + output_path = (Path(rw_repo.working_tree_dir) / "alternate-index").as_posix() | ||
| 333 | + unsafe_option = f"--index-output={output_path}" | ||
| 334 | + | ||
| 335 | + with pytest.raises(UnsafeOptionError): | ||
| 336 | + IndexFile.from_tree(rw_repo, unsafe_option) | ||
| 337 | + with pytest.raises(UnsafeOptionError): | ||
| 338 | + IndexFile.from_tree(rw_repo, "HEAD", index_output=output_path) | ||
| 339 | + with pytest.raises(UnsafeOptionError): | ||
| 340 | + rw_repo.index.reset(unsafe_option) | ||
| 341 | + with pytest.raises(UnsafeOptionError): | ||
| 342 | + rw_repo.index.merge_tree(unsafe_option) | ||
| 343 | + with pytest.raises(UnsafeOptionError): | ||
| 344 | + rw_repo.index.merge_tree("HEAD", base=unsafe_option) | ||
| 345 | + | ||
| 330 | 346 | @with_rw_repo("0.1.6") | |
| 331 | 347 | def test_index_file_from_tree(self, rw_repo): | |
| 332 | 348 | common_ancestor_sha = "5117c9c8a4d3af19a9958677e45cda9269de1541" | |
| Back | FazBrowse Home | New Git URL |
0 commit comments