Make git hooks work in git worktrees (#40038)

Make hooks work in worktrees and cleanup hooks
This commit is contained in:
War Pigeon
2025-09-02 18:01:43 -05:00
committed by GitHub
parent 7511b3bed3
commit d3731395b6
3 changed files with 23 additions and 25 deletions

View File

@@ -1,17 +1,19 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Installs git hooks, updates them, updates submodules, that kind of thing. """
Installs git hooks, updates them, updates submodules, that kind of thing.
"""
import subprocess
import sys
import os import os
import shutil import shutil
import subprocess
import sys
import time import time
from pathlib import Path from pathlib import Path
from typing import List from typing import List
SOLUTION_PATH = Path("..") / "SpaceStation14.sln" SOLUTION_PATH = Path("..") / "SpaceStation14.sln"
# If this doesn't match the saved version we overwrite them all. # If this doesn't match the saved version we overwrite them all.
CURRENT_HOOKS_VERSION = "2" CURRENT_HOOKS_VERSION = "3"
QUIET = len(sys.argv) == 2 and sys.argv[1] == "--quiet" QUIET = len(sys.argv) == 2 and sys.argv[1] == "--quiet"
@@ -25,12 +27,10 @@ def run_command(command: List[str], capture: bool = False) -> subprocess.Complet
sys.stdout.flush() sys.stdout.flush()
completed = None
if capture: if capture:
completed = subprocess.run(command, cwd="..", stdout=subprocess.PIPE) completed = subprocess.run(command, stdout=subprocess.PIPE, text=True)
else: else:
completed = subprocess.run(command, cwd="..") completed = subprocess.run(command)
if completed.returncode != 0: if completed.returncode != 0:
print("Error: command exited with code {}!".format(completed.returncode)) print("Error: command exited with code {}!".format(completed.returncode))
@@ -43,7 +43,7 @@ def update_submodules():
Updates all submodules. Updates all submodules.
""" """
if ('GITHUB_ACTIONS' in os.environ): if 'GITHUB_ACTIONS' in os.environ:
return return
if os.path.isfile("DISABLE_SUBMODULE_AUTOUPDATE"): if os.path.isfile("DISABLE_SUBMODULE_AUTOUPDATE"):
@@ -76,22 +76,21 @@ def install_hooks():
print("No hooks change detected.") print("No hooks change detected.")
return return
with open("INSTALLED_HOOKS_VERSION", "w") as f:
f.write(CURRENT_HOOKS_VERSION)
print("Hooks need updating.") print("Hooks need updating.")
hooks_target_dir = Path("..")/".git"/"hooks" hooks_target_dir = Path(run_command(["git", "rev-parse", "--git-path", "hooks"], True).stdout.strip())
hooks_source_dir = Path("hooks") hooks_source_dir = Path("hooks")
# Clear entire tree since we need to kill deleted files too. # Clear entire tree since we need to kill deleted files too.
for filename in os.listdir(str(hooks_target_dir)): for filename in os.listdir(hooks_target_dir):
os.remove(str(hooks_target_dir/filename)) os.remove(hooks_target_dir / filename)
for filename in os.listdir(str(hooks_source_dir)): for filename in os.listdir(hooks_source_dir):
print("Copying hook {}".format(filename)) print("Copying hook {}".format(filename))
shutil.copy2(str(hooks_source_dir/filename), shutil.copy2(hooks_source_dir / filename, hooks_target_dir / filename)
str(hooks_target_dir/filename))
with open("INSTALLED_HOOKS_VERSION", "w") as f:
f.write(CURRENT_HOOKS_VERSION)
def reset_solution(): def reset_solution():
@@ -107,8 +106,7 @@ def reset_solution():
def check_for_zip_download(): def check_for_zip_download():
# Check if .git exists, # Check if .git exists,
cur_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) if run_command(["git", "rev-parse"]).returncode != 0:
if not os.path.isdir(os.path.join(cur_dir, ".git")):
print("It appears that you downloaded this repository directly from GitHub. (Using the .zip download option) \n" print("It appears that you downloaded this repository directly from GitHub. (Using the .zip download option) \n"
"When downloading straight from GitHub, it leaves out important information that git needs to function. " "When downloading straight from GitHub, it leaves out important information that git needs to function. "
"Such as information to download the engine or even the ability to even be able to create contributions. \n" "Such as information to download the engine or even the ability to even be able to create contributions. \n"

View File

@@ -1,10 +1,10 @@
#!/bin/bash #!/bin/bash
gitroot=`git rev-parse --show-toplevel` gitroot=$(git rev-parse --show-toplevel)
cd "$gitroot/BuildChecker" cd "$gitroot/BuildChecker" || exit
if [[ `uname` == MINGW* || `uname` == CYGWIN* ]]; then if [[ $(uname) == MINGW* || $(uname) == CYGWIN* ]]; then
# Windows # Windows
py -3 git_helper.py --quiet py -3 git_helper.py --quiet
else else

View File

@@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
# Just call post-checkout since it does the same thing. # Just call post-checkout since it does the same thing.
gitroot=`git rev-parse --show-toplevel` gitroot=$(git rev-parse --git-path hooks)
bash "$gitroot/.git/hooks/post-checkout" bash "$gitroot/post-checkout"