This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
spacemacs/.circleci/format
2018-08-29 23:12:02 +03:00

107 lines
3 KiB
Bash
Executable file

#!/usr/bin/env bash
## Formatting check script for CircleCI
##
## Copyright (c) 2012-2014 Sylvain Benner
## Copyright (c) 2014-2018 Sylvain Benner & Contributors
##
## Author: Eugene Yaremenko
## URL: https://github.com/syl20bnr/spacemacs
##
## This file is not part of GNU Emacs.
##
## License: GPLv3
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "${DIR}/shared"
if [[ -z "${CIRCLE_PR_NUMBER// }" ]]; then
echo "Skipping formatting check for branch update."
exit 0
fi
pr_patch_URL="https://patch-diff.githubusercontent.com/raw/"
pr_patch_URL+="${CIRCLE_PROJECT_USERNAME}/"
pr_patch_URL+="${CIRCLE_PROJECT_REPONAME}/pull/"
pr_patch_URL+="${CIRCLE_PR_NUMBER}.patch"
echo "PATCH_URL: \"${pr_patch_URL}\""
curl "${pr_patch_URL}" --output /tmp/patch --silent
first_commit=$(echo $(head -n 1 /tmp/patch) | sed -n 's/From \([a-zA-Z0-9]\+\).*/\1/p')
if [[ -z "${first_commit// }" ]]; then
echo "Can't find first commit"
exit 1
else
echo "First commit: ${first_commit}"
fi
git diff --name-only "${first_commit}^" HEAD > /tmp/changed_files
if [ $? -ne 0 ]; then
echo "Git diff failed"
exit 1
fi
changed_f_as_args=()
while read p
do
if [ -f "$p" ]; then
if [ ${p: -4} == ".org" ]; then
changed_f_as_args+=("${p}")
fi
fi
done </tmp/changed_files
echo_headline "CHECKING FOR MISPLACED SPACES AND TABS:"
old_head=$(git rev-parse --short HEAD)
git reset -q "${first_commit}^"
git add -N .
git diff --check --color > space_test_result
if [[ -s space_test_result ]]; then
cat space_test_result
exit 1
fi
git reset $old_head
echo "Done."
if [ ${#changed_f_as_args[@]} -ne 0 ]; then
echo_headline "TESTING DOCUMENTATION FORMATTING:"
emacs -batch -l /opt/spacedoc/emacs_tools/docfmt/run.el -no-site-file \
-q $(printf "%s " "${changed_f_as_args[@]}")
if [ $? -ne 0 ]; then
echo "Documentation formatting script failed"
exit 2
fi
git diff --color HEAD > spacefmt_result
if [[ -s spacefmt_result ]]; then
echo_headline "PLEASE APPLY CHANGES BELOW:"
cat spacefmt_result
git diff HEAD > /tmp/format.patch
bar=$(curl -sF 'file=@/tmp/format.patch' https://file.io)
uploaded=$(echo $bar | jq '.success')
if [ "${uploaded}" = true ] ; then
patch_link=$(echo $bar | jq '.link')
echo
echo_headline "You can run this commands:"
echo "curl ${patch_link} | git apply -v --index"
echo "git commit -m \"fixed formatting\""
echo "git push"
printf '=%.0s' {1..80}
echo
fi
exit 1
fi
echo_headline "TESTING DOCUMENTATION WITH SDN EXPORT:"
emacs -batch -l /opt/spacedoc/emacs_tools/export/run.el -no-site-file \
-q /root/.emacs.d/ $(printf "%s " "${changed_f_as_args[@]}")
if [ $? -ne 0 ]; then
echo "Documentation needs some fixing ;)"
exit 1
fi
else
echo "This commit doesn't change documentation files."
fi
echo "All test passed."