Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # An example hook script to verify what is about to be committed.
  4. # Called by "git commit" with no arguments. The hook should
  5. # exit with non-zero status after issuing an appropriate message if
  6. # it wants to stop the commit.
  7.  
  8. prompt_confirm() {
  9. while true; do
  10. read -r -n 1 -p "${1:-Continue?} [y/n]: " REPLY
  11. case $REPLY in
  12. [yY]) echo ; return 0 ;;
  13. [nN]) echo ; return 1 ;;
  14. *) printf " \033[31m %s \n\033[0m" "invalid input"
  15. esac
  16. done
  17. }
  18.  
  19. if git rev-parse --verify HEAD >/dev/null 2>&1
  20. then
  21. against=HEAD
  22. else
  23. # Initial commit: diff against an empty tree object
  24. against=$(git hash-object -t tree /dev/null)
  25. fi
  26.  
  27. # Redirect output to stderr and input from console
  28. exec 1>&2 < /dev/tty
  29.  
  30. set -a unformatted_files
  31.  
  32. while IFS= read -r -d '' fn; do
  33. diff=$(diff <(git show :"$fn") <(git show :"$fn" | clang-format))
  34. if [[ ! -z "$diff" ]]; then
  35. #if git show :${fn} | clang-format -output-replacements-xml | grep -q '</replacement>'; then
  36. unformatted_files+=("$fn")
  37. echo
  38. echo Need to format ${fn}:
  39. echo "$diff"
  40. fi
  41. done < <(git diff --cached --name-only --diff-filter=ACMR -z $against -- '*.[ch]pp')
  42.  
  43. if [ ${#unformatted_files[@]} -ne 0 ]; then
  44. echo
  45. echo "There are unformatted files:"
  46. printf '%s\n' "${unformatted_files[@]}"
  47.  
  48. unstaged_changes_in_unformatted_files=$(comm -12 <(git diff --name-only) <(printf '%s\n' "${unformatted_files[@]}"))
  49. if [[ ! -z $unstaged_changes_in_unformatted_files ]]; then
  50. echo
  51. echo Can not run formatting because there are unstaged changes in unformatted files:
  52. echo "$unstaged_changes_in_unformatted_files"
  53. echo
  54. exit 1
  55. fi
  56.  
  57. format_command="clang-format -i ${unformatted_files[@]}"
  58. echo
  59. if prompt_confirm "Apply formatting?"; then
  60. echo Applying formatting:
  61. echo $format_command
  62. $format_command
  63. git add ${unformatted_files[@]}
  64. if git diff --cached --name-only | sed 's/^/:/' | xargs git show | clang-format -output-replacements-xml | grep -q '</replacement>'; then
  65. echo Aw, there are still some unformatted files. Please check manually.
  66. exit 1
  67. fi
  68. else
  69. echo You can still apply it manually with:
  70. echo $format_command
  71. exit 1
  72. fi
  73. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement