This is a series of how I automate making small changes in multiple git repos:
- Script (some of) your git commits
- Tend to your garden of git repos
- Function arguments in bash
- Source an adjacent bash script
In my last post, I wrote about how to use shellscript to generate changes in a git repo... or in many repos.
I have a situation at work, where I sometimes want (or need) to do the same very specific change in several git repos.
Let's revisit the script to change the Renovate config for a bunch of repos.
I could do these changes manually in one repo at a time, of course. And I would probably do that if it was only a few repos, maybe up to five or so. The reality, however, is that when I'm in this kind of situation, I generally tend to update around 30 or so repos with the same thing.
But why?
What about DRY, you may ask? Could I not abstract this away somewhere? Well, sometimes yes, and sometimes no. From the things we have set up, I have all of these git repos that I sometimes need to do the same upgrade in.
Oh, but could we not just put all of this code in the same repo, and have a monorepo instead? Of course we could, and we have some repos that are monorepos in a way, but in my team we have opted to have each service in its own repo. We think it helps a bit with isolating the bits that are relevant to each service. And besides, while a monorepo would probably alleviate some of this, it would also introduce different complexity. It's a tradeoff, like everything, really. But that can be a topic for another time.
Back to the current situation: I need to perform the same change in 30 different git repos. What do I do?
Where we left off
We had some script like this, to update the .extends key in a Renovate config to point to a shared config in one specific repo.
#!/usr/bin/env bash
branch=change-renovate-extends-value
service=
worktree="/tmp//"
commit_message="Change renovate extends value to new shared config repo"
(
default_branch=
if ! ; then
fi
)
And then I basically ran it in a loop in my terminal, more or less like this:
for; do
( && )
done
(Just imagine that I had a list of 30 repos instead of just repo-{a,b,c})
Abstracting away the loop
At some point, I realized that it's kind of tedious to try to find the right for loop in my shell history when I want to run some new script over all of these repos, and I would also have to modify it each time I wanted to run it, because:
- The name of the script I wanted to invoke was different, because I had written a new script for the new kind of change
- Some repo might have been added to or removed from the list of relevant repos
So, I went looking for some tool to do this for me.
I tried a couple of times with some various tools, and then after a while, I have now ended up with garden. It calls each git repository a "tree", and a "garden" is a collection of trees, which may be grouped in some way.
Since I have all of these (and a whole bunch of other work-related) repos in the same directory, I can initialize a new garden by going to the repos directory and run garden init. This will create a garden.yaml file with some config about my garden.
Then I can plant all of these repos by running garden plant *. This will add all of the repos to my garden.yaml.
I can now say that I want to run some arbitrary command in all of my repos by doing something like garden exec git status, and that will give me the status of all of the trees in my garden.
Since only a subset of these repos are the ones belonging to my team, I can create a separate group for them in garden.yaml:
groups:
my-team:
- repo-a
- repo-b
- repo-c
This will allow me to run garden commands in only these repos, by adding a "tree query" to the invocation, like so:
garden exec '%my-team' git status
...and with that in place, I can run my custom scripts without manually typing a for loop in the shell, like this:
garden exec '%my-team' ~/garden-scripts/change-renovate-extends-value.sh