forked from nhs-england-tools/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcheck-file-format.sh
More file actions
executable file
Β·143 lines (114 loc) Β· 4.85 KB
/
check-file-format.sh
File metadata and controls
executable file
Β·143 lines (114 loc) Β· 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/bash
# WARNING: Please, DO NOT edit this file! It is maintained in the Repository Template (https://github.com/nhs-england-tools/repository-template). Raise a PR instead.
set -euo pipefail
# Pre-commit git hook to check the EditorConfig rules compliance over changed
# files. It ensures all non-binary files across the codebase are formatted
# according to the style defined in the `.editorconfig` file. This is a
# editorconfig command wrapper. It will run editorconfig natively if it is
# installed, otherwise it will run it in a Docker container.
#
# Usage:
# $ [options] ./check-file-format.sh
#
# Options:
# check={all,staged-changes,working-tree-changes,branch} # Check mode, default is 'working-tree-changes'
# dry_run=true # Do not check, run dry run only, default is 'false'
# BRANCH_NAME=other-branch-than-main # Branch to compare with, default is `origin/main`
# FORCE_USE_DOCKER=true # If set to true the command is run in a Docker container, 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
#
# Notes:
# 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.
# ==============================================================================
function main() {
if [[ ${#@} != 0 ]] && declare -f "run-editorconfig$1" >/dev/null 2>&1 ; then
"run-editorconfig$1" "${@:2}"
else
main-invocation "$@"
fi
}
function main-invocation() {
cd "$(git rev-parse --show-toplevel)"
# shellcheck disable=SC2154
is-arg-true "${dry_run:-false}" && dry_run_opt="--dry-run"
check=${check:-working-tree-changes}
if ! declare -f "list-files-to-check--$check" >/dev/null 2>&1 ; then
echo "Unrecognised check mode: $check" >&2 && exit 1
fi
if command -v editorconfig > /dev/null 2>&1 && ! is-arg-true "${FORCE_USE_DOCKER:-false}"; then
method=--natively
else
method=--via-docker
fi
list-files-to-check--"$check" |
# Maximum size of command line and environment combined is typically
# 2MB on Linux but only 256KB on macOS. Assume a maximum filename
# length of 200 characters and limiting us to batches of 1000 files
# gives us a bit of a safety margin.
dry_run_opt="${dry_run_opt:-}" xargs -0 --max-args=1000 --no-run-if-empty scripts/githooks/check-file-format.sh "$method"
}
# Run editorconfig natively.
# Arguments (provided as environment variables):
# dry_run_opt=[dry run option]
# Arguments (provided as positional parameters): files to check
function run-editorconfig--natively() {
# shellcheck disable=SC2046,SC2086
editorconfig \
--exclude '.git/' $dry_run_opt "$@"
}
# Run editorconfig in a Docker container.
# Arguments (provided as environment variables):
# dry_run_opt=[dry run option]
# Arguments (provided as positional parameters): files to check
function run-editorconfig--via-docker() {
# shellcheck disable=SC1091
source ./scripts/docker/docker.lib.sh
# shellcheck disable=SC2155
local image=$(name=mstruebing/editorconfig-checker docker-get-image-version-and-pull)
docker run --rm --platform linux/amd64 \
--volume "$PWD":/check \
"$image" \
sh -c "ec --exclude '.git/' $dry_run_opt $(printf '%q ' "$@")"
}
# ==============================================================================
# These all produce filenames terminated by a NUL character, so that we
# can handle filenames that contain spaces.
function list-files-to-check--all() {
git ls-files -z
}
function list-files-to-check--staged-changes() {
git diff -z --diff-filter=ACMRT --name-only --cached
}
function list-files-to-check--working-tree-changes() {
git diff -z --diff-filter=ACMRT --name-only
}
function list-files-to-check--branch() {
git diff -z --diff-filter=ACMRT --name-only ${BRANCH_NAME:-origin/main}
}
# ==============================================================================
function is-arg-true() {
if [[ "$1" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$ ]]; then
return 0
else
return 1
fi
}
# ==============================================================================
is-arg-true "${VERBOSE:-false}" && set -x
main "$@"
exit 0