#!/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 '.sync' in dirpath:
            continue
        if '.git/modules' in dirpath:
            # FIXME
            continue
        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):
        try:
            subprocess.check_output(['git', 'fsck', '--no-dangling'], stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            print((Fore.RED + 'git fsck is unhappy with {}' + Fore.RESET)
                  .format(git_directory))
            print(e.output)