See PEP8 errors introduced in a branch

There are times when you need to see PEP8 errors only introduced in a branch, so that you can fix those and say the excuse “I didn’t introduce, those, they’re not my problem.”

This generous script lets you do that. It compares the current branch with a target branch, and shows the new ones in the current branch, and hides the ones in the target.

This requires that you use git, and pep8 should be installed.

#!/bin/bash

CURRENT_BRANCH=`git branch --no-color | grep '*' | sed s/\*\ //`
TARGET_BRANCH=$1
CHANGED_FILE_LIST=filelist.diff.tmp
TARGET_PEP8="$TARGET_BRANCH".pep8.tmp
CURRENT_PEP8="$CURRENT_BRANCH".pep8.tmp
IGNORED_RULES="E501,W293"

if [[ -z "$1" ]]; then
    echo 'Usage: ./pep_introduced.sh target_branch'
    echo
else
    exec 7>&2
    exec 2>&-
    git checkout $TARGET_BRANCH
    git diff --name-only "$TARGET_BRANCH".."$CURRENT_BRANCH" | grep .py < $CHANGED_FILE_LIST

    echo > $TARGET_PEP8
    echo > $CURRENT_PEP8

    while read line
    do
        pep8 --ignore $IGNORED_RULES $line >> $TARGET_PEP8
    done < $CHANGED_FILE_LIST

    git checkout $CURRENT_BRANCH
    while read line
    do
        pep8 --ignore $IGNORED_RULES $line >> $CURRENT_PEP8
    done < $CHANGED_FILE_LIST

    exec 2<&7
    exec 7<&-
    diff $TARGET_PEP8 $CURRENT_PEP8 | grep '^>' | sed s/^\>\ //
    rm $TARGET_PEP8
    rm $CURRENT_PEP8
    rm $CHANGED_FILE_LIST
fi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s