Skip to content

Instantly share code, notes, and snippets.

@atnartur
Last active December 19, 2020 06:32
Show Gist options
  • Save atnartur/b27b5a129749a94eb396e8f2c8f07384 to your computer and use it in GitHub Desktop.
Save atnartur/b27b5a129749a94eb396e8f2c8f07384 to your computer and use it in GitHub Desktop.
Git group repo helper

Helper for git for executing commands in multiple repositories

Initialization

  1. Create a directory for repositories
  2. Create repos.txt with list of repositories links
  3. Locate the git_helper.py in the directory

Commands

  • python git_helper.py - makes pull of all repositories
  • python git_helper.py [command] - executes a command in all repositories
    • python git_helper.py checkout master - will execute git checkout master in all repositories
import sys
from pathlib import Path
from subprocess import run
def get_repo_dir(link):
link = link.strip('/').strip('.git')
return link.split('/')[-1]
def clean_repo_link(link):
link = link.strip('/')
if 'bitbucket' in link:
link = link.replace('https://bitbucket.org/', '[email protected]:')
if 'gitlab' in link:
link = link.replace('https://gitlab.com/', '[email protected]:')
return link
CURRENT_DIR = Path(__file__).parent
COMMAND = sys.argv[1:]
if len(COMMAND) == 0:
COMMAND = ['pull', '-aq']
print('command:', COMMAND)
with open('repos.txt') as f:
repos = f.read().strip().split('\n')
for repo in repos:
print(f'--- {repo} ---')
repo_dir = (CURRENT_DIR / get_repo_dir(repo)).resolve()
if not repo_dir.exists():
repo = clean_repo_link(repo)
run(['git', 'clone', repo, '-q'])
res = run(['git', *COMMAND], cwd=repo_dir, capture_output=True)
print(res.stdout.decode())
if res.returncode != 0:
print(res.stderr.decode())
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment