-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfgkeeper
executable file
·368 lines (293 loc) · 7.58 KB
/
cfgkeeper
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env bash
# Name: cfgkeeper
# Version: 0.4.0 - 2016-02-08 (yyyy-mm-dd)
# Author/Copyright: Christoph Polcin <labs-AT-polcin.de>
# Projectpage: http://www.christoph-polcin.com/project/cfgkeeper
# Modified by:
# Created: 2010-09-26 (yyyy-mm-dd)
# Licence: GPL2
VCS="git"
VCS_CONTRIB="/usr/share/git/contrib"
CFG="${HOME}/.config/cfgkeeper.rc"
OS="`uname`"
case $OS in
'Linux')
STAT="stat -c '%a%u%g'"
;;
'FreeBSD')
STAT="stat -f '%Lp%u%g'"
;;
'Darwin')
STAT="stat -f '%Lp%u%g'"
;;
*) ;;
esac
# init vars {{{
FP=''
DIFF="diff '%sf' '%bf'"
#}}}
vcs_init() { #{{{
[ -d "$BAKDIR/.git" ] && echo "error: repository already initialized!" && exit 1
if [ ! -f "$VCS_CONTRIB/hooks/setgitperms.perl" ]; then
echo VCS_CONTRIB="$VCS_CONTRIB" directory must contain 'hooks/setgitperms.perl'.
echo Adjust the path or pull it from 'http://git.kernel.org/?p=git/git.git;a=blob_plain;f=contrib/hooks/setgitperms.perl;hb=HEAD'
exit 1
fi
if [ ! -z "$1" ]; then
${VCS} clone "$1" "$BAKDIR"
else
${VCS} init "$BAKDIR"
fi
touch "$BAKDIR/.gitmeta"
echo -en '#!/bin/sh\nSUBDIRECTORY_OK=1 . git-sh-setup\n$GIT_DIR/hooks/setgitperms.perl -r' > "$BAKDIR/.git/hooks/pre-commit"
echo -en '#!/bin/sh\nSUBDIRECTORY_OK=1 . git-sh-setup\n$GIT_DIR/hooks/setgitperms.perl -w' > "$BAKDIR/.git/hooks/post-merge"
cp "$BAKDIR/.git/hooks/post-merge" "$BAKDIR/.git/hooks/post-checkout"
ln -s "$VCS_CONTRIB/hooks/setgitperms.perl" "$BAKDIR/.git/hooks/setgitperms.perl"
chmod +x "$BAKDIR/.git/hooks/pre-commit" "$BAKDIR/.git/hooks/post-merge" "$BAKDIR/.git/hooks/post-checkout"
echo -en "!.gitmeta\n!.gitignore" >> "$BAKDIR/.gitignore"
vcs_add ".gitmeta"
vcs_add ".gitignore"
vcs_commit "initial commit"
}
#}}}
vcs_init_raw() {
# [ -d "$BAKDIR/.git" ] && echo "error: repository already initialized!" && exit 1
if [ ! -f "$VCS_CONTRIB/hooks/setgitperms.perl" ]; then
echo VCS_CONTRIB="$VCS_CONTRIB" directory must contain 'hooks/setgitperms.perl'.
echo Adjust the path or pull it from 'http://git.kernel.org/?p=git/git.git;a=blob_plain;f=contrib/hooks/setgitperms.perl;hb=HEAD'
exit 1
fi
#if [ ! -z "$1" ]; then
# ${VCS} clone "$1" "$BAKDIR"
#else
# ${VCS} init "$BAKDIR"
#fi
touch "$BAKDIR/.gitmeta"
echo -en '#!/bin/sh\nSUBDIRECTORY_OK=1 . git-sh-setup\n$GIT_DIR/hooks/setgitperms.perl -r' > "$BAKDIR/.git/hooks/pre-commit"
echo -en '#!/bin/sh\nSUBDIRECTORY_OK=1 . git-sh-setup\n$GIT_DIR/hooks/setgitperms.perl -w' > "$BAKDIR/.git/hooks/post-merge"
cp "$BAKDIR/.git/hooks/post-merge" "$BAKDIR/.git/hooks/post-checkout"
ln -s "$VCS_CONTRIB/hooks/setgitperms.perl" "$BAKDIR/.git/hooks/setgitperms.perl"
chmod +x "$BAKDIR/.git/hooks/pre-commit" "$BAKDIR/.git/hooks/post-merge" "$BAKDIR/.git/hooks/post-checkout"
echo -en "!.gitmeta\n!.gitignore" >> "$BAKDIR/.gitignore"
vcs_add ".gitmeta"
vcs_add ".gitignore"
vcs_commit "initial commit"
}
vcs_add() { #{{{
echo "$(cd "${BAKDIR}"; ${VCS} add -v -- "$1")"
}
#}}}
vcs_rm() { #{{{
echo "$(cd "${BAKDIR}"; ${VCS} rm -f -- "$1")"
}
#}}}
vcs_commit() { #{{{
echo "$(cd "${BAKDIR}"; ${VCS} commit --message="$1")"
}
#}}}
vcs_status() { #{{{
echo "$(cd "${BAKDIR}"; ${VCS} status $@)"
}
#}}}
vcs_pt() { #{{{
echo "$(cd "${BAKDIR}"; ${VCS} $@)"
}
#}}}
_usage(){ #{{{
echo -en "usage: $(basename "$0")\n"
echo -en " a\tadd FILEs\t\tAdd files to the index\n"
echo -en " c\tcheck [PATH]\t\tShow modified files\n"
echo -en " co\tcommit MSG\t\tRecord changes to the repository\n"
echo -en " d\tdiff FILE\t\tShow changes between backup and system file\n"
echo -en "\tinit [GIT_REPO]\t\tInitialize the backup directory\n"
echo -en " l\tls [PATH]\t\tList repository content\n"
echo -en " pl\tpull\t\t\tPull changes\n"
echo -en " p\tpush [ARGS]\t\tPush changes\n"
echo -en " re\trestore FILEs\t\tRestore files\n"
echo -en "\trm FILEs\t\tRemove files from the repository\n"
echo -en " s\tstatus [ARGS]\t\tShow the repository status\n"
echo -en "\t-- ARGS\t\t\tForward arguments to git\n"
echo -en "\n"
echo -en "backupdir: $BAKDIR\n"
echo -en "\n"
exit 1
}
#}}}
_setFP(){ #{{{
FP="$1"
if [ ! "${FP:0:1}" = "/" ]; then
FP=$(cd $(dirname "$PWD/$FP"); pwd -P)/$(basename "$FP")
fi
}
#}}}
_add(){ #{{{
_setFP "$1"
if [ -f "$FP" ]; then
[ -L "$FP" ] && echo "warn: symbolic link '$FP'"
local dp="$BAKDIR`dirname "$FP"`"
mkdir -p "$dp"
dp=$dp/`basename "${FP}"`
cp -Pp "$FP" "$dp"
if [ $? = 0 ]; then
vcs_add "${FP#/}"
else
echo 'error: copy error has occurred'
fi
elif [ -d "$FP" ]; then
echo 'error: add files separately.'
else
echo "error: file not found '$FP'"
fi
}
#}}}
_rm(){ #{{{
_setFP "$1"
if [ ! -e "${BAKDIR}${FP}" ]; then
echo "error: file not found '${BAKDIR}${FP}'"
exit 1
else
if [ -d "${BAKDIR}${FP}" ]; then
echo "error: remove files separatley."
exit 1
else
vcs_rm "${FP#/}"
fi
fi
}
#}}}
_check(){ #{{{
local sf=""
if [ $# != 0 ]; then
_setFP "$1"
[ ! -d "$FP" ] && FP=`dirname "$FP"`
[ ! -e "${BAKDIR}${FP}" ] && echo "error: path not found: ${BAKDIR}${FP}" && exit 1
fi
find "${BAKDIR}${FP}" -type f -print0 | while read -d $'\0' file
do
case "$file" in
*$VCS_IGNORE_MASK*) continue;;
esac
sf="${file#${BAKDIR}}"
[ ! -e "$sf" ] && echo " missing: '$sf'" && continue
if ! diff "$file" "$sf" > /dev/null
then
echo " content: '$sf'"
elif [ $($STAT "$file") != $($STAT "$sf") ]
then
echo " meta: '$sf'"
fi
done
}
#}}}
_pull(){ #{{{
echo "$(cd "${BAKDIR}"; ${VCS} pull --rebase)"
} #}}}
_push(){ #{{{
echo "$(cd "${BAKDIR}"; ${VCS} push $@)"
} #}}}
_restore(){ #{{{
_setFP "$1"
if [ ! -e "${BAKDIR}${FP}" ]; then
echo "error: file not found '${BAKDIR}${FP}'"
return 1
else
if [ -d "${BAKDIR}${FP}" ]; then
echo "error: restore files separatley."
return 1
else
cp -Pp "${BAKDIR}${FP}" "${FP}"
[ $? = 0 ] && echo "restored: '$FP'"
fi
fi
}
#}}}
_ls(){ #{{{
local sf=""
if [ $# != 0 ]; then
_setFP "$1"
[ ! -d "$FP" ] && FP=`dirname "$FP"`
[ ! -e "${BAKDIR}${FP}" ] && echo "error: path not found: ${BAKDIR}${FP}" && exit 1
fi
find "${BAKDIR}${FP}" -type f -print0 | while read -d $'\0' file
do
case "$file" in
*$VCS_IGNORE_MASK*) continue;;
esac
sf="${file#${BAKDIR}}"
echo "'$sf'"
done
}
#}}}
_diff(){ #{{{
_setFP "$1"
[ ! -e "${FP}" ] && echo "error: file not found '${BAKDIR}${FP}'" && exit 1
[ ! -e "${BAKDIR}${FP}" ] && echo "error: file not found '${BAKDIR}${FP}'" && exit 1
DIFF=${DIFF/\%sf/$FP}
DIFF=${DIFF/\%bf/${BAKDIR}${FP}}
eval "$DIFF"
return 0
}
#}}}
_printBakdir(){ #{{{
echo ${BAKDIR}
}
#}}}
# checks {{{
[ ! -f "${CFG}" ] && echo -en "error: need configfile '${CFG}'\n" && exit 1
. "${CFG}"
VCS_IGNORE_MASK="${BACKDIR}/.git"
[ $# = 0 ] && _usage
#}}}
case "$1" in #{{{
init)
shift
vcs_init $@
;;
init-raw)
shift
vcs_init_raw $@
;;
a|add)
shift;
while [ $# -gt 0 ]; do
_add "$1"
shift
done
;;
d|diff)
[ $# = 2 ] && _diff "$2" || _usage;;
rm)
shift;
while [ $# -gt 0 ]; do
_rm "$1"
shift
done
;;
co|commit)
[ $# = 2 ] && vcs_commit "$2" || _usage;;
s|status)
shift; vcs_status $@;;
c|ch|check)
shift; _check $@;;
l|ls)
shift; _ls $@;;
pl|pull)
_pull;;
p|push)
shift; _push $@;;
re|restore)
shift;
while [ $# -gt 0 ]; do
_restore "$1"
shift
done
;;
pb|printBakdir)
_printBakdir;;
--)
shift; vcs_pt $@;;
*)
_usage;;
esac
#}}}
# vim:ft=bash:ts=2:foldmethod=marker:noexpandtab