Last active
May 4, 2024 08:31
-
-
Save dreness/0486046c4735d7dc542057c106509abd to your computer and use it in GitHub Desktop.
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}" |
For line 9, I think you mean
set -o pipefail
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:
# Use a fifo for a POSIX compatible array read.
In closing, I will try to settle down :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a great script. I took your ideas further and added some features:
https://github.com/huyz/trustytools/blob/master/unixy/gh-remote-add-fork.sh
'2024
). (Btw, I don't think that a fifo helps for this)origin
ifgh
can't detect what the default repo is.