2016-04-02 11:48:11 +08:00
|
|
|
#!/usr/bin/env fish
|
|
|
|
#
|
|
|
|
# This is meant to be run by "make style" or "make style-all". It is not meant to
|
|
|
|
# be run directly from a shell prompt although it can be.
|
|
|
|
#
|
|
|
|
# This runs C++ files and fish scripts (*.fish) through their respective code
|
|
|
|
# formatting programs.
|
|
|
|
#
|
2016-04-13 10:57:07 +08:00
|
|
|
set git_clang_format no
|
2016-04-02 11:48:11 +08:00
|
|
|
set c_files
|
|
|
|
set f_files
|
|
|
|
set all no
|
|
|
|
|
|
|
|
if test "$argv[1]" = "--all"
|
|
|
|
set all yes
|
|
|
|
set -e argv[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
if set -q argv[1]
|
|
|
|
echo "Unexpected arguments: '$argv'"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if test $all = yes
|
2016-04-13 10:57:07 +08:00
|
|
|
set files (git status --porcelain --short --untracked-files=all | sed -e 's/^ *[^ ]* *//')
|
|
|
|
if set -q files[1]
|
2016-07-23 20:00:56 +08:00
|
|
|
set_color --bold
|
|
|
|
echo "Will not proceed with style.fish --all as there are uncommited or untracked files."
|
|
|
|
set_color normal
|
|
|
|
echo "hint: git-clang-format can be ran directly"
|
|
|
|
exit 2
|
2016-04-13 10:57:07 +08:00
|
|
|
end
|
2016-07-23 20:00:56 +08:00
|
|
|
set c_files {src,osx,build_tools,share}/{***.h,***.cpp,***.m,***.js}
|
2016-04-02 11:48:11 +08:00
|
|
|
else
|
2016-04-13 10:57:07 +08:00
|
|
|
# We haven't been asked to reformat all the source. If there are uncommitted changes reformat
|
|
|
|
# those using `git clang-format`. Else reformat the files in the most recent commit.
|
2016-05-19 07:00:30 +08:00
|
|
|
# Select (cached files) (modified but not cached, and untracked files)
|
|
|
|
set files (git diff-index --cached HEAD --name-only) (git ls-files --exclude-standard --others --modified)
|
2016-04-13 10:57:07 +08:00
|
|
|
if set -q files[1]
|
|
|
|
set git_clang_format yes
|
2016-04-02 11:48:11 +08:00
|
|
|
else
|
|
|
|
# No pending changes so lint the files in the most recent commit.
|
2016-05-19 07:00:30 +08:00
|
|
|
set files (git diff-tree --no-commit-id --name-only -r HEAD)
|
2016-04-02 11:48:11 +08:00
|
|
|
end
|
|
|
|
|
2016-07-23 20:00:56 +08:00
|
|
|
# Extract just the C/C++/ObjC/js files that exist.
|
2016-05-09 03:08:23 +08:00
|
|
|
set c_files
|
2016-07-23 20:00:56 +08:00
|
|
|
for file in (string match -ri '^.*\.(?:c|cpp|h|m|js)$' -- $files)
|
|
|
|
test -f $file
|
|
|
|
and set c_files $c_files $file
|
2016-05-09 03:08:23 +08:00
|
|
|
end
|
2016-04-02 11:48:11 +08:00
|
|
|
# Extract just the fish files.
|
|
|
|
set f_files (string match -r '^.*\.fish$' -- $files)
|
|
|
|
end
|
|
|
|
|
2016-07-23 20:00:56 +08:00
|
|
|
# Run the git-clang-format if we have any C++ files.
|
2016-04-02 11:48:11 +08:00
|
|
|
if set -q c_files[1]
|
2016-04-13 10:57:07 +08:00
|
|
|
if test $git_clang_format = yes
|
|
|
|
if type -q git-clang-format
|
|
|
|
echo
|
|
|
|
echo Running git-clang-format
|
2016-07-23 20:00:56 +08:00
|
|
|
echo ========================
|
2016-04-13 10:57:07 +08:00
|
|
|
git add $c_files
|
|
|
|
git-clang-format
|
|
|
|
else
|
2016-07-23 20:00:56 +08:00
|
|
|
echo (set_color bold)'Warning: git-clang-format not installed or in $PATH'(set_color normal)
|
2016-04-13 10:57:07 +08:00
|
|
|
end
|
|
|
|
else if type -q clang-format
|
2016-04-02 11:48:11 +08:00
|
|
|
echo
|
|
|
|
echo Running clang-format
|
2016-07-23 20:00:56 +08:00
|
|
|
echo ====================
|
2016-04-02 11:48:11 +08:00
|
|
|
for file in $c_files
|
2016-06-12 06:28:40 +08:00
|
|
|
cp $file $file.new # preserves mode bits
|
2016-04-13 10:57:07 +08:00
|
|
|
clang-format $file >$file.new
|
2016-04-02 11:48:11 +08:00
|
|
|
if cmp --quiet $file $file.new
|
2016-07-23 20:00:56 +08:00
|
|
|
echo $file
|
2016-04-02 11:48:11 +08:00
|
|
|
rm $file.new
|
|
|
|
else
|
2016-07-23 20:00:56 +08:00
|
|
|
echo (set_color --underline)$file(set_color normal) is being reformatted
|
2016-04-02 11:48:11 +08:00
|
|
|
mv $file.new $file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
echo 'WARNING: Cannot find clang-format command'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Run the fish reformatter if we have any fish files.
|
|
|
|
if set -q f_files[1]
|
|
|
|
if not type -q fish_indent
|
|
|
|
make fish_indent
|
|
|
|
set PATH . $PATH
|
|
|
|
end
|
|
|
|
echo
|
|
|
|
echo Running fish_indent
|
2016-07-23 20:00:56 +08:00
|
|
|
echo ===================
|
2016-04-02 11:48:11 +08:00
|
|
|
for file in $f_files
|
2016-06-12 06:28:40 +08:00
|
|
|
cp $file $file.new # preserves mode bits
|
2016-04-13 10:57:07 +08:00
|
|
|
fish_indent <$file >$file.new
|
2016-04-02 11:48:11 +08:00
|
|
|
if cmp --quiet $file $file.new
|
2016-07-23 20:00:56 +08:00
|
|
|
echo $file
|
2016-04-02 11:48:11 +08:00
|
|
|
rm $file.new
|
|
|
|
else
|
2016-07-25 07:39:43 +08:00
|
|
|
echo (set_color --underline)$file(set_color normal)is being reformatted
|
2016-04-02 11:48:11 +08:00
|
|
|
mv $file.new $file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|