sreekumar_droid
10/07/2020, 2:48 AMSha Sha Chu
10/07/2020, 2:51 AMsreekumar_droid
10/07/2020, 2:56 AMephemient
10/07/2020, 6:19 AMephemient
10/07/2020, 6:21 AMephemient
10/07/2020, 6:21 AMromtsn
10/07/2020, 8:22 AM#!/bin/bash
function join_by { local IFS="$1"; shift; echo "$*"; }
# grep the unstaged files and concat them into one string using '|' delimiter to supply it to grep -Ev later on
# to exclude the files from formatting, which are staged and unstaged at the same time (meaning that only a part of a file is being
# commited --> ktlint can run only over the complete files)
unstaged_files=$(git diff --name-only --diff-filter=ACM | grep '\.kt[s"]\?$')
unstaged_files=$(join_by , $unstaged_files)
unstaged_files=${unstaged_files//,/|}
if [ -z "$unstaged_files" ] ; then
files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.kt[s"]\?$')
else
files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.kt[s"]\?$' | grep -Ev $unstaged_files)
fi
if [ -z "$files" ] ; then
echo 'No .kt files to commit, skip formatting'
exit 0
fi
echo "Running ktlint formatter..."
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
project_path=${parent_path%/*/*}
# Format only staged files using ktlint
chmod a+x $project_path/scripts/ktlint/run-format
bash $project_path/scripts/ktlint/run-format $files
status=$?
# Stage the changes after formatting
git add $files
echo "ktlint formatted the code successfully."
exit 0
romtsn
10/07/2020, 8:24 AMromtsn
10/07/2020, 8:25 AMromtsn
10/07/2020, 8:26 AMtapchicoma
10/07/2020, 9:21 AMktlint-gradle
- it has such hook that runs only on files that will be committed