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
Sometimes I need to do the same change in multiple git repos at work, so I have started writing shellscripts to do those things.
Partly because I don't like doing the same thing manually a bunch of times, but also because these changes tend to be trivial (and boring), and it's more fun to figure out how to manipulate some code, or more commonly, config file, from outside of emacs. (Honestly though, it's almost always a YAML file)
It can be something like changing the Renovate config for those repos, to either include, or point to another, shared config to extend from.
Some might say this is a perfect opportunity to use an LLM to do the fix(es) for me. I don't want to do that, though, because I don't like them for moral, environmental, deskilling, and copyright stealing perspectives. And besides, I already said it was a bit fun to figure out how to do this. I am very much the kind of person who likes to figure out how to do things rather than just get them done.
Anyway, I have tackled this process in a series of steps. First time I did this, I just wrote a script and ran it over a bunch of repos, then I picked up on some issues I had and tried to improve those over time.
1: Bash
I wrote a shellscript to generate the change, commit it to a branch, and create a pull request for me, of course. Something like this:
#!/usr/bin/env bash
branch=change-renovate-extends-value
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})
2: git worktrees
This worked, but I fairly quickly realized this approach had some issues.
- If I wasn't already on the default branch of the given repo, I would branch off of the wrong branch
- If the repo wasn't entirely up-to-date with its default remote, I would branch off an old default branch
- If the script didn't generate a diff, there would be some ugly errors in the output
The first two can be solved with worktrees:
service=
worktree="/tmp//"
(
)
This can then be run with the same loop as the first one, and would be a bit more robust.
The third issue is mostly cosmetic, and I decided I could ignore that for now...
3: what is a default branch
So, the very first time I tried to run that second iteration of the script with some change I wanted to do (it was a different change, but I'm keeping the renovate example here, because that's not the focus here), I ran into another problem:
- Not all of these repos use
mainas the name of their default branch.
Ok, sure, not everyone conforms to the same standards, different repos may behave differently. Just like people.
This one is kind of easy to solve though. I can just ask the remote for the name of the default branch.
default_branch=
This works, but it introduces a network call, so it's a bit slow. If someone knows a reliable and fast way to check this locally without making a network call, please tell me. There should be a link to my Mastodon handle in the footer of this page.
4: only do things when needed
At some point, I threw in an if statement around the git add/commit/push and gh pr create parts as well:
if ! ; then
fi
Now I wouldn't get the ugly output if I accidentally ran the script in the same repo twice.