forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit-check-formatting
executable file
·81 lines (64 loc) · 2.26 KB
/
pre-commit-check-formatting
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
#!/bin/sh
# @author Klemens Böswirth <[email protected]>
# @brief Pre-commit hook that checks formatting
# @date 29.03.2019
# @tags reformat
# HOW TO USE
#
# If this is the only pre-commit hook you want to use,
# you can simply use a symlink:
#
# ln -s ../../scripts/dev/pre-commit-check-formatting .git/hooks/pre-commit
#
# If you want to use multiple pre-commit hooks, you have
# to use a script, that calls all of your hooks, including this one.
DATE=$(date +%s)
SRC_ROOT=$(git rev-parse --show-toplevel)
# check for unstaged changes
UNSTAGED=$(git diff --name-only)
if [ -n "$UNSTAGED" ]; then
# create temporary commit for staged changes
git commit --no-verify --message "WIP" --quiet
# stash away unstaged changes and remember stash id
echo "$UNSTAGED" | xargs git add
git stash push -q -m "pre-commit-$DATE"
UNSTAGED_STASH=$(git rev-parse -q --verify refs/stash)
# undo temporary commit
git reset --quiet --soft HEAD^
fi
# ensure that all unstaged changes are gone
if ! git diff --quiet; then
printf >&2 "Couldn't stash away unstaged changes, aborting!\n"
exit 1
fi
# get changed files
STAGED_FILES="$(git diff --cached --name-only --diff-filter=ACMR)"
# run reformat script
echo "$STAGED_FILES" | xargs "$SRC_ROOT/scripts/dev/reformat-all" > /dev/null
# get files changed by reformatting
UNFORMATTED_FILES="$(git diff --name-only)"
# create patch
PATCH_FILE="/tmp/elektra-$DATE-reformat.patch"
git diff > "$PATCH_FILE"
# undo formatting
echo "$UNFORMATTED_FILES" | xargs git checkout --quiet --
if [ -n "$UNSTAGED" ]; then
# convert stash hash to stash ref
STASH_REF=$(git reflog stash --format='%H %gd' | awk -v h="$UNSTAGED_STASH" '$1 == h { print $2 }')
# restore unstaged changes
git stash pop --quiet "$STASH_REF"
fi
if [ -n "$UNFORMATTED_FILES" ]; then
echo "The following files are not correctly formatted:"
printf "%s\n" "$UNFORMATTED_FILES"
echo "A diff of the formatting changes is stored at:"
echo "$PATCH_FILE"
PATCH_SCRIPT="/tmp/elektra-$DATE-reformat.patch.sh"
echo 'OLD_DIR=$(pwd)' > "$PATCH_SCRIPT"
echo "cd $SRC_ROOT" >> "$PATCH_SCRIPT"
echo "git apply $PATCH_FILE" >> "$PATCH_SCRIPT"
echo 'cd $OLD_DIR' >> "$PATCH_SCRIPT"
echo "You can try to apply it with, but unstaged changes may cause conflicts:"
echo "sh $PATCH_SCRIPT"
exit 1
fi