diff --git a/Makefile b/Makefile index e81804fc29..7daf3697c7 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,7 @@ all: zenith postgres ### Zenith Rust bits # # The 'postgres_ffi' depends on the Postgres headers. +.PHONY: zenith zenith: postgres-headers cargo build @@ -37,15 +38,18 @@ tmp_install/build/config.status: --prefix=$(abspath tmp_install) > configure.log) # nicer alias for running 'configure' +.PHONY: postgres-configure postgres-configure: tmp_install/build/config.status # Install the PostgreSQL header files into tmp_install/include +.PHONY: postgres-headers postgres-headers: postgres-configure +@echo "Installing PostgreSQL headers" $(MAKE) -C tmp_install/build/src/include MAKELEVEL=0 install # Compile and install PostgreSQL and contrib/zenith +.PHONY: postgres postgres: postgres-configure +@echo "Compiling PostgreSQL" $(MAKE) -C tmp_install/build MAKELEVEL=0 install @@ -67,14 +71,10 @@ distclean: rm -rf tmp_install cargo clean +.PHONY: fmt fmt: - @files=$$(git diff --cached --name-only --diff-filter=ACM | grep ".rs"); \ - if [ "$$files" ]; then \ - rustfmt $$files --edition=2018; \ - fi; - + ./pre-commit.py --fix-inplace +.PHONY: setup-pre-commit-hook setup-pre-commit-hook: - ln -s -f ../../pre-commit.sh.tpl .git/hooks/pre-commit - -.PHONY: postgres-configure postgres postgres-headers zenith + ln -s -f ../../pre-commit.py .git/hooks/pre-commit diff --git a/pre-commit.py b/pre-commit.py new file mode 100755 index 0000000000..5a4e0def5c --- /dev/null +++ b/pre-commit.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 + +from typing import List +import subprocess +import sys + +RED = "\033[0;31m" +GREEN = "\033[0;33m" +CYAN = "\033[0;36m" +NC = "\033[0m" # No Color + + +def fix_inplace() -> bool: + if len(sys.argv) == 2 and sys.argv[1] == "--fix-inplace": + return True + return False + + +def get_commit_files() -> List[str]: + files = subprocess.check_output( + "git diff --cached --name-only --diff-filter=ACM".split() + ) + return files.decode().splitlines() + + +def check(name: str, suffix: str, cmd: str, changed_files: List[str]): + print(f"Checking: {name} ", end="") + applicable_files = list( + filter(lambda fname: fname.strip().endswith(suffix), changed_files) + ) + if not applicable_files: + print(f"{CYAN}[NOT APPLICABLE]{NC}") + return + + cmd = f'{cmd} {" ".join(applicable_files)}' + res = subprocess.run(cmd.split(), capture_output=True) + if res.returncode != 0: + print(f"{RED}[FAILED]{NC}") + print("Please inspect the output below and run make fmt to fix automatically\n") + print(res.stdout.decode()) + exit(1) + print(f"{GREEN}[OK]{NC}") + + +if __name__ == "__main__": + files = get_commit_files() + rustfmt = "rustfmt --edition=2018" + if not fix_inplace(): + rustfmt += " --check" + check( + name="rustfmt", + suffix=".rs", + cmd=rustfmt, + changed_files=files, + ) diff --git a/pre-commit.sh.tpl b/pre-commit.sh.tpl deleted file mode 100755 index ca0e76ab06..0000000000 --- a/pre-commit.sh.tpl +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/bash - -RED='\033[0;31m' -GREEN='\033[0;33m' -CYAN='\033[0;36m' -NC='\033[0m' # No Color - -function check { - name=$1 - prefix=$2 - files=$(git diff --cached --name-only --diff-filter=ACM | grep $prefix) - shift; shift; - echo -n "checking $name "; - if [ -z "$files" ]; then - echo -e "${CYAN}[NOT APPLICABLE]${NC}" - exit 0; - fi - - cd $(git rev-parse --show-toplevel) - - out=$($@ $files) - if [ $? -eq 0 ]; - then - echo -e "${GREEN}[OK]${NC}" - cd - - else - echo -e "${RED}[FAILED]${NC}" - echo -e "Please inspect the output below and run make fmt to fix automatically\n" - echo -e "$out" - exit 1 - fi -} - -check rustfmt .rs rustfmt --check --edition=2018