Last active
May 23, 2024 09:18
-
-
Save tiagojsag/807e2684db15cab8dda3d1c09a2b846f to your computer and use it in GitHub Desktop.
Git lazy cherry-pick
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
######################## | |
# git lazy-pick | |
# Author: https://github.com/tiagojsag | |
# | |
# This command accepts 4 arguments: | |
# -c <commit hash>: commit hash - required | |
# -j <jira ticket id>: jira issue id. Used to create branch names - required | |
# -p: if set, will cherry-pick to prod - optional | |
# -r: if set, cherry-pick to rc - optional | |
# | |
# At least one of -p or -r is required | |
# | |
# It executes the following actions: | |
# - If -b provided, creates and checks out a new branch named after the commit message | |
# - Adds all unstaged files to staging | |
# - Commits the changes | |
# - Pushes the changes to "origin" | |
######################### | |
OPTSTRING="prc:j:" | |
old_branch_name=$(git rev-parse --abbrev-ref HEAD) | |
to_prod=false | |
to_rc=false | |
commit_hash=false | |
jira_id=false | |
while getopts ${OPTSTRING} option | |
do | |
case "${option}" in | |
c) commit_hash="$OPTARG";; | |
j) jira_id="$OPTARG";; | |
p) to_prod=true;; | |
r) to_rc=true;; | |
?) | |
echo "Invalid option: -${OPTARG}." | |
exit 1 | |
;; | |
esac | |
done | |
if [ "$commit_hash" = false ] | |
then | |
echo "Please enter your commit hash: " | |
read commit_hash | |
fi | |
if [ "$jira_id" = false ] | |
then | |
echo "Please enter your commit Jira ticket id (ABC-1234): " | |
read jira_id | |
fi | |
if [ "$to_prod" = false ] && [ "$to_rc" = false ]; then | |
echo "You need to select either/both prod or rc as targets for the cherry-pick" | |
exit 1; | |
fi | |
STASH_OUTPUT=$(git stash --include-untracked) | |
if [ "$STASH_OUTPUT" = "No local changes to save" ] | |
then | |
POP_STASH=false | |
else | |
printf '\nStashing your changes so they can be recovered later...\n' | |
POP_STASH=true | |
fi | |
function cherry_pick() { | |
printf '\ngit lazy-pick - Cherry-picking to %s...\n' $1 | |
git checkout $2 || { printf 'git checkout $2 failed' ; exit 1; } | |
git pull --rebase origin $2 || { printf 'git pull --rebase failed' ; exit 1; } | |
git checkout -b ${jira_id}-$1 || { printf 'git checkout failed' ; exit 1; } | |
git cherry-pick -m1 ${commit_hash} || { printf 'git cherry-pick -m1 ${commit_hash} failed' ; exit 1; } | |
git push --set-upstream origin ${jira_id}-$1 | |
printf '\ngit lazy-pick to %s finished\n' $1 | |
} | |
if [ "$to_rc" = true ] | |
then | |
cherry_pick RC release-dev | |
fi | |
if [ "$to_prod" = true ] | |
then | |
cherry_pick PROD master | |
fi | |
printf '\nGoing back to your work branch...\n' | |
git checkout ${old_branch_name} | |
if [ "$POP_STASH" = true ] | |
then | |
printf '\nPoping stashed changes...\n' | |
git stash pop | |
printf '\nStashed changes poped successfully \n' | |
fi | |
printf '\ngit lazy-pick - Finished\n' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment