-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff_history.sh
executable file
·313 lines (290 loc) · 7.29 KB
/
diff_history.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/bin/bash
exit_log_creator() {
echo "CMD: $0 FILE: $FILE MSG: $MSG" 2>&1 |logger
case ${EXIT} in
0)
echo $MSG >/dev/stdout
exit ${EXIT}
;;
10)
echo $MSG >/dev/stdout
;;
11)
echo $MSG >/dev/stderr
;;
*)
echo $MSG >/dev/stderr
exit ${EXIT}
;;
esac
}
pid_clean() {
if [ -f ${PID_FILE} ];then
rm $PID_FILE
find $PID_dir -empty -type d -delete
fi
}
pid_init() {
if [ -f ${PID_FILE} ];then
pid_test=`cat $PID_FILE`
ps $pid_test|grep $0
if [ $? -eq 0 ];then
exit 0
else
pid_clean
fi
fi
mkdir -p $(dirname $PID_FILE)
echo $$ >$PID_FILE
}
time_test() {
let iter_time=$(date +%s)
let commit_time=$($GIT log -1 --pretty=format:%at)
let diff_uptime=${iter_time}-${commit_time}
if [ $diff_uptime -lt $min_time ]||[ $diff_uptime -gt $max_time ];then
if [ $diff_uptime -gt $max_time ];then
let diff_uptime_min=$diff_uptime/60
MSG="SYS_ERROR: Тimeout '$diff_uptime_min' min is very long"
EXIT=11
exit_log_creator
else
exit 0
fi
fi
}
repo_init() {
pid_init
($GIT status || $GIT init) &&
$GIT add $FILE &&
$GIT commit -m "$FILE"
if [ $? -eq 0 ];then
pid_clean
MSG="GIT_OK: Create GIT repository for '$FILE' complete"
EXIT=0
exit_log_creator
else
pid_clean
MSG="GIT_ERROR: Create GIT repository for '$FILE' failed"
EXIT=1
exit_log_creator
fi
}
mode_iter() {
time_test
pid_init
$GIT add $FILE &&
$GIT commit -m "$FILE"
if [ $? -eq 0 ];then
pid_clean
MSG="GIT_OK: Commit '$FILE' create"
EXIT=0
exit_log_creator
else
pid_clean
exit 0
exit_log_creator
fi
}
old_mark() {
actual_cache=$($GIT log --pretty=format:%at^%h^%s | \
grep $FILE | \
grep $old_one_timestamp | \
awk -F "^" '{print $2}')
wordkdir=$pwd
cd $FILE_DIR
$GIT filter-branch -f --msg-filter "
if [ \"\$GIT_COMMIT\" = \"\$(git rev-parse --verify $actual_cache)\" ]
then
echo $old_comit_name
else
sed 's/.*/\0/g'
fi
" -- --all
cd $wordkdir
}
mode_rebase() {
let num=$($GIT log --pretty=format:%s | \
grep $FILE | \
wc -l)
if [ $num -gt $max_nums ];then
let old_nums=$num-$max_nums
old_timestamps=$($GIT log --pretty=format:%at^%h^%s | \
grep $FILE | \
awk -F "^" '{print $1}' | \
tail -n $old_nums)
for old_one_timestamp in $old_timestamps
do
old_mark
done
$GIT rebase -i --root
fi
}
log_menu() {
let height=$max_nums+1
let width=$(echo $FILE|wc -c)+7
echo $width
exec 3>&1
select=$($DIALOG --clear --title "Time Select Menu" --no-tags \
--menu "Select log from file '$FILE' :" 25 $width $height $allmenu 2>&1 1>&3)
err=$?
exec 3>&-
#reject test
if [ $err -ne 0 ]; then exit 1; fi
}
mode_view() {
# test dialog type
if [ -n "$DISPLAY" ]&&[ -f /usr/bin/Xdialog ];then
DIALOG=${DIALOG=Xdialog}
elif [ -f /usr/bin/dialog ];then
DIALOG=${DIALOG=dialog}
else
MSG="Please install dialog"
EXIT=1
exit_log_creator
fi
# menu
while true
do
$GIT log --pretty=format:%h^%s^%cI|cat
allmenu=""
for string in $($GIT log --pretty=format:%h^%s^%cI|grep $FILE)
do
allmenu+="$(echo $string|awk -F "^" '{print $1}') $(echo $string|awk -F "^" '{print $NF}') "
done
old_comit_menu=$($GIT log --pretty=format:%h^%s^%cI|grep "\^${old_comit_name}\^")
if [ "$old_comit_menu" != "" ];then
allmenu+="$(echo $old_comit_menu|awk -F "^" '{print $1}') $old_comit_name"
fi
log_menu
if [ "$select" == "" ];then
break
else
$GIT show $select
fi
done
exit 0
}
usage() {
# help kommand keys
echo "Usage: $0 -f <filename> [-m <modename: iter|rebase|view>|-d <start|stop>] -c <conf file>"
echo "-f - filename for log history"
echo "-m - mode: iter (iteration:DEFAULT); rebase(cut the log); view(view log history)"
echo "-d - start/stop/status iter daemon mode (replace -m {mode})"
echo "-c - config file"
echo "-h - help"
}
# getopts for command keys
while getopts ":f:m:d:c:h" o; do
case "${o}" in
f)
f=${OPTARG}
;;
m)
m=${OPTARG}
;;
d)
d=${OPTARG}
;;
c)
c=${OPTARG}
;;
h)
usage
exit 0
;;
*)
usage
exit 0
;;
esac
done
#read config file and init config variables
CONFIG_name=$(realpath ./diff_history.cfg)
if [ -n "$c" ];then CONFIG_name=$(realpath ${c});fi
if [ -f ${CONFIG_name} ];then
. ${CONFIG_name}
else
MSG="ERROR: Config File '${CONFIG_name}' not found"
EXIT=1
exit_log_creator
fi
# let min_time=10
let min_time=${CONFIG_OPT_min_time}*60
let max_time=${CONFIG_OPT_max_time}*60
let max_nums=${CONFIG_OPT_max_nums}
GIT_dir=$(realpath ${CONFIG_GIT_dir})
GIT_rebaser=$(realpath ${CONFIG_GIT_rebaser})
PID_dir=$(realpath ${CONFIG_SYS_pid_dir})
PID_dir=$(realpath ${CONFIG_SYS_pid_dir})
DAEMON_name=$(realpath ${CONFIG_SYS_daemon_name})
let DAEMON_step=${CONFIG_SYS_daemon_step}
old_comit_name="old"
# Dynamic variables && GIT test
if [ -n "$f" ];then
if [ -f "$f" ];then
FILE=$(realpath $f)
FILE_DIR=$(dirname $(realpath $f))
PID_FILE=${PID_dir}${FILE_DIR}/diff_history.pid
GIT_FILE_DIR=$GIT_dir$FILE_DIR
mkdir -p $GIT_FILE_DIR
if [ -d $GIT_FILE_DIR ];then
GIT="git --git-dir=$GIT_FILE_DIR --work-tree=$FILE_DIR"
$GIT log --oneline|grep $FILE >/dev/null 2>&1 || repo_init
$GIT config --local core.editor "$GIT_rebaser"
else
MSG="SYS_ERROR: GIT directory '$GIT_FILE_DIR' not found"
EXIT=1
exit_log_creator
fi
else
echo "File for log {-f} not found"
usage
exit 0
fi
else
echo "Please insert filename {-f}"
usage
exit 0
fi
#Run mode
if [ ! -n "$d" ];then
if [ ! -n "$m" ];then m="iter";fi
case "${m}" in
iter)
mode_iter
;;
rebase)
mode_rebase
;;
view)
mode_view
;;
*)
echo "Mode ${m} not found. Please insert mode: iter or rebase or view"
usage
exit 0
;;
esac
elif [ ! -n "$m" ];then
case "${d}" in
start)
nohup $DAEMON_name $FILE $PID_dir ${d} $0 $DAEMON_step &
;;
stop)
$DAEMON_name $FILE $PID_dir ${d}
;;
status)
$DAEMON_name $FILE $PID_dir ${d}
;;
*)
echo "Daemon mode ${d} not found. Please insert daemon mode: start or stop or status"
usage
exit 0
;;
esac
else
echo "Please select {-m} or {-d} key only"
usage
exit 0
fi
exit 0