diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000000..882aabe19d --- /dev/null +++ b/.clang-format @@ -0,0 +1,17 @@ +# For more information, see: +# +# https://clang.llvm.org/docs/ClangFormat.html +# https://clang.llvm.org/docs/ClangFormatStyleOptions.html +# +--- + +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: Consecutive +AlignConsecutiveDeclarations: Consecutive +AlignArrayOfStructures: Left +AlwaysBreakAfterReturnType: AllDefinitions +UseTab: Never +IndentWidth: 2 +SpaceBeforeParens: Always +BreakBeforeBraces: GNU +ColumnLimit: 80 \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9979deb81f..cabcd2ac48 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -661,6 +661,22 @@ cppcheck: - report needs: [] +clang-format: + only: + - merge_requests + stage: analysis + before_script: + - apt-get update + - apt-get install -y clang-format git + allow_failure: true + script: + - .gitlab/run_style_check_diff.sh + artifacts: + paths: ['fetch_upstream.log', 'fetch_origin.log'] + when: on_failure + expire_in: 1 week + needs: [] + ## Ready-to-distribute ## win-installer-nightly: diff --git a/.gitlab/run_style_check_diff.sh b/.gitlab/run_style_check_diff.sh new file mode 100755 index 0000000000..beeca0c1b9 --- /dev/null +++ b/.gitlab/run_style_check_diff.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +set -e + +ancestor_horizon=28 # days (4 weeks) + +echo "" +echo "This script may be wrong. You may disregard it if it conflicts with" +echo "https://gitlab.gnome.org/GNOME/gimp/-/blob/master/CODING_STYLE.md" + +clang-format --version + +# Wrap everything in a subshell so we can propagate the exit status. +( + +source .gitlab/search-common-ancestor.sh + +git diff -U0 --no-color "${newest_common_ancestor_sha}" | clang-format-diff -p1 > format-diff.log +) +exit_status=$? + +[ ${exit_status} == 0 ] || exit ${exit_status} + +format_diff="$(/dev/null 2>&1 ; then + git remote add upstream https://gitlab.gnome.org/GNOME/gimp.git +fi +git fetch --shallow-since="$(date --date="${ancestor_horizon} days ago" +%Y-%m-%d)" upstream &> ./fetch_upstream.log + +# Work out the newest common ancestor between the detached HEAD that this CI job +# has checked out, and the upstream target branch (which will typically be +# `upstream/main` or `upstream/glib-2-62`). +# `${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}` or `${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME}` +# are only defined if we’re running in a merge request pipeline, +# fall back to `${CI_DEFAULT_BRANCH}` or `${CI_COMMIT_BRANCH}` respectively +# otherwise. + +# add mr-origin +git remote add mr-origin ${CI_MERGE_REQUEST_SOURCE_PROJECT_URL} + +source_branch="${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME:-${CI_COMMIT_BRANCH}}" +git fetch --shallow-since="$(date --date="${ancestor_horizon} days ago" +%Y-%m-%d)" mr-origin "${source_branch}" &> ./fetch_origin.log + +newest_common_ancestor_sha=$(diff --old-line-format='' --new-line-format='' <(git rev-list --first-parent "upstream/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME:-${CI_DEFAULT_BRANCH}}") <(git rev-list --first-parent "origin/${source_branch}") | head -1) +if [ -z "${newest_common_ancestor_sha}" ]; then + echo "Couldn’t find common ancestor with upstream main branch. This typically" + echo "happens if you branched from main a long time ago. Please update" + echo "your clone, rebase, and re-push your branch." + exit 1 +fi