Skip to content

Instantly share code, notes, and snippets.

@YuriyGuts
Last active June 12, 2018 13:25
Show Gist options
  • Save YuriyGuts/c3808d1e64fa95931726 to your computer and use it in GitHub Desktop.
Save YuriyGuts/c3808d1e64fa95931726 to your computer and use it in GitHub Desktop.
Update all Git repositories in the current folder
#!/usr/bin/env python
"""
Update all Git repos in the current directory. Fast-forward only, no merge commits.
Copyright (c) 2018 Yuriy Guts
usage: git-update-all.py
"""
from __future__ import division, print_function
import os
import subprocess
import sys
import traceback
def is_git_repo(path):
return os.path.isdir(path) and os.path.isdir(os.path.join(path, '.git'))
def run_command(command):
print('> Running:', command)
subprocess.call(command, shell=True)
def update_repo(repo):
print('-' * 30)
print('Updating {}...'.format(os.path.basename(repo)))
os.chdir(repo)
run_command('git remote update -p && git merge --ff-only @{u}')
os.chdir('..')
print()
def main():
workdir = os.getcwd()
repos = [
os.path.join(workdir, dir_entry)
for dir_entry in os.listdir(workdir)
if is_git_repo(dir_entry)
]
for repo in repos:
try:
update_repo(repo)
except Exception:
print('ERROR while updating repo')
traceback.print_exc()
sys.exit(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment