Changeset View
Changeset View
Standalone View
Standalone View
contrib/arcanist/rebase.sh
- This file was added.
Property | Old Value | New Value |
---|---|---|
File Mode | null | 100755 |
#!/usr/bin/env bash | |||||
# Copyright (c) 2019 The Bitcoin developers | |||||
# Distributed under the MIT software license, see the accompanying | |||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | |||||
export LC_ALL=C.UTF-8 | |||||
set -euxo pipefail | |||||
DEFAULT_PATCH_ARGS="--skip-dependencies" | |||||
DEFAULT_REMOTE="origin" | |||||
DEFAULT_BRANCH="master" | |||||
help_message() { | |||||
set +x | |||||
echo "Apply a patch from Phabricator and rebase on latest branch (typically master)." | |||||
echo "" | |||||
echo "Options:" | |||||
echo "-b, --branch The git branch to fetch and rebase onto. Default: '${DEFAULT_BRANCH}'" | |||||
echo "-h, --help Display this help message." | |||||
echo "-o, --remote The git remote to fetch latest from. Default: '${DEFAULT_REMOTE}'" | |||||
echo "-p, --patch-args Args to pass to 'arc patch'. Default: '${DEFAULT_PATCH_ARGS}'" | |||||
echo "-r, --revision The Differential revision ID used in Phabricator that you want to land. (ex: D1234)" | |||||
echo " This argument is required if --patch-args does not specify a revision or diff ID." | |||||
set -x | |||||
} | |||||
BRANCH="${DEFAULT_BRANCH}" | |||||
PATCH_ARGS="${DEFAULT_PATCH_ARGS}" | |||||
REMOTE="${DEFAULT_REMOTE}" | |||||
REVISION="" | |||||
# Parse command line arguments | |||||
while [[ $# -gt 0 ]]; do | |||||
case $1 in | |||||
-b|--branch) | |||||
BRANCH="$2" | |||||
shift # shift past argument | |||||
shift # shift past value | |||||
;; | |||||
-h|--help) | |||||
help_message | |||||
exit 0 | |||||
shift # shift past argument | |||||
;; | |||||
-o|--remote) | |||||
REMOTE="$2" | |||||
shift # shift past argument | |||||
shift # shift past value | |||||
;; | |||||
-p|--patch-args) | |||||
PATCH_ARGS="$2" | |||||
shift # shift past argument | |||||
shift # shift past value | |||||
;; | |||||
-r|--revision) | |||||
REVISION="$2" | |||||
shift # shift past argument | |||||
shift # shift past value | |||||
;; | |||||
*) | |||||
echo "Unknown argument: $1" | |||||
help_message | |||||
exit 1 | |||||
shift # shift past argument | |||||
;; | |||||
esac | |||||
done | |||||
PATCH_ARGS="${REVISION} ${PATCH_ARGS}" | |||||
# Stash unstaged changes, just incase this script is being run locally | |||||
git stash --include-untracked | |||||
# Fetch and checkout latest changes, ignoring local commits | |||||
git fetch "${REMOTE}" "${BRANCH}:origin/${BRANCH}" | |||||
git checkout "${BRANCH}" | |||||
git reset --hard "origin/${BRANCH}" | |||||
echo " | |||||
INFO: Local commits built on remote branch '${BRANCH}' not been branched | |||||
or tagged. See 'git reflog' to recover any lost commits." | |||||
( | |||||
# If arc fails, there may be a dangling branch. Clean it up before exiting. | |||||
cleanup() { | |||||
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) | |||||
git checkout master | |||||
git branch -D "${CURRENT_BRANCH}" || true | |||||
} | |||||
trap "cleanup" ERR | |||||
# Note: `: | arc ...` pipes an empty string to stdin incase arcanist prompts | |||||
# for user input. This fails and is treated as an error. | |||||
: | arc patch ${PATCH_ARGS} | |||||
) | |||||
git rebase "${BRANCH}" |