Function arguments in bash

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

Let's have a look at the script in my previous post:

#!/usr/bin/env bash
set -e

branch=change-renovate-extends-value
service=$(basename "$PWD")
worktree="/tmp/$branch/$service"
commit_message="Change renovate extends value to new shared config repo"

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

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

git worktree add -b "$branch" "$worktree"
(
    cd "$worktree"
    git fetch
    default_branch=$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)
    git reset --hard "origin/$default_branch"

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

    if ! git diff --staged --quiet; then
        create_pr "$branch" "$commit_message"
    fi
)
git worktree remove -f "$worktree"

As mentioned in that post, I tend to write these kind of scripts every now and then. And when I do, I basically want to have the same safeguards for all of them. A framework if you will.

So, this week, after having written two more almost identical scripts containing all of these safeguard boilerplate, I decided to try to abstract some of it away.

Kinda sorta function argument in bash

It turns out, that if I have some function in bash, I can store the name of that function in a variable, and then just invoke it like ($var_with_name_of_function with some args).

This means that I can send the name of a function as an argument to another function and invoke the first one from the second one. Kind of like a wrapper function.

#!/usr/bin/env bash
set -e

wrapper() {
    echo "Start"
    ($1) # <-- this tries to execute whatever is in the first argument,
         # as if it were a function.
    echo "End"
}

inner_fun() {
    echo "I am in the middle"
}

wrapper "inner_fun"

This will output the following:

Start
I am in the middle
End

Wrap this up

So, that means I can write a run_in_worktree wrapper:

branch=change-renovate-extends-value
commit_message="Change renovate extends value to new shared config repo"

reset_worktree() {
    git fetch
    default_branch=$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)
    git reset --hard "origin/$default_branch"
}

maybe_create_pr() {
    if ! git diff --staged --quiet; then
        git commit -m "$2"
        git push -u origin "$1"
        gh pr create --fill
    fi
}

run_in_worktree() {
    run_fun="$1"
    branch="$2"
    commit_message="$3"
    service=$(basename "$PWD")
    worktree="/tmp/$branch/$service"

    git worktree add -b "$branch" "$worktree"
    (
        cd "$worktree"
        reset_worktree
        
        $run_fun # <-- invoke the argument as a function

        maybe_create_pr
    )
    git worktree remove -f "$worktree"
}

change_renovate_extends() {
    jq '.extends = "github>my-org/my-shared-config-repo//renovate/base"' .github/renovate.json
    git add .github/renovate.json
}

run_in_worktree "$change_renovate_extends" "$branch" "$commit_message"