Python Logo

I have quite a few downloaded repositories that I keep on my local machine. Most are versioned with Git (obviously) and I was after a way to update them all via a script. After searching on the internet for a bit and not finding anything I was happy with, I decided to write my own in Python.

I have my downloaded Git repos in the following directory structure:

[simon@computer 17:14:32] ~/repo/got
$ tree -L 1
.
├── alpine.git
├── apache
├── breezy.git
├── bsd
├── debian
├── edit.git
├── epr.git
├── fdm.git
├── fizzy.git
├── gnu
├── got.git
├── got-portable.git
├── image-blob-reduce.git
├── lr.git
├── mblaze.git
├── micro.git
├── nanorc.git
├── nq.git
├── odftoolkit.git
├── OpenSMTPD.git
├── pdftk.git
├── perl
├── python
├── qbe.git
├── ruby
├── rwc.git
├── scc.git
├── src.git
├── superdoc.git
├── sysvinit.git
├── treq.git
├── trix.git
├── wcal.git
├── wfm.git
└── xe.git

The script will update each repository using Got for Git repo’s and Mercurial for Mercurial repo’s. Subdirectories (apache, gnu, python, ruby etc.) will also be updated.

In the future I might add error checking and a timeout for repositories that hang, but as it is currently, the script works fine for my needs.

update-repos.py

#!/usr/bin/python3
# v0.2

import os

got_dir = '/home/simon/repo/got'
hg_dir = '/home/simon/repo/hg'

def update_got():
    print('\nUpdating Git Repositories...')
    for i in os.listdir(got_dir):
        if i.endswith('.git'):
            os.system(f'got fe -r {os.path.join(got_dir, i)}')
        else:
            print(f'\n\n*** Updating subdirectory: {i} ***\n\n')
            for s in os.listdir(os.path.join(got_dir, i)):
                os.system(f'got fe -r {os.path.join(got_dir, i, s)}')

def update_hg():
    print('\n\nUpdating Mercurial Repositories...\n\n')
    for i in os.listdir(hg_dir):
        os.system(f'hg pull -R {os.path.join(hg_dir, i)}')

update_got()
update_hg()

And that’s it. Twenty lines of Python and ten minutes work! Feel free to adapt it to your needs. If you prefer Git, change the got fetch line to:

os.system(f'git pull -r {os.path.join(got_dir, i)}')