-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-patches
executable file
·80 lines (66 loc) · 2.1 KB
/
git-patches
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
################################################################################
#
# Description:
# ------------
# Creates file-specific patches of a commit
#
# Usage:
# ------
# $> git patches COMMIT
#
################################################################################
# Confirm that we are in a git repository
GIT_TOP=$(git rev-parse --show-toplevel)
RC=$?
if [ $RC != "0" ]; then
exit $RC
fi
# Confirm correct number of arguments
if [ $# -ne 1 ]; then
echo "Incorrect number of arguments."
echo "Usage:"
echo " $> git patches <commit-hash>"
exit 1
fi
COMMIT=$1
# Confirm the commit hash is valid
SHORT_HASH=$(git rev-parse --quiet --verify --short ${COMMIT})
if [ $? != "0" ]; then
echo "${COMMIT} is not a valid git object"
exit 1
fi
FILES_LIST=(`git diff-tree --no-commit-id --name-only -r ${COMMIT}`)
PATCH_FILES_DIR="/tmp/${SHORT_HASH}_patches"
if [ -e "${PATCH_FILES_DIR}" ]
then
echo "Patch files directory '${PATCH_FILES_DIR}' already exists. Aborting."
exit 2
else
mkdir ${PATCH_FILES_DIR}
fi
NUM_PATCH_FILES=0
for (( i=0; i<${#FILES_LIST[@]}; ++i ))
do
FULL_FILE_PATH=${FILES_LIST[$i]}
if [ ! -e ${FULL_FILE_PATH} ]
then
echo -e "\e[33m'\e[34m${FULL_FILE_PATH}\e[33m' is not accessible from current commit. Skipping.\e[0m"
continue
fi
FILE_WITH_UNDERSCORES=$(echo ${FULL_FILE_PATH} | sed 's/\//__/g')
PATCH_FILE_NAME="${FILE_WITH_UNDERSCORES}.patch"
# If the patch file name starts with a period, then replace the leading '.'
# with 'dot__'. Otherwise, the generated patch file will be hidden, and
# would therefore be susceptible to being overlooked.
if [[ "${PATCH_FILE_NAME}" == .* ]]
then
PATCH_FILE_NAME=${PATCH_FILE_NAME/./"dot__"}
fi
ORIG_PATCH_FILE=$(git format-patch -1 ${COMMIT} ${FULL_FILE_PATH})
mv "${ORIG_PATCH_FILE}" "${PATCH_FILES_DIR}/${PATCH_FILE_NAME}"
NUM_PATCH_FILES=$(expr ${NUM_PATCH_FILES} + 1)
done
echo "Saved ${NUM_PATCH_FILES} patch files"
echo "To apply them selectively, use:"
echo " git apply-from-dir ${PATCH_FILES_DIR}"