-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.sh
executable file
·732 lines (695 loc) · 14 KB
/
functions.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#
# A useful set of bash functions for my scripts
#
# if is_interactive; then echo "interactive" fi
#
# Check for an interactive shell
if [ -z "$(declare -F | grep "\<is_interactive\>")" ]; then
is_interactive() {
case $- in
*i*)
# Don't die in interactive shells
return 0
;;
*)
return 1
;;
esac
}
fi
# if can_die; then exit
#
# Check to see if it's legal to exit during die
if [ -z "$(declare -F | grep "\<can_die\>")" ]; then
can_die() {
if (( BASH_SUBSHELL > 0 )); then
echo -e "\t\tbaby shell; exiting"
return 0
fi
if ! is_interactive; then
return 0
fi
return 1
}
fi
# command | die "message"
#
# Print a message and exit with failure
if [ -z "$(declare -F | grep "\<die\>")" ]; then
die() {
echo "Failed: $@"
if [ ! -z "$(declare -F | grep "DFGcleanup")" ]; then
DFGcleanup "$@"
fi
if can_die; then
exit 1
fi
}
fi
# How do use die properly to handle both interactive and script useage:
#testfunc() {
# (
# command1 || die "command1 failed"
# command2 || die "command2 failed"
# command3 || die "command3 failed"
# )
# return $?
#}
#
# Optionally, the return can be replaced with this:
# local val=$?
# [[ "${val}" == "0" ]] || die
# return ${val}
# This will cause the contaning script to abort
usage() {
local myusage;
if [ -n "${USAGE}" ]; then
myusage=${USAGE}
else
myusage="No usage given"
fi
if [ -n "$1" ]; then
echo "$@"
fi
echo ""
echo "Usage:"
echo "`basename $0` ${myusage}"
if [ -n "${LONGUSAGE}" ]; then
echo -e "${LONGUSAGE}"
fi
exit 1
}
#
# Script templates:
#
# Long options, using getopt(1)
##!/bin/bash
##
## Script that does something
##
#
## Set usage output
#USAGE="[-h |--help] [-k | --kernel] [-f <file> | --file=<file>] [<directories>]"
#LONGUSAGE="\t-h, --help\n\t\tPrint this help message
#\t-k, --kernel\n\t\tKernel database (no system includes)
#\t-f <file>, --file=<file>\n\t\tstore output into <file>
#\t<directories>\n\t\tDirectories to scan (defaults to '.')"
#
## Standard functions
#source ${SCRIPTS}/functions.sh
#
## Script name
#ME=$(basename $0)
#
## Parse arguments
#ARGS=`getopt -o hkf: --long help,kernel,file: -n "${ME}" -- "$@"`
#
#if [ $? != 0 ] ; then
# usage
#fi
#eval set -- "$ARGS"
#
#while true ; do
# case "$1" in
# -h|--help) usage; shift ;;
# -k|--kernel) KERNEL="yes"; shift ;;
# -f|--file) FILE=$2 ; shift 2 ;;
# --) shift ; break ;;
# * ) usage "Invalid argument $1";;
# esac
#done
#
# Remaining arguments are in $1, $2, etc. as normal
#
# Short args using getopts
##!/bin/bash
##
## Script that does something
##
#
## Set usage output
#USAGE="[-hk] [-f <file>] [<directories>]"
#LONGUSAGE="\t-h\tPrint this help message
#\t-k\tKernel database (no system includes)
#\t-f <file>\store output into <file>
#\t<directories>\tDirectories to scan (defaults to '.')"
#
## Standard functions
#source ${SCRIPTS}/functions.sh
#
## Parse arguments
#while getopts ":hkf:" option; do
# case ${option} in
# k ) KERNEL="yes";;
# f ) FILE=${OPTARG};;
# h ) usage;;
# \? ) usage "Invalid argument ${OPTARG}";;
# * ) usage "Invalid argument ${option}";;
# esac
#done
#
#if [ "${OPTIND}" != "0" ]; then
# shift $((OPTIND-1))
#fi
#
# Remaining arguments are in $1, $2, etc. as normal
#
# VCS functions.
# These functions wrap svn/cvs/etc, so that scripts don't have to care
# Set VCS_FATAL_ERRORS if you want errors to be fatal.
# Set VCS_FAKE if you want file operations without actual VCS operations
# VCS - default to unknown
VCS="unknown"
# Detect the VCS. Call this first.
function vcs_detect() {
VCS="unknown"
if [ -n "$(svn info 2>/dev/null | grep Path)" ]; then
VCS="svn"
elif [ -n "$(git rev-parse --git-dir 2>/dev/null)" ]; then
VCS="git"
elif [ -d "${PWD}/CVS" ]; then
VCS="cvs"
fi
if [ -n "${VCS_FAKE}" ]; then
VCS="fake"
fi
if [ "${VCS}" == "unknown" ]; then
if [ -n "${VCS_FATAL_ERRORS}" ]; then
die "Unknown VCS for ${PWD}"
else
echo "Unknown VCS for ${PWD}"
VCS="fake"
fi
fi
}
# Internal setup, called at the beginning of each function. Sets up local variables.
function vcs_int_setup() {
case "${VCS}" in
svn)
if [ -n "${VCS_FORCE}" ]; then
VCS_INT_FORCE="--force"
else
VCS_INT_FORCE=""
fi
;;
git)
if [ -n "${VCS_FORCE}" ]; then
VCS_INT_FORCE="-f"
else
VCS_INT_FORCE=""
fi
;;
cvs)
if [ -n "${VCS_FORCE}" ]; then
VCS_INT_FORCE="-f"
else
VCS_INT_FORCE=""
fi
;;
fake)
VCS_INT_FORCE=""
;;
*)
if [ -n "${VCS_FATAL_ERRORS}" ]; then
die "Unknown VCS for ${PWD}"
else
echo "Unknown VCS for ${PWD}"
fi
;;
esac
}
# Delete something from a VCS
function vcs_rm() {
vcs_int_setup
case "${VCS}" in
svn)
svn rm ${VCS_INT_FORCE} $* || die "svn rm failed"
;;
git)
git rm ${VCS_INT_FORCE} $* || die "git rm failed"
;;
cvs)
for i in $*; do
if [ ! -d "${i}" ]; then
rm ${i} || die "cvs rm failed (rm)"
fi
done
colorcvs rm $* || die "cvs rm failed (cvs rm)"
;;
fake)
for i in $*; do
if [ ! -d "${i}" ]; then
rm ${i} || die "rm $i failed"
fi
done
for i in $*; do
if [ -d "${i}" ]; then
rmdir ${i} || die "rmdir $i failed"
fi
done
;;
*)
if [ -n "${VCS_FATAL_ERRORS}" ]; then
die "Unknown VCS for ${PWD}"
else
echo "Unknown VCS for ${PWD}"
fi
;;
esac
}
# Add something to a VCS
function vcs_add() {
vcs_int_setup
case "${VCS}" in
svn)
svn add $* || die "svn add failed"
;;
git)
git add $* || die "git add failed"
;;
cvs)
colorcvs add $* || die "cvs add failed"
;;
fake)
;;
*)
if [ -n "${VCS_FATAL_ERRORS}" ]; then
die "Unknown VCS for ${PWD}"
else
echo "Unknown VCS for ${PWD}"
fi
;;
esac
}
# commit something to a VCS
# ${VCS_COMMITMSG} contains the commit message, ${VCS_COMMITFILE} contains the
# file with the commit message. Use VCS_REPOMAN_OPTS to pass options to
# repoman.
function vcs_commit() {
vcs_int_setup
case "${VCS}" in
svn)
if [ -n "${VCS_COMMITFILE}" ]; then
svn commit -F "${VCS_COMMITFILE}" $* || die "svn commit failed"
else
svn commit -m "${VCS_COMMITMSG}" $* || die "svn commit failed"
fi
;;
git)
if [ -n "${VCS_COMMITFILE}" ]; then
git commit -a -F "${VCS_COMMITFILE}" $* || die "git commit failed"
else
git commit -a -m "${VCS_COMMITMSG}" $* || die "git commit failed"
fi
if [ -z "${VCS_NOPUSH}" ]; then
git push || die "git push failed"
fi
;;
cvs)
if [ -n "${VCS_COMMITFILE}" ]; then
repoman ${VCS_REPOMAN_OPTS} --commitmsgfile "${VCS_COMMITFILE}" commit $* || die "cvs comit failed"
else
repoman ${VCS_REPOMAN_OPTS} --commitmsg "${VCS_COMMITMSG}" commit $* || die "cvs comit failed"
fi
;;
fake)
;;
*)
if [ -n "${VCS_FATAL_ERRORS}" ]; then
die "Unknown VCS for ${PWD}"
else
echo "Unknown VCS for ${PWD}"
fi
;;
esac
}
# update something in a VCS
function vcs_update() {
vcs_int_setup
case "${VCS}" in
svn)
svn up $* || die "svn update failed"
;;
git)
git pull $* || die "git pull failed"
;;
cvs)
colorcvs up $* || die "cvs update failed"
;;
fake)
;;
*)
if [ -n "${VCS_FATAL_ERRORS}" ]; then
die "Unknown VCS for ${PWD}"
else
echo "Unknown VCS for ${PWD}"
fi
;;
esac
}
# update something in a VCS, checking the results to see if there's conflicts
function vcs_update_check_conflicts() {
vcs_int_setup
case "${VCS}" in
svn)
OUTPUT="$(svn up $*)" || die "svn update failed"
STATUS=`echo ${OUTPUT} | grep "\<C\>"`
echo "${OUTPUT}"
;;
git)
OUTPUT="$(git pull $*)" || die "git pull failed"
STATUS=`echo ${OUTPUT} | grep "\<C\>"`
echo "${OUTPUT}"
;;
cvs)
OUTPUT="$(cvs up $*)" || die "cvs update failed"
STATUS=`echo ${OUTPUT} | grep "\<C\>"`
echo "${OUTPUT}"
;;
fake)
;;
*)
if [ -n "${VCS_FATAL_ERRORS}" ]; then
die "Unknown VCS for ${PWD}"
else
echo "Unknown VCS for ${PWD}"
fi
;;
esac
}
# get status on something in a VCS
function vcs_status() {
vcs_int_setup
case "${VCS}" in
svn)
svn status $* || die "svn status failed"
;;
git)
git status $*
;;
cvs)
colorcvs -nq up $* 2>/dev/null || die "cvs status failed"
;;
fake)
;;
*)
if [ -n "${VCS_FATAL_ERRORS}" ]; then
die "Unknown VCS for ${PWD}"
else
echo "Unknown VCS for ${PWD}"
fi
;;
esac
}
# diff something in a VCS
function vcs_diff() {
vcs_int_setup
case "${VCS}" in
svn)
svn diff $* || die "svn diff failed"
;;
git)
git diff $* || die "git diff failed"
;;
cvs)
colorcvs diff $* || die "cvs diff failed"
;;
fake)
;;
*)
if [ -n "${VCS_FATAL_ERRORS}" ]; then
die "Unknown VCS for ${PWD}"
else
echo "Unknown VCS for ${PWD}"
fi
;;
esac
}
# Do a changelog
function vcs_echangelog() {
vcs_int_setup
case "${VCS}" in
svn)
VCS_ECHANGELOG=echangelog
;;
git)
# Don't use echangelog on git
return
;;
cvs)
VCS_ECHANGELOG=echangelog
;;
fake)
VCS_ECHANGELOG="echo fake "
;;
*)
if [ -n "${VCS_FATAL_ERRORS}" ]; then
die "Unknown VCS for ${PWD}"
else
echo "Unknown VCS for ${PWD}"
fi
;;
esac
if [ -n "${VCS_COMMITFILE}" ]; then
EDITOR= ECHANGELOG_EDITOR= ${VCS_ECHANGELOG} < ${VCS_COMMITFILE} || die "${VCS_ECHANGELOG} failed"
elif [ -n "${VCS_COMMITMSG}" ]; then
${VCS_ECHANGELOG} "${VCS_COMMITMSG}" || die "${VCS_ECHANGELOG} died"
fi
}
# See if a file is under version control
function vcs_is_added() {
vcs_int_setup
case "${VCS}" in
svn)
OUTPUT=$(svn info $@ | grep Path:)
;;
git)
OUTPUT=$(git log $@ | grep commit)
;;
cvs)
OUTPUT=$(cvs status $@ 2>/dev/null | grep Status | grep -v Unknown)
;;
fake)
OUTPUT="foo"
;;
*)
if [ -n "${VCS_FATAL_ERRORS}" ]; then
die "Unknown VCS for ${PWD}"
else
echo "Unknown VCS for ${PWD}"
fi
;;
esac
if [ -z "${OUTPUT}" ]; then
return 1
fi
return 0
}
# Move a file from one name to another
function vcs_mv() {
vcs_int_setup
case "${VCS}" in
svn)
svn mv ${VCS_INT_FORCE} $1 $2 || die "svn mv failed"
;;
git)
git mv ${VCS_INT_FORCE} $1 $2 || die "git mv failed"
;;
cvs)
# CVS doesn't do move...
cp $1 $2 || die "cp failed"
vcs_add $2 || die "cvs add failed"
vcs_rm $1 || die "cvs rm failed"
;;
fake)
mv $1 $2 || die "mv failed"
;;
*)
if [ -n "${VCS_FATAL_ERRORS}" ]; then
die "Unknown VCS for ${PWD}"
else
echo "Unknown VCS for ${PWD}"
fi
;;
esac
}
# Copy a file from one name to another
function vcs_cp() {
vcs_int_setup
case "${VCS}" in
svn)
svn cp $1 $2 || die "svn cp failed"
;;
git)
# Can't copy in git yet...
cp $1 $2 || die "cp failed"
vcs_add $2 || die "git add failed"
;;
cvs)
# CVS doesn't do copy...
cp $1 $2 || die "cp failed"
vcs_add $2 || die "cvs add failed"
;;
fake)
cp $1 $2 || die "cp failed"
;;
*)
if [ -n "${VCS_FATAL_ERRORS}" ]; then
die "Unknown VCS for ${PWD}"
else
echo "Unknown VCS for ${PWD}"
fi
;;
esac
}
# Revert to last checked in state
function vcs_revert() {
vcs_int_setup
case "${VCS}" in
svn)
svn revert $* || die "svn revert failed"
;;
git)
if [ -n "$*" ] ; then
FILES="$*"
else
# git uses checkout . for recursive revert
FILES="."
fi
git checkout ${FILES} || die "git checkout failed"
;;
cvs)
# CVS doesn't do revert...
rm $*
vcs_update $* || die "cvs up failed"
;;
fake)
;;
*)
if [ -n "${VCS_FATAL_ERRORS}" ]; then
die "Unknown VCS for ${PWD}"
else
echo "Unknown VCS for ${PWD}"
fi
;;
esac
}
# annotate a file
function vcs_annotate() {
vcs_int_setup
case "${VCS}" in
svn)
svn annotate $* || die "svn annotate failed"
#svn annotate --use-merge-history $* || die "svn annotate failed"
;;
git)
git annotate $* | sed -r 's/\<([0-1][0-9]|[2][0-3]):([0-5][0-9]):([0-5][0-9])\>//' | sed 's/ +0000//' || die "git annotate failed"
;;
cvs)
colorcvs annotate $* || die "cvs annotate failed"
;;
fake)
;;
*)
if [ -n "${VCS_FATAL_ERRORS}" ]; then
die "Unknown VCS for ${PWD}"
else
echo "Unknown VCS for ${PWD}"
fi
;;
esac
}
# get log messages for a file
function vcs_log() {
vcs_int_setup
VERSION=$1
shift
case "${VCS}" in
svn)
svn log -r ${VERSION} || die "svn log failed"
;;
git)
git show ${VERSION} || die "git log failed"
;;
cvs)
# CVS doesn't do revert...
colorcvs log -r ${VERSION} $* || die "cvs log failed"
;;
fake)
;;
*)
if [ -n "${VCS_FATAL_ERRORS}" ]; then
die "Unknown VCS for ${PWD}"
else
echo "Unknown VCS for ${PWD}"
fi
;;
esac
}
# blame a line in a file
function vcs_blame() {
vcs_int_setup
file=$1
line=$2
case "${VCS}" in
git)
tig blame $file +$line
;;
svn|cvs)
blamename=`mktemp`
logname=`mktemp`
vcs_annotate $file > $blamename
if [ -z "${line}" ]; then
echo "No version" > ${logname}
line="0"
else
version=`awk '(NR == line){print $1}' line=${line} ${blamename}`
echo "version ${version}"
vcs_log ${version} ${file} > ${logname}
fi
vim +${line} -o ${blamename} ${logname}
rm ${blamename} ${logname}
;;
fake)
;;
*)
if [ -n "${VCS_FATAL_ERRORS}" ]; then
die "Unknown VCS for ${PWD}"
else
echo "Unknown VCS for ${PWD}"
fi
;;
esac
}
#
# Set up portage variables
function portage_setup() {
BASEDIR="/"
if [ -n "${1}" ]; then
BASEDIR="${1}"
shift
fi
if [ -f "${BASEDIR}/etc/make.globals" ]; then
source ${BASEDIR}/etc/make.globals
elif [ -f "${BASEDIR}/usr/share/portage/config/make.globals" ]; then
source "${BASEDIR}/usr/share/portage/config/make.globals"
else
die "Could not find make.globals"
fi
if [ -f "${BASEDIR}/etc/make.conf" ]; then
source ${BASEDIR}/etc/make.conf
elif [ -f "${BASEDIR}/etc/portage/make.conf" ]; then
source "${BASEDIR}/etc/portage/make.conf"
else
die "Could not find make.conf"
fi
if [ -z "${PORTDIR}" ]; then
die "No PORTDIR. Are make.globals and make.conf broken?"
fi
if [ -z "${PORTAGE_TMPDIR}" ]; then
die "No PORTAGE_TMPDIR. Are make.globals and make.conf broken?"
fi
if [ -z "${DISTDIR}" ]; then
die "No DISTDIR. Are make.globals and make.conf broken?"
fi
}