Skip to content

Instantly share code, notes, and snippets.

@jwliechty
Last active January 31, 2017 16:20
Show Gist options
  • Save jwliechty/18ecfbf97a803f4b1f35 to your computer and use it in GitHub Desktop.
Save jwliechty/18ecfbf97a803f4b1f35 to your computer and use it in GitHub Desktop.
This script helps in creating a tar.gz file containing only modified and added files from between two commits in a git repository. The idea is for deployed applications, to update only the changed files for a hotfix or small update, this tar can be created and extracted over that existing directory. Of course this only works if there were additi…
#/bin/bash -e
START_BRANCH="$1"
END_BRANCH="$2"
PATCH_TAR=patch.tar.gz
COMMIT_HASH_FILE=commit_hash
usage(){
echo ""
echo "USAGE: $0 START END"
echo ""
echo " START and END mark the beginning and end commits that define the patch. The"
echo " arguments can be branch names, tag names, or commit hashes. The script file"
echo " must be invoked from within the top level of the repository. The script "
echo " file itself can live anywhere. "
echo ""
echo " Creating a patch tar only works if no files were deleted or renamed. Only "
echo " modifications and additions make sense. The reason is this tar will be "
echo " extracted over the existing directory, replacing modified files and adding "
echo " any new files. Files cannot be deleted from the existing directory by "
echo " extracting a tar file over it. If any deletions or renames are detected, a "
echo " WARNING message will be displayed as well as some warnings from the tar "
echo " create command. "
echo ""
}
checkArguments(){
if [ $# -ne 2 ]; then
usage
exit 1
fi
}
warnOfDeletes(){
local warning="WARNING: Differences contain deletes or renames. Patching won't work by simply untaring patch"
diffOutput | awk '{print $1}' | egrep "[CDRTUXB]" > /dev/null && echo "${warning}" >&2
}
createCommitHash(){
git show -s --format=%H HEAD > "${COMMIT_HASH_FILE}"
}
createPatch(){
diffOutput | awk "{print \$2} END {print \"${COMMIT_HASH_FILE}\"}" | tar czf "${PATCH_TAR}" -T -
echo "Created '${PATCH_TAR}'"
}
diffOutput(){
git diff --name-status "${START_BRANCH}" "${END_BRANCH}" | awk '{if ($2 !~ /(spec\/|features\/)/) {print $0}}'
}
cleanup(){
rm "${COMMIT_HASH_FILE}"
}
checkArguments "$@"
warnOfDeletes
createCommitHash
createPatch
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment