Script (some of) your git commits

Published:

This is a series of how I automate making small changes in multiple git repos:

  1. Script (some of) your git commits
  2. Tend to your garden of git repos
  3. Function arguments in bash
  4. 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
set -e

create_pr() {
    branch=$1
    commit_message=$2

    git commit -m "$commit_message"
    git push -u origin "$branch"
    gh pr create --fill
}

branch=change-renovate-extends-value
git checkout -b "$branch"

jq '.extends = "github>my-org/my-shared-config-repo//renovate/base"' .github/renovate.json

git add .github/renovate.json

create_pr "$branch" "Change renovate extends value to new shared config repo"

And then I basically ran it in a loop in my terminal, more or less like this:

for service in repo-a repo-b repo-c; do
    (cd "$service" && ~/multi-git-scripts/change-renovate-extends-value.sh)
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.

  1. If I wasn't already on the default branch of the given repo, I would branch off of the wrong branch
  2. If the repo wasn't entirely up-to-date with its default remote, I would branch off an old default branch
  3. 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=$(basename "$PWD")
worktree="/tmp/$branch/$service"

git worktree add -b "$branch" "$worktree"
(
    cd "$worktree"
    git fetch
    git reset --hard origin/main

    jq '.extends = "github>my-org/my-shared-config-repo//renovate/base"' .github/renovate.json

    git add .github/renovate.json
    create_pr "$branch" "Change renovate extends value to new shared config repo"
)
git worktree remove -f "$worktree"

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 main as 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=$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)
git reset --hard "origin/$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 ! git diff --quiet; then
    git add .github/renovate.json
    create_pr "$branch" "Change renovate extends value to new shared config repo"
fi

Now I wouldn't get the ugly output if I accidentally ran the script in the same repo twice.