Skip to content

Instantly share code, notes, and snippets.

@motemen
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motemen/041c024221c5c2009223 to your computer and use it in GitHub Desktop.
Save motemen/041c024221c5c2009223 to your computer and use it in GitHub Desktop.
Git: checkout "this" (currently shown in Chrome) Pull Request's branch
#!/bin/sh
set -e
chrome-cli execute '(function () {
return [
location.hostname,
location.pathname.split("/")[1],
location.pathname.split("/")[2],
$.trim($(".gh-header-title").text()).replace(/\n\s*/g, " "),
$.trim($(".gh-header-meta .commit-ref:eq(1)").text())
].join("\n");
})()' | {
read host
read user
read repo
read title
read pr_ref
remote_url=$(git config remote.origin.url)
remote_url=${remote_url%.git}
if [ "$remote_url" != "git@$host:$user/$repo" \
-a "$remote_url" != "https://$host/$user/$repo" \
-a "$remote_url" != "git://$host/$user/$repo" ]; then
echo "Remote differs: $remote_url VS $host,$user,$repo" >&2
exit 1
fi
pr_branch=${pr_ref#*:}
pr_user=${pr_ref%:*}
if [ "$pr_user" = "$pr_ref" ]; then
pr_user=
fi
echo "$title ($pr_ref)"
if [ -n "$pr_user" ]; then
git remote add -f "$pr_user" "https://$host/$pr_user/$repo" || git remote update "$pr_user"
git checkout -b $pr_branch --track $pr_user/$pr_branch
else
git remote update origin
git checkout $pr_branch
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment