Skip to content

Commit

Permalink
Fix crash when repository is not git repository
Browse files Browse the repository at this point in the history
Now the script checks if creating a git repository fails,
and acts like there is no git functionality otherwise.
  • Loading branch information
LiquidFun committed Dec 25, 2023
1 parent 2de1f78 commit 2f7e4e4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ __pycache__
session.cookie
cache
.idea
.aoc_tiles
.aoc_tiles
/tests/samples/*
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ Add this pre-commit hook to your `.pre-commit-config.yaml` (create it, if you do
```yaml
repos:
- repo: https://github.com/LiquidFun/aoc_tiles
rev: 0.5.1
rev: 0.5.3
hooks:
- id: aoc-tiles
# Optionally use these arguments. Auto add tiles to git adds the tiles to git,
# possibly amends your commit by creating the tile images and updating the README.
# Language sorting shows the preference of the order of the languages to use.
# Exclude paterns are globs which can be used to exclude files when creating
# the tiles. See the customization section in the README for more flags.
# Simply remove the comments (#) below for args and the flags you want.
# args:
# - --auto-add-tiles-to-git=amend
# - --language-sorting=jl,kt,py,rs
Expand Down
21 changes: 15 additions & 6 deletions aoc_tiles/solutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import List, Dict, Optional

import git
from git import GitCommandError
from git import GitCommandError, InvalidGitRepositoryError
from loguru import logger

from aoc_tiles.colors import extension_to_colors
Expand All @@ -16,7 +16,11 @@
class SolutionFinder:
def __init__(self, config: Config):
self.config = config
self.repository = git.Repo(config.aoc_dir)
try:
self.repository = git.Repo(config.aoc_dir)
except InvalidGitRepositoryError:
print(f"{config.aoc_dir} is an InvalidGitRepository. Git functionality will not be available.")
self.repository = None

if self.config.session_cookie_path.exists():
msg = f"This is a security risk!" \
Expand Down Expand Up @@ -61,10 +65,10 @@ def sort_key(path: Path):
return solution_paths_dict

def _find_recursive_solution_files(self, directory: Path) -> List[Path]:
if self.config.only_use_solutions_in_git:
if self.config.only_use_solutions_in_git and self.repository is not None:
files = [Path(s) for s in self.git_get_tracked_files()]
else:
files = directory.rglob("*")
files = list(directory.rglob("*"))

logger.debug("Found {} files", len(files))
solution_paths = []
Expand All @@ -79,6 +83,8 @@ def _find_recursive_solution_files(self, directory: Path) -> List[Path]:
return solution_paths

def git_is_file_ignored(self, filepath):
if self.repository is None:
return False
try:
self.repository.git.execute(['git', 'check-ignore', '-q', str(filepath)])
return True
Expand All @@ -87,20 +93,23 @@ def git_is_file_ignored(self, filepath):

@lru_cache
def git_get_tracked_files(self) -> List[str]:
if self.repository is None:
return []
return self.repository.git.ls_files().split('\n')

def git_is_file_tracked(self, filepath: Path):
tracked_files = self.git_get_tracked_files()
return str(filepath) in tracked_files

def git_add(self, path: Path):
if path.exists():
if repository is not None and path.exists():
self.repository.git.add(str(path))

def git_commit_amend(self):
# Command based on this:
# https://stackoverflow.com/questions/3284292/can-a-git-hook-automatically-add-files-to-the-commit
self.repository.git.commit('--amend', '-C', 'HEAD', '--no-verify')
if self.repository is not None:
self.repository.git.commit('--amend', '-C', 'HEAD', '--no-verify')


def main():
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "aoc-tiles"
version = "0.5.2"
version = "0.5.3"
description = "Fancy Advent of Code README tiles showing the completion status of the challenges per day"
authors = ["Brutenis Gliwa, <brutenis@gmail.com>"]
license = "Apache-2.0"
Expand Down

0 comments on commit 2f7e4e4

Please sign in to comment.