Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent same commit rules from being evaluated twice #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions gitbark/cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import click
import logging
import subprocess
import sys
import os

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gitbark/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 11 additions & 6 deletions gitbark/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down
12 changes: 11 additions & 1 deletion gitbark/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
Expand Down
Loading