123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- usage='usage: clang-format.bash [<options>] [--]
- --help Print usage plus more detailed help.
- --clang-format <tool> Use given clang-format tool.
- --amend Filter files changed by HEAD.
- --cached Filter files locally staged for commit.
- --modified Filter files locally modified from HEAD.
- --tracked Filter files tracked by Git.
- '
- help="$usage"'
- Example to format locally modified files:
- Utilities/Scripts/clang-format.bash --modified
- Example to format locally modified files staged for commit:
- Utilities/Scripts/clang-format.bash --cached
- Example to format files modified by the most recent commit:
- Utilities/Scripts/clang-format.bash --amend
- Example to format all files:
- Utilities/Scripts/clang-format.bash --tracked
- Example to format the current topic:
- git filter-branch \
- --tree-filter "Utilities/Scripts/clang-format.bash --tracked" \
- master..
- '
- die() {
- echo "$@" 1>&2; exit 1
- }
- clang_format=''
- mode=''
- while test "$#" != 0; do
- case "$1" in
- --amend) mode="amend" ;;
- --cached) mode="cached" ;;
- --clang-format) shift; clang_format="$1" ;;
- --help) echo "$help"; exit 0 ;;
- --modified) mode="modified" ;;
- --tracked) mode="tracked" ;;
- --) shift ; break ;;
- -*) die "$usage" ;;
- *) break ;;
- esac
- shift
- done
- test "$#" = 0 || die "$usage"
- tools='
- clang-format
- clang-format-3.8
- '
- if test "x$clang_format" = "x"; then
- for tool in $tools; do
- if type -p "$tool" >/dev/null; then
- clang_format="$tool"
- break
- fi
- done
- fi
- if ! type -p "$clang_format" >/dev/null; then
- echo "Unable to locate a 'clang-format' tool."
- exit 1
- fi
- case "$mode" in
- '') echo "$usage"; exit 0 ;;
- amend) git_ls='git diff-tree --diff-filter=AM --name-only HEAD -r --no-commit-id' ;;
- cached) git_ls='git diff-index --diff-filter=AM --name-only HEAD --cached' ;;
- modified) git_ls='git diff-index --diff-filter=AM --name-only HEAD' ;;
- tracked) git_ls='git ls-files' ;;
- *) die "invalid mode: $mode" ;;
- esac
- $git_ls |
- # Select sources with our attribute.
- git check-attr --stdin format.clang-format |
- grep -e ': format\.clang-format: set$' |
- sed -n 's/:[^:]*:[^:]*$//p' |
- # Update sources in-place.
- tr '\n' '\0' |
- xargs -0 "$clang_format" -i
|