Imagine you work on a large number of projects. Each of those projects has its own git repository and accompanying notes file outside of the repo. Each git repository has its own githooks and custom setup.
Imagine having to work with multiple namespaces on different remote servers. Imagine setting up these projects by hand, multiple times a week.
Automation to the rescue ! Where I usually use Bash shell scripts to automate workflows, I'm moving more and more towards Python. It's cross-platform and sometimes easier to work with, as you have a large number of libraries at your disposal.
I wrote a simple Python script that does all of those things more or less 'automated'. Feed the script the name of the repository you want to clone, and optionally a namespace, patchfile and templatefile variable (either command-line or using a configuration file). The script will then:
- clone the repository
- modify the repository (e.g. apply githooks)
- optionally modify the repository based on given variables
- create a new notes file from a template
- optionally modify the notes file based on given variables
The advantage is that you can use a configuration file containing the location of the remote git repository, the patchfile and the templatefile. If you setup multiple repositories from the same location and use the same repetitive tasks (e.g. adding git hooks for XML validation), this can save you a lot of time (at least it saves me a lot of time...).
Example of cloning a basic repo to the location
/var/git/
:
setuprepo.py git-utilities --remote https://github.com --namespace
PeterMosmans --target /var/git
This will perform a basic clone of the repo
https://github.com/PeterMosmans/git-utilities to
/var/git/
.
If you usually set up some git-specific parameters like the author's email address, and git hooks for XML validation, then you could apply these automatically. First create a patchfile which applies the hooks (see for an example which adds the git hooks for XML validation), and specify that patchfile:
setuprepo.py git-utilities --remote https://github.com --namespace
PeterMosmans --patch hooks.patch
Besides cloning to the current directory (as no target is specified), this will
apply the patchfile
git.patch
. Before patching, it will automatically
replace the keywords
NAMESPACE
,
REMOTE
,
REPO
and
TARGET
with the corresponding variables.
If you create a notes file for each project from a templates file (outside of the repository directory) you could do that like this:
setuprepo.py git-utilities --remote https://github.com --namespace
PeterMosmans --template MY-NOTES --notes /location/of/notes
Again, this will replace the keywords
NAMESPACE
,
REMOTE
,
REPO
and
TARGET
in MY-NOTES with the corresponding variables.
And that's where the script really shines: You can specify all parameters in a configuration file, and specify that configuration file on the command line. So, you're setting up the remote, the namespace and the locations in the config file once, and you can use it a gazillion times:
setuprepo.py NAME-OF-REPO --config github.yml
The format of the configuration file (in this example
github.yml
) is
namespace: PeterMosmans
notes: /location/where/notes/should/be/stored
patchfile: /location/of/patch.file
remote: https://github.com
target: /where/repositories/are/cloned/to
template: /base/template.txt
You can find the script at https://github.com/PeterMosmans/git-utilities/blob/master/setuprepo.py.
Enjoy !
usage: setuprepo.py [-h] [-c CONFIG] [-r REMOTE] [-n NAMESPACE] [--no-clone] [--no-modify] [--no-patch] [--no-template] [--patchfile PATCHFILE] [--prepare] [-t TARGET] [--notes NOTES] [--template TEMPLATE] [-v] repo setuprepo.py version 0.6 - Clones and configures (patches) a git repository Copyright (C) 2016 Peter Mosmans [Go Forward] This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. positional arguments: repo repository name optional arguments: -h, --help show this help message and exit -c CONFIG, --config CONFIG load config file -r REMOTE, --remote REMOTE remote repository address -n NAMESPACE, --namespace NAMESPACE namespace of the repository --no-clone do not clone repository --no-modify do not modify patch and/or template --no-patch do not patch repository --no-template do not create template --patchfile PATCHFILE patchfile for new repositories --prepare clone the repo, create an original and modified folder, and exit -t TARGET, --target TARGET local target for the repository structure --notes NOTES the directory where to create a notes file --template TEMPLATE an empty notes template -v, --verbose Be more verbose
Comments
comments powered by Disqus