diff options
| author | Mike Frysinger <vapier@google.com> | 2024-04-23 12:17:13 -0400 |
|---|---|---|
| committer | LUCI <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2024-04-23 18:31:51 +0000 |
| commit | 5591d99ee239be9116f4879bfea4a538b0b89e9c (patch) | |
| tree | 282752df0312e75204026badcc72acc9901e3222 | |
| parent | 9d865454aaf0ea41927332768e55b5b2f56e2533 (diff) | |
| download | git-repo-5591d99ee239be9116f4879bfea4a538b0b89e9c.tar.gz | |
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>
| -rwxr-xr-x | hooks/commit-msg | 8 | ||||
| -rwxr-xr-x | hooks/pre-auto-gc | 47 | ||||
| -rwxr-xr-x | release/update-hooks | 143 | ||||
| -rwxr-xr-x | run_tests | 1 |
4 files changed, 166 insertions, 33 deletions
diff --git a/hooks/commit-msg b/hooks/commit-msg index 112df63f..a6721d40 100755 --- a/hooks/commit-msg +++ b/hooks/commit-msg | |||
| @@ -1,5 +1,8 @@ | |||
| 1 | #!/bin/sh | 1 | #!/bin/sh |
| 2 | # From Gerrit Code Review 3.10.0 d5403dbf335ba7d48977fc95170c3f7027c34659 | 2 | # DO NOT EDIT THIS FILE |
| 3 | # All updates should be sent upstream: https://gerrit.googlesource.com/gerrit/ | ||
| 4 | # This is synced from commit: 62f5bbea67f6dafa6e22a601a0c298214c510caf | ||
| 5 | # DO NOT EDIT THIS FILE | ||
| 3 | # | 6 | # |
| 4 | # Part of Gerrit Code Review (https://www.gerritcodereview.com/) | 7 | # Part of Gerrit Code Review (https://www.gerritcodereview.com/) |
| 5 | # | 8 | # |
| @@ -31,8 +34,7 @@ if test ! -f "$1" ; then | |||
| 31 | fi | 34 | fi |
| 32 | 35 | ||
| 33 | # Do not create a change id if requested | 36 | # Do not create a change id if requested |
| 34 | create_setting=$(git config --get gerrit.createChangeId) | 37 | case "$(git config --get gerrit.createChangeId)" in |
| 35 | case "$create_setting" in | ||
| 36 | false) | 38 | false) |
| 37 | exit 0 | 39 | exit 0 |
| 38 | ;; | 40 | ;; |
diff --git a/hooks/pre-auto-gc b/hooks/pre-auto-gc index ec29be43..fb7b7630 100755 --- a/hooks/pre-auto-gc +++ b/hooks/pre-auto-gc | |||
| @@ -1,33 +1,25 @@ | |||
| 1 | #!/bin/sh | 1 | #!/bin/sh |
| 2 | # DO NOT EDIT THIS FILE | ||
| 3 | # All updates should be sent upstream: https://github.com/git/git | ||
| 4 | # This is synced from commit: 00e10ef10e161a913893b8cb33aa080d4ca5baa6 | ||
| 5 | # DO NOT EDIT THIS FILE | ||
| 2 | # | 6 | # |
| 3 | # An example hook script to verify if you are on battery, in case you | 7 | # An example hook script to verify if you are on battery, in case you |
| 4 | # are running Windows, Linux or OS X. Called by git-gc --auto with no | 8 | # are running Linux or OS X. Called by git-gc --auto with no arguments. |
| 5 | # arguments. The hook should exit with non-zero status after issuing an | 9 | # The hook should exit with non-zero status after issuing an appropriate |
| 6 | # appropriate message if it wants to stop the auto repacking. | 10 | # message if it wants to stop the auto repacking. |
| 7 | |||
| 8 | # This program is free software; you can redistribute it and/or modify | ||
| 9 | # it under the terms of the GNU General Public License as published by | ||
| 10 | # the Free Software Foundation; either version 2 of the License, or | ||
| 11 | # (at your option) any later version. | ||
| 12 | # | 11 | # |
| 13 | # This program is distributed in the hope that it will be useful, | 12 | # This hook is stored in the contrib/hooks directory. Your distribution |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 | # may have put this somewhere else. If you want to use this hook, you |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 14 | # should make this script executable then link to it in the repository |
| 16 | # GNU General Public License for more details. | 15 | # you would like to use it in. |
| 17 | # | 16 | # |
| 18 | # You should have received a copy of the GNU General Public License | 17 | # For example, if the hook is stored in |
| 19 | # along with this program; if not, write to the Free Software | 18 | # /usr/share/git-core/contrib/hooks/pre-auto-gc-battery: |
| 20 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 19 | # |
| 21 | 20 | # cd /path/to/your/repository.git | |
| 22 | if uname -s | grep -q "_NT-" | 21 | # ln -sf /usr/share/git-core/contrib/hooks/pre-auto-gc-battery \ |
| 23 | then | 22 | # hooks/pre-auto-gc |
| 24 | if test -x $SYSTEMROOT/System32/Wbem/wmic | ||
| 25 | then | ||
| 26 | STATUS=$(wmic path win32_battery get batterystatus /format:list | tr -d '\r\n') | ||
| 27 | [ "$STATUS" = "BatteryStatus=2" ] && exit 0 || exit 1 | ||
| 28 | fi | ||
| 29 | exit 0 | ||
| 30 | fi | ||
| 31 | 23 | ||
| 32 | if test -x /sbin/on_ac_power && (/sbin/on_ac_power;test $? -ne 1) | 24 | if test -x /sbin/on_ac_power && (/sbin/on_ac_power;test $? -ne 1) |
| 33 | then | 25 | then |
| @@ -48,11 +40,6 @@ elif test -x /usr/bin/pmset && /usr/bin/pmset -g batt | | |||
| 48 | grep -q "drawing from 'AC Power'" | 40 | grep -q "drawing from 'AC Power'" |
| 49 | then | 41 | then |
| 50 | exit 0 | 42 | exit 0 |
| 51 | elif test -d /sys/bus/acpi/drivers/battery && test 0 = \ | ||
| 52 | "$(find /sys/bus/acpi/drivers/battery/ -type l | wc -l)"; | ||
| 53 | then | ||
| 54 | # No battery exists. | ||
| 55 | exit 0 | ||
| 56 | fi | 43 | fi |
| 57 | 44 | ||
| 58 | echo "Auto packing deferred; not on AC" | 45 | echo "Auto packing deferred; not on AC" |
diff --git a/release/update-hooks b/release/update-hooks new file mode 100755 index 00000000..def4bba9 --- /dev/null +++ b/release/update-hooks | |||
| @@ -0,0 +1,143 @@ | |||
| 1 | #!/usr/bin/env python3 | ||
| 2 | # Copyright (C) 2024 The Android Open Source Project | ||
| 3 | # | ||
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | # you may not use this file except in compliance with the License. | ||
| 6 | # You may obtain a copy of the License at | ||
| 7 | # | ||
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | # | ||
| 10 | # Unless required by applicable law or agreed to in writing, software | ||
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | # See the License for the specific language governing permissions and | ||
| 14 | # limitations under the License. | ||
| 15 | |||
| 16 | """Helper tool for updating hooks from their various upstreams.""" | ||
| 17 | |||
| 18 | import argparse | ||
| 19 | import base64 | ||
| 20 | import json | ||
| 21 | from pathlib import Path | ||
| 22 | import sys | ||
| 23 | from typing import List, Optional | ||
| 24 | import urllib.request | ||
| 25 | |||
| 26 | |||
| 27 | assert sys.version_info >= (3, 8), "Python 3.8+ required" | ||
| 28 | |||
| 29 | |||
| 30 | TOPDIR = Path(__file__).resolve().parent.parent | ||
| 31 | HOOKS_DIR = TOPDIR / "hooks" | ||
| 32 | |||
| 33 | |||
| 34 | def update_hook_commit_msg() -> None: | ||
| 35 | """Update commit-msg hook from Gerrit.""" | ||
| 36 | hook = HOOKS_DIR / "commit-msg" | ||
| 37 | print( | ||
| 38 | f"{hook.name}: Updating from https://gerrit.googlesource.com/gerrit/" | ||
| 39 | "+/HEAD/resources/com/google/gerrit/server/tools/root/hooks/commit-msg" | ||
| 40 | ) | ||
| 41 | |||
| 42 | # Get the current commit. | ||
| 43 | url = "https://gerrit.googlesource.com/gerrit/+/HEAD?format=JSON" | ||
| 44 | with urllib.request.urlopen(url) as fp: | ||
| 45 | data = fp.read() | ||
| 46 | # Discard the xss protection. | ||
| 47 | data = data.split(b"\n", 1)[1] | ||
| 48 | data = json.loads(data) | ||
| 49 | commit = data["commit"] | ||
| 50 | |||
| 51 | # Fetch the data for that commit. | ||
| 52 | url = ( | ||
| 53 | f"https://gerrit.googlesource.com/gerrit/+/{commit}/" | ||
| 54 | "resources/com/google/gerrit/server/tools/root/hooks/commit-msg" | ||
| 55 | ) | ||
| 56 | with urllib.request.urlopen(f"{url}?format=TEXT") as fp: | ||
| 57 | data = fp.read() | ||
| 58 | |||
| 59 | # gitiles base64 encodes text data. | ||
| 60 | data = base64.b64decode(data) | ||
| 61 | |||
| 62 | # Inject header into the hook. | ||
| 63 | lines = data.split(b"\n") | ||
| 64 | lines = ( | ||
| 65 | lines[:1] | ||
| 66 | + [ | ||
| 67 | b"# DO NOT EDIT THIS FILE", | ||
| 68 | ( | ||
| 69 | b"# All updates should be sent upstream: " | ||
| 70 | b"https://gerrit.googlesource.com/gerrit/" | ||
| 71 | ), | ||
| 72 | f"# This is synced from commit: {commit}".encode("utf-8"), | ||
| 73 | b"# DO NOT EDIT THIS FILE", | ||
| 74 | ] | ||
| 75 | + lines[1:] | ||
| 76 | ) | ||
| 77 | data = b"\n".join(lines) | ||
| 78 | |||
| 79 | # Update the hook. | ||
| 80 | hook.write_bytes(data) | ||
| 81 | hook.chmod(0o755) | ||
| 82 | |||
| 83 | |||
| 84 | def update_hook_pre_auto_gc() -> None: | ||
| 85 | """Update pre-auto-gc hook from git.""" | ||
| 86 | hook = HOOKS_DIR / "pre-auto-gc" | ||
| 87 | print( | ||
| 88 | f"{hook.name}: Updating from https://github.com/git/git/" | ||
| 89 | "HEAD/contrib/hooks/pre-auto-gc-battery" | ||
| 90 | ) | ||
| 91 | |||
| 92 | # Get the current commit. | ||
| 93 | headers = { | ||
| 94 | "Accept": "application/vnd.github+json", | ||
| 95 | "X-GitHub-Api-Version": "2022-11-28", | ||
| 96 | } | ||
| 97 | url = "https://api.github.com/repos/git/git/git/refs/heads/master" | ||
| 98 | req = urllib.request.Request(url, headers=headers) | ||
| 99 | with urllib.request.urlopen(req) as fp: | ||
| 100 | data = fp.read() | ||
| 101 | data = json.loads(data) | ||
| 102 | |||
| 103 | # Fetch the data for that commit. | ||
| 104 | commit = data["object"]["sha"] | ||
| 105 | url = ( | ||
| 106 | f"https://raw.githubusercontent.com/git/git/{commit}/" | ||
| 107 | "contrib/hooks/pre-auto-gc-battery" | ||
| 108 | ) | ||
| 109 | with urllib.request.urlopen(url) as fp: | ||
| 110 | data = fp.read() | ||
| 111 | |||
| 112 | # Inject header into the hook. | ||
| 113 | lines = data.split(b"\n") | ||
| 114 | lines = ( | ||
| 115 | lines[:1] | ||
| 116 | + [ | ||
| 117 | b"# DO NOT EDIT THIS FILE", | ||
| 118 | ( | ||
| 119 | b"# All updates should be sent upstream: " | ||
| 120 | b"https://github.com/git/git/" | ||
| 121 | ), | ||
| 122 | f"# This is synced from commit: {commit}".encode("utf-8"), | ||
| 123 | b"# DO NOT EDIT THIS FILE", | ||
| 124 | ] | ||
| 125 | + lines[1:] | ||
| 126 | ) | ||
| 127 | data = b"\n".join(lines) | ||
| 128 | |||
| 129 | # Update the hook. | ||
| 130 | hook.write_bytes(data) | ||
| 131 | hook.chmod(0o755) | ||
| 132 | |||
| 133 | |||
| 134 | def main(argv: Optional[List[str]] = None) -> Optional[int]: | ||
| 135 | parser = argparse.ArgumentParser(description=__doc__) | ||
| 136 | parser.parse_args(argv) | ||
| 137 | |||
| 138 | update_hook_commit_msg() | ||
| 139 | update_hook_pre_auto_gc() | ||
| 140 | |||
| 141 | |||
| 142 | if __name__ == "__main__": | ||
| 143 | sys.exit(main(sys.argv[1:])) | ||
| @@ -32,6 +32,7 @@ def run_black(): | |||
| 32 | extra_programs = [ | 32 | extra_programs = [ |
| 33 | "repo", | 33 | "repo", |
| 34 | "run_tests", | 34 | "run_tests", |
| 35 | "release/update-hooks", | ||
| 35 | "release/update-manpages", | 36 | "release/update-manpages", |
| 36 | ] | 37 | ] |
| 37 | return subprocess.run( | 38 | return subprocess.run( |
