release: update-hooks: helper for automatically syncing hooks
These hooks are maintained in other projects. Add a script to automate
their import so people don't send us changes directly, and we can try to
steer them to the correct place.
Change-Id: Iac0bdb3aae84dda43a1600e73107555b513ce82b
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/422177
Commit-Queue: Mike Frysinger <vapier@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
Reviewed-by: Josip Sokcevic <sokcevic@google.com>
diff --git a/hooks/commit-msg b/hooks/commit-msg
index 112df63..a6721d4 100755
--- a/hooks/commit-msg
+++ b/hooks/commit-msg
@@ -1,5 +1,8 @@
#!/bin/sh
-# From Gerrit Code Review 3.10.0 d5403dbf335ba7d48977fc95170c3f7027c34659
+# DO NOT EDIT THIS FILE
+# All updates should be sent upstream: https://gerrit.googlesource.com/gerrit/
+# This is synced from commit: 62f5bbea67f6dafa6e22a601a0c298214c510caf
+# DO NOT EDIT THIS FILE
#
# Part of Gerrit Code Review (https://www.gerritcodereview.com/)
#
@@ -31,8 +34,7 @@
fi
# Do not create a change id if requested
-create_setting=$(git config --get gerrit.createChangeId)
-case "$create_setting" in
+case "$(git config --get gerrit.createChangeId)" in
false)
exit 0
;;
diff --git a/hooks/pre-auto-gc b/hooks/pre-auto-gc
index ec29be4..fb7b763 100755
--- a/hooks/pre-auto-gc
+++ b/hooks/pre-auto-gc
@@ -1,33 +1,25 @@
#!/bin/sh
+# DO NOT EDIT THIS FILE
+# All updates should be sent upstream: https://github.com/git/git
+# This is synced from commit: 00e10ef10e161a913893b8cb33aa080d4ca5baa6
+# DO NOT EDIT THIS FILE
#
# An example hook script to verify if you are on battery, in case you
-# are running Windows, Linux or OS X. Called by git-gc --auto with no
-# arguments. The hook should exit with non-zero status after issuing an
-# appropriate message if it wants to stop the auto repacking.
-
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
+# are running Linux or OS X. Called by git-gc --auto with no arguments.
+# The hook should exit with non-zero status after issuing an appropriate
+# message if it wants to stop the auto repacking.
#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
+# This hook is stored in the contrib/hooks directory. Your distribution
+# may have put this somewhere else. If you want to use this hook, you
+# should make this script executable then link to it in the repository
+# you would like to use it in.
#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-
-if uname -s | grep -q "_NT-"
-then
- if test -x $SYSTEMROOT/System32/Wbem/wmic
- then
- STATUS=$(wmic path win32_battery get batterystatus /format:list | tr -d '\r\n')
- [ "$STATUS" = "BatteryStatus=2" ] && exit 0 || exit 1
- fi
- exit 0
-fi
+# For example, if the hook is stored in
+# /usr/share/git-core/contrib/hooks/pre-auto-gc-battery:
+#
+# cd /path/to/your/repository.git
+# ln -sf /usr/share/git-core/contrib/hooks/pre-auto-gc-battery \
+# hooks/pre-auto-gc
if test -x /sbin/on_ac_power && (/sbin/on_ac_power;test $? -ne 1)
then
@@ -48,11 +40,6 @@
grep -q "drawing from 'AC Power'"
then
exit 0
-elif test -d /sys/bus/acpi/drivers/battery && test 0 = \
- "$(find /sys/bus/acpi/drivers/battery/ -type l | wc -l)";
-then
- # No battery exists.
- exit 0
fi
echo "Auto packing deferred; not on AC"
diff --git a/release/update-hooks b/release/update-hooks
new file mode 100755
index 0000000..def4bba
--- /dev/null
+++ b/release/update-hooks
@@ -0,0 +1,143 @@
+#!/usr/bin/env python3
+# Copyright (C) 2024 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Helper tool for updating hooks from their various upstreams."""
+
+import argparse
+import base64
+import json
+from pathlib import Path
+import sys
+from typing import List, Optional
+import urllib.request
+
+
+assert sys.version_info >= (3, 8), "Python 3.8+ required"
+
+
+TOPDIR = Path(__file__).resolve().parent.parent
+HOOKS_DIR = TOPDIR / "hooks"
+
+
+def update_hook_commit_msg() -> None:
+ """Update commit-msg hook from Gerrit."""
+ hook = HOOKS_DIR / "commit-msg"
+ print(
+ f"{hook.name}: Updating from https://gerrit.googlesource.com/gerrit/"
+ "+/HEAD/resources/com/google/gerrit/server/tools/root/hooks/commit-msg"
+ )
+
+ # Get the current commit.
+ url = "https://gerrit.googlesource.com/gerrit/+/HEAD?format=JSON"
+ with urllib.request.urlopen(url) as fp:
+ data = fp.read()
+ # Discard the xss protection.
+ data = data.split(b"\n", 1)[1]
+ data = json.loads(data)
+ commit = data["commit"]
+
+ # Fetch the data for that commit.
+ url = (
+ f"https://gerrit.googlesource.com/gerrit/+/{commit}/"
+ "resources/com/google/gerrit/server/tools/root/hooks/commit-msg"
+ )
+ with urllib.request.urlopen(f"{url}?format=TEXT") as fp:
+ data = fp.read()
+
+ # gitiles base64 encodes text data.
+ data = base64.b64decode(data)
+
+ # Inject header into the hook.
+ lines = data.split(b"\n")
+ lines = (
+ lines[:1]
+ + [
+ b"# DO NOT EDIT THIS FILE",
+ (
+ b"# All updates should be sent upstream: "
+ b"https://gerrit.googlesource.com/gerrit/"
+ ),
+ f"# This is synced from commit: {commit}".encode("utf-8"),
+ b"# DO NOT EDIT THIS FILE",
+ ]
+ + lines[1:]
+ )
+ data = b"\n".join(lines)
+
+ # Update the hook.
+ hook.write_bytes(data)
+ hook.chmod(0o755)
+
+
+def update_hook_pre_auto_gc() -> None:
+ """Update pre-auto-gc hook from git."""
+ hook = HOOKS_DIR / "pre-auto-gc"
+ print(
+ f"{hook.name}: Updating from https://github.com/git/git/"
+ "HEAD/contrib/hooks/pre-auto-gc-battery"
+ )
+
+ # Get the current commit.
+ headers = {
+ "Accept": "application/vnd.github+json",
+ "X-GitHub-Api-Version": "2022-11-28",
+ }
+ url = "https://api.github.com/repos/git/git/git/refs/heads/master"
+ req = urllib.request.Request(url, headers=headers)
+ with urllib.request.urlopen(req) as fp:
+ data = fp.read()
+ data = json.loads(data)
+
+ # Fetch the data for that commit.
+ commit = data["object"]["sha"]
+ url = (
+ f"https://raw.githubusercontent.com/git/git/{commit}/"
+ "contrib/hooks/pre-auto-gc-battery"
+ )
+ with urllib.request.urlopen(url) as fp:
+ data = fp.read()
+
+ # Inject header into the hook.
+ lines = data.split(b"\n")
+ lines = (
+ lines[:1]
+ + [
+ b"# DO NOT EDIT THIS FILE",
+ (
+ b"# All updates should be sent upstream: "
+ b"https://github.com/git/git/"
+ ),
+ f"# This is synced from commit: {commit}".encode("utf-8"),
+ b"# DO NOT EDIT THIS FILE",
+ ]
+ + lines[1:]
+ )
+ data = b"\n".join(lines)
+
+ # Update the hook.
+ hook.write_bytes(data)
+ hook.chmod(0o755)
+
+
+def main(argv: Optional[List[str]] = None) -> Optional[int]:
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.parse_args(argv)
+
+ update_hook_commit_msg()
+ update_hook_pre_auto_gc()
+
+
+if __name__ == "__main__":
+ sys.exit(main(sys.argv[1:]))
diff --git a/run_tests b/run_tests
index 7307f82..3e0e501 100755
--- a/run_tests
+++ b/run_tests
@@ -32,6 +32,7 @@
extra_programs = [
"repo",
"run_tests",
+ "release/update-hooks",
"release/update-manpages",
]
return subprocess.run(