2016-04-02 11:48:11 +08:00
|
|
|
#!/usr/bin/env fish
|
|
|
|
#
|
|
|
|
# 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
|
2019-05-04 01:15:25 +08:00
|
|
|
set fish_files
|
|
|
|
set python_files
|
2016-04-02 11:48:11 +08:00
|
|
|
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-25 08:58:56 +08:00
|
|
|
echo
|
2019-11-25 19:03:25 +08:00
|
|
|
echo You have uncommitted changes. Cowardly refusing to restyle the entire code base.
|
2016-07-25 08:58:56 +08:00
|
|
|
echo
|
|
|
|
exit 1
|
2016-04-13 10:57:07 +08:00
|
|
|
end
|
2018-02-11 02:46:34 +08:00
|
|
|
set c_files src/*.h src/*.cpp src/*.c
|
2019-05-05 18:53:48 +08:00
|
|
|
set fish_files (printf '%s\n' share/***.fish)
|
2019-05-04 01:15:25 +08:00
|
|
|
set python_files **.py
|
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-25 08:58:56 +08:00
|
|
|
# Extract just the C/C++ files that exist.
|
2016-05-09 03:08:23 +08:00
|
|
|
set c_files
|
2016-07-25 08:58:56 +08:00
|
|
|
for file in (string match -r '^.*\.(?:c|cpp|h)$' -- $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.
|
2019-05-04 01:15:25 +08:00
|
|
|
set fish_files (string match -r '^.*\.fish$' -- $files)
|
|
|
|
set python_files (string match -r '^.*\.py$' -- $files)
|
2016-04-02 11:48:11 +08:00
|
|
|
end
|
|
|
|
|
2019-05-04 01:20:45 +08:00
|
|
|
set -l red (set_color red)
|
|
|
|
set -l green (set_color green)
|
|
|
|
set -l blue (set_color blue)
|
|
|
|
set -l normal (set_color normal)
|
|
|
|
|
2016-07-25 08:58:56 +08:00
|
|
|
# Run the C++ reformatter 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
|
2019-05-04 01:20:45 +08:00
|
|
|
echo === Running "$red"git-clang-format"$normal"
|
2016-04-13 10:57:07 +08:00
|
|
|
git add $c_files
|
|
|
|
git-clang-format
|
|
|
|
else
|
2016-07-25 08:58:56 +08:00
|
|
|
echo
|
|
|
|
echo 'WARNING: Cannot find git-clang-format command'
|
|
|
|
echo
|
2016-04-13 10:57:07 +08:00
|
|
|
end
|
|
|
|
else if type -q clang-format
|
2019-05-04 01:20:45 +08:00
|
|
|
echo === Running "$red"clang-format"$normal"
|
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
|
|
|
|
rm $file.new
|
|
|
|
else
|
2016-07-25 08:58:56 +08:00
|
|
|
echo $file was NOT correctly formatted
|
2016-04-02 11:48:11 +08:00
|
|
|
mv $file.new $file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2016-07-25 08:58:56 +08:00
|
|
|
echo
|
2016-04-02 11:48:11 +08:00
|
|
|
echo 'WARNING: Cannot find clang-format command'
|
2016-07-25 08:58:56 +08:00
|
|
|
echo
|
2016-04-02 11:48:11 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Run the fish reformatter if we have any fish files.
|
2019-05-04 01:15:25 +08:00
|
|
|
if set -q fish_files[1]
|
2016-04-02 11:48:11 +08:00
|
|
|
if not type -q fish_indent
|
|
|
|
make fish_indent
|
|
|
|
set PATH . $PATH
|
|
|
|
end
|
2019-05-04 01:20:45 +08:00
|
|
|
echo === Running "$green"fish_indent"$normal"
|
2019-05-21 03:04:51 +08:00
|
|
|
fish_indent -w -- $fish_files
|
2016-04-02 11:48:11 +08:00
|
|
|
end
|
2019-05-04 01:15:25 +08:00
|
|
|
|
|
|
|
if set -q python_files[1]
|
|
|
|
if not type -q black
|
|
|
|
echo
|
|
|
|
echo Please install "`black`" to style python
|
|
|
|
echo
|
|
|
|
else
|
2019-05-04 01:20:45 +08:00
|
|
|
echo === Running "$blue"black"$normal"
|
2019-05-04 01:15:25 +08:00
|
|
|
black $python_files
|
|
|
|
end
|
|
|
|
end
|