Skip to content

Instantly share code, notes, and snippets.

@devinrhode2
Last active December 16, 2021 02:19
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 devinrhode2/7d4843a5a8bfa461c761b89882f9f034 to your computer and use it in GitHub Desktop.
Save devinrhode2/7d4843a5a8bfa461c761b89882f9f034 to your computer and use it in GitHub Desktop.
this pre-push check just throws you into rebasing :)
#!/bin/sh
GIT_ROOT="$(git rev-parse --show-toplevel)"
. "$GIT_ROOT/.husky/common/colors.sh"
REMOTE_NAME="$1"
# REMOTE_URL="$2"
STDIN=$(cat -)
echo "REMOTE_NAME: $REMOTE_NAME"
echo "STDIN: $STDIN"
# TODO: move these checks into smartAutoRebase, and pass in REMOTE_NAME and STDIN as parameters?
# TODO: maybe, we can copy in the defs too? each sub-routine can just have it's own copy of STDIN?
# Skip if you are not pushing to origin:
[[ $REMOTE_NAME != "origin" ]] && exit 0
# Skip if you are deleting a branch:
[[ $STDIN =~ "\(delete\)" ]] && exit 0;
# Stop push if you are missing upstream changes from master.
# If there are merge conflicts, usually they are easier to resolve via rebasing, since the merge conflicts are more isolated
# Therefore, we'll automatically run `git pull origin master --rebase` after a brief pause
assumed_base=$(
# If most PRs target a `develop` branch:
# git merge-base --is-ancestor develop HEAD &&
# echo "develop" || (
git merge-base --is-ancestor master HEAD &&
echo "master" ||
exit 0
# )
)
current_branch=$(git rev-parse --abbrev-ref HEAD)
base="${BASE_BRANCH:-$assumed_base}"
# Could not determine base branch, skip this check entirely.
[[ "$base" == "" ]] && exit 0;
# Mostly irrelevant since we only have a `master` branch right now.
# echo "${yellow}\$BASE_BRANCH${SC} $(
# [[ "$BASE_BRANCH" == "" ]] && echo 'defaulted' || echo 'set'
# ) to ${green}$base${SC}"
# ===
# The rest of this function is just an elaborate form of `git pull origin master --rebase`
# ===
echo "checking if you have latest code from origin/$base..."
git fetch origin $base
# Does current branch contain latest remote commit?
git merge-base --is-ancestor origin/$base HEAD && {
# Done - we're all good. Already up to date! Continue with push!
echo "${green}up-to-date with origin/$base${SC}" && exit 0
} || echo "${yellow}current branch is${SC} ${red}MISSING CHANGES${SC} ${yellow}from origin/$base${SC}"
default_delay=2
delay="${AUTO_UPDATE_ON_PUSH_DELAY_SECONDS:-$default_delay}"
[[ "$AUTO_UPDATE_ON_PUSH_DELAY_SECONDS" ]] ||
echo "\$AUTO_UPDATE_ON_PUSH_DELAY_SECONDS defaulted to $default_delay seconds"
echo ""
echo "running: git pull origin $base --rebase.."
sleep $delay
git pull origin $base --rebase || {
echo "\n${yellow}encountered merge conflicts${SC}"
echo ""
echo "Rebasing 101:"
echo "Rebase conflicts are more isolated and easier to resolve than typical merge conflicts"
echo "1. Resolve conflicts"
echo "2. git add ."
echo "3. git commit"
echo "4. git rebase --continue"
echo "5. Repeat 1-4, then `git push --force`"
echo ""
echo "git status is your ${green}friend${SC}:"
echo "git status:"
git status
exit 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment