Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # An example hook script to verify what is about to be committed.
- # Called by "git commit" with no arguments. The hook should
- # exit with non-zero status after issuing an appropriate message if
- # it wants to stop the commit.
- prompt_confirm() {
- while true; do
- read -r -n 1 -p "${1:-Continue?} [y/n]: " REPLY
- case $REPLY in
- [yY]) echo ; return 0 ;;
- [nN]) echo ; return 1 ;;
- *) printf " \033[31m %s \n\033[0m" "invalid input"
- esac
- done
- }
- if git rev-parse --verify HEAD >/dev/null 2>&1
- then
- against=HEAD
- else
- # Initial commit: diff against an empty tree object
- against=$(git hash-object -t tree /dev/null)
- fi
- # Redirect output to stderr and input from console
- exec 1>&2 < /dev/tty
- set -a unformatted_files
- while IFS= read -r -d '' fn; do
- diff=$(diff <(git show :"$fn") <(git show :"$fn" | clang-format))
- if [[ ! -z "$diff" ]]; then
- #if git show :${fn} | clang-format -output-replacements-xml | grep -q '</replacement>'; then
- unformatted_files+=("$fn")
- echo
- echo Need to format ${fn}:
- echo "$diff"
- fi
- done < <(git diff --cached --name-only --diff-filter=ACMR -z $against -- '*.[ch]pp')
- if [ ${#unformatted_files[@]} -ne 0 ]; then
- echo
- echo "There are unformatted files:"
- printf '%s\n' "${unformatted_files[@]}"
- unstaged_changes_in_unformatted_files=$(comm -12 <(git diff --name-only) <(printf '%s\n' "${unformatted_files[@]}"))
- if [[ ! -z $unstaged_changes_in_unformatted_files ]]; then
- echo
- echo Can not run formatting because there are unstaged changes in unformatted files:
- echo "$unstaged_changes_in_unformatted_files"
- echo
- exit 1
- fi
- format_command="clang-format -i ${unformatted_files[@]}"
- echo
- if prompt_confirm "Apply formatting?"; then
- echo Applying formatting:
- echo $format_command
- $format_command
- git add ${unformatted_files[@]}
- if git diff --cached --name-only | sed 's/^/:/' | xargs git show | clang-format -output-replacements-xml | grep -q '</replacement>'; then
- echo Aw, there are still some unformatted files. Please check manually.
- exit 1
- fi
- else
- echo You can still apply it manually with:
- echo $format_command
- exit 1
- fi
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement