Files
zulip/tools/reset-to-pull-request
Greg Price da8763d889 tools: Avoid readlink -f in *-pull-request scripts.
This is a feature of GNU readlink that isn't in the BSD readlink
found on macOS.

For using this and other GNU coreutils features in our scripts in
general, we could use a solution like mobile's tools/lib/ensure-coreutils.sh
to get GNU coreutils on the PATH -- check if it's there already,
if not then try to find a Homebrew install of it and use that, if not
then print a helpful message.

But even then there'd be a bootstrapping problem of how to find
ensure-coreutils.sh .  That involves exactly the same problem as we
have for finding git-tools.sh in these lines.  So in fact in mobile
for the task of finding ensure-coreutils.sh in the first place, we
do without `readlink -f` anyway.

The one consequence of this behavior-wise is that if you make a
symlink somewhere that points directly at that script (say in your
`~/bin/`), and try to run it using that symlink, it won't work.
(It'll still work just fine if there are symlinks somewhere higher
up in the paths involved -- just not for the script itself.)
An ideal CLI program really should support that, I think, but
lacking a better idea, this seems an acceptable compromise.
2021-04-30 14:57:13 -07:00

58 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
usage() {
cat >&2 <<EOF
usage: $0 PULL_REQUEST_ID [REMOTE]
Fetch the given GitHub pull request branch, and reset our
current branch to it.
Useful for anyone reading or reviewing a PR, in order to
run the code and to study it with a full local set of tools.
REMOTE defaults to the value of the Git config variable
\`zulip.zulipRemote\` if set, else to \`upstream\`.
If the Git config variable \`zulip.prPseudoRemote\` is set,
e.g. with:
git config zulip.prPseudoRemote pr
then the PR branch is also recorded as a local ref, like a
remote-tracking branch as if the PRs make up a "remote". In
the example, PR #1234 is recorded as \`pr/1234\`, or in full
\`refs/remotes/pr/1234\`. This is useful for keeping track of
the PR branch while comparing with other code, and for using
the reflog to compare with previous versions of the same PR.
See also \`push-to-pull-request\`.
EOF
exit 1
}
remote_default="$(git config zulip.zulipRemote || echo upstream)"
pseudo_remote="$(git config zulip.prPseudoRemote || echo)"
request_id="$1"
remote=${2:-"$remote_default"}
if [ -z "$request_id" ]; then
usage
fi
this_dir=${BASH_SOURCE[0]%/*}
# shellcheck source=lib/git-tools.bash
. "${this_dir}"/lib/git-tools.bash
require_clean_work_tree 'reset to PR'
if [ -z "$pseudo_remote" ]; then
set -x
git fetch "$remote" "pull/$request_id/head"
git reset --hard FETCH_HEAD
else
target_ref=refs/remotes/"$pseudo_remote"/"$request_id"
set -x
git fetch "$remote" +"pull/$request_id/head:$target_ref"
git reset --hard "$target_ref"
fi