Debian Codegraph @

dev/tools/pre-commit

raw symbols SCIP
1 #!/bin/sh
2
3 # configure automatically sets up a wrapper at .git/hooks/pre-commit
4 # which calls this script (if it exists).
5
6 set -e
7
8 dev/tools/check-overlays.sh
9
10 log=$(mktemp "git-fix-ws-log.XXXXXX")
11 exec > "$log"
12
13 1>&2 echo "Auto fixing whitespace issues ($log)..."
14
15 # We fix whitespace in the index and in the working tree
16 # separately to preserve non-added changes.
17 index=$(mktemp "git-fix-ws-index.XXXXXX")
18 fixed_index=$(mktemp "git-fix-ws-index-fixed.XXXXXX")
19 tree=$(mktemp "git-fix-ws-tree.XXXXXX")
20 echo "Patches are saved in '$index', '$fixed_index' and '$tree'."
21 echo "If an error destroys your changes you can recover using them."
22 echo "(The files are cleaned up on success.)"
23 echo #newline
24
25 git diff-index -p --binary --cached HEAD > "$index"
26 git diff-index -p --binary HEAD > "$tree"
27
28 # reset work tree and index
29 # NB: untracked files which were not added are untouched
30 if [ -s "$index" ]; then git apply --whitespace=nowarn --cached -R "$index"; fi
31 if [ -s "$tree" ]; then git apply --whitespace=nowarn -R "$tree"; fi
32
33 # Fix index
34 # For end of file newlines we must go through the worktree
35 if [ -s "$index" ]; then
36 echo "Fixing staged changes..."
37 git apply --cached --whitespace=fix "$index"
38 git apply --whitespace=fix "$index" 2>/dev/null # no need to repeat yourself
39 git diff --cached --name-only -z | xargs -0 dev/tools/check-eof-newline.sh --fix
40 git add -u
41 echo #newline
42 fi
43
44 # reset work tree
45 git diff-index -p --binary --cached HEAD > "$fixed_index"
46 # If all changes were bad whitespace changes the patch is empty
47 # making git fail. Don't fail now: we fix the worktree first.
48 if [ -s "$fixed_index" ]; then git apply --whitespace=nowarn -R "$fixed_index"; fi
49
50 # Fix worktree
51 if [ -s "$tree" ]; then
52 echo "Fixing unstaged changes..."
53 git apply --whitespace=fix "$tree"
54 git diff --name-only -z | xargs -0 dev/tools/check-eof-newline.sh --fix
55 echo #newline
56 fi
57
58 if [ -s "$index" ] && ! [ -s "$fixed_index" ]; then
59 echo "Fixing whitespace issues cancelled all changes."
60 exit 1
61 fi
62
63 # Check that we did fix whitespace
64 if ! git diff-index --check --cached HEAD; then
65 echo "Auto-fixing whitespace failed: errors remain."
66 echo "This may fix itself if you try again."
67 echo "(Consider whether the number of errors decreases after each run.)"
68 exit 1
69 fi
70 echo "Whitespace pass complete."
71
72 # clean up temporary files
73 rm "$index" "$tree" "$fixed_index" "$log"