Skip to content

Commit aef22eb

Browse files
committed
add format-check cirrus workflow.
1 parent 0ca347d commit aef22eb

File tree

8 files changed

+182
-2
lines changed

8 files changed

+182
-2
lines changed

.cirrus.format.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
## Cirrus CI: PR-only Linux FormatCheck
2+
task:
3+
name: FormatCheck
4+
only_if: $CIRRUS_PR != ''
5+
6+
container:
7+
image: debian:bookworm-slim
8+
cpu: 2
9+
memory: 2G
10+
11+
format_check_script: |
12+
set -euo pipefail
13+
echo "[FormatCheck] Installing build dependencies..."
14+
apt-get update -qq
15+
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends \
16+
build-essential git ca-certificates perl python3 meson ninja-build pkg-config flex bison
17+
18+
echo "[FormatCheck] Configuring minimal Meson build..."
19+
meson setup build --buildtype=release --auto-features=disabled
20+
21+
echo "[FormatCheck] Building pg_bsd_indent via Meson..."
22+
ninja -C build src/tools/pg_bsd_indent/pg_bsd_indent || ninja -C build pg_bsd_indent || ninja -C build
23+
24+
echo "[FormatCheck] Determining changed files..."
25+
HEAD_SHA=$(git rev-parse HEAD)
26+
BASE_BRANCH="${CIRRUS_BASE_BRANCH:-}"
27+
if [ -z "$BASE_BRANCH" ]; then
28+
BASE_BRANCH=$(git remote show origin 2>/dev/null | sed -n '/HEAD branch/s/.*: //p')
29+
fi
30+
BASE=""
31+
if [ -n "$BASE_BRANCH" ]; then
32+
git fetch --quiet origin "$BASE_BRANCH" || true
33+
BASE=$(git merge-base HEAD "origin/$BASE_BRANCH" || true)
34+
fi
35+
if [ -z "$BASE" ]; then
36+
BASE="${CIRRUS_BASE_SHA:-}"
37+
fi
38+
if [ -z "$BASE" ] || [ "$BASE" = "$HEAD_SHA" ]; then
39+
BASE=$(git rev-parse HEAD~1 2>/dev/null || echo "$HEAD_SHA")
40+
fi
41+
echo "HEAD=$HEAD_SHA BASE_BRANCH=$BASE_BRANCH BASE=$BASE"
42+
43+
files=$(git diff --name-only "$BASE"..HEAD | grep -E -i '\.(c|h|cpp|hpp)$' | grep -v '^src/tools/pg_bsd_indent/' || true)
44+
if [ -z "$files" ]; then
45+
echo "[FormatCheck] No C/C++ changes detected; skipping."
46+
exit 0
47+
fi
48+
export PGTYPEDEFS=$(pwd)/src/tools/pgindent/typedefs.list
49+
export PGINDENT=$(pwd)/build/src/tools/pg_bsd_indent/pg_bsd_indent
50+
51+
export LANG=C.UTF-8
52+
export LC_ALL=C.UTF-8
53+
54+
echo "[FormatCheck] Running pgindent --check..."
55+
set +e
56+
./src/tools/pgindent/pgindent --check $files
57+
rc=$?
58+
set -e
59+
if [ $rc -ne 0 ]; then
60+
echo "[FormatCheck] Unformatted changes detected; suggested patch below:"
61+
./src/tools/pgindent/pgindent --diff $files || true
62+
exit $rc
63+
fi
64+
echo "[FormatCheck] Passed."

.cirrus.star

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ def main():
3939
output += "\n# REPO_CI_CONFIG_URL was not set\n"
4040

4141
# Add 3)
42-
output += config_from(".cirrus.tasks.yml")
42+
# output += config_from(".cirrus.tasks.yml")
43+
44+
# PR-only format check
45+
output += config_from(".cirrus.format.yml")
4346

4447
return output
4548

.githooks/pre-commit

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
# Allow skipping via env var (e.g. to temporarily skip formatting):
6+
# PG_NO_FORMAT=1 git commit -m "..."
7+
if [[ "${PG_NO_FORMAT:-}" == "1" ]]; then
8+
exit 0
9+
fi
10+
11+
repo_root=$(git rev-parse --show-toplevel)
12+
cd "$repo_root"
13+
14+
# Process only staged C/C++ files (exclude pg_bsd_indent sources)
15+
mapfile -t files < <(\
16+
git diff --cached --name-only --diff-filter=ACMR | \
17+
grep -E '\.(c|h|cpp|hpp)$' | \
18+
grep -vE '^src/tools/pg_bsd_indent/' || true)
19+
20+
if [[ ${#files[@]} -eq 0 ]]; then
21+
exit 0
22+
fi
23+
24+
echo "[pre-commit] Ensuring pg_bsd_indent is built..."
25+
if ! make -C src/tools/pg_bsd_indent -s pg_bsd_indent >/dev/null; then
26+
echo "[pre-commit] Failed to build src/tools/pg_bsd_indent/pg_bsd_indent." >&2
27+
echo "[pre-commit] Install build tools and try: make -C src/tools/pg_bsd_indent V=1" >&2
28+
exit 1
29+
fi
30+
31+
if [[ ! -x src/tools/pg_bsd_indent/pg_bsd_indent ]]; then
32+
echo "[pre-commit] Built binary not found: src/tools/pg_bsd_indent/pg_bsd_indent" >&2
33+
exit 1
34+
fi
35+
36+
export PGINDENT="$repo_root/src/tools/pg_bsd_indent/pg_bsd_indent"
37+
38+
# Pin typedefs list
39+
export PGTYPEDEFS="$repo_root/src/tools/pgindent/typedefs.list"
40+
41+
pgindent_script="$repo_root/src/tools/pgindent/pgindent"
42+
if [[ ! -x "$pgindent_script" ]]; then
43+
echo "[pre-commit] Executable $pgindent_script not found; cannot format." >&2
44+
exit 1
45+
fi
46+
47+
echo "[pre-commit] Formatting staged files with pgindent..."
48+
"$pgindent_script" "${files[@]}"
49+
50+
# Re-add formatted files to the index
51+
git add -- "${files[@]}"
52+
53+
# Second check to ensure no remaining diffs
54+
echo "[pre-commit] Verifying formatting consistency..."
55+
if ! "$pgindent_script" --check "${files[@]}"; then
56+
echo "[pre-commit] Unformatted changes remain. Review the diff and retry commit." >&2
57+
exit 1
58+
fi
59+
60+
exit 0
61+

CONTRIBUTING.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ When it comes to C and C++ parts of IvorySQL, we try to follow PostgreSQL Coding
2222

2323
For **C** and **Perl** code, please run pgindent if necessary. We recommend using **git diff --color** when reviewing your changes so that you don't have any spurious whitespace issues in the code that you submit.
2424

25+
Formatting hooks and CI:
26+
- A pre-commit formatting hook is provided at `.githooks/pre-commit`. Enable it with `git config core.hooksPath .githooks`, or run `make enable-git-hooks` (equivalently `bash tools/enable-git-hooks.sh`).
27+
- The hook depends only on in-tree tools `src/tools/pgindent` and `src/tools/pg_bsd_indent`. On commit it formats staged C/C++ files with pgindent and re-adds them to the index.
28+
- A Cirrus workflow `FormatCheck` runs `pgindent --check` on files changed in a PR.
29+
2530
All new functionality that is contributed to IvorySQL should be covered by regression tests that are contributed alongside it. If you are uncertain about how to test or document your work, please raise the question on the ivorysql-hackers mailing list and the developer community will do its best to help you.
2631

2732
At the very minimum, you should always be running make installcheck-world to make sure that you're not breaking anything.

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ all check oracle-check all-check install installdirs installcheck oracle-install
4343
echo "You must use GNU make to build PostgreSQL." ; \
4444
false; \
4545
fi
46+
47+
48+
.PHONY: enable-git-hooks
49+
enable-git-hooks:
50+
@bash tools/enable-git-hooks.sh

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ Furthermore, for more detailed installation instructions, please refer to the [I
3131
- [Rpm installation](https://docs.ivorysql.org/en/ivorysql-doc/v4.5/v4.5/6#Rpm-installation)
3232
- [Source code installation](https://docs.ivorysql.org/en/ivorysql-doc/v4.5/v4.5/6#Source-code-installation)
3333

34-
34+
## Developer Formatting hooks and CI:
35+
- A pre-commit formatting hook is provided at `.githooks/pre-commit`. Enable it with `git config core.hooksPath .githooks`, or run `make enable-git-hooks` (equivalently `bash tools/enable-git-hooks.sh`).
36+
- The hook depends only on in-tree tools `src/tools/pgindent` and `src/tools/pg_bsd_indent`. On commit it formats staged C/C++ files with pgindent and re-adds them to the index.
37+
- A Cirrus workflow `FormatCheck` runs `pgindent --check` on files changed in a PR.
3538

3639
## Contributing to the IvorySQL
3740
There are plenty of ways to contribute to IvorySQL. You can contribute by providing the documentation updates, by providing the

README_CN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ IvorySQL 项目采用 Apache 2.0 许可协议发布,并鼓励各种形式的
1616

1717
</br>
1818

19+
## 开发者代码格式化
20+
- 提交前自动格式化(推荐):
21+
- 已克隆仓库:在仓库根目录执行 `make enable-git-hooks`(或 `bash tools/enable-git-hooks.sh`
22+
- 提交时行为:Git 钩子会自动用 `pgindent` 格式化已暂存的 C/C++ 文件并回加到暂存区,未通过二次校验会阻止提交。
23+
- PR 阶段:Cirrus 将运行 `FormatCheck`(pgindent --check)对差异文件做只读校验。
24+
1925
## 我们致力于遵循开源理念的原则
2026
我们致力于遵循[开源之道](https://opensource.com/open-source-way)的原则,并坚定地相信构建一个健康且包容的社区至关重要。我们始终坚信,优秀的想法可以来源于任何地方,而最优的想法应当脱颖而出。只有在多元观点的碰撞下,才能做出最明智的决策。尽管 IvorySQL 的首个版本主要聚焦于 Oracle 兼容性功能,但未来的路线图和功能集将由社区以开源的方式共同决定。
2127
</br>

tools/enable-git-hooks.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
# Enable the repository's pre-commit hook and prebuild pg_bsd_indent.
3+
# Usage: bash tools/enable-git-hooks.sh
4+
5+
set -euo pipefail
6+
7+
repo_root=$(git rev-parse --show-toplevel 2>/dev/null || true)
8+
if [[ -z "$repo_root" ]]; then
9+
echo "Please run this script inside a Git repository." >&2
10+
exit 1
11+
fi
12+
cd "$repo_root"
13+
14+
echo "[enable-git-hooks] Setting core.hooksPath=.githooks ..."
15+
git config --local core.hooksPath .githooks
16+
17+
# Optional: support tools that only check .git/hooks
18+
if [[ ! -e .git/hooks/pre-commit ]]; then
19+
mkdir -p .git/hooks
20+
ln -s ../.githooks/pre-commit .git/hooks/pre-commit || true
21+
fi
22+
23+
if [[ -x src/tools/pg_bsd_indent/pg_bsd_indent ]]; then
24+
echo "[enable-git-hooks] pg_bsd_indent already present; skipping build."
25+
else
26+
echo "[enable-git-hooks] Building pg_bsd_indent ..."
27+
if ! make -C src/tools/pg_bsd_indent -j$(nproc 2>/dev/null || echo 2) >/dev/null; then
28+
echo "[enable-git-hooks] Warning: build failed. you can build manually: make -C src/tools/pg_bsd_indent pg_bsd_indent"
29+
fi
30+
fi
31+
32+
echo "[enable-git-hooks] Done. Commits will now auto-run pgindent."
33+

0 commit comments

Comments
 (0)