-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport
executable file
·88 lines (76 loc) · 2.55 KB
/
import
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
#!/bin/bash
#
# Copyright (c) 2013, Anton Backer <[email protected]>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
set -e
die() { echo "$1" >&2; exit 1; }
while :
do
case $1 in
--gc)
gc=1;
shift
;;
*)
break
;;
esac
done
find=find
if which gfind >/dev/null; then find=gfind; fi
readlink=readlink
if which greadlink >/dev/null; then readlink=greadlink; fi
src_path="$HOME"/Dropbox/Apps/WorkFlowy
dst_path="$(dirname "$("$readlink" -f "$0")")"
cd "$dst_path" || die "Unable to change directory"
branch=backups
orig_branch="$(git symbolic-ref HEAD | sed s,^refs/heads/,,)"
echo "Checking out the '$branch' branch..."
if git show-ref --verify --quiet refs/heads/"$branch"; then
git checkout "$branch"
last_imported="$(git log --format='%ad' --date=short)"
else
git checkout --orphan "$branch" && git rm -rf .
last_imported="0000-00-00"
fi
echo "Importing..."
ln "$src_path"/Data/* .
ln "$src_path"/History/* .
echo "Normalizing..."
# At some point files were being backed up with +x
chmod 644 *.txt *.backup
# Make sorting trivial
rename 's/-(\d)\b/-0$1/g' *.txt *.backup # rename -M?M-D?D to -MM-DD
rename 's/.*\.(\d*-\d*-\d*)(?:\.workflowy)?/$1/' *.txt *.backup # rename to YYYY-MM-DD.txt or .backup
echo "Committing..."
for f in *-*-*.txt; do
if [[ ! "${f%.txt}" > "$last_imported" ]]; then
rm "$f" "${f%.txt}.backup"
continue
fi
mv -f "$f" history.txt
mv -f "${f%.txt}.backup" data.backup
git add history.txt data.backup
# Try to commit if the index actually has changes; ignore otherwise
git commit --date="${f%.txt}T06:00:00" -m "Backup ${f%.txt}" || true
done
echo "Cleaning up..."
$find \
-mindepth 1 \
-not -name data.backup -and \
-not -name history.txt -and \
-not -path '*/\.*' \
-delete
[[ -z "$gc" ]] || git gc --aggressive
git checkout "$orig_branch"