|
3 | 3 | #
|
4 | 4 | # This module is part of GitPython and is released under
|
5 | 5 | # the BSD License: http://www.opensource.org/licenses/bsd-license.php
|
6 |
| - |
7 | 6 | import logging
|
8 | 7 | import os
|
9 | 8 | import re
|
10 | 9 | import warnings
|
11 | 10 |
|
| 11 | +from gitdb.exc import BadObject |
| 12 | + |
12 | 13 | from git.cmd import (
|
13 | 14 | Git,
|
14 | 15 | handle_process_output
|
@@ -402,7 +403,17 @@ def tags(self) -> 'IterableList':
|
402 | 403 | def tag(self, path: PathLike) -> TagReference:
|
403 | 404 | """:return: TagReference Object, reference pointing to a Commit or Tag
|
404 | 405 | :param path: path to the tag reference, i.e. 0.1.5 or tags/0.1.5 """
|
405 |
| - return TagReference(self, path) |
| 406 | + full_path = self._to_full_tag_path(path) |
| 407 | + return TagReference(self, full_path) |
| 408 | + |
| 409 | + @staticmethod |
| 410 | + def _to_full_tag_path(path): |
| 411 | + if path.startswith(TagReference._common_path_default + '/'): |
| 412 | + return path |
| 413 | + if path.startswith(TagReference._common_default + '/'): |
| 414 | + return Reference._common_path_default + '/' + path |
| 415 | + else: |
| 416 | + return TagReference._common_path_default + '/' + path |
406 | 417 |
|
407 | 418 | def create_head(self, path: PathLike, commit: str = 'HEAD',
|
408 | 419 | force: bool = False, logmsg: Optional[str] = None
|
@@ -608,6 +619,23 @@ def is_ancestor(self, ancestor_rev: 'Commit', rev: 'Commit') -> bool:
|
608 | 619 | raise
|
609 | 620 | return True
|
610 | 621 |
|
| 622 | + def is_valid_object(self, sha: str, object_type: str = None) -> bool: |
| 623 | + try: |
| 624 | + complete_sha = self.odb.partial_to_complete_sha_hex(sha) |
| 625 | + object_info = self.odb.info(complete_sha) |
| 626 | + if object_type: |
| 627 | + if object_info.type == object_type.encode(): |
| 628 | + return True |
| 629 | + else: |
| 630 | + log.debug("Commit hash points to an object of type '%s'. Requested were objects of type '%s'", |
| 631 | + object_info.type.decode(), object_type) |
| 632 | + return False |
| 633 | + else: |
| 634 | + return True |
| 635 | + except BadObject: |
| 636 | + log.debug("Commit hash is invalid.") |
| 637 | + return False |
| 638 | + |
611 | 639 | def _get_daemon_export(self) -> bool:
|
612 | 640 | if self.git_dir:
|
613 | 641 | filename = osp.join(self.git_dir, self.DAEMON_EXPORT_FILE)
|
|
0 commit comments