Skip to content

Instantly share code, notes, and snippets.

@zaneb
Created June 20, 2019 17:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zaneb/7a8c752bfd97dd8972756d296fc5e41f to your computer and use it in GitHub Desktop.
Save zaneb/7a8c752bfd97dd8972756d296fc5e41f to your computer and use it in GitHub Desktop.
Run flake8 against only files that have changed
#!/bin/bash
set -e
enable_env() {
local env_name="$1"
local env_dir=".tox/${env_name}"
if [ ! -d "${env_dir}" ]; then
if [ ! -f tox.ini ]; then
echo 'No tox.ini found' >&2
exit 1
elif tox -l | grep -q "^${env_name}$$"; then
tox -e"${env_name}" --notest
else
echo "No ${env_name} tox environment found" >&2
exit 1
fi
fi
source "${env_dir}"/bin/activate
}
diff_files_branch() {
local branch=${1:-"@{u}"}
git diff --name-only --diff-filter=d "${branch}" -- '*.py'
}
diff_files() {
diff_files_branch 2>/dev/null || diff_files_branch origin/master
}
enable_env pep8
flake8 $(diff_files)
@tbreeds
Copy link

tbreeds commented Jun 20, 2019

So you've hardcoded origin/master on line 28 can you use @{u} or maybe $(git rev-parse --abbrev-ref --symbolic-full-name @{u}) so it works on stable branches?

@zaneb
Copy link
Author

zaneb commented Jun 21, 2019

It does use @{u} (see line 23) and only falls back to origin/master if that fails. So it does the Right Thing on stable branches and falls back to diffing against master if you're working on e.g. a detached HEAD.

@tbreeds
Copy link

tbreeds commented Jun 21, 2019

Ahh okay somehow I missed the '||' my mistake

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment