Skip to content

Instantly share code, notes, and snippets.

@neingeist
Created June 26, 2015 20:16
Show Gist options
  • Save neingeist/7d448e574e9da30b6bcd to your computer and use it in GitHub Desktop.
Save neingeist/7d448e574e9da30b6bcd to your computer and use it in GitHub Desktop.

Revisions

  1. neingeist created this gist Jun 26, 2015.
    36 changes: 36 additions & 0 deletions git-fsck-all
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/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))