From e53ffd053621e1d21da5605c6e2111ec629e57ad Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Wed, 19 Nov 2025 13:48:01 +0000 Subject: [PATCH] Update git hooks with upstream fixes (#4690) * Make git hooks work in git worktrees (#40038) Make hooks work in worktrees and cleanup hooks (cherry picked from commit d3731395b6de358b13364b02eaeff1244a54aa2f) * Replace all usages of /bin/bash shebang with /usr/bin/env (#40756) Replace all usages of /bin/bash with /usr/bin/env /usr/bin/env is nearly guaranteed to always exist at that location, which can't be said about /bin/bash and /bin/sh. Co-authored-by: opl <4833621+opl@users.noreply.github.com> (cherry picked from commit b9254d9ebf79ff28e27f1c54205e89fece209ad4) --------- Co-authored-by: War Pigeon <54217755+minus1over12@users.noreply.github.com> Co-authored-by: opl- Co-authored-by: opl <4833621+opl@users.noreply.github.com> --- BuildChecker/git_helper.py | 38 +++++++++++++++----------------- BuildChecker/hooks/post-checkout | 8 +++---- BuildChecker/hooks/post-merge | 6 ++--- Content.Tools/test/run.sh | 2 +- Tools/mapping-merge-driver.sh | 2 +- runclient-Tools.sh | 2 +- runclient.sh | 2 +- runserver-Tools.sh | 2 +- runserver.sh | 2 +- 9 files changed, 31 insertions(+), 33 deletions(-) diff --git a/BuildChecker/git_helper.py b/BuildChecker/git_helper.py index 53e3e1a7e1..53526ee442 100644 --- a/BuildChecker/git_helper.py +++ b/BuildChecker/git_helper.py @@ -1,17 +1,19 @@ #!/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 shutil +import subprocess +import sys import time from pathlib import Path from typing import List SOLUTION_PATH = Path("..") / "SpaceStation14.sln" # If this doesn't match the saved version we overwrite them all. -CURRENT_HOOKS_VERSION = "2" +CURRENT_HOOKS_VERSION = "4" 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() - completed = None - if capture: - completed = subprocess.run(command, cwd="..", stdout=subprocess.PIPE) + completed = subprocess.run(command, stdout=subprocess.PIPE, text=True) else: - completed = subprocess.run(command, cwd="..") + completed = subprocess.run(command) if completed.returncode != 0: print("Error: command exited with code {}!".format(completed.returncode)) @@ -43,7 +43,7 @@ def update_submodules(): Updates all submodules. """ - if ('GITHUB_ACTIONS' in os.environ): + if 'GITHUB_ACTIONS' in os.environ: return if os.path.isfile("DISABLE_SUBMODULE_AUTOUPDATE"): @@ -76,22 +76,21 @@ def install_hooks(): print("No hooks change detected.") return - with open("INSTALLED_HOOKS_VERSION", "w") as f: - f.write(CURRENT_HOOKS_VERSION) - 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") # Clear entire tree since we need to kill deleted files too. - for filename in os.listdir(str(hooks_target_dir)): - os.remove(str(hooks_target_dir/filename)) + for filename in os.listdir(hooks_target_dir): + 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)) - shutil.copy2(str(hooks_source_dir/filename), - str(hooks_target_dir/filename)) + shutil.copy2(hooks_source_dir / filename, hooks_target_dir / filename) + + with open("INSTALLED_HOOKS_VERSION", "w") as f: + f.write(CURRENT_HOOKS_VERSION) # Begin DeltaV Additions - add pre-commit hooks if you have it installed if shutil.which("pre-commit") is not None: @@ -111,8 +110,7 @@ def reset_solution(): def check_for_zip_download(): # Check if .git exists, - cur_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) - if not os.path.isdir(os.path.join(cur_dir, ".git")): + if run_command(["git", "rev-parse"]).returncode != 0: 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. " "Such as information to download the engine or even the ability to even be able to create contributions. \n" diff --git a/BuildChecker/hooks/post-checkout b/BuildChecker/hooks/post-checkout index c5662445c2..1b91112ff0 100755 --- a/BuildChecker/hooks/post-checkout +++ b/BuildChecker/hooks/post-checkout @@ -1,10 +1,10 @@ -#!/bin/bash +#!/usr/bin/env 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 py -3 git_helper.py --quiet else diff --git a/BuildChecker/hooks/post-merge b/BuildChecker/hooks/post-merge index 85fe61d966..864a9cff50 100755 --- a/BuildChecker/hooks/post-merge +++ b/BuildChecker/hooks/post-merge @@ -1,5 +1,5 @@ -#!/bin/bash +#!/usr/bin/env bash # Just call post-checkout since it does the same thing. -gitroot=`git rev-parse --show-toplevel` -bash "$gitroot/.git/hooks/post-checkout" +gitroot=$(git rev-parse --git-path hooks) +bash "$gitroot/post-checkout" diff --git a/Content.Tools/test/run.sh b/Content.Tools/test/run.sh index 7809b4dc92..876325ba56 100755 --- a/Content.Tools/test/run.sh +++ b/Content.Tools/test/run.sh @@ -1,3 +1,3 @@ -#!/bin/sh +#!/usr/bin/env bash cp 0A.yml out.yml ../bin/Debug/net5.0/Content.Tools out.yml 0B.yml 0C.yml diff --git a/Tools/mapping-merge-driver.sh b/Tools/mapping-merge-driver.sh index 0421278f31..fe114ac9cd 100755 --- a/Tools/mapping-merge-driver.sh +++ b/Tools/mapping-merge-driver.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/usr/bin/env bash # Add this to .git/config: # [merge "mapping-merge-driver"] diff --git a/runclient-Tools.sh b/runclient-Tools.sh index cc0c958b86..b564c35438 100755 --- a/runclient-Tools.sh +++ b/runclient-Tools.sh @@ -1,3 +1,3 @@ -#!/bin/sh +#!/usr/bin/env bash dotnet run --project Content.Client --configuration Tools read -p "Press enter to continue" diff --git a/runclient.sh b/runclient.sh index 9417cc9218..5df80bb08e 100755 --- a/runclient.sh +++ b/runclient.sh @@ -1,3 +1,3 @@ -#!/bin/sh +#!/usr/bin/env bash dotnet run --project Content.Client read -p "Press enter to continue" diff --git a/runserver-Tools.sh b/runserver-Tools.sh index 6280dc4279..3a786920a7 100755 --- a/runserver-Tools.sh +++ b/runserver-Tools.sh @@ -1,3 +1,3 @@ -#!/bin/sh +#!/usr/bin/env bash dotnet run --project Content.Server --configuration Tools read -p "Press enter to continue" diff --git a/runserver.sh b/runserver.sh index 33258e3190..33638d6bb5 100755 --- a/runserver.sh +++ b/runserver.sh @@ -1,3 +1,3 @@ -#!/bin/sh +#!/usr/bin/env bash dotnet run --project Content.Server read -p "Press enter to continue"