clang-format.bash 3.54 KB
Newer Older
1 2
#!/usr/bin/env bash
#=============================================================================
3
# Copyright 2015-2017 Kitware, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#=============================================================================

usage='usage: clang-format.bash [<options>] [--]

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    --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

39
Example to format files modified by the most recent commit:
40

41
    Utilities/Scripts/clang-format.bash --amend
42 43 44 45

Example to format all files:

    Utilities/Scripts/clang-format.bash --tracked
46 47 48 49 50 51

Example to format the current topic:

    git filter-branch \
      --tree-filter "Utilities/Scripts/clang-format.bash --tracked" \
      master..
52 53 54 55 56 57 58 59 60 61
'

die() {
    echo "$@" 1>&2; exit 1
}

#-----------------------------------------------------------------------------

# Parse command-line arguments.
clang_format=''
62
mode=''
63 64
while test "$#" != 0; do
    case "$1" in
65 66
    --amend) mode="amend" ;;
    --cached) mode="cached" ;;
67
    --clang-format) shift; clang_format="$1" ;;
68 69 70
    --help) echo "$help"; exit 0 ;;
    --modified) mode="modified" ;;
    --tracked) mode="tracked" ;;
71 72 73 74 75 76 77 78 79 80
    --) shift ; break ;;
    -*) die "$usage" ;;
    *) break ;;
    esac
    shift
done
test "$#" = 0 || die "$usage"

# Find a default tool.
tools='
81
  clang-format-6.0
82
  clang-format
83 84 85 86 87 88 89 90 91 92 93 94
'
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

# Verify that we have a tool.
if ! type -p "$clang_format" >/dev/null; then
95
    echo "Unable to locate a 'clang-format' tool."
96 97 98
    exit 1
fi

99 100
if ! "$clang_format" --version | grep 'clang-format version 6\.0' >/dev/null 2>/dev/null; then
    echo "clang-format version 6.0 is required (exactly)"
101 102 103
    exit 1
fi

104 105 106 107 108 109 110 111 112 113
# Select listing mode.
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

114 115
# List files as selected above.
$git_ls |
116

117
  # Select sources with our attribute.
118 119
  git check-attr --stdin format.clang-format-6.0 |
  grep -e ': format\.clang-format-6\.0: set$'     |
120
  sed -n 's/:[^:]*:[^:]*$//p'                |
121 122

  # Update sources in-place.
123 124
  tr '\n' '\0'                               |
  xargs -0 "$clang_format" -i