Created
June 26, 2015 20:16
-
-
Save neingeist/7d448e574e9da30b6bcd to your computer and use it in GitHub Desktop.
find all git repositories (and git working directories) starting from the current directory and perform a 'git fsck' on them.
This file contains hidden or 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 | |
""" | |
find all git repositories (and git working directories) starting from the | |
current directory and perform a 'git fsck' on them. | |
""" | |
from __future__ import division, print_function | |
from colorama import Fore | |
import contextlib | |
import os | |
import subprocess | |
def git_directories(startdir): | |
for dirpath, dirnames, _ in os.walk(startdir): | |
if set(['info', 'objects', 'refs']).issubset(set(dirnames)): | |
yield dirpath | |
@contextlib.contextmanager | |
def working_directory(directory): | |
saved_cwd = os.getcwd() | |
os.chdir(directory) | |
yield | |
os.chdir(saved_cwd) | |
for git_directory in git_directories('.'): | |
with working_directory(git_directory): | |
print('\n{}:'.format(os.getcwd())) | |
ret = subprocess.call(['git', 'fsck']) | |
if ret != 0: | |
print((Fore.RED + 'git fsck is unhappy with {}' + Fore.RESET) | |
.format(git_directory)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you