forked from handy1989/vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diffdir
executable file
·147 lines (137 loc) · 3.75 KB
/
diffdir
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
if [ $# -ne 2 ];then
echo "Usage:$0 dir1 dir2"
exit 1
fi
if [ ! -d $1 -o ! -d $2 ];then
echo "$1 or $2 is not derectory!"
exit 1
fi
## 注意,Mac的readlink程序和GNU readlink功能不同,Mac需要下载greadlink
arg1=`greadlink -f $1`
arg2=`greadlink -f $2`
tmp_dir=/tmp/tmp.$$
rm -rf $tmp_dir
mkdir -p $tmp_dir || exit 0
#echo $tmp_dir
trap "rm -rf $tmp_dir; exit 0" SIGINT SIGTERM
## 注意,Mac和Linux的MD5程序不同,请根据需求使用,这里是Mac版的用法
function get_file_md5
{
if [ $# -ne 1 ];then
echo "get_file_md5 arg num error!"
return 1
fi
local file=$1
md5 $file | awk -F"=" '{print $2}'
}
function myexit
{
rm -rf $tmp_dir
exit 0
}
function show_diff
{
if [ $# -ne 1 ];then
return 1
fi
local diff_file=$1
echo "diff file:"
printf " %-55s %-52s\n" $arg1 $arg2
if [ -f $tmp_dir/A_ony_file ];then
awk '{printf(" [%2d] %-50s\n", NR, $1)}' $tmp_dir/A_ony_file
python -c 'print "-"*100'
fi
awk '{printf(" [%2d] %-50s %-50s\n", NR, $1, $1)}' $diff_file
echo "(s):show diff files (a):open all diff files (q):exit"
echo
}
function check_value
{
local diff_file=$1
local value=$2
tmp_file=$tmp_dir/tmp_file
>$tmp_file
for numbers in `echo "$value" | tr ',' ' '`
do
nf=`echo "$numbers" | awk -F"-" '{print NF}'`
if [ $nf -ne 1 -a $nf -ne 2 ];then
return 1
fi
begin=`echo "$numbers" | awk -F"-" '{print $1}'`
end=`echo "$numbers" | awk -F"-" '{print $2}'`
if [ -z "$end" ];then
sed -n $begin'p' $diff_file >> $tmp_file
else
if [ "$end" -lt $begin ];then
return 1
fi
sed -n $begin','$end'p' $diff_file >> $tmp_file
fi
if [ $? -ne 0 ];then
return 1
fi
done
awk -v dir1=$arg1 -v dir2=$arg2 '{
if (NR==1)
{
printf("edit %s/%s\nvertical diffsplit %s/%s\n", dir1, $0, dir2, $0)
}
else
{
printf("tabnew %s/%s\nvertical diffsplit %s/%s\n", dir1, $0, dir2, $0)
}
}' $tmp_file
}
#############################################################
# 获取diff info
#############################################################
for file in `find $arg1 | grep -v "/\." | grep -v "^\."`
do
file_relative_name=${file#$arg1/}
file $file | grep -Eq "text"
if [ $? -ne 0 ];then
continue
fi
if [ -f $arg2/$file_relative_name ];then
file $arg2/$file_relative_name | grep -Eq "text"
if [ $? -ne 0 ];then
continue
fi
md5_1=`get_file_md5 $file`
md5_2=`get_file_md5 $arg2/$file_relative_name`
if [[ "$md5_1" = "$md5_2" ]];then
continue
fi
## file not same
echo "$file_relative_name" >> $tmp_dir/diff_file
else
echo "$file_relative_name" >> $tmp_dir/A_ony_file
fi
done
#############################################################
# 根据输入标签打开用vim打开文件比较diff
#############################################################
if [ ! -f $tmp_dir/diff_file ];then
exit
fi
show_diff $tmp_dir/diff_file
while true
do
echo -n "Please choose file number list (like this:1,3-4,5):"
read value
if [[ "$value" = "s" ]] || [[ "$value" = "S" ]];then
show_diff $tmp_dir/diff_file
continue
elif [[ "$value" = "q" ]] || [[ "$value" = "Q" ]];then
myexit
elif [[ "$value" = "a" ]] || [ "$value" = "A" ];then
value="1-$"
fi
vim_script=`check_value $tmp_dir/diff_file "$value" 2>/dev/null`
if [ $? -ne 0 ];then
echo "invalid parameter[$value]!"
else
vim -c "$vim_script"
fi
done