Skip to content

Commit 8fdc645

Browse files
committed
fix: cross-platform hook tests and ruff format
- Use Python scripts instead of .sh for hook tests (works on Windows CI) - Write output to working_dir instead of git_dir (bare repo compatibility) - Apply ruff format to git/index/fun.py - Removes xfail dependency on bash.exe for hooksPath tests
1 parent 33c3373 commit 8fdc645

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

git/index/fun.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None:
8686
Respects the ``core.hooksPath`` git configuration option. When set, hooks are
8787
resolved relative to that path instead of the default ``.git/hooks`` directory.
8888
"""
89-
hooks_path = index.repo.config_reader().get(
90-
"core", "hooksPath", fallback=None
91-
)
89+
hooks_path = index.repo.config_reader().get("core", "hooksPath", fallback=None)
9290
if hooks_path is not None:
9391
hp = osp.join(hooks_path, name)
9492
if not osp.isabs(hooks_path):

test/test_index.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,13 +1261,15 @@ def test_run_commit_hook_respects_absolute_hooks_path(self, rw_repo):
12611261
custom_hooks_dir = osp.join(rw_repo.working_dir, "custom-hooks")
12621262
os.makedirs(custom_hooks_dir, exist_ok=True)
12631263
hp = osp.join(custom_hooks_dir, "fake-hook")
1264+
# Use a Python script so it works on all platforms (no bash required)
1265+
hook_script = "#!" + sys.executable + "\nimport sys; open('output.txt', 'w').write('ran custom hook\\n')\n"
12641266
with open(hp, "w", encoding="utf-8") as f:
1265-
f.write("#!/bin/sh\necho 'ran custom hook' >output.txt\n")
1267+
f.write(hook_script)
12661268
os.chmod(hp, 0o755)
12671269

12681270
rw_repo.config_writer().set_value("core", "hooksPath", custom_hooks_dir).release()
12691271
run_commit_hook("fake-hook", index)
1270-
output = Path(rw_repo.git_dir, "output.txt").read_text(encoding="utf-8")
1272+
output = Path(rw_repo.working_dir, "output.txt").read_text(encoding="utf-8")
12711273
self.assertEqual(output, "ran custom hook\n")
12721274

12731275
@pytest.mark.xfail(
@@ -1287,13 +1289,15 @@ def test_run_commit_hook_respects_relative_hooks_path(self, rw_repo):
12871289
custom_hooks_dir = osp.join(rw_repo.working_dir, "hooks-relative")
12881290
os.makedirs(custom_hooks_dir, exist_ok=True)
12891291
hp = osp.join(custom_hooks_dir, "fake-hook")
1292+
# Use a Python script so it works on all platforms (no bash required)
1293+
hook_script = "#!" + sys.executable + "\nimport sys; open('output.txt', 'w').write('ran relative hook\\n')\n"
12901294
with open(hp, "w", encoding="utf-8") as f:
1291-
f.write("#!/bin/sh\necho 'ran relative hook' >output.txt\n")
1295+
f.write(hook_script)
12921296
os.chmod(hp, 0o755)
12931297

12941298
rw_repo.config_writer().set_value("core", "hooksPath", "hooks-relative").release()
12951299
run_commit_hook("fake-hook", index)
1296-
output = Path(rw_repo.git_dir, "output.txt").read_text(encoding="utf-8")
1300+
output = Path(rw_repo.working_dir, "output.txt").read_text(encoding="utf-8")
12971301
self.assertEqual(output, "ran relative hook\n")
12981302

12991303
@with_rw_repo("HEAD", bare=True)

0 commit comments

Comments
 (0)