Skip to content

Instantly share code, notes, and snippets.

@bartoszmajsak
Last active February 16, 2024 09:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bartoszmajsak/c68abb2bb99367a1b54e748c996eae1a to your computer and use it in GitHub Desktop.
Save bartoszmajsak/c68abb2bb99367a1b54e748c996eae1a to your computer and use it in GitHub Desktop.
GH PR workflow

Motivation

Most of the times, when PR is not trivial in scope and size, Web UI can lead to loosing focus and context while scrolling down through the blob of changes.

It’s way more efficient to go through the code in the IDE. The GH plugins available for most of them can help with that, but they simply checkout the branch for you…​

I got tired of stashing changes, creating "wip" commits and shuffling branches.

And so this workflow was born.

How it works

  1. Leverages git worktree to have multiple branches within the same cloned repo.

  2. Relies on GH CLI to easily checkout PRs the same way as branches.

  3. Uses simple git hook to populate common project settings into new PR directory (optional)

Project structure (root folder)
├── dev-settings # (1)
└── main # (2)
  1. Common project settings, usually files globally .gitignored. I use this to add my .autoenv, tools-version and other utilities, so I have consistent set up across the PRs without manual steps.

  2. Cloned repository, main branch, where you would usually work.

For the main folder, assuming you have your settings in dev-settings folder, you have to populate them

find ../dev-settings -type f -exec ln -s '{}' .

This step is only needed once, for subsequent PR projects it is automated (TODO: add "hook" for clone)

Now, from the main folder we can add PR worktrees using special alias:

❯ git worktree-add-pr 402
remote: Enumerating objects: 2, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 2 (delta 1), reused 1 (delta 1), pack-reused 1
Unpacking objects: 100% (2/2), 258 bytes | 258.00 KiB/s, done.
From github.com:opendatahub-io/opendatahub-operator
 * [new ref]         refs/pull/402/head -> pr-402
Switched to branch 'pr-402'
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
Preparing worktree (checking out 'pr-402')
HEAD is now at 1a90cd0 Allow multiple component names for Dashboard
Setting up the worktree...

This updates directory structure:

├── dev-settings
├── main
└── pr-402

Now you can open pr-402 as a project in your IDE of choice and you can comfortably walk through the code, hack suggestions and run tests!

To remove, just call regular git command git worktree remove -f pr-402

Git configuration

git config --global alias.worktree-add-pr "!f() { gh pr checkout $1 -b pr-$1; git co -; git worktree-add pr-$1; }; f"
.git/hooks/post-checkout
#!/bin/bash

previous_head="$1"
new_head="$2"
checkout_type="$3"

if [ "$previous_head" = "0000000000000000000000000000000000000000" ]; then
    echo "Setting up the worktree..."
    find ../dev-settings -type f -exec ln -s '{}' . \;
fi

exit 0

Regular branches

If you want to use the same approach for regular (existing) branches, use this alias instead

git config --global alias.worktree-add "f() { git worktree add  ../"$1" "$1"; }; f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment