You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
4.6 KiB

  1. #!/usr/bin/env bash
  2. # This program source code file is part of KICAD, a free EDA CAD application.
  3. #
  4. # Copyright (C) 2019 Kicad Developers, see AUTHORS.txt for contributors.
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, you may find one here:
  18. # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. # or you may search the http://www.gnu.org website for the version 2 license,
  20. # or you may write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. # Simple program to check and fix formatting in KiCad source files,
  23. # while ignoring violations in "uncontrolled" files, such as generated files
  24. # or 3rd party sources.
  25. usage='usage: check_coding.sh [<options>] [--]
  26. --help Print usage plus more detailed help.
  27. --diff Only show proposed changes, do not format files
  28. --cached Re-format changes currently staged for commit (default)
  29. --amend Re-format changes made in the previous commit
  30. --commit <commit-rev> Re-format changes made since commit-rev
  31. --ci Run in CI mode to return non-zero when there are formatting errors
  32. '
  33. help="$usage"'
  34. Example to format cached files:
  35. check_coding.sh
  36. To show what would be done:
  37. check_coding.sh --diff
  38. '
  39. die() {
  40. echo "$@" 1>&2; exit 1
  41. }
  42. # Parse command-line arguments.
  43. ci=false
  44. diff=false
  45. mode='cached'
  46. while test "$#" != 0; do
  47. case "$1" in
  48. --diff) diff=true ;;
  49. --amend) mode='amend' ;;
  50. --cached) mode='cached' ;;
  51. --commit) mode='commit'
  52. format_ref_commit="$2"
  53. shift
  54. ;;
  55. --ci) ci=true ;;
  56. --) shift ; break ;;
  57. -*) die "$usage" ;;
  58. *) break ;;
  59. esac
  60. shift
  61. done
  62. test "$#" = 0 || die "$usage"
  63. # This is the main KiCad formatting attribute
  64. # used by .gitattributes to mark KiCad source files
  65. format_attribute="format.clang-format-kicad"
  66. format_commit='HEAD'
  67. # Select the git file list command and the commit to check
  68. case "${mode}" in
  69. '') echo "$usage"; exit 0 ;;
  70. commit)
  71. # Files changed since listed commit
  72. git_list_files="git diff-tree --diff-filter=ACM --name-only HEAD ${format_ref_commit} -r --no-commit-id"
  73. format_commit=${format_ref_commit}
  74. ;;
  75. amend)
  76. # Files changed by the last commit
  77. git_list_files='git diff-tree --diff-filter=ACM --name-only HEAD -r --no-commit-id'
  78. format_commit='HEAD^'
  79. ;;
  80. cached)
  81. # Currently staged files
  82. git_list_files='git diff-index --diff-filter=ACM --name-only HEAD --cached'
  83. ;;
  84. *) die "Invalid mode: $mode" ;;
  85. esac
  86. if [ "${diff}" = true ]; then
  87. # Only show the proposed changes
  88. format_command="git clang-format --diff ${format_commit}"
  89. else
  90. # Actually make the changes
  91. format_command="git clang-format ${format_commit}"
  92. fi
  93. if [ "${ci}" = true ]; then
  94. # In CI mode we want to set the return value based on modifications (1 = modifications
  95. # needed, 0 = no modifications). We must capture the output to do this (since git clang-format
  96. # will always return 0). By capturing the output, we break the terminal coloring, so we hide
  97. # this inside a special CI mode.
  98. format_results="$( \
  99. ${git_list_files} |
  100. # Filter sources with the formatting attribute set
  101. git check-attr ${format_attribute} --stdin |
  102. # output only the file names
  103. grep ": set$" |
  104. cut -d: -f1 |
  105. # Apply the formatting command
  106. xargs ${format_command}
  107. )"
  108. echo "$format_results"
  109. # Read the results to see if modifications have been requested
  110. if [[ $format_results == "no modified files to format" ]] \
  111. || [[ $format_results == "clang-format did not modify any files" ]];
  112. then
  113. true
  114. else
  115. false
  116. fi
  117. else
  118. ${git_list_files} |
  119. # Filter sources with the formatting attribute set
  120. git check-attr ${format_attribute} --stdin |
  121. # output only the file names
  122. grep ": set$" |
  123. cut -d: -f1 |
  124. # Apply the formatting command
  125. xargs ${format_command}
  126. fi