This repository has been archived by the owner on Nov 20, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo_init.sh
executable file
·309 lines (282 loc) · 9.83 KB
/
repo_init.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
#!/bin/bash
####################################################
# GRIM REPO ########################################
####################################################
# repo_init.sh
#
# Initializes a new repository.
# When initializing a new repository, everything is
# conflicting. That is, the user has to consider all
# the cases of whether to syncronize on way or the
# other and manually resolve files that are conflicting
#
####################################################
if [ -f ~/.grimreporc ]; then
source ~/.grimreporc
else
source `pwd`/.grimreporc
fi
#use pure text menus
source $GR_PATH/text_ui/gr_textmenus.sh
source $GR_PATH/file_info.sh
source $GR_PATH/sync_functions.sh
source $GR_PATH/repo_bookkeeper.sh
####
##
## stupid global
sql_wrapper_rm_flag="-i";
##
while getopts ":hd" opt; do
case $opt in
d)
log 1 "-d was triggered"
sql_wrapper_rm_flag=""
;;
\?)
log 1 "Invalid option: -$OPTARG"
exit 1
;;
h)
echo "$0 [-d ] delete sqlite db without asking "
exit 0
esac
done
#run list rsync changes from server to local
#using head and tail to avoid the garbage info from rsync
s_to_c=`rsync -vrc -n $GR_SERVER:$GR_SERVERROOT $GR_LOCALROOT | tail --lines=+2 | head --lines=-3`
c_to_s=`rsync -vrc -n $GR_LOCALROOT $GR_SERVER:$GR_SERVERROOT | tail --lines=+2 | head --lines=-3`
####
# Implements the action used if a file only exists
# on server and not locally. This is done by presenting
# user with a menu, and handling his choice.
#
# $1 - file: the conflicting file
# $2 - serverroot: the root of the server (in the form user@server:serverpath/)
# $3 - localroot: the root of the client (eg. /root/to/local/repo/)
function file_exists_server {
file=$1;
serverroot=$2;
localroot=$3;
log 0 "localroot $localroot";
#present conflict to user (choice saved in $?)
file_exists_server_menu "$file"
choice=$?;
#if user chooses "cancel", we quit
if [ $choice -eq "0" ]; then
exit 73;
fi;
#if user chooses to copy:
if [ $choice -eq "1" ]; then
copy_data "$file" "$serverroot" "$localroot";
else
if [ $choice -eq "3" ]; then
#if user wants to view file info:
echo print_remote_file_info "$serverroot" "$file";
print_remote_file_info "$serverroot" "$file";
#after showing info, go back to menu
file_exists_server "$file" "$serverroot" "$localroot";
return $?;
else
#user chooses to delete -- confirm first:
confirm_menu "Do you really want to delete file: \033[1m$file\033[0m from server";
confirmed=$?;
#if user regrets, give him new prompt on what to do
if [ $confirmed -eq "0" ]; then
file_exists_server "$file" "$serverroot" "$localroot";
return $?;
else
delete_data "$file" "$serverroot"
fi; #end if user prompted on certain to delete
fi; #end if view/delete file
fi; #end if user chooses copy or delete
}
####
# Implements the action used if a directory only exists
# on server and not locally. This is done by presenting
# user with a menu, and handling his choice.
#
# $1 - dir: the conflicting dir
# $2 - serverroot: the root of the server (in the form user@server:serverpath/)
# $3 - localroot: the root of the local (eg. /root/to/local/repo/)
function dir_exists_server {
dir=$1;
serverroot=$2;
localroot=$3;
#present conflict to user (choice saved in $?)
dir_exists_server_menu "$dir"
choice=$?;
#if user chooses "cancel", we quit
if [ $choice -eq "0" ]; then
exit 74;
fi;
#if user chooses to copy:
if [ $choice -eq "1" ]; then
copy_data "$dir" "$serverroot" "$localroot";
else #user chooses to delete -- confirm first:
confirm_menu "Do you really want to delete directory: \033[1m$dir\033[0m from server";
confirmed=$?;
#if user regrets, give him new prompt on what to do
if [ $confirmed -eq "0" ]; then
dir_exists_server "$dir" "$serverroot" "$localroot";
return $?;
else
delete_data "$dir" "$serverroot";
fi; #end if user prompted on certain to delete
fi; #end if user chooses copy or delete
#if we made it this far, user has either copied or deleted
#since its a dir, and copying/deleting is recursive, we can remove
}
####
# Implements the action used if a file only exists
# on locally and not on server. This is done by presenting
# user with a menu, and handling his choice.
#
# $1 - file: the conflicting file
# $2 - serverroot: the root of the server (in the form user@server:serverpath/)
# $3 - localroot: the root of the client (eg. /root/to/local/repo/)
function file_exists_local {
file=$1;
serverroot=$2;
localroot=$3;
echo localroot $localroot;
#present conflict to user (choice saved in $?)
file_exists_local_menu "$file"
choice=$?;
#if user chooses "cancel", we quit
if [ $choice -eq "0" ]; then
exit 73;
fi;
#if user chooses to copy:
if [ $choice -eq "1" ]; then
copy_data "$file" "$localroot" "$serverroot";
else
if [ $choice -eq "3" ]; then
#if user wants to view file info:
echo print_local_file_info "$file" "$localroot";
print_local_file_info "$file" "$localroot";
#after showing info, go back to menu
file_exists_local "$file" "$serverroot" "$localroot";
return $?;
else
#user chooses to delete -- confirm first:
confirm_menu "Do you really want to delete file: \033[1m$file\033[0m from this computer";
confirmed=$?;
#if user regrets, give him new prompt on what to do
if [ $confirmed -eq "0" ]; then
file_exists_local "$file" "$serverroot" "$localroot";
return $?;
else
delete_data "$file" "$localroot"
fi; #end if user prompted on certain to delete
fi; #end if view/delete file
fi; #end if user chooses copy or delete
}
####
# Implements the action used if a directory only exists
# on local repository and not on server. This is done by presenting
# user with a menu, and handling his choice.
#
# $1 - dir: the conflicting dir
# $2 - serverroot: the root of the server (in the form user@server:serverpath/)
# $3 - localroot: the root of the local (eg. /root/to/local/repo/)
function dir_exists_local {
dir=$1;
serverroot=$2;
localroot=$3;
#present conflict to user (choice saved in $?)
dir_exists_local_menu "$dir"
choice=$?;
#if user chooses "cancel", we quit
if [ $choice -eq "0" ]; then
exit 74;
fi;
#if user chooses to copy:
if [ $choice -eq "1" ]; then
copy_data "$dir" "$localroot" "$serverroot";
else #user chooses to delete -- confirm first:
confirm_menu "Do you really want to delete directory: \033[1m$dir\033[0m from local repository";
confirmed=$?;
#if user regrets, give him new prompt on what to do
if [ $confirmed -eq "0" ]; then
dir_exists_local "$dir";
return $?;
else
delete_data "$dir" "$localroot";
fi; #end if user prompted on certain to delete
fi; #end if user chooses copy or delete
#if we made it this far, user has either copied or deleted
#since its a dir, and copying/deleting is recursive, we can remove
}
##############
# First consider the files that are conflicting from server to local
##############
#save localroot and serverroot short path (rootdir/ instead of /home/user/rootdir/)
#server_root=`echo $GR_SERVERROOT | sed 's#^.*/\(.\)#\1#'i`
#local_root=`echo $GR_LOCALROOT | sed 's#^.*/\(.\)#\1#'i`
#while we still have more conflicts to consider
server_root=$GR_SERVER:$GR_SERVERROOT;
local_root=$GR_LOCALROOT;
alt_conflict_list=$c_to_s;
conflict_list=$s_to_c;
while [ `echo $conflict_list | wc -w` != 0 ]; do
log 0 "`tree *root`";
echo "Conflict List: $conflict_list";
#take first line from list of conflicts:
conflict=`echo "$conflict_list" | head -n 1`
#then remove that line from c_to_s
conflict_list=`echo "$conflict_list" | tail --lines=+2`;
echo "next conflict=$conflict";
#if it is a directory (which can be identified by the trailing '/')
#it means the dir only exists on server and not on locally
if echo $conflict | grep -q -P '/$'; then
dir_exists_server "$conflict" "$server_root" "$local_root";
#since user must either delete or copy (recursively) the
#we can delete all conflicting entries that are subfiles or
#subfolders
conflict_list=`echo "$conflict_list" | grep -P -v "^$conflict"`;
else
#it is a file.
#check if file exists both locally and on server
#this is the case if $conflict is in $alt_conflict_list
if echo "$alt_conflict_list" | grep -q $conflict; then
echo "CONFLICT: $conflict exists in both";
solve_conflict "$conflict" "$server_root" "$local_root";
#remove $next_entry from opposite list (that is alt_conflict_list)
alt_conflict_list=`echo "$alt_conflict_list" | grep -P -v "^$conflict$"`;
#echo "RESOLVED: $alt_conflict_list";
else
#file exists only on server
file_exists_server "$conflict" "$server_root" "$local_root";
fi;
fi;
done;
#echo "=====================================================================================";
#then consider the files that are conflicting from local to server (s_to_c):
while [ `echo $alt_conflict_list | wc -w` != 0 ]; do
log 0 "`tree *root`";
echo "Conflict List: $alt_conflict_list";
#take first line from list of conflicts:
conflict=`echo "$alt_conflict_list" | head -n 1`
#then remove that line from c_to_s
alt_conflict_list=`echo "$alt_conflict_list" | tail --lines=+2`;
echo "next conflict=$conflict";
#if it is a directory (which can be identified by the trailing '/')
#it means the dir only exists on server and not on locally
if echo $conflict | grep -q -P '/$'; then
dir_exists_local "$conflict" "$server_root" "$local_root"
#since user must either delete or copy (recursively) the
#we can delete all conflicting entries that are subfiles or
#subfolders
alt_conflict_list=`echo "$alt_conflict_list" | grep -P -v "^$conflict"`;
else
#it is a file.
#we resolved all cases where file exists both locally and on server
#in the other conflict list
#file exists only on local repository
file_exists_local "$conflict" "$server_root" "$local_root";
fi;
done;
#After all conflicts are solved, we clear the database,
recalculate_all "$GR_LOCALROOT";
#tree $GR_LOCALROOT $GR_SERVERROOT
#echo $GR_LOCALROOT