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 the previous post, I extracted a lot of the wrapping/boilerplate away from the function creating the actual change I wanted to make.
Let's take that one step further by extracting those to a separate file as well.
Separate files
Let's put this in a file called run_in_worktree.sh and place that next to the script with the change we actually want to run:
#!/usr/bin/env bash
We then create a function in another script, change_renovate_exteds.sh with the change we actually want to do:
branch=change-renovate-extends-value
commit_message="Change renovate extends value to new shared config repo"
This will not work until we figure out how to load the run_in_worktree.sh file so that we can actually call that function.
Sourcing external scripts
In bash, you can source some-other-script.sh to load that script (and execute whatever that script does).
We can use that to store our "library" with the run_in_worktree, but first we must put that function in a script file somewhere, and figure out how we load it.
The script to do the actual change to the config file will be executed via garden, and that means it will be run inside of the repo root. However, the script itself will not live inside the repo root (because then I would have to copy it to all the repos first, and that's something I don't want to do). Instead, I will store that in a separate git repo. Preferrably the same git repo where I store the script with the run_in_worktree function, for example right next to it.
Because these scripts will not live in the same directory as where they are executed, I cannot simply do source ./run_in_worktree.sh from the other script. . will refer to whatever "current working directory is".
So, then we need to figure where the script being executed lives.
That is ${BASH_SOURCE[0]}, and we can do a slightly magic incantation to figure out where in the file system it actually lives by using realpath. Then we dig out the directory name from that with dirname:
loadpath=$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
...and then we can source "$loadpath/run_in_worktree.sh".
Put this all together
Finally, we can add the load-path and source thing to change_renovate_extends.sh
#!/usr/bin/env bash
loadpath=
branch=change-renovate-extends-value
commit_message="Change renovate extends value to new shared config repo"
Conclusion
Now we have a kind of framework for creating bash scripts where we have abstracted away a lot of the ceremony around creating a worktree and performing changes in that, and we mostly have to write the stuff we actually want to change.
Is all this a bit overkill? Sure, but it was a fun learning experience, and I have had gotten a bit of mileage out of it already.
Potential improvements
There are some improvements that can be made to this that I have already identified.
One is that I could provide support for an additional function that can determine whether it needs to do the check at all. If one of these repos wouldn't have that particular config, then the jq command would fail. That would look messy in the terminal output, but it's not that big of a deal. It would be nice to have if I did something that took a bit of time, or if something could do the wrong thing.
Another thing is that there is still some potential issues around if a script fails for some reason, then this harness tends to leave unused git-branches around, so I have to figure that out and go around deleting them. That's kind of easy to do with garden though, since I can do garden exec some-repo git branch -D my-branch-name. But it would be nice to improve the error handling.
I might tackle those later.