Determining if your branch is part of a GitHub Stacked PR

Featured image for sharing metadata for article

Since the availability of GitHub's Stacked PRs finally landing for repositories that use a Merge Queue, I've been "kicking the tyres" with some of my contributions to Renovate.

They're working mostly well so far, but I'm finding it a bit annoying trying to remember if I'm on a branch that's part of a stack.

I'd started wiring this into my zsh config, still using Grml's zsh config as a base, and found it was a bit awkward.

I decided to outsource it to Claude Sonnet 5 - cause I guess that's the sort of person I am now? πŸ˜… - and it came out with this:

# in case you're working in a Git worktree
git_common_dir="$(git rev-parse --git-common-dir 2>/dev/null)" || { REPLY=""; return }
stack_file="$git_common_dir/gh-stack"

position="$(jq -r --arg b "$(git branch --show-current)" '
  .stacks[]
  | select(.branches[]?.branch == $b)
  | (.branches | map(.branch)) as $names
  | ($names | index($b)) as $idx
  | "\($idx + 1)/\($names | length)"
' "$stack_file" 2>/dev/null | head -n1)"

echo "$position"
# outputs i.e. `x/y` or `` if not in a stack

I end up wiring this into my zsh prompt to show a πŸ₯ž 1/3 indicator like so:

A screenshot of a zsh prompt, showing Jamie's regular prompt that provides useful information, and after it shows a Git branch name, it also has a πŸ₯ž 1/3 to indicate that we're on a stacked PR branch, and the branch is the first of three branches in this stack

This works quite nicely, but I've found that it doesn't always stay in sync with GitHub. If a branch has been merged, but you've not yet run gh stack sync, you'll show as still on a stack.

However, you can't run gh stack sync without being on a stacked branch, so your cached data may be out-of-sync until that's fixed.

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #git #github #shell-scripting.

πŸ€– Content in this blog post (prose or code snippets) includes code derived from the following LLMs:

  • claude:sonnet-5

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.