blob: b8a92b53a255a3e7457f29970f99a5f7973c425a [file] [log] [blame]
dbeam54c48b32016-12-22 21:12:041#!/bin/bash
2# Copyright 2016 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6if [[ -z "${@}" ]]; then
dbeamc4278782017-01-10 04:38:517 echo >&2 "Usage: `basename $0` <paths_to_clang_format...>"
8 exit 1
dbeam54c48b32016-12-22 21:12:049fi
10
dbeamc4278782017-01-10 04:38:5111which clang-format >/dev/null 2>&1
dbeam54c48b32016-12-22 21:12:0412if [[ $? -ne 0 ]]; then
dbeamc4278782017-01-10 04:38:5113 echo >&2 "Sorry, but you need \`clang-format\` on your \$PATH to run this script"
14 exit 1
dbeam54c48b32016-12-22 21:12:0415fi
16
dbeamc4278782017-01-10 04:38:5117for arg in ${@}; do
18 echo "Processing ${arg}"
19
dbeam54c48b32016-12-22 21:12:0420 dir=`readlink -f "${arg}"`
dbeame6533f52016-12-23 01:28:2321 if [[ -d "${dir}" ]]; then
22 dir="${dir}/stripped-by-dirname-on-next-line"
23 fi
dbeam54c48b32016-12-22 21:12:0424 while dir=`dirname ${dir}`; do
25 if [[ -f "${dir}/.clang-format" ]]; then
dbeamc4278782017-01-10 04:38:5126 echo "Using style from: ${dir}/.clang-format"
27 break
dbeam54c48b32016-12-22 21:12:0428 elif [[ "${dir}" == "/" ]]; then
dbeamc4278782017-01-10 04:38:5129 echo >&2 "No .clang-format file found. Make one at or above ${arg}"
30 exit 1
dbeam54c48b32016-12-22 21:12:0431 fi
32 done
dbeam54c48b32016-12-22 21:12:0433
dbeamc4278782017-01-10 04:38:5134 js_files=$(git ls-tree -r --name-only HEAD -- "${arg}" | grep '\.js$')
35
36 for js_file in ${js_files}; do
37 echo "Formatting ${js_file}"
38 clang-format -i -style=file "$js_file"
39 done
40done