swiftgitversion-controlswiftlint

How to run swiftlint for git diff files only


I am working on an iOS Swift project. Git is being used for version controlling. I want to run swiftlint tool only for newly changed files. On other words, I want to check static issues only for the files that have been edited till last commit. If possible, I will add the script in Build phase -> Run script, so that every developer can find the static issues only for the files they are working on. Is it possible?


Solution

  • Adding this below command in Build phase - run script will be helpful.

    Below code will work for staged files

    git diff --cached --name-only | grep "\.swift" | while read filename; do
        swiftlint lint --path "$filename";
    done
    

    For unstaged files you need to remove --cached argument

    git diff --name-only | grep "\.swift" | while read filename; do
        swiftlint lint --path "$filename";
    done