From 133306e0b932a49ee11f49a79ca76aa270914f81 Mon Sep 17 00:00:00 2001 From: Emil Lundberg Date: Wed, 24 Jan 2024 19:23:05 +0100 Subject: [PATCH] Resolve tags in Commit constructor `bark` currently fails to run if there are annotated tags in the repository. Lightweight tags work, though. First off, no tags works: ``` $ poetry run -- bark -l debug setup INFO 19:20:37.605 [gitbark.logging.set_log_level:18] Logging at level: DEBUG Define what Bark Modules to add! Module to add (leave blank to skip): ^CAborted! ``` A lightweight tag also does not interfere: ``` $ git tag tag-lightweight $ poetry run -- bark -l debug setup INFO 19:20:58.889 [gitbark.logging.set_log_level:18] Logging at level: DEBUG Define what Bark Modules to add! Module to add (leave blank to skip): ^CAborted! ``` But an annotated tag does make `bark` crash on startup: ``` $ git tag tag-annotated -m "TEST" $ poetry run -- bark -l debug setup INFO 19:21:09.945 [gitbark.logging.set_log_level:18] Logging at level: DEBUG ERROR 19:21:09.964 [gitbark.cli.__main__.main:333] An unexpected error occured. Traceback (most recent call last): File "/home/emlun/dev/gitbark/gitbark/cli/__main__.py", line 324, in main cli(obj={}) File "/home/emlun/.cache/pypoetry/virtualenvs/gitbark-X6l5axH9-py3.11/lib/python3.11/site-packages/click/core.py", line 1130, in __call__ return self.main(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/emlun/.cache/pypoetry/virtualenvs/gitbark-X6l5axH9-py3.11/lib/python3.11/site-packages/click/core.py", line 1055, in main rv = self.invoke(ctx) ^^^^^^^^^^^^^^^^ File "/home/emlun/.cache/pypoetry/virtualenvs/gitbark-X6l5axH9-py3.11/lib/python3.11/site-packages/click/core.py", line 1657, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/emlun/.cache/pypoetry/virtualenvs/gitbark-X6l5axH9-py3.11/lib/python3.11/site-packages/click/core.py", line 1404, in invoke return ctx.invoke(self.callback, **ctx.params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/emlun/.cache/pypoetry/virtualenvs/gitbark-X6l5axH9-py3.11/lib/python3.11/site-packages/click/core.py", line 760, in invoke return __callback(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/emlun/.cache/pypoetry/virtualenvs/gitbark-X6l5axH9-py3.11/lib/python3.11/site-packages/click/decorators.py", line 26, in new_func return f(get_current_context(), *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/emlun/dev/gitbark/gitbark/cli/__main__.py", line 120, in setup setup_cmd(project) File "/home/emlun/dev/gitbark/gitbark/commands/setup.py", line 304, in setup if not has_valid_bark_rules(project): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/emlun/dev/gitbark/gitbark/commands/setup.py", line 119, in has_valid_bark_rules if BARK_RULES_REF in project.repo.references: ^^^^^^^^^^^^^^^^^^^^^^^ File "/home/emlun/dev/gitbark/gitbark/git.py", line 201, in references return { ^ File "/home/emlun/dev/gitbark/gitbark/git.py", line 202, in ref.name: Commit(ref.resolve().target.raw, self) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/emlun/dev/gitbark/gitbark/git.py", line 76, in __init__ raise ValueError(f"No commit found with hash {hash.hex()}") ValueError: No commit found with hash 75b81168733bfc515e7204d5be9767ccb1dc2be5 ``` --- gitbark/git.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/gitbark/git.py b/gitbark/git.py index 1288303..8eda7ae 100644 --- a/gitbark/git.py +++ b/gitbark/git.py @@ -15,7 +15,7 @@ from .objects import RuleData from .util import cmd -from pygit2 import Commit as _Commit, Tree, Repository as _Repository +from pygit2 import Commit as _Commit, Tree, Repository as _Repository, Tag as _Tag from typing import Union, Tuple, Optional import yaml import re @@ -72,7 +72,9 @@ def __init__(self, hash: bytes, repo: "Repository") -> None: if not isinstance(hash, bytes): raise ValueError(f"Commit hash is not bytes {hash}") self._object: _Commit = repo._object.get(hash.hex()) - if not isinstance(self._object, _Commit): + if isinstance(self._object, _Tag): + self._object = self._object.get_object() + if not isinstance(self._object, (_Commit, _Tag)): raise ValueError(f"No commit found with hash {hash.hex()}") @property