Skip to content

Commit

Permalink
Load .githubactivity from the top-level of the git checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
manics committed Jun 14, 2024
1 parent e003c24 commit e9bfe5e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
16 changes: 10 additions & 6 deletions github_activity/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from subprocess import run

from .git import _git_installed_check
from .git import _git_toplevel_path
from .github_activity import _parse_target
from .github_activity import generate_activity_md
from .github_activity import generate_all_activity_md
Expand Down Expand Up @@ -141,15 +142,18 @@

def load_config_and_defaults(args):
"""
Load .githubactivity.json from the current directory,
Load .githubactivity.json from the Git top-level directory,
override unset args with values from .githubactivity.json,
and set defaults for remaining args.
"""
try:
with open(".githubactivity.json") as f:
config = json.load(f)
except FileNotFoundError:
config = {}
config = {}
git_toplevel = _git_toplevel_path()
if git_toplevel:
try:
with open(os.path.join(git_toplevel, ".githubactivity.json")) as f:
config = json.load(f)
except FileNotFoundError:
pass

# Treat args as a dict
# https://docs.python.org/3/library/argparse.html#the-namespace-object
Expand Down
10 changes: 10 additions & 0 deletions github_activity/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ def _git_installed_check():
return True
except subprocess.CalledProcessError:
return False


def _git_toplevel_path():
"""Fetch the top-level of the local Git repository"""
cmd = ["git", "rev-parse", "--show-toplevel"]
try:
top = subprocess.check_output(cmd, stderr=subprocess.DEVNULL)
return top.strip().decode()
except subprocess.CalledProcessError:
return None
24 changes: 19 additions & 5 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,28 @@ def test_cli_dot_config(tmp_path, monkeypatch, file_regression):

path_output = tmp_path / "out.md"

repo_dir = Path(__file__).parent.parent.absolute()
shutil.copytree(str(repo_dir), str(tmp_path), dirs_exist_ok=True)
monkeypatch.chdir(tmp_path)
# We need to augment the repository with a custom .githubactivity.json
# but since a local git repo is only needed to get the origin a shallow
# clone is enough
run(
[
"git",
"clone",
"--depth=1",
"https://github.com/executablebooks/github-activity",
str(tmp_path / "repo"),
],
check=True,
)
tests_dir = Path(__file__).parent
shutil.copyfile(
repo_dir / "tests" / "resources" / "cli_no_target.githubactivity.json",
".githubactivity.json",
str(tests_dir / "resources" / "cli_no_target.githubactivity.json"),
str(tmp_path / "repo" / ".githubactivity.json"),
)

# cd into a subdirectory so we test the lookup of .githubactivity.json
monkeypatch.chdir(tmp_path / "repo" / "tests")

command = cmd.format(path_output=path_output)
run(command.split(), check=True)
md = path_output.read_text()
Expand Down

0 comments on commit e9bfe5e

Please sign in to comment.