53 lines
1.5 KiB
Bash
Executable File
53 lines
1.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
set -Eeuo pipefail
|
|
|
|
readonly upstream_url="${1:-https://github.com/jeremiah-c-leary/vhdl-style-guide.git}"
|
|
readonly upstream_branch="${2:-master}"
|
|
readonly upstream_ref="refs/remotes/upstream/${upstream_branch}"
|
|
|
|
repo_root="$(git rev-parse --show-toplevel 2>/dev/null)" || {
|
|
echo "ERROR: update-fork.sh must run inside a Git worktree." >&2
|
|
exit 1
|
|
}
|
|
cd "${repo_root}"
|
|
|
|
if [ -n "$(git status --porcelain)" ]; then
|
|
echo "ERROR: refusing to rebase a dirty worktree." >&2
|
|
git status --short >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [ -z "$(git symbolic-ref --quiet --short HEAD)" ]; then
|
|
echo "ERROR: refusing to rebase a detached HEAD." >&2
|
|
exit 1
|
|
fi
|
|
|
|
abort_rebase() {
|
|
exit_status=$?
|
|
trap - EXIT HUP INT TERM
|
|
|
|
if git rev-parse --verify --quiet REBASE_HEAD >/dev/null; then
|
|
echo "ERROR: upstream rebase failed; aborting it now." >&2
|
|
if ! git rebase --abort; then
|
|
echo "FATAL: git rebase --abort also failed; manual recovery is required." >&2
|
|
exit 2
|
|
fi
|
|
else
|
|
echo "ERROR: fork update failed before a rebase was started." >&2
|
|
fi
|
|
|
|
exit "${exit_status}"
|
|
}
|
|
trap abort_rebase EXIT HUP INT TERM
|
|
|
|
echo "Fetching ${upstream_branch} and release tags from ${upstream_url}..."
|
|
git fetch --tags "${upstream_url}" "${upstream_branch}:${upstream_ref}"
|
|
|
|
upstream_commit="$(git rev-parse "${upstream_ref}")"
|
|
echo "Rebasing fork commits onto ${upstream_commit}..."
|
|
git rebase "${upstream_commit}"
|
|
|
|
trap - EXIT HUP INT TERM
|
|
echo "Fork update completed successfully."
|