Skip to content

Commit

Permalink
Add check modes to check-file-format.sh, and use them in precommit an…
Browse files Browse the repository at this point in the history
…d github action
  • Loading branch information
regularfry authored and stefaniuk committed Sep 23, 2023
1 parent 9642792 commit 18af9e0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
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
language: script
pass_filenames: false
- repo: local
Expand Down
67 changes: 47 additions & 20 deletions scripts/githooks/check-file-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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:
# BRANCH_NAME=other-branch-than-main # Branch to compare with, default is `origin/main`
Expand All @@ -18,11 +18,23 @@ set +e
# 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.

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

Expand All @@ -31,27 +43,18 @@ image_version=2.7.1@sha256:dd3ca9ea50ef4518efe9be018d669ef9cf937f6bb5cfe2ef84ff2

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


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
docker run --rm --platform linux/amd64 \
--volume=$PWD:/check \
mstruebing/editorconfig-checker:$image_version \
sh -c 'ec --exclude ".git/" $(git diff --diff-filter=ACMRT --name-only)'
fi
# 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 @@ -65,7 +68,31 @@ function is-arg-true() {

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

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

echo $check
echo $filter

is-arg-true "$VERBOSE" && set -x
is-arg-true "$dry_run" && dry_run_opt="--dry-run"

main $*

Expand Down

0 comments on commit 18af9e0

Please sign in to comment.