I have an iOS/Swift project under git version control system. Hierarchy is below,
home/user/git_root/docs
home/user/git_root/project
home/user/git_root/project/project.xcodeproj
As you see my git_root
directory contains some other documents in docs
folder along with actual project directory.
I have installed swiftlint
in my Mac using homebrew,
brew install swiftlint
Current location of swiftlint installed is,
which swiftlint
/usr/local/bin/swiftlint
When I am running my below script which was added in Build phase -> Run scripts
, Weird thing happens,
if which swiftlint >/dev/null; then
echo "swiftlint already installed in machine";
git diff --cached --name-only | grep "\.swift" | while read filename; do
echo "swiftlinting file $filename";
swiftlint lint --path "$filename";
done
fi
Some swift files are printed by echo, but swiftlint
cant find the files and produces below error,
No lintable files found at paths : ''
For example, below is the output from build log,
swiftlinting file project/a.swift
No lintable files found at paths : 'project/a.swift
But when I apply cd home/user/git_root
at the beginning of the script, it works perfectly. But I cannot know the actual root of the git because other teammates might rename it on their own and so, I cannot use hardcoded path in script.
Why git diff
produces file paths which are not recognised by swiftlint
from the same directory in script? How can I solve this problem?
The purpose of the script is to run swiftlint only in newly modified unstaged swift files. Also note that I am not using any .swiftlint.yaml
files for swiftlint configurations.
It seems that the --path argument only takes absolute paths. I don't know how you can make it take a relative path, but you can slightly alter the script to append the rest of the path to the filename, like this:
git diff --cached --name-only | grep "\.swift" | while read filename; do
echo "swiftlinting file $filename";
swiftlint lint --path "$(git rev-parse --show-toplevel)/${filename}";
done