Last active
May 4, 2024 08:31
Use gh cli and fzf to add a remote for a fork of the current repo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Scenario: | |
# - you have a local checkout of a github repo | |
# - you're looking at public forks of that repo | |
# - you want to add a remote to your local checkout for one of the forks | |
set -e | |
set -o pipefail | |
# Use a fifo for a POSIX compatible array read. | |
FP=$(mktemp -d)/fifo | |
mkfifo "${FP}" | |
# Fetch all forks of the repo in the current directory. | |
# Sort by last pushed date. | |
# For each fork, print .pushed_at, owner.login, and .clone_url | |
# Use fzf to select a fork, and: | |
# - don't include column 3 in the preview | |
# - sort in reverse order (most recently pushed at the bottom) | |
# Redirect selection to the fifo; background so we don't block. | |
gh api --paginate "repos/{owner}/{repo}/forks" \ | |
| jq -s '.[] | sort_by(.pushed_at)' \ | |
| jq -r '.[] | "\(.pushed_at) \(.owner.login) \(.clone_url)"' \ | |
| fzf --with-nth=1,2 --tac > "${FP}" & | |
# Read the fifo and let word splitting do its thing. We only need the last two words. | |
read -r _ FORK_OWNER FORK_CLONE_URL < "${FP}" | |
git remote add "${FORK_OWNER}" "${FORK_CLONE_URL}" | |
echo "Added remote ${FORK_OWNER}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
oh no. good catch, fixed!
Your script is definitely better, thanks for sharing :) Let me go on record to say that I re-read my script, and laughed at this part:
In closing, I will try to settle down :)