aboutsummaryrefslogtreecommitdiffhomepage
path: root/dev
diff options
context:
space:
mode:
authorGravatar Gaëtan Gilbert <gaetan.gilbert@skyskimmer.net>2018-01-23 16:36:25 +0100
committerGravatar Gaëtan Gilbert <gaetan.gilbert@skyskimmer.net>2018-02-08 17:16:39 +0100
commit85bcd29052d30db75df5b8e8aeeb91b0327077f2 (patch)
tree791ce33ee2a14a628be9aa8ecbf6e2f23d6f64d5 /dev
parent04a3fa7eac845039b0b1a11d1a5958486f183a4d (diff)
pre-commit: add files after fixing ending newlines.
Diffstat (limited to 'dev')
-rwxr-xr-xdev/tools/check-eof-newline.sh17
-rwxr-xr-xdev/tools/pre-commit57
2 files changed, 46 insertions, 28 deletions
diff --git a/dev/tools/check-eof-newline.sh b/dev/tools/check-eof-newline.sh
index c5a654e21..e244d9ab8 100755
--- a/dev/tools/check-eof-newline.sh
+++ b/dev/tools/check-eof-newline.sh
@@ -4,8 +4,9 @@
# Detect missing end of file newlines for FILES.
# Files are skipped if untracked by git and depending on gitattributes.
# With --fix, automatically append a newline.
-# Exit status: 1 if any file had a missing newline, 0 otherwise
-# (regardless of --fix).
+# Exit status:
+# Without --fix: 1 if any file had a missing newline, 0 otherwise.
+# With --fix: 1 if any non writable file had a missing newline, 0 otherwise.
FIX=
if [ "$1" = --fix ];
@@ -22,12 +23,18 @@ for f in "$@"; do
then
if [ -n "$FIX" ];
then
- echo >> "$f"
- echo "Newline appended to file $f!"
+ if [ -w "$f" ];
+ then
+ echo >> "$f"
+ echo "Newline appended to file $f!"
+ else
+ echo "File $f is missing a newline and not writable!"
+ CODE=1
+ fi
else
echo "No newline at end of file $f!"
+ CODE=1
fi
- CODE=1
fi
done
diff --git a/dev/tools/pre-commit b/dev/tools/pre-commit
index 0cd0a0b70..c5f6868d1 100755
--- a/dev/tools/pre-commit
+++ b/dev/tools/pre-commit
@@ -5,34 +5,42 @@
set -e
-CODE=0
-git diff --cached --name-only -z | xargs -0 dev/tools/check-eof-newline.sh --fix || CODE=1
-
-if [ $CODE -ne 0 ]
-then
- 1>&2 echo "Some files had newline errors; they have been fixed in the working tree."
- 1>&2 echo "Make sure to add them before committing."
- 1>&2 echo "This may fix itself if you were using git commit -a, and you try again."
- exit 1
-fi
-
-if git diff-index --check --cached HEAD >/dev/null 2>&1 ;
+if ! git diff --cached --name-only -z | xargs -0 dev/tools/check-eof-newline.sh ||
+ ! git diff-index --check --cached HEAD >/dev/null 2>&1 ;
then
- :
-else
1>&2 echo "Auto fixing whitespace issues..."
- TEMP=$(mktemp)
# We fix whitespace in the index and in the working tree
# separately to preserve non-added changes.
- git diff-index -p --cached HEAD > "$TEMP"
- git apply --cached -R "$TEMP"
- git apply --cached --whitespace=fix "$TEMP"
-
- git diff-index -p HEAD > "$TEMP"
- git apply -R "$TEMP"
- git apply --whitespace=fix "$TEMP"
- rm "$TEMP"
+ index=$(mktemp "git-fix-ws-index.XXXXX")
+ fixed_index=$(mktemp "git-fix-ws-index-fixed.XXXXX")
+ tree=$(mktemp "git-fix-ws-tree.XXXXX")
+ 1>&2 echo "Patches are saved in '$index', '$fixed_index' and '$tree'."
+ 1>&2 echo "If an error destroys your changes you can recover using them."
+ 1>&2 echo "(The files are cleaned up on success.)"
+
+ git diff-index -p --cached HEAD > "$index"
+ git diff-index -p HEAD > "$tree"
+
+ # reset work tree and index
+ # NB: untracked files which were not added are untouched
+ git apply --cached -R "$index"
+ git apply -R "$tree"
+
+ # Fix index
+ # For end of file newlines we must go through the worktree
+ git apply --cached --whitespace=fix "$index"
+ git apply --whitespace=fix "$index"
+ git diff --cached --name-only -z | xargs -0 dev/tools/check-eof-newline.sh --fix
+ git add -u
+
+ # reset work tree
+ git diff-index -p --cached HEAD > "$fixed_index"
+ git apply -R "$fixed_index"
+
+ # Fix worktree
+ git apply --whitespace=fix "$tree"
+ git diff --name-only -z | xargs -0 dev/tools/check-eof-newline.sh --fix
# Check that we did fix whitespace
if ! git diff-index --check --cached HEAD;
@@ -43,4 +51,7 @@ else
exit 1
fi
1>&2 echo "Whitespace issues fixed!"
+
+ # clean up temporary files
+ rm "$index" "$tree" "$fixed_index"
fi