From e9bfe5e6ce8af080fe87a8037a0036e0a69b9989 Mon Sep 17 00:00:00 2001 From: Simon Li Date: Fri, 14 Jun 2024 14:14:07 +0100 Subject: [PATCH] Load .githubactivity from the top-level of the git checkout --- github_activity/cli.py | 16 ++++++++++------ github_activity/git.py | 10 ++++++++++ tests/test_cli.py | 24 +++++++++++++++++++----- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/github_activity/cli.py b/github_activity/cli.py index 6235308..c248074 100644 --- a/github_activity/cli.py +++ b/github_activity/cli.py @@ -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 @@ -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 diff --git a/github_activity/git.py b/github_activity/git.py index a38c5ed..20a8539 100644 --- a/github_activity/git.py +++ b/github_activity/git.py @@ -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 diff --git a/tests/test_cli.py b/tests/test_cli.py index 9b1363b..9d04e6b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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()