Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 167 additions & 0 deletions tools/generate_patches.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
#!/usr/bin/env bash

echo_error()
{
echo "ERROR: $1"
}

echo_usage()
{
echo "Usage: ./generate_patches.sh -p <port-dir absolute path> -s <src-dir absolute path> [optional]-> --dry-run (Lists the modified files without generating patches)"

echo ""
echo "** Follow these steps to generate patch files using this script **"
echo "1) Apply the patches and generate source code repo using zopen build command"
echo "2) Commit the files modified by applying patches"
echo "3) Perform development or bug fix changes on top of the commit created in step 2. Once the changes are ready, create a new commit(s)."
echo "4) Use this script to generate patch files (in port/patches/ directory) for the files modified in step 3."
echo ""
echo "NOTE: The source repo should not be in a detached state"
}

get_commit_ids()
{
BASE_COMMIT= # source code gets cloned with this latest commit
PATCHED_COMMIT= # Run zopen build to apply all the patches from port
CURRENT_COMMIT= # Commit with further dev changes

#Check here if the branch is in detached state
if [ $(git branch -a | grep '* ' | grep -c "detached") != 0 ]
then
echo_error "This is in detached state, please create a branch and try again!"
echo_usage
exit 1;
fi

#Find the current branch name
curr_branch=$(git branch -a | grep '* ' | cut -d ' ' -f 2)

#Find the commit from which curr_branch diverged
BASE_COMMIT=$(git reflog show ${curr_branch} | grep "branch: Created" | cut -d ' ' -f 1)

#Find the first commit after curr_branch branch diverged, this is the commit
#with all previous patches applied
PATCHED_COMMIT=$(git rev-list --reverse ${curr_branch} | grep -A1 ${BASE_COMMIT} | tail -n 1)

#Find the latest commit
CURRENT_COMMIT=$(git rev-list ${curr_branch} | head -n 1)

if [[ -z ${BASE_COMMIT} || -z ${PATCHED_COMMIT} || -z ${CURRENT_COMMIT} ]]
then
echo_error "Something is wrong, BASE_COMMIT, PATCHED_COMMIT and CURRENT_COMMIT needed but are not set"
echo_usage
exit
fi
}

gen_patches()
{
if [ ${PATCHED_COMMIT} == ${CURRENT_COMMIT} ]
then
#Generating patch files for the first time
MODIFIED_FILES=$(git diff-tree --no-commit-id --name-only ${BASE_COMMIT} ${CURRENT_COMMIT} -r)
else
MODIFIED_FILES=$(git diff-tree --no-commit-id --name-only ${PATCHED_COMMIT} ${CURRENT_COMMIT} -r)
fi

echo -e "\nModified files:\n"
echo -e "${MODIFIED_FILES}\n"

if [ ${DRY_RUN} == "true" ] ;
then
return
fi

for file in ${MODIFIED_FILES}
do
echo "Generating diff for ${file}"

#Save the patch files to port/patches/
PATCH_FILE=${PORT_DIR}/patches/${file}.patch
PATCH_FILE_DIR=$(dirname ${PATCH_FILE})

if [ ! -d ${PATCH_FILE_DIR} ]
then
echo "${PATCH_FILE_DIR} does not exists, creating one"
mkdir -p ${PATCH_FILE_DIR}
fi

if [ -f ${PATCH_FILE} ]
then
echo "WARNING: ${PATCH_FILE} already exists, contents will be overwritten"
fi

echo ""
#Generate patch file
git diff ${BASE_COMMIT} -- ${file} > ${PATCH_FILE}

#Add the file to the staging area
# cd ${PORT_DIR} ; git add ${PATCH_FILE} ; cd -
done
}

## main() STARTS HERE ##
DRY_RUN=false

if [[ $# != "4" && $# != 5 ]]
then
echo_usage
exit 1;
fi

while [ ! -z $1 ]; do
case "$1" in
-s)
SRC_DIR=${2}
shift 2
;;
-p)
PORT_DIR=${2}
shift 2
;;
--dry-run)
DRY_RUN=true
echo "--dry-run is set to true: modified files will be listed but patches won't be generated"
shift
break
;;
-h | --help)
echo_usage
exit 1
;;
:)
echo_error "Invalid option"
echo_usage
exit 1
;;
esac
done

if [ ! -d ${SRC_DIR} ]
then
echo_error "${SRC_DIR} does not exist"
echo_usage
exit
fi

if [ ! -d ${PORT_DIR} ]
then
echo_error "${PORT_DIR} does not exist"
echo_usage
exit
fi

pushd ${SRC_DIR}

get_commit_ids

echo "SRC_DIR ${SRC_DIR}"
echo "PORT_DIR ${PORT_DIR}"
echo "BASE_COMMIT ${BASE_COMMIT}"
echo "PATCHED_COMMIT ${PATCHED_COMMIT}"
echo "CURRENT_COMMIT ${CURRENT_COMMIT}"

# Generate patches later
gen_patches

popd