austinmcconnell
11/21/2017 - 10:24 PM

Simple python script to reinitialize all git repositories nested one level under a "projects" folder. Primarily this is useful to "upgrade"

Simple python script to reinitialize all git repositories nested one level under a "projects" folder. Primarily this is useful to "upgrade" global hooks in a ~/.git-templates folder as git init will not overwrite any existing file.

from pathlib import Path
import subprocess

CURRENT_DIR = Path('.')

projects = sorted([x for x in CURRENT_DIR.iterdir() if x.is_dir()])

for project in projects:

    print('*' * 70)
    print(f'Checking project: {project}')
    print('*' * 70)

    git_dir = project / '.git'

    if not git_dir.exists():
        print('Not a git repo. Skipping...')
    	continue
    
    hooks_dir = git_dir / 'hooks'

    hooks = [x for x in hooks_dir.iterdir() if x.is_file()]
    for hook in hooks:
        print(f'Removing hook: {hook.name}')
        hook.unlink()

    subprocess.run(['git', 'init'], cwd=project)