5 ms·
Two things I use fuzzy finder for: 1. flog: go to git branches I had checked out in the past quickly 2. pr: preview and check out PRs I've been assigned to re
by 1-more 4y ago
Two things I use fuzzy finder for:
1. flog: go to git branches I had checked out in the past quickly
2. pr: preview and check out PRs I've been assigned to review
flog () {
branch="$(
git branch --sort=-committerdate --format="%(committerdate:relative)%09%(refname:short)%09%(subject)" \
| column -ts $'\t' \
| fzf \
| sed 's/.*ago \+\([^ ]*\) .*/\1/'
)"
git co $branch || (
echo -n "git co $branch" | pbcopy
)
}
errcho() {
echo "$@" 1>&2
}
jq_query=$(cat <<- EOF
map(
{ key: "\(.number)"
, value:
{ line: "\(.number):\(if .isDraft then "" else " " end):\(.author.login):\(.title):\(.headRefName)"
, body: "# \(.number): \(.title)
## \(.headRefName)
\(.body)"
}
}
)
| from_entries
EOF
)
pr () {
local gh_user=${1:-}
if [ -z "$gh_user" ]; then
gh_user="@me"
fi
tmpFile=$(mktemp /tmp/prs-XXXXXX.json)
cleanup () {
rm "$tmpFile"
}
trap cleanup EXIT
gh pr list -S "review-requested:${gh_user}" \
--json number,title,headRefName,body,author,isDraft \
| jq -r "$jq_query" > "$tmpFile"
preview_command="\
jq -r '.[\"{1}\"].body' $tmpFile \
| pandoc -f gfm -t markdown \
| glow -s light -
"
pr_number=$(
jq -r \
'to_entries | map(.value.line) | join("
")' \
"$tmpFile" \
| column -t -s: \
| fzf \
--ansi \
--delimiter=' ' \
--preview="$preview_command" \
--preview-window=up:80% \
| grep -o '^[0-9]\+'
)
if [ -n "$pr_number" ]; then
errcho "checking out #$pr_number"
gh pr checkout "$pr_number" && gh pr view --web "$pr_number" && git pull || echo -e "gh pr checkout $pr_number && gh pr view --web $pr_number && git pull"
else
errcho "canceled"
fi
}
- unit_circle 4y agoNice. I'm going to dig through these tomorrow. Thanks for posting! The PR review helper looks excellent and I'm excited to try it out. Til about `pandoc` and `glow`. I whipped up a nice, performant branch picker last night that I'm pretty happy with, hopefully there are some useful tidbits for others. It's similar to your `flog` command but it uses the reflog to find the most recently checked out branches. It filters those which have been deleted using a set structure (well, map of bools), thus requiring BASH 4+. I'll be interested to see the differences in behavior and performance of your approach https://gist.github.com/pnovotnak/4dfe9b2867bf6fea60fa94b4c8a2cc9d https://gist.github.com/pnovotnak/4dfe9b2867bf6fea60fa94b4c8...
- 1-more 4y agohaha I called it "flog" because I always read "ref log" as "re flog" and it's kind of a replacement for that. And no problem!! So happy if it helps.