Skip to content

Instantly share code, notes, and snippets.

@smokinggoats
Created March 8, 2018 12:17
Show Gist options
  • Save smokinggoats/62008e5d440ae4117d49e93f477e0102 to your computer and use it in GitHub Desktop.
Save smokinggoats/62008e5d440ae4117d49e93f477e0102 to your computer and use it in GitHub Desktop.
iSort changed files using git diff
import os
import sys
from subprocess import call, check_output
from isort import SortImports
def isort_all():
path = sys.argv[1]
abs_path = os.path.abspath(path)
os.chdir(abs_path)
file_list = check_output(['git', 'diff', '--name-only'])
if type(file_list) is bytes:
file_list = file_list.decode().strip().split('\n')
for file_name in file_list:
if file_name != '':
print("Checking: {}".format(file_name))
SortImports(file_name)
print("="*80)
print("DONE")
else:
return None
if __name__ == '__main__':
if len(sys.argv) != 2:
print('Usage: {} [path]'.format(sys.argv[0]))
else:
isort_all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment