Skip to content

Commit e9bfe5e

Browse files
committed
Load .githubactivity from the top-level of the git checkout
1 parent e003c24 commit e9bfe5e

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

github_activity/cli.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from subprocess import run
77

88
from .git import _git_installed_check
9+
from .git import _git_toplevel_path
910
from .github_activity import _parse_target
1011
from .github_activity import generate_activity_md
1112
from .github_activity import generate_all_activity_md
@@ -141,15 +142,18 @@
141142

142143
def load_config_and_defaults(args):
143144
"""
144-
Load .githubactivity.json from the current directory,
145+
Load .githubactivity.json from the Git top-level directory,
145146
override unset args with values from .githubactivity.json,
146147
and set defaults for remaining args.
147148
"""
148-
try:
149-
with open(".githubactivity.json") as f:
150-
config = json.load(f)
151-
except FileNotFoundError:
152-
config = {}
149+
config = {}
150+
git_toplevel = _git_toplevel_path()
151+
if git_toplevel:
152+
try:
153+
with open(os.path.join(git_toplevel, ".githubactivity.json")) as f:
154+
config = json.load(f)
155+
except FileNotFoundError:
156+
pass
153157

154158
# Treat args as a dict
155159
# https://docs.python.org/3/library/argparse.html#the-namespace-object

github_activity/git.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,13 @@ def _git_installed_check():
88
return True
99
except subprocess.CalledProcessError:
1010
return False
11+
12+
13+
def _git_toplevel_path():
14+
"""Fetch the top-level of the local Git repository"""
15+
cmd = ["git", "rev-parse", "--show-toplevel"]
16+
try:
17+
top = subprocess.check_output(cmd, stderr=subprocess.DEVNULL)
18+
return top.strip().decode()
19+
except subprocess.CalledProcessError:
20+
return None

tests/test_cli.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,28 @@ def test_cli_dot_config(tmp_path, monkeypatch, file_regression):
5252

5353
path_output = tmp_path / "out.md"
5454

55-
repo_dir = Path(__file__).parent.parent.absolute()
56-
shutil.copytree(str(repo_dir), str(tmp_path), dirs_exist_ok=True)
57-
monkeypatch.chdir(tmp_path)
55+
# We need to augment the repository with a custom .githubactivity.json
56+
# but since a local git repo is only needed to get the origin a shallow
57+
# clone is enough
58+
run(
59+
[
60+
"git",
61+
"clone",
62+
"--depth=1",
63+
"https://github.com/executablebooks/github-activity",
64+
str(tmp_path / "repo"),
65+
],
66+
check=True,
67+
)
68+
tests_dir = Path(__file__).parent
5869
shutil.copyfile(
59-
repo_dir / "tests" / "resources" / "cli_no_target.githubactivity.json",
60-
".githubactivity.json",
70+
str(tests_dir / "resources" / "cli_no_target.githubactivity.json"),
71+
str(tmp_path / "repo" / ".githubactivity.json"),
6172
)
6273

74+
# cd into a subdirectory so we test the lookup of .githubactivity.json
75+
monkeypatch.chdir(tmp_path / "repo" / "tests")
76+
6377
command = cmd.format(path_output=path_output)
6478
run(command.split(), check=True)
6579
md = path_output.read_text()

0 commit comments

Comments
 (0)