From 330b62b6ff59fbf5b6d9b5fdfeb0d6f82d310eb5 Mon Sep 17 00:00:00 2001 From: Elias Date: Wed, 19 Jun 2024 13:52:20 +0200 Subject: [PATCH 1/2] Prevent same commit rules from being evaluated twice --- gitbark/core.py | 2 +- gitbark/git.py | 17 +++++++++++------ gitbark/rule.py | 12 +++++++++++- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/gitbark/core.py b/gitbark/core.py index c816943..6747867 100644 --- a/gitbark/core.py +++ b/gitbark/core.py @@ -54,7 +54,7 @@ def _validate_rules(commit: Commit, cache: Cache) -> None: "all", commit, cache, - [_get_commit_rule(v, cache) for v in validators], + list({_get_commit_rule(v, cache) for v in validators}), ) else: rule = _get_commit_rule(validators.pop(), cache) diff --git a/gitbark/git.py b/gitbark/git.py index 8eda7ae..a2eff70 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, Tag as _Tag +from pygit2 import Commit as _Commit, Tree, Repository as _Repository, Tag as _Tag, Blob from typing import Union, Tuple, Optional import yaml import re @@ -150,15 +150,20 @@ def list_files(self, pattern: Union[list[str], str], root: str = "") -> set[str] split_patterns = [p.split("/") for p in patterns] return _glob_files(tree, split_patterns, root) - def read_file(self, filename: str) -> bytes: - """Read the file content of a file in the commit.""" + def _get_file(self, filename: str) -> Blob: try: - return self.repo._object.revparse_single( - f"{self.hash.hex()}:{filename}" - ).data + return self.repo._object.revparse_single(f"{self.hash.hex()}:{filename}") except KeyError: raise FileNotFoundError(f"'{filename}' does not exist in commit") + def read_file(self, filename: str) -> bytes: + """Read the file content of a file in the commit.""" + return self._get_file(filename).data + + def get_file_blob_id(self, filename: str) -> str: + """Get the blob ID of a file in the commit.""" + return self._get_file(filename).id + def get_files_modified(self, other: "Commit") -> set[str]: """Get a list of files modified between two commits.""" diff = self.repo._object.diff(self.hash.hex(), other.hash.hex()) diff --git a/gitbark/rule.py b/gitbark/rule.py index 2550b45..40d9cbc 100644 --- a/gitbark/rule.py +++ b/gitbark/rule.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from .git import Commit +from .git import Commit, COMMIT_RULES from .objects import RuleData from .project import Cache @@ -55,6 +55,16 @@ def load_rule(rule: RuleData, commit: Commit, cache: Cache) -> "_Rule": class CommitRule(_Rule): + def __eq__(self, other): + return isinstance(other, CommitRule) and self.id == other.id + + def __hash__(self): + return hash(self.id) + + @property + def id(self) -> str: + return self.validator.get_file_blob_id(COMMIT_RULES) + @abstractmethod def validate(self, commit: Commit) -> None: raise RuleViolation(f"{self}.validate is not defined") From 6d93e22037d62d63355ff1b681df2ba9dfbc67a0 Mon Sep 17 00:00:00 2001 From: Elias Date: Thu, 20 Jun 2024 13:04:55 +0200 Subject: [PATCH 2/2] Catch stderr from subprocesses --- gitbark/cli/__main__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gitbark/cli/__main__.py b/gitbark/cli/__main__.py index ac8c0b7..6903774 100644 --- a/gitbark/cli/__main__.py +++ b/gitbark/cli/__main__.py @@ -46,6 +46,7 @@ import click import logging +import subprocess import sys import os @@ -327,6 +328,9 @@ def main(): if isinstance(e, CliFail): status = e.status msg = e.args[0] + elif isinstance(e, subprocess.CalledProcessError): + status = e.returncode + msg = e.stderr.strip() else: msg = "An unexpected error occured." formatter.show_trace = True