| 1 | #!/bin/shscip-shell filesystem . . `/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=$(mktempscip-shell system . . mktemp. "git-fix-ws-log.XXXXXX") |
| 11 | exec > "$log" |
| 12 | |
| 13 | 1>&2 echoscip-shell system . . 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=$(mktempscip-shell system . . mktemp. "git-fix-ws-index.XXXXXX") |
| 18 | fixed_index=$(mktempscip-shell system . . mktemp. "git-fix-ws-index-fixed.XXXXXX") |
| 19 | tree=$(mktempscip-shell system . . mktemp. "git-fix-ws-tree.XXXXXX") |
| 20 | echoscip-shell system . . echo. "Patches are saved in '$index', '$fixed_index' and '$tree'." |
| 21 | echoscip-shell system . . echo. "If an error destroys your changes you can recover using them." |
| 22 | echoscip-shell system . . echo. "(The files are cleaned up on success.)" |
| 23 | echoscip-shell system . . echo. #newline |
| 24 | |
| 25 | gitscip-shell system . . git. diff-index -p --binary --cached HEAD > "$index" |
| 26 | gitscip-shell system . . 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 [scip-shell system . . `[`. -s "$index" ]; then gitscip-shell system . . git. apply --whitespace=nowarn --cached -R "$index"; fi |
| 31 | if [scip-shell system . . `[`. -s "$tree" ]; then gitscip-shell system . . 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 [scip-shell system . . `[`. -s "$index" ]; then |
| 36 | echoscip-shell system . . echo. "Fixing staged changes..." |
| 37 | gitscip-shell system . . git. apply --cached --whitespace=fix "$index" |
| 38 | gitscip-shell system . . git. apply --whitespace=fix "$index" 2>/dev/nullscip-shell filesystem . . `/dev/null`. # no need to repeat yourself |
| 39 | gitscip-shell system . . git. diff --cached --name-only -z | xargsscip-shell system . . xargs. -0 dev/tools/check-eof-newline.sh --fix |
| 40 | gitscip-shell system . . git. add -u |
| 41 | echoscip-shell system . . echo. #newline |
| 42 | fi |
| 43 | |
| 44 | # reset work tree |
| 45 | gitscip-shell system . . 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 [scip-shell system . . `[`. -s "$fixed_index" ]; then gitscip-shell system . . git. apply --whitespace=nowarn -R "$fixed_index"; fi |
| 49 | |
| 50 | # Fix worktree |
| 51 | if [scip-shell system . . `[`. -s "$tree" ]; then |
| 52 | echoscip-shell system . . echo. "Fixing unstaged changes..." |
| 53 | gitscip-shell system . . git. apply --whitespace=fix "$tree" |
| 54 | gitscip-shell system . . git. diff --name-only -z | xargsscip-shell system . . xargs. -0 dev/tools/check-eof-newline.sh --fix |
| 55 | echoscip-shell system . . echo. #newline |
| 56 | fi |
| 57 | |
| 58 | if [scip-shell system . . `[`. -s "$index" ] && ! [scip-shell system . . `[`. -s "$fixed_index" ]; then |
| 59 | echoscip-shell system . . echo. "Fixing whitespace issues cancelled all changes." |
| 60 | exit 1 |
| 61 | fi |
| 62 | |
| 63 | # Check that we did fix whitespace |
| 64 | if ! gitscip-shell system . . git. diff-index --check --cached HEAD; then |
| 65 | echoscip-shell system . . echo. "Auto-fixing whitespace failed: errors remain." |
| 66 | echoscip-shell system . . echo. "This may fix itself if you try again." |
| 67 | echoscip-shell system . . echo. "(Consider whether the number of errors decreases after each run.)" |
| 68 | exit 1 |
| 69 | fi |
| 70 | echoscip-shell system . . echo. "Whitespace pass complete." |
| 71 | |
| 72 | # clean up temporary files |
| 73 | rmscip-shell system . . rm. "$index" "$tree" "$fixed_index" "$log" |