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

Change how check-file-format.sh checks changed files #130

Merged
merged 7 commits into from
Sep 26, 2023
2 changes: 1 addition & 1 deletion .github/actions/check-file-format/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ runs:
shell: bash
run: |
export BRANCH_NAME=origin/${{ github.event.repository.default_branch }}
./scripts/githooks/check-file-format.sh
check=branch ./scripts/githooks/check-file-format.sh
2 changes: 1 addition & 1 deletion scripts/config/pre-commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repos:
hooks:
- id: check-file-format
name: Check File Format
entry: ./scripts/githooks/check-file-format.sh
entry: check=staged-changes./scripts/githooks/check-file-format.sh
stefaniuk marked this conversation as resolved.
Show resolved Hide resolved
language: script
pass_filenames: false
- repo: local
Expand Down
81 changes: 48 additions & 33 deletions scripts/githooks/check-file-format.sh
regularfry marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,75 @@ set +e
# according to the style defined in the `.editorconfig` file.
#
# Usage:
# $ ./check-file-format.sh
# $ check={all,staged-changes,working-tree-changes,branch} [dry_run=true] ./check-file-format.sh
#
# Options:
stefaniuk marked this conversation as resolved.
Show resolved Hide resolved
# BRANCH_NAME=other-branch-than-main # Branch to compare with, default is `origin/main`
# ALL_FILES=true # Check all files, default is `false`
# VERBOSE=true # Show all the executed commands, default is `false`
#
# Exit codes:
# 0 - All files are formatted correctly
# 1 - Files are not formatted correctly
#
#
# The `check` parameter controls which files are checked, so you can
# limit the scope of the check according to what is appropriate at the
# point the check is being applied.
#
# check=all: check all files in the repository
# check=staged-changes: check only files staged for commit.
# check=working-tree-changes: check modified, unstaged files. This is the default.
# check=branch: check for all changes since branching from $BRANCH_NAME
#
# If the `dry_run` parameter is set to a truthy value, the list of
# files that ec would check is output, with no check done.
#
# Notes:
# 1) Please, make sure to enable EditorConfig linting in your IDE. For the
# Please, make sure to enable EditorConfig linting in your IDE. For the
# Visual Studio Code editor it is `editorconfig.editorconfig` that is already
# specified in the `./.vscode/extensions.json` file.
# 2) Due to the file name escaping issue files are checked one by one.

# ==============================================================================

# SEE: https://hub.docker.com/r/mstruebing/editorconfig-checker/tags, use the `linux/amd64` os/arch
image_version=2.7.0@sha256:0f8f8dd4f393d29755bef2aef4391d37c34e358d676e9d66ce195359a9c72ef3
exit_code=0
image_version=2.7.1@sha256:dd3ca9ea50ef4518efe9be018d669ef9cf937f6bb5cfe2ef84ff2a620b5ddc24

# ==============================================================================


function main() {

cd $(git rev-parse --show-toplevel)

if is-arg-true "$ALL_FILES"; then

# Check all files
docker run --rm --platform linux/amd64 \
--volume $PWD:/check \
mstruebing/editorconfig-checker:$image_version \
ec \
--exclude '.git/'

else

# Check changed files only
files=$( (git diff --diff-filter=ACMRT --name-only ${BRANCH_NAME:-origin/main}; git diff --name-only) | sort | uniq )
if [ -n "$files" ]; then
while read file; do
docker run --rm --platform linux/amd64 \
--volume=$PWD:/check \
mstruebing/editorconfig-checker:$image_version \
ec \
--exclude '.git/' \
"$file"
[ $? != 0 ] && exit_code=1 ||:
done < <(echo "$files")
fi

fi
is-arg-true "$dry_run" && dry_run_opt="--dry-run"

check=${check:-working-tree-changes}
case $check in
"all")
filter="git ls-files"
;;
"staged-changes")
filter="git diff --diff-filter=ACMRT --name-only --cached"
;;
"working-tree-changes")
filter="git diff --diff-filter=ACMRT --name-only"
;;
"branch")
filter="git diff --diff-filter=ACMRT --name-only ${BRANCH_NAME:-origin/main}"
;;
*)
echo "Unrecognised check mode: $check" >&2 && exit 1
;;
esac


# We use /dev/null here as a backstop in case there are no files in the state
# we choose. If the filter comes back empty, adding `/dev/null` onto it has
# the effect of preventing `ec` from treating "no files" as "all the files".
docker run --rm --platform linux/amd64 \
--volume=$PWD:/check \
mstruebing/editorconfig-checker:$image_version \
sh -c "ec --exclude '.git/' $dry_run_opt \$($filter) /dev/null"
}

function is-arg-true() {
Expand All @@ -75,8 +89,9 @@ function is-arg-true() {

# ==============================================================================


is-arg-true "$VERBOSE" && set -x

main $*

exit $exit_code
exit 0
Loading