-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtws.sh
992 lines (887 loc) · 22.9 KB
/
gtws.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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
#!/bin/bash
# Functions for gtws
#
GTWS_LOC=$(readlink -f $(dirname "${BASH_SOURCE[0]}"))
export GTWS_LOC
# if is_interactive; then echo "interactive" fi
#
# Check for an interactive shell
is_interactive() {
case $- in
*i*)
# Don't die in interactive shells
return 0
;;
*)
return 1
;;
esac
}
# if can_die; then exit
#
# Check to see if it's legal to exit during die
can_die() {
if (( BASH_SUBSHELL > 0 )); then
debug_print "\t\tbaby shell; exiting"
return 0
fi
if ! is_interactive; then
debug_print "\t\tNot interactive; exiting"
return 0
fi
debug_print "\t\tParent interactive; not exiting"
return 1
}
# In a function:
# command || die "message" || return 1
# Outside a function:
# command || die "message"
#
# Print a message and exit with failure
die() {
echo -e "Failed: $1" >&2
if [ ! -z "$(declare -F | grep "GTWScleanup")" ]; then
GTWScleanup
fi
if can_die; then
exit 1
fi
return 1
}
# Alternativess for using die properly to handle both interactive and script useage:
#
# Version 1:
#
#testfunc() {
# command1 || die "${FUNCNAME}: command1 failed" || return 1
# command2 || die "${FUNCNAME}: command2 failed" || return 1
# command3 || die "${FUNCNAME}: command3 failed" || return 1
#}
#
# Version 2:
#
#testfunc() {
# (
# command1 || die "${FUNCNAME}: command1 failed"
# command2 || die "${FUNCNAME}: command2 failed"
# command3 || die "${FUNCNAME}: 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 "You need to provide a frobnicator"
#
# Print a message and the usage for the current script and exit with failure.
usage() {
local myusage;
if [ -n "${USAGE}" ]; then
myusage=${USAGE}
else
myusage="No usage given"
fi
local me;
if [ -n "${ME}" ]; then
me=${ME}
else
me=$(basename $0)
fi
if [ -n "$1" ]; then
echo "$@"
fi
echo ""
if [ -n "${DESCRIPTION}" ]; then
echo -e "${me}: ${DESCRIPTION}"
echo ""
fi
echo "Usage:"
echo "${me} ${myusage}"
if [ -n "${LONGUSAGE}" ]; then
echo -e "${LONGUSAGE}"
fi
exit 1
}
# debug_print "Print debug information"
#
# Print debug information based on GTWS_VERBOSE
debug_print() {
if [ -n "${GTWS_VERBOSE}" ]; then
echo -e "${GTWS_INDENT}$@" >&2
fi
}
# debug_trace_start
#
# Start tracing all commands
debug_trace_start() {
if [ -n "${GTWS_VERBOSE}" ]; then
set -x
fi
}
# debug_trace_stop
#
# Stop tracing all commands
debug_trace_stop() {
set +x
}
# cmd_exists ${cmd}
#
# Determine if a command exists on the system
function cmd_exists {
which $1 > /dev/null 2>&1
if [ "$?" == "1" ]; then
die "You don't have $1 installed, sorry" || return 1
fi
}
# is_git_repo ${dir}
#
# return success if ${dir} is in a git repo, or failure otherwise
is_git_repo() {
debug_print "is_git_repo $1"
if [[ $1 == *:* ]]; then
debug_print " remote; assume good"
return 0
elif [ ! -d "$1" ]; then
debug_print " fail: not dir"
return 1
fi
cd "$1"
git rev-parse --git-dir >/dev/null 2>&1
local ret=$?
cd - > /dev/null
debug_print " retval: $ret"
return $ret
}
# find_git_repo ${basedir} ${repo_name} repo_dir
#
# Find the git repo for ${repo_name} in ${basedir}. It's one of ${repo_name}
# or ${repo_name}.git
#
# Result will be in the local variable repo_dir Or:
#
# repo_dir=$(find_git_repo ${basedir} ${repo_name})
#
function find_git_repo {
local basedir=$1
local repo_name=$2
local __resultvar=$3
local try="${basedir}/${repo_name}"
if ! is_git_repo "${try}" ; then
try=${try}.git
fi
is_git_repo "${try}" || die "${repo_name} in ${basedir} is not a git repository" || return 1
if [[ "$__resultvar" ]]; then
eval $__resultvar="'$try'"
else
echo "$try"
fi
}
# git_top_dir top
#
# Get the top level of the git repo contaning PWD, or return failure;
#
# Result will be in local variable top Or:
#
# top = $(git_top_dir)
#
# Result will be in local variable top
function git_top_dir {
local __resultvar=$1
local __top="$(git rev-parse --show-toplevel 2>/dev/null)"
if [ -z "${__top}" ]; then
die "${PWD} is not a git repo" || return 1
fi
if [[ "$__resultvar" ]]; then
eval $__resultvar="'$__top'"
else
echo "$__top"
fi
}
# is_git_rebase
#
# return success if git repo is in a rebase
is_git_rebase() {
debug_print "is_git_rebase $1"
(test -d "$(git rev-parse --git-path rebase-merge)" || \
test -d "$(git rev-parse --git-path rebase-apply)" )
local ret=$?
debug_print " retval: $ret"
return $ret
}
# is_docker
#
# return success if process is running inside docker
is_docker() {
debug_print "is_docker"
grep -q docker /proc/self/cgroup
return $?
}
# is_gtws
#
# return success if process is running inside a workspace
is_gtws() {
if [ -n "${GTWS_WS_GUARD}" ]; then
return 0
fi
return 1
}
function gtws_rcp {
rsync --rsh=ssh -avzS --progress --ignore-missing-args --quiet "$@"
}
function gtws_cpdot {
local srcdir=$1
local dstdir=$2
debug_print "${FUNCNAME} - ${srcdir} to ${dstdir}"
if [ -d "${srcdir}" ] && [ -d "${dstdir}" ]; then
shopt -s dotglob
cp -a "${srcdir}"/* "${dstdir}"/
shopt -u dotglob
fi
}
# gtws_find_dockerfile dockerfile
#
# Result will be in local variable dockerfile Or:
#
# dockerfile = $(gtws_find_dockerfile)
#
# Result will be in local variable dockerfile
#
# Get the path to the most-specific Dockerfile
function gtws_find_dockerfile {
local __resultvar=$1
local __dir="${GTWS_WSPATH}"
local __file="Dockerfile"
debug_print "${FUNCNAME} - trying ${__dir}/${__file}"
if [ ! -f "${__dir}/${__file}" ]; then
# Version dir
__dir=$(dirname "${__dir}")
debug_print "${FUNCNAME} - trying ${__dir}/${__file}"
fi
if [ ! -f "${__dir}/${__file}" ]; then
# Project dir
__dir=$(dirname "${__dir}")
debug_print "${FUNCNAME} - trying ${__dir}/${__file}"
fi
if [ ! -f "${__dir}/${__file}" ]; then
# Top level, flavor
__dir="${GTWS_LOC}/dockerfiles"
__file="Dockerfile-${FLAVOR}"
debug_print "${FUNCNAME} - trying ${__dir}/${__file}"
fi
if [ ! -f "${__dir}/${__file}" ]; then
# Top level, base
__dir="${GTWS_LOC}/dockerfiles"
__file="Dockerfile-base"
debug_print "${FUNCNAME} - trying ${__dir}/${__file}"
fi
if [ ! -f "${__dir}/${__file}" ]; then
die "Could not find a Dockerfile" || return 1
fi
debug_print "${FUNCNAME} - found ${__dir}/${__file}"
if [[ "$__resultvar" ]]; then
eval $__resultvar="'${__dir}/${__file}'"
else
echo "$__dir"
fi
}
# gtws_smopvn ${GTWS_SUBMODULE_ORIGIN:-${GTWS_ORIGIN}} ${GTWS_PROJECT} ${GTWS_PROJECT_VERSION} ${GTWS_WSNAME} smopvn
#
# Result will be in local variable smopvn. Or:
#
# smopvn = $(gtws_smopvn ${GTWS_SUBMODULE_ORIGIN:-${GTWS_ORIGIN}} ${GTWS_PROJECT} ${GTWS_PROJECT_VERSION} ${GTWS_WSNAME})
#
# Result will be in local variable smovpn
#
# Get the path to submodules for this workspace
function gtws_smopvn {
local origin=$1
local project=$2
local version=$3
local name=$4
local __resultvar=$5
local __smopv="${origin}/${project}/submodule"
if [[ "$__resultvar" ]]; then
eval $__resultvar="'$__smopv'"
else
echo "$__smopv"
fi
}
# gtws_opvn ${GTWS_ORIGIN} ${GTWS_PROJECT} ${GTWS_PROJECT_VERSION} ${GTWS_WSNAME} opvn
#
# Result will be in local variable opvn. Or:
#
# opvn = $(gtws_opvn ${GTWS_ORIGIN} ${GTWS_PROJECT} ${GTWS_PROJECT_VERSION} ${GTWS_WSNAME})
#
# Result will be in local variable opvn.
#
# Get the path to git repos for this workspace
function gtws_opvn {
local origin=$1
local project=$2
local version=$3
local name=$4
local __resultvar=$5
local __opv="${origin}/${project}/${version}"
if [[ $__opv == *:* ]]; then
__opv="${__opv}/${name}"
debug_print "remote; using opvn $__opv"
elif [ ! -d "${__opv}" ]; then
__opv="${origin}/${project}/git"
if [ ! -d "${__opv}" ]; then
die "No opvn for ${origin} ${project} ${version}" || return 1
fi
fi
if [[ "$__resultvar" ]]; then
eval $__resultvar="'$__opv'"
else
echo "$__opv"
fi
}
# gtws_submodule_url ${submodule} url
#
# Result will be in local variable url Or:
#
# url = $(gtws_submodule_url ${submodule})
#
# Result will be in local variable url
#
# Get the URL for a submodule
function gtws_submodule_url {
local sub=$1
local __resultvar=$2
local __url=$(git config --list | grep "submodule.*url" | grep "\<${sub}\>" | cut -d = -f 2)
if [ -z "${__url}" ]; then
local rpath=${PWD}
local subsub=$(basename "${sub}")
cd "$(dirname "${sub}")"
debug_print "${FUNCNAME} trying ${PWD}"
__url=$(git config --list | grep submodule | grep "\<${subsub}\>" | cut -d = -f 2)
cd "${rpath}"
fi
debug_print "${FUNCNAME} $sub url: $__url"
if [[ "$__resultvar" ]]; then
eval $__resultvar="'$__url'"
else
echo "$__url"
fi
}
# gtws_submodule_mirror ${smopv} ${submodule} ${sub_sub_basename} mloc
#
# Result will be in local variable mloc Or:
#
# mloc = $(gtws_submodule_mirror ${smopv} ${submodule} ${sub_sub_basename})
#
# Result will be in local variable mloc
#
# Get the path to a local mirror of the submodule, if it exists
function gtws_submodule_mirror {
local smopv=$1
local sub=$2
local sub_sub=$3
local __resultvar=$4
local __mloc=""
local url=$(gtws_submodule_url ${sub})
if [ -n "${url}" ]; then
local urlbase=$(basename ${url})
# XXX TODO - handle remote repositories
#if [[ ${smopv} == *:* ]]; then
## Remote SMOPV means clone from that checkout; I don't cm
#refopt="--reference ${smopv}/${name}/${sub}"
if [ -d "${smopv}/${urlbase}" ]; then
__mloc="${smopv}/${urlbase}"
fi
fi
if [[ "$__resultvar" ]]; then
eval $__resultvar="'$__mloc'"
else
echo "$__mloc"
fi
}
# gtws_submodule_paths subpaths
#
# Result will be in local variable subpaths Or:
#
# subpaths = $(gtws_submodule_paths)
#
# Result will be in local variable subpaths
#
# Get the paths to submodules in a get repo. Does not recurse
function gtws_submodule_paths {
local __resultvar=$1
local __subpaths=$(git submodule status | sed 's/^ *//' | cut -d ' ' -f 2)
if [[ "$__resultvar" ]]; then
eval $__resultvar="'$__subpaths'"
else
echo "$__subpaths"
fi
}
# gtws_submodule_clone [<base-submodule-path>] [<sub-sub-basename>]
#
# This will set up all the submodules in a repo. Should be called from inside
# the parent repo
function gtws_submodule_clone {
local smopv=$1
local sub_sub=$2
local sub_paths=$(gtws_submodule_paths)
local rpath="${PWD}"
if [ -z "${smopv}" ]; then
smopv=$(gtws_smopvn "${GTWS_SUBMODULE_ORIGIN:-${GTWS_ORIGIN}}" "${GTWS_PROJECT}" "${GTWS_PROJECT_VERSION}" "${GTWS_WSNAME}")
fi
git submodule init || die "${FUNCNAME}: Failed to init submodules" || return 1
for sub in ${sub_paths}; do
local refopt=""
local mirror=$(gtws_submodule_mirror "${smopv}" "${sub}" "${sub_sub}")
debug_print "${FUNCNAME} mirror: ${mirror}"
if [ -n "${mirror}" ]; then
refopt="--reference ${mirror}"
fi
git submodule update ${refopt} "${sub}"
# Now see if there are recursive submodules
cd "${sub}"
gtws_submodule_clone "${smopv}/${sub}_submodule" "${sub}" || return 1
cd "${rpath}"
done
}
# gtws_repo_clone <base-repo-path> <repo> <branch> [<base-submodule-path>] [<target-directory>]
function gtws_repo_clone {
local baserpath=${1%/}
local repo=$2
local branch=$3
local basesmpath=$4
local rname=${5:-${repo%.git}}
local rpath="${baserpath}/${repo}"
local origpath=${PWD}
if [[ ${rpath} != *:* ]]; then
if [ ! -d "${rpath}" ]; then
rpath="${rpath}.git"
fi
fi
if [ -z "${basesmpath}" ]; then
basesmpath="${baserpath}"
fi
debug_print "${FUNCNAME}: cloning ${baserpath} - ${repo} : ${branch} into ${GTWS_WSNAME}/${rname} submodules: ${basesmpath}"
# Main repo
#git clone --recurse-submodules -b "${branch}" "${rpath}" || die "failed to clone ${rpath}:${branch}" || return 1
git clone -b "${branch}" "${rpath}" ${rname} || die "${FUNCNAME}: failed to clone ${rpath}:${branch}" || return 1
# Update submodules
cd "${rname}" || die "${FUNCNAME}: failed to cd to ${rpath}" || return 1
gtws_submodule_clone "${basesmpath}" || return 1
cd "${origpath}" || die "${FUNCNAME}: Failed to cd to ${origpath}" || return 1
# Copy per-repo settings, if they exist
gtws_cpdot "${baserpath%/git}/extra/repo/${rname}" "${origpath}/${rname}"
# Extra files
for i in ${GTWS_FILES_EXTRA}; do
local esrc=
IFS=':' read -ra ARR <<< "$i"
if [ -n "${ARR[1]}" ]; then
dst="${rname}/${ARR[1]}"
else
dst="${rname}/${ARR[0]}"
fi
if [ -n "${GTWS_REMOTE_IS_WS}" ]; then
esrc="${baserpath}/${dst}"
else
esrc="${baserpath%/git}"
fi
gtws_rcp "${esrc}/${ARR[0]}" "${dst}"
done
}
# gtws_project_clone_default ${GTWS_ORIGIN} ${GTWS_PROJECT} ${GTWS_PROJECT_VERSION} ${GTWS_WSNAME} [${SUBMODULE_BASE}]
#
# Clone a version of a project into ${GTWS_WSPATH} (which is the current working directory). This is the default version of this that clones <origin>/<project>/<version>/*
function gtws_project_clone_default {
local origin=$1
local project=$2
local version=$3
local name=$4
local basesmpath=$5
local opv=$(gtws_opvn "${origin}" "${project}" "${version}" "${name}")
local wspath=${PWD}
local repos=
local -A branches
if [ -z "${GTWS_PROJECT_REPOS}" ]; then
for i in "${opv}"/*; do
repos="$(basename $i) $repos"
branches[$i]=${version}
done
else
for i in ${GTWS_PROJECT_REPOS}; do
IFS=':' read -ra ARR <<< "$i"
repos="${ARR[0]} $repos"
if [ -n "${ARR[1]}" ]; then
branches[${ARR[0]}]=${ARR[1]}
else
branches[${ARR[0]}]=${version}
fi
done
fi
if [ -z "${basesmpath}" ] || [ ! -d "${basesmpath}" ]; then
basesmpath="${opv}"
fi
for repo in ${repos}; do
gtws_repo_clone "${opv}" "${repo}" "${branches[${repo}]}" "${basesmpath}"
done
# Copy per-WS settings, if they exist
gtws_cpdot "${opv%/git}/extra/ws" "${wspath}"
}
# gtws_repo_setup ${wspath} ${repo_path}
#
# The project can define gtws_repo_setup_local taking the same args to do
# project-specific setup. It will be called last.
#
# Post-clone setup for an individual repo
function gtws_repo_setup {
local wspath=$1
local rpath=$2
local savedir="${PWD}"
if [ ! -d "${rpath}" ]; then
return 0
fi
cd "${rpath}/src" 2>/dev/null \
|| cd ${rpath} \
|| die "Couldn't cd to ${rpath}" || return 1
maketags ${GTWS_MAKETAGS_OPTS} > /dev/null 2> /dev/null &
cd ${wspath} || die "Couldn't cd to ${wspath}" || return 1
mkdir -p "${wspath}/build/$(basename ${rpath})"
cd "${savedir}"
if [ -n "$(declare -F | grep "\<gtws_repo_setup_local\>")" ]; then
gtws_repo_setup_local "${wspath}" "${rpath}" \
|| die "local repo setup failed" || return 1
fi
}
# gtws_project_setup${GTWS_WSNAME} ${GTWS_ORIGIN} ${GTWS_PROJECT} ${GTWS_PROJECT_VERSION}
#
# The project can define gtws_project_setup_local taking the same args to do
# project-specific setup. It will be called last.
#
# Post clone setup of a workspace in ${GTWS_WSPATH} (which is PWD)
function gtws_project_setup {
local wsname=$1
local origin=$2
local project=$3
local version=$4
local wspath=${PWD}
local opv=$(gtws_opvn "${origin}" "${project}" "${version}" "placeholder")
for i in "${wspath}"/*; do
gtws_repo_setup "${wspath}" "${i}"
done
mkdir "${wspath}"/install
mkdir "${wspath}"/chroots
mkdir "${wspath}"/patches
if [ -n "$(declare -F | grep "\<gtws_project_setup_local\>")" ]; then
gtws_project_setup_local "${wsname}" "${origin}" "${project}" \
"${version}" || die "local project setup failed" || return 1
fi
}
# load_rc /path/to/workspace
#
# This should be in the workspace-level gtwsrc file
# Recursively load all RC files, starting at /
function load_rc {
local BASE=$(readlink -f "${1}")
# Load base RC first
debug_print "load_rc: Enter + Top: ${BASE}"
source "${HOME}"/.gtwsrc
while [ "${BASE}" != "/" ]; do
if [ -f "${BASE}"/.gtwsrc ]; then
load_rc "$(dirname ${BASE})"
debug_print "\tLoading ${BASE}/.gtwsrc"
source "${BASE}"/.gtwsrc
return 0
fi
BASE=$(readlink -f $(dirname "${BASE}"))
done
# Stop at /
return 1
}
# clear_env
#
# Clear the environment of GTWS_* except for the contents of GTWS_SAVEVARS.
# The default values for GTWS_SAVEVARS are below.
function clear_env {
local savevars=${GTWS_SAVEVARS:-"LOC PROJECT PROJECT_VERSION VERBOSE WSNAME"}
local verbose="${GTWS_VERBOSE}"
debug_print "savevars=$savevars"
# Reset prompt
if [ -n "${GTWS_SAVEPS1}" ]; then
PS1="${GTWS_SAVEPS1}"
fi
if [ -n "${GTWS_SAVEPATH}" ]; then
export PATH=${GTWS_SAVEPATH}
fi
unset LD_LIBRARY_PATH
unset PYTHONPATH
unset PROMPT_COMMAND
unset CDPATH
unset SDIRS
# Save variables
for i in ${savevars}; do
SRC=GTWS_${i}
DST=SAVE_${i}
debug_print "\t $i: ${DST} = ${!SRC}"
eval ${DST}=${!SRC}
done
# Clear GTWS evironment
for i in ${!GTWS*} ; do
if [ -n "${verbose}" ]; then
echo -e "unset $i" >&2
fi
unset $i
done
# Restore variables
for i in ${savevars}; do
SRC=SAVE_${i}
DST=GTWS_${i}
if [ -n "${verbose}" ]; then
echo -e "\t $i: ${DST} = ${!SRC}" >&2
fi
if [ -n "${!SRC}" ]; then
eval export ${DST}=${!SRC}
fi
unset ${SRC}
done
}
# save_env ${file} ${nukevars}
#
# Save the environment of GTWS_* to the give file, except for the variables
# given to nuke. The default values to nuke are given below.
function save_env {
local fname=${1}
local nukevars=${2:-"SAVEPATH ORIGIN WS_GUARD LOC SAVEPS1"}
debug_print "nukevars=$nukevars"
for i in ${!GTWS*} ; do
for j in ${nukevars}; do
if [ "${i}" == "GTWS_${j}" ]; then
debug_print "skipping $i"
continue 2
fi
done
debug_print "saving $i"
echo "export $i=\"${!i}\"" >> "${fname}"
done
}
# gtws_tmux_session_name ${PROJECT} ${VERSION} ${WSNAME} sesname
#
# Result will be in local variable sesname Or:
#
# sesname = $(gtws_tmux_session_name ${PROJECT} ${VERSION} ${WSNAME})
#
# Result will be in local variable sesname
#
# Get the tmux session name for a given workspace
function gtws_tmux_session_name {
local project=$1
local version=$2
local wsname=$3
local __resultvar=$4
local sesname="${project//./_}/${version//./_}/${wsname//./_}"
if [[ "$__resultvar" ]]; then
eval $__resultvar="'$sesname'"
else
echo "$sesname"
fi
}
# gtws_tmux_session_info ${SESSION_NAME} running attached
#
# Determine if a session is running, and if it is attached
#
# Result will be in local variables running and attached
#
# Test with:
# if $running ; then
# echo "is running"
# fi
function gtws_tmux_session_info {
local ses_name=$1
local __result_running=$2
local __result_attached=$3
local __num_ses=$(tmux ls | grep "^${ses_name}" | wc -l)
local __attached=$(tmux ls | grep "^${ses_name}" | grep attached)
echo "$ses_name ses=${__num_ses}"
if [[ "$__result_running" ]]; then
if [ "${__num_ses}" != "0" ]; then
eval $__result_running="true"
else
eval $__result_running="false"
fi
fi
if [[ "$__result_attached" ]]; then
if [ -n "${__attached}" ]; then
eval $__result_attached="true"
else
eval $__result_attached="false"
fi
fi
}
# gtws_tmux_kill ${BASENAME}
#
# Kill all sessiont matching a pattern
function gtws_tmux_kill {
local basename=$1
local old_sessions=$(tmux ls 2>/dev/null | fgrep "${basename}" | cut -f 1 -d:)
for session in ${old_sessions}; do
tmux kill-session -t "${session}"
done
}
# gtws_tmux_cleanup
#
# Clean up defunct tmux sessions
function gtws_tmux_cleanup {
local old_sessions=$(tmux ls 2>/dev/null | egrep "^[0-9]{14}.*[0-9]+\)$" | cut -f 1 -d:)
for session in ${old_sessions}; do
tmux kill-session -t "${session}"
done
}
# gtws_tmux_attach ${SESSION_NAME}
#
# Attach to a primary session. It will remain after detaching.
function gtws_tmux_attach {
local ses_name=$1
tmux attach-session -t "${ses_name}"
}
# gtws_tmux_slave ${SESSION_NAME}
#
# Create a secondary session attached to the primary session. It will exit it
# is detached.
function gtws_tmux_slave {
local ses_name=$1
# Session is is date and time to prevent conflict
local session=`date +%Y%m%d%H%M%S`
# Create a new session (without attaching it) and link to base session
# to share windows
tmux new-session -d -t "${ses_name}" -s "${session}"
# Attach to the new session
gtws_tmux_attach "${session}"
# When we detach from it, kill the session
tmux kill-session -t "${session}"
}
function cdorigin() {
if [ -n "$(declare -F | grep "gtws_project_cdorigin")" ]; then
gtws_project_cdorigin $@
else
gtws_cdorigin $@
fi
}
function gtws_get_origin {
local opv=$1
local target=$2
local __origin=
local __resultvar=$3
# If it's a git repo with a local origin, use that.
__origin=$(git config --get remote.origin.url)
if [ ! -d "${__origin}" ]; then
__origin="${__origin}.git"
fi
if [ ! -d "${__origin}" ]; then
# Try to figure it out
if [ ! -d "${opv}" ]; then
die "No opv for $target" || return 1
fi
find_git_repo "${opv}" "${target}" __origin || return 1
fi
if [[ "$__resultvar" ]]; then
eval $__resultvar="'$__origin'"
else
echo "$__origin"
fi
}
function gtws_cdorigin() {
local opv=$(gtws_opvn "${GTWS_ORIGIN}" "${GTWS_PROJECT}" "${GTWS_PROJECT_VERSION}" "${GTWS_WSNAME}")
local gitdir=""
local target=""
if [ -n "$1" ]; then
target="$@"
else
git_top_dir gitdir || return 1
target=$(basename $gitdir)
fi
gtws_get_origin $opv $target origin || return 1
cd "${origin}"
}
# Copy files to another machine in the same workspace
function wsrcp {
local target="${!#}"
local length=$(($#-1))
local base=${PWD}
if [ -z "${1}" -o -z "${2}" ]; then
echo "usage: ${FUNCNAME} <path> [<path>...] <target>"
return 1
fi
for path in "${@:1:$length}"; do
gtws_rcp "${path}" "${target}:${base}/${path}"
done
}
# Override "cd" inside the workspace to go to GTWS_WSPATH by default
function cd {
if [ -z "$@" ]; then
cd "${GTWS_WSPATH}"
else
builtin cd $@
fi
}
# Generate diffs/interdiffs for changes and ship to WS on other boxes
function gtws_interdiff {
local targets=$@
local target=
local savedir=${PWD}
local topdir=$(git_top_dir)
local repo=$(basename ${topdir})
local mainpatch="${GTWS_WSPATH}/patches/${repo}-full.patch"
local interpatch="${GTWS_WSPATH}/patches/${repo}-incremental.patch"
if [ -z "${targets}" ]; then
echo "Usage: ${FUNCNAME} <targethost>"
die "Must give targethost" || return 1
fi
cd "${topdir}"
if [ -f "${mainpatch}" ]; then
git diff | interdiff "${mainpatch}" - > "${interpatch}"
fi
git diff > "${mainpatch}"
for target in ${targets}; do
gtws_rcp "${mainpatch}" "${interpatch}" \
"${target}:${GTWS_WSPATH}/patches"
done
cd "${savedir}"
}
function gtws_debug {
local cmd=$1
if [ -z "${cmd}" ]; then
echo "Must give a command"
echo
die "${FUNCNAME} <cmd-path>" || return 1
fi
local cmdbase=$(basename $cmd)
local pid=$(pgrep "${cmdbase}")
ASAN_OPTIONS="abort_on_error=1" cgdb ${cmd} ${pid}
}
# remote_cmd "${target}" "${command}" output
#
# Result will be in local variable output Or:
#
# output = $(remote_cmd "${target}" "${command}")
#
# Result will be in local variable output
#
# Run a command remotely and capture sdtout. Make sure to quote the command
# appropriately.
remote_cmd() {
local target=$1
local cmd=$2
local __resultvar=$3
local output=
if [ -z "${GTWS_VERBOSE}" ]; then
output=$(ssh "${target}" "${cmd}" 2>/dev/null)
else
output=$(ssh "${target}" "${cmd}")
fi
local ret=$?
if [[ "$__resultvar" ]]; then
eval $__resultvar="'$output'"
else
echo "${output}"
fi
return ${ret}
}