|
| 1 | +#!/usr/bin/env bash |
| 2 | +#============================================================================= |
| 3 | +# Copyright 2015-2016 Kitware, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +#============================================================================= |
| 17 | + |
| 18 | +usage='usage: clang-format.bash [<options>] [--] |
| 19 | +
|
| 20 | + --help Print usage plus more detailed help. |
| 21 | +
|
| 22 | + --clang-format <tool> Use given clang-format tool. |
| 23 | +
|
| 24 | + --amend Filter files changed by HEAD. |
| 25 | + --cached Filter files locally staged for commit. |
| 26 | + --modified Filter files locally modified from HEAD. |
| 27 | + --tracked Filter files tracked by Git. |
| 28 | +' |
| 29 | + |
| 30 | +help="$usage"' |
| 31 | +Example to format locally modified files: |
| 32 | +
|
| 33 | + Utilities/Scripts/clang-format.bash --modified |
| 34 | +
|
| 35 | +Example to format locally modified files staged for commit: |
| 36 | +
|
| 37 | + Utilities/Scripts/clang-format.bash --cached |
| 38 | +
|
| 39 | +Example to format files modified by the most recent commit: |
| 40 | +
|
| 41 | + Utilities/Scripts/clang-format.bash --amend |
| 42 | +
|
| 43 | +Example to format all files tracked by Git: |
| 44 | +
|
| 45 | + Utilities/Scripts/clang-format.bash --tracked |
| 46 | +
|
| 47 | +Example to format the current topic: |
| 48 | +
|
| 49 | + git filter-branch \ |
| 50 | + --tree-filter "Utilities/Scripts/clang-format.bash --tracked" \ |
| 51 | + master.. |
| 52 | +' |
| 53 | + |
| 54 | +die() { |
| 55 | + echo "$@" 1>&2; exit 1 |
| 56 | +} |
| 57 | + |
| 58 | +#----------------------------------------------------------------------------- |
| 59 | + |
| 60 | +# Parse command-line arguments. |
| 61 | +clang_format='' |
| 62 | +mode='' |
| 63 | +while test "$#" != 0; do |
| 64 | + case "$1" in |
| 65 | + --amend) mode="amend" ;; |
| 66 | + --cached) mode="cached" ;; |
| 67 | + --clang-format) shift; clang_format="$1" ;; |
| 68 | + --help) echo "$help"; exit 0 ;; |
| 69 | + --modified) mode="modified" ;; |
| 70 | + --tracked) mode="tracked" ;; |
| 71 | + --) shift ; break ;; |
| 72 | + -*) die "$usage" ;; |
| 73 | + *) break ;; |
| 74 | + esac |
| 75 | + shift |
| 76 | +done |
| 77 | +test "$#" = 0 || die "$usage" |
| 78 | + |
| 79 | +# Find a default tool. |
| 80 | +tools=' |
| 81 | + clang-format |
| 82 | + clang-format-3.8 |
| 83 | +' |
| 84 | +if test "x$clang_format" = "x"; then |
| 85 | + for tool in $tools; do |
| 86 | + if type -p "$tool" >/dev/null; then |
| 87 | + clang_format="$tool" |
| 88 | + break |
| 89 | + fi |
| 90 | + done |
| 91 | +fi |
| 92 | + |
| 93 | +# Verify that we have a tool. |
| 94 | +if ! type -p "$clang_format" >/dev/null; then |
| 95 | + echo "Unable to locate '$clang_format'" |
| 96 | + exit 1 |
| 97 | +fi |
| 98 | + |
| 99 | +# Select listing mode. |
| 100 | +case "$mode" in |
| 101 | + '') echo "$usage"; exit 0 ;; |
| 102 | + amend) git_ls='git diff-tree --diff-filter=AM --name-only HEAD -r --no-commit-id' ;; |
| 103 | + cached) git_ls='git diff-index --diff-filter=AM --name-only HEAD --cached' ;; |
| 104 | + modified) git_ls='git diff-index --diff-filter=AM --name-only HEAD' ;; |
| 105 | + tracked) git_ls='git ls-files' ;; |
| 106 | + *) die "invalid mode: $mode" ;; |
| 107 | +esac |
| 108 | +echo `$git_ls | git check-attr --stdin format.clang-format| sed -n '/: format\.clang-format: set$/ {s/:[^:]*:[^:]*$//p}'` |
| 109 | +# List files as selected above. |
| 110 | +$git_ls | |
| 111 | + |
| 112 | + # Select sources with our attribute. |
| 113 | + git check-attr --stdin format.clang-format | |
| 114 | + sed -n '/: format\.clang-format: set$/ {s/:[^:]*:[^:]*$//p}' | |
| 115 | + |
| 116 | + # Update sources in-place. |
| 117 | + xargs -d '\n' "$clang_format" -i |
| 118 | + |
0 commit comments