2016-04-02 03:48:11 +00:00
|
|
|
#!/usr/bin/env fish
|
|
|
|
#
|
|
|
|
# This runs C++ files and fish scripts (*.fish) through their respective code
|
|
|
|
# formatting programs.
|
|
|
|
#
|
2016-04-13 02:57:07 +00:00
|
|
|
set git_clang_format no
|
2016-04-02 03:48:11 +00:00
|
|
|
set c_files
|
2019-05-03 17:15:25 +00:00
|
|
|
set fish_files
|
|
|
|
set python_files
|
2016-04-02 03:48:11 +00: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 02:57:07 +00:00
|
|
|
set files (git status --porcelain --short --untracked-files=all | sed -e 's/^ *[^ ]* *//')
|
|
|
|
if set -q files[1]
|
2016-07-25 00:58:56 +00:00
|
|
|
echo
|
|
|
|
echo You have uncommited changes. Cowardly refusing to restyle the entire code base.
|
|
|
|
echo
|
|
|
|
exit 1
|
2016-04-13 02:57:07 +00:00
|
|
|
end
|
2018-02-10 18:46:34 +00:00
|
|
|
set c_files src/*.h src/*.cpp src/*.c
|
2016-12-04 04:36:47 +00:00
|
|
|
# For now we don't restyle all fish scripts other than completion scripts. That's because people
|
|
|
|
# really like to vertically align the elements of the `complete` command and fish_indent
|
|
|
|
# currently does not honor that whitespace.
|
2019-05-03 17:15:25 +00:00
|
|
|
set fish_files (printf '%s\n' share/***.fish | grep -v /completions/)
|
|
|
|
set python_files **.py
|
2016-04-02 03:48:11 +00:00
|
|
|
else
|
2016-04-13 02:57:07 +00: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-18 23:00:30 +00: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 02:57:07 +00:00
|
|
|
if set -q files[1]
|
|
|
|
set git_clang_format yes
|
2016-04-02 03:48:11 +00:00
|
|
|
else
|
|
|
|
# No pending changes so lint the files in the most recent commit.
|
2016-05-18 23:00:30 +00:00
|
|
|
set files (git diff-tree --no-commit-id --name-only -r HEAD)
|
2016-04-02 03:48:11 +00:00
|
|
|
end
|
|
|
|
|
2016-07-25 00:58:56 +00:00
|
|
|
# Extract just the C/C++ files that exist.
|
2016-05-08 19:08:23 +00:00
|
|
|
set c_files
|
2016-07-25 00:58:56 +00:00
|
|
|
for file in (string match -r '^.*\.(?:c|cpp|h)$' -- $files)
|
|
|
|
test -f $file; and set c_files $c_files $file
|
2016-05-08 19:08:23 +00:00
|
|
|
end
|
2016-04-02 03:48:11 +00:00
|
|
|
# Extract just the fish files.
|
2019-05-03 17:15:25 +00:00
|
|
|
set fish_files (string match -r '^.*\.fish$' -- $files)
|
|
|
|
set python_files (string match -r '^.*\.py$' -- $files)
|
2016-04-02 03:48:11 +00:00
|
|
|
end
|
|
|
|
|
2019-05-03 17:20:45 +00: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 00:58:56 +00:00
|
|
|
# Run the C++ reformatter if we have any C++ files.
|
2016-04-02 03:48:11 +00:00
|
|
|
if set -q c_files[1]
|
2016-04-13 02:57:07 +00:00
|
|
|
if test $git_clang_format = yes
|
|
|
|
if type -q git-clang-format
|
2019-05-03 17:20:45 +00:00
|
|
|
echo === Running "$red"git-clang-format"$normal"
|
2016-04-13 02:57:07 +00:00
|
|
|
git add $c_files
|
|
|
|
git-clang-format
|
|
|
|
else
|
2016-07-25 00:58:56 +00:00
|
|
|
echo
|
|
|
|
echo 'WARNING: Cannot find git-clang-format command'
|
|
|
|
echo
|
2016-04-13 02:57:07 +00:00
|
|
|
end
|
|
|
|
else if type -q clang-format
|
2019-05-03 17:20:45 +00:00
|
|
|
echo === Running "$red"clang-format"$normal"
|
2016-04-02 03:48:11 +00:00
|
|
|
for file in $c_files
|
2016-06-11 22:28:40 +00:00
|
|
|
cp $file $file.new # preserves mode bits
|
2016-04-13 02:57:07 +00:00
|
|
|
clang-format $file >$file.new
|
2016-04-02 03:48:11 +00:00
|
|
|
if cmp --quiet $file $file.new
|
|
|
|
rm $file.new
|
|
|
|
else
|
2016-07-25 00:58:56 +00:00
|
|
|
echo $file was NOT correctly formatted
|
2016-04-02 03:48:11 +00:00
|
|
|
mv $file.new $file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2016-07-25 00:58:56 +00:00
|
|
|
echo
|
2016-04-02 03:48:11 +00:00
|
|
|
echo 'WARNING: Cannot find clang-format command'
|
2016-07-25 00:58:56 +00:00
|
|
|
echo
|
2016-04-02 03:48:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Run the fish reformatter if we have any fish files.
|
2019-05-03 17:15:25 +00:00
|
|
|
if set -q fish_files[1]
|
2016-04-02 03:48:11 +00:00
|
|
|
if not type -q fish_indent
|
|
|
|
make fish_indent
|
|
|
|
set PATH . $PATH
|
|
|
|
end
|
2019-05-03 17:20:45 +00:00
|
|
|
echo === Running "$green"fish_indent"$normal"
|
2019-05-03 17:15:25 +00:00
|
|
|
for file in $fish_files
|
2016-06-11 22:28:40 +00:00
|
|
|
cp $file $file.new # preserves mode bits
|
2016-04-13 02:57:07 +00:00
|
|
|
fish_indent <$file >$file.new
|
2016-04-02 03:48:11 +00:00
|
|
|
if cmp --quiet $file $file.new
|
|
|
|
rm $file.new
|
|
|
|
else
|
2016-07-25 00:58:56 +00:00
|
|
|
echo $file was NOT correctly formatted
|
2016-04-02 03:48:11 +00:00
|
|
|
mv $file.new $file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-05-03 17:15:25 +00:00
|
|
|
|
|
|
|
if set -q python_files[1]
|
|
|
|
if not type -q black
|
|
|
|
echo
|
|
|
|
echo Please install "`black`" to style python
|
|
|
|
echo
|
|
|
|
else
|
2019-05-03 17:20:45 +00:00
|
|
|
echo === Running "$blue"black"$normal"
|
2019-05-03 17:15:25 +00:00
|
|
|
black $python_files
|
|
|
|
end
|
|
|
|
end
|