Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: git hook pre-push #2359

Merged
merged 12 commits into from
Jun 14, 2024
41 changes: 41 additions & 0 deletions pre-push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.

# Check 'format' and 'golangci-lint' before 'git push',
# Copy this script to .git/hooks to activate,
# and remove it from .git/hooks to deactivate.

set -Euo pipefail

unset GIT_DIR
ROOT_DIR="$(git rev-parse --show-toplevel)"
cd "$ROOT_DIR"

run_check() {
local check_name=$1
echo "Running pre-push script $ROOT_DIR/x.py $check_name"
./x.py check "$check_name"

if [ $? -ne 0 ]; then
echo "You may use \`git push --no-verify\` to skip this check."
exit 1
fi
}

run_check format
run_check golangci-lint
17 changes: 14 additions & 3 deletions x.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@

from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter, REMAINDER
from glob import glob
from os import makedirs
import os
from pathlib import Path
import re
import stat
from subprocess import Popen, PIPE
import sys
from typing import List, Any, Optional, TextIO, Tuple
Expand Down Expand Up @@ -91,7 +92,14 @@ def check_version(current: str, required: Tuple[int, int, int], prog_name: Optio
raise RuntimeError(f"{prog_name} {require_version} or higher is required, got: {current}")

return semver

def enable_git_hook(hook_path : str) -> None:
hook_name = os.path.basename(hook_path)
dst = ".git/hooks/" + hook_name.split('.')[0]
if os.path.exists(dst):
os.remove(dst)
os.link(hook_path, dst)
os.chmod(dst, os.stat(dst).st_mode | stat.S_IEXEC)
print(hook_name, "installed at", dst)

def build(dir: str, jobs: Optional[int], ghproxy: bool, ninja: bool, unittest: bool, compiler: str, cmake_path: str, D: List[str],
skip_build: bool) -> None:
Expand All @@ -106,7 +114,7 @@ def build(dir: str, jobs: Optional[int], ghproxy: bool, ninja: bool, unittest: b
cmake_version = output.read().strip()
check_version(cmake_version, CMAKE_REQUIRE_VERSION, "CMake")

makedirs(dir, exist_ok=True)
os.makedirs(dir, exist_ok=True)

cmake_options = ["-DCMAKE_BUILD_TYPE=RelWithDebInfo"]
if ghproxy:
Expand All @@ -122,6 +130,9 @@ def build(dir: str, jobs: Optional[int], ghproxy: bool, ninja: bool, unittest: b

run(cmake, str(basedir), *cmake_options, verbose=True, cwd=dir)

os.makedirs("./git/hooks", exist_ok=True)
enable_git_hook("pre-push.sh")

if skip_build:
return

Expand Down
Loading