Created
September 5, 2014 17:38
-
-
Save Tatsh/553393e7f6e77b3e2264 to your computer and use it in GitHub Desktop.
Recursively find all unclean directories managed by Git.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Recursively find all unclean directories managed by Git. | |
from __future__ import print_function | |
from os import listdir | |
from os.path import isdir, join as path_join, realpath | |
import os | |
from sh import git | |
git = git.bake('-c', 'color.status=false') | |
def git_status(**kwargs): | |
dir = kwargs.pop('work_tree') | |
git_dir = kwargs.pop('git_dir', path_join(dir, '.git')) | |
return git('--git-dir=%s' % (git_dir,), | |
'--work-tree=%s' % (dir,), | |
'status') | |
def find_unclean_git_dirs(base): | |
candidate = path_join(base, '.git') | |
out = [] | |
if isdir(candidate): | |
status = git_status(work_tree=fn).strip() | |
if 'nothing to commit, working directory clean' not in status: | |
return [realpath(fn)] | |
return None | |
if isdir(base): | |
for item in sorted(listdir(base)): | |
if item in ('.', '..'): | |
continue | |
fn = path_join(base, item) | |
candidate = path_join(fn, '.git') | |
if isdir(candidate): | |
status = git_status(work_tree=fn).strip() | |
if 'nothing to commit, working directory clean' not in status: | |
out.append(realpath(fn)) | |
elif isdir(fn): | |
ret = find_unclean_git_dirs(fn) | |
if ret: | |
out += ret | |
return out | |
if __name__ == '__main__': | |
for dir in sorted(find_unclean_git_dirs('.')): | |
print('%s:' % (dir,)) | |
print(git_status(work_tree=dir).strip()) | |
print('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment