|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# clang-format-all: a tool to run clang-format on an entire project |
| 4 | +# Copyright (C) 2016 Evan Klitzke <[email protected]> |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (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, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +function usage { |
| 20 | + echo "Usage: $0 DIR..." |
| 21 | + exit 1 |
| 22 | +} |
| 23 | + |
| 24 | +if [ $# -eq 0 ]; then |
| 25 | + usage |
| 26 | +fi |
| 27 | + |
| 28 | +# Variable that will hold the name of the clang-format command |
| 29 | +FMT="" |
| 30 | + |
| 31 | +# Some distros just call it clang-format. Others (e.g. Ubuntu) are insistent |
| 32 | +# that the version number be part of the command. We prefer clang-format if |
| 33 | +# that's present, otherwise we work backwards from highest version to lowest |
| 34 | +# version. |
| 35 | +for clangfmt in clang-format{,-{4,3}.{9,8,7,6,5,4,3,2,1,0}}; do |
| 36 | + if which "$clangfmt" &>/dev/null; then |
| 37 | + FMT="$clangfmt" |
| 38 | + break |
| 39 | + fi |
| 40 | +done |
| 41 | + |
| 42 | +# Check if we found a working clang-format |
| 43 | +if [ -z "$FMT" ]; then |
| 44 | + echo "failed to find clang-format" |
| 45 | + exit 1 |
| 46 | +fi |
| 47 | + |
| 48 | +# Check all of the arguments first to make sure they're all directories |
| 49 | +for dir in "$@"; do |
| 50 | + if [ ! -d "${dir}" ]; then |
| 51 | + echo "${dir} is not a directory" |
| 52 | + usage |
| 53 | + fi |
| 54 | +done |
| 55 | + |
| 56 | +# Find a dominating file, starting from a given directory and going up. |
| 57 | +find-dominating-file() { |
| 58 | + if [ -r "$1"/"$2" ]; then |
| 59 | + return 0 |
| 60 | + fi |
| 61 | + if [ "$1" = "/" ]; then |
| 62 | + return 1 |
| 63 | + fi |
| 64 | + find-dominating-file "$(realpath "$1"/..)" "$2" |
| 65 | + return $? |
| 66 | +} |
| 67 | + |
| 68 | +# Run clang-format -i on all of the things |
| 69 | +for dir in "$@"; do |
| 70 | + pushd "${dir}" &>/dev/null |
| 71 | + if ! find-dominating-file . .clang-format; then |
| 72 | + echo "Failed to find dominating .clang-format starting at $PWD" |
| 73 | + continue |
| 74 | + fi |
| 75 | + find . \ |
| 76 | + \( -name '*.c' \ |
| 77 | + -o -name '*.cc' \ |
| 78 | + -o -name '*.cpp' \ |
| 79 | + -o -name '*.h' \ |
| 80 | + -o -name '*.hh' \ |
| 81 | + -o -name '*.hpp' \) \ |
| 82 | + -exec "${FMT}" -i '{}' \; |
| 83 | + popd &>/dev/null |
| 84 | +done |
0 commit comments