-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_file_from_patch.sh
executable file
·88 lines (78 loc) · 1.85 KB
/
generate_file_from_patch.sh
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
81
82
83
84
85
86
#!/bin/bash
# As a Linux Kernel newbie, it usually drives me mad when
# reviewing other's patch. Since the patches are in diff format,
# it is quite hard to get the whole picture of what and where
# this patch has changed.
#
# This script generates two directories according to the patch
# content. The first one is 'old', the other one is 'new'. The
# 'old' directory contains the files that are involved in this
# patch, but has not been modified by the patch, AKA, the original
# files. The 'new' also contains the files that are mentioned
# in the patch, but with the patch applied.
# In this way, the user can compare the full content of corresponding
# files very conveniently, by launching:
# meld compare_dir/old compare_dir/new
#
#
if [ $# != 1 ]; then
echo "USAGE: cd source_code_dir"
echo " $0 patch_file"
echo " e.g.: $0 0001-x86-fix-s4.patch"
exit 1;
fi
OLD_DIR="compare_dir/old"
NEW_DIR="compare_dir/new"
patch_files=`cat "$1" | grep diff`
OLD_IFS="$IFS"
IFS=" "
array=($patch_files)
IFS="$OLD_IFS"
if [ ! -d "$OLD_DIR" ];then
mkdir -p $OLD_DIR
fi
if [ ! -d "$NEW_DIR" ];then
mkdir -p $NEW_DIR
fi
# copy the original files
for var in ${array[@]}
do
if [[ "$var" =~ ^a/.* ]]; then
file=${var:2}
echo "Copying original "$file"..."
dir=${file%/*}
if [ ! -d "$OLD_DIR/$dir" ];then
mkdir -p $OLD_DIR"/"$dir
fi
if [ -f "$file" ];then
cp $file $OLD_DIR"/"$dir"/"
fi
fi
done
echo "Patch it!"
# patch it!
git apply $1
if [ $? -ne 0 ]; then
echo "git apply fail"
exit 1;
fi
# copy the patched files
for var in ${array[@]}
do
if [[ "$var" =~ ^a/.* ]]; then
file=${var:2}
dir=${file%/*}
echo "Copying patched "$file"..."
if [ ! -d "$NEW_DIR/$dir" ];then
mkdir -p $NEW_DIR"/"$dir
fi
cp $file $NEW_DIR"/"$dir"/"
#rollback!
old_file=$OLD_DIR"/"$file
if [ -f "$old_file" ];then
cp $old_file $file
else
rm $file
fi
fi
done