Created
February 20, 2018 19:19
-
-
Save alexandervalencia/633ed8cbbfaf9f187e6da9f5d62c038c to your computer and use it in GitHub Desktop.
Git patch functions
This file contains 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
# For the following patch functions, we assume your liferay-portal, | |
# liferay-plugins, and bundles folders live in a parent folder named "liferay" | |
# Git Format Patch | |
# Usage: fp <# of commits> <Commit Sha> | |
# Example: `fp 3 34s3446e` | |
function fp { | |
local dirPath=`find ~ -maxdepth 3 -type d -name 'liferay' -print -quit` | |
local fileCount=$(ls $dirPath/patches | wc -l) | |
if [ "$fileCount" -gt "0" ]; then | |
echo -e "\033[02;31mPatch folder already contains the following files:\033[01;00m" | |
ls "$dirPath"/patches | while read -r file; do echo -e "\033[03;33mRemoval required: \033[01;00m$file"; done | |
echo -e "Clear files before continuing with 'dp'." | |
return "0" | |
fi | |
git format-patch -"$1" "$2" -o "$dirPath"/patches -q || { dp; echo -e "\033[02;33mPatch files were not created. Check arguments and try again... Clearing any old patch files.\033[01;00m"; return; } | |
echo -e "Patch files created in $dirPath/patches/" | |
ls "$dirPath"/patches | while read -r file; do echo -e "\033[03;32mCreated: \033[01;00m$file"; done | |
echo "Navigate to topic branch and run command 'ap' to apply patch." | |
} | |
# Git Apply Patch | |
# Usage: ap | |
function ap { | |
local dirPath=`find ~ -maxdepth 3 -type d -name 'liferay' -print -quit` | |
ls "$dirPath"/patches | while read -r file; do echo -e "\033[03;33mPending: \033[01;00m$file"; done | |
while true; do | |
echo "Would you like to apply these patch files?" | |
read yn | |
case $yn in | |
[Yy]* ) git am -3 /"$dirPath"/patches/*; echo -e "\033[00;33mIf there are merge conflicts, resolve in your text editor and use \033[00;32m'git am --continue', \033[00;31m'git am --abort', \033[00;00mor \033[00;33m'git am --skip'."; break;; | |
[Nn]* ) echo "Selected No, Exiting."; break;; | |
* ) echo "Please answer yes or no."; break;; | |
esac | |
done | |
} | |
# Git Delete Patches | |
# Clean out your old patch files before working with new ones. | |
# Usage: dp | |
function dp { | |
echo "Cleaning patch files." | |
local dirPath=`find ~ -maxdepth 3 -type d -name 'liferay' -print -quit` | |
ls "$dirPath"/patches | while read -r file; do echo -e "\033[03;31mRemoving \033[00;32m$file\033[00;00m" && rm -f "$dirPath"/patches/"$file"; done | |
echo "Patch files removed." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment