Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # This script is a git clean filter (https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes#filters_b)
- # This will run when files are staged (specifically for all *.py files as defined in the .gitattributes file)
- # The point of this script will be to lint our python files with Python Black, and iSort upon committing them
- # This script takes in stdin data and expects stdout data. So in order to run black and iSort on the input
- # data, we need to save the data into a file somewhere, process that, and then output the results
- # Because this expects stdout, we can't let any of our commands output any data, so we just pipe them to
- # /dev/null. Unfortunatley this means that we won't get to see any errors.
- # Because of this, this script will fallback to output the original data if any error occurs.
- # Make a temporary file to store the stdin code
- tmpfile=$(mktemp)
- tmplint=$(mktemp)
- # The "file" is either stdin, or an argument (not sure we need this)
- file=${1--}
- # Read in the stdin data into our temp files
- while IFS= read -r line ; do
- echo "$line" >> "$tmpfile"
- echo "$line" >> "$tmplint"
- done < <(cat -- "$file")
- # Keep a status variable. If either command returns a non zero
- # then we just return the original input
- rc=0
- # Run black and iSort on our modified temp file
- isort $tmplint > /dev/null 2>&1
- if [ $? -ne 0 ] ; then
- rc=1
- fi
- black $tmplint > /dev/null 2>&1
- if [ $? -ne 0 ] ; then
- rc=1
- fi
- # Check the return codes
- if [ $rc -ne 0 ] ; then
- # If we had a bad exit, then just cat the original input
- cat $tmpfile
- else
- # else we cat the linted contents
- cat $tmplint
- fi
- # Remove the temp files
- rm $tmpfile > /dev/null 2>&1
- rm $tmplint > /dev/null 2>&1
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement