-
Notifications
You must be signed in to change notification settings - Fork 2
/
mtl-sync_old
executable file
·91 lines (72 loc) · 2.55 KB
/
mtl-sync_old
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
#!/bin/bash
showUsage() {
cat <<EOF
Usage:
sync doc ssh://user@host.com:~/mynotes/test
EOF
}
### this is just quick proof of concept version
### later we add a way to store remotes
if [ $# -eq 1 ]; then
#THIS METHOD WILL GO AND BE REPLACED BY LOOP USING PER DOC SYNC METHOD BELOW
# copy the remote index
echo "DELETING OLD INDEXES"
rm .mtl/remote/*
echo "COPYING INDEXES"
scp ${1}/.mtl/*.index .mtl/remote/.
# merge them
for index in .mtl/remote/*.index
do
DOC=`echo "$index" | sed 's/.mtl\/remote\/\([a-z0-9_-]*\)\.index/\1/'`
echo "MERGING: $DOC"
mtl-merge $DOC $index
done
scp .mtl/*.index ${1}/.mtl/
# copy the blobs that arent already here, copy the missing there
echo "RSYNCING THE BLOBS"
rsync -v -a ${1}/.mtl/d/* .mtl/d
rsync -v -a .mtl/d/* ${1}/.mtl/d
echo "OK - DONE"
#TODO LATER check for conflicts ...
elif [ $# -eq 2 ]; then
# we sync just specific doc
# copy the remote index here
echo "DELETING OLD INDEXES"
rm .mtl/remote/*
echo "COPYING INDEXES"
scp ${2}/.mtl/${1}.index .mtl/remote/.
# merge the indexes for index in .mtl/remote/*.index
# do
# DOC=`echo "$index" | sed 's/.mtl\/remote\/\([a-z0-9_-]*\)\.index/\1/'`
# echo "MERGING: $DOC"
# mtl-merge $DOC $index
# done
#scp .mtl/${1}.index ${2}/.mtl/
# copy the blobs that arent already here, copy the missing there
echo "RESYNCING THE BLOBS"
for blob in `comm -13 <(cat .mtl/${1}.index | cut -f 1 | uniq | sort) <(cat .mtl/remote/${1}.index | cut -f 1 | uniq | sort)`
do
echo "copying blob from remote: $blob"
scp ${2}/.mtl/d/$blob .mtl/d/.
done
for blob in `comm -23 <(cat .mtl/${1}.index | cut -f 1 | uniq | sort) <(cat .mtl/remote/${1}.index | cut -f 1 | uniq | sort)`
do
echo "copying blob to remote: $blob"
scp .mtl/d/$blob ${2}/.mtl/d/.
done
#do a 3way diff to merge the files
echo "-" | cat > .mtl/sepa
#### find last common src
OLDBL=`cat .mtl/${1}.index .mtl/sepa .mtl/remote/${1}.index | awk '
BEGIN {first=0}
{if ($1=="-") { first = 1 } else { if (first) { a[x++]=$2 } else { b[$2]=y++ } } }
END { for (x1=x-1; x1>=0;) { if (b[a[x1]]) { print a[x1]; break }; x1--; } }'`
MYBL=`tail -n 1 .mtl/${1}.index | cut -f 1`
THEIRBL=`tail -n 1 .mtl/remote/${1}.index | cut -f 1`
echo "merging MY: $MYBL OLD: $OLDBL THEIR: $THEIRBL"
echo "diff3 -m .mtl/d/$MYBL .mtl/d/$OLDBL .mtl/d/$THEIRBL | tee .mtl/$1"
diff3 -m .mtl/d/$MYBL .mtl/d/$OLDBL .mtl/d/$THEIRBL | tee .mtl/$1
echo "OK - DONE"
else
showUsage
fi