-
Notifications
You must be signed in to change notification settings - Fork 3
/
gitstorage.sh
1346 lines (828 loc) · 25.2 KB
/
gitstorage.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
993
994
995
996
997
998
999
1000
#!/bin/bash
_SHELL="$(ps -p $$ -o comm=)"; # bash || sh || zsh
_SHELL="${_SHELL##*/}"
case ${_SHELL} in
zsh)
_DIR="$( cd "$( dirname "${(%):-%N}" )" && pwd -P )"
_0="$( basename "${(%):-%N}" )"
_SCRIPT="${(%):-%N}"
_BINARY="/bin/zsh"
_PWD="$(pwd)"
;;
sh)
_DIR="$( cd "$( dirname "${0}" )" && pwd -P )"
_0="$( basename "${0}" )"
_SCRIPT="${0}"
_BINARY="/bin/sh"
_PWD="$(pwd)"
;;
*)
_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd -P )"
_0="$( basename "${BASH_SOURCE[0]}" )"
_SCRIPT="${BASH_SOURCE[0]}"
_BINARY="/bin/bash"
_PWD="$(pwd)"
;;
esac
LOCKDIR=".git/xxlockdir"
LOCKDIRABS="${_PWD}/.git/xxlockdir"
realpath . &> /dev/null
if [ "${?}" != "0" ]; then
{ red "realpath is not installed run: brew install coreutils"; } 2>&3
exit 1;
fi
function checkIfGITSTORAGESOURCEdefined {
if [ "${GITSTORAGESOURCE}" = "" ]; then
echo "${0} error: can't operate when GITSTORAGESOURCE env var is not defined, either globally or in local .git/gitstorage-config.sh"
exit 1
fi
}
if [ "${1}" = "" ] || [ "${1}" = "--help" ]; then
cat << EOF
/bin/bash ${0} isinsync
/bin/bash ${0} diff
/bin/bash ${0} pull
/bin/bash ${0} push
/bin/bash ${0} url
/bin/bash ${0} move git@xxx:project/source-repo.git git@yyy/target-repo.git
/bin/bash ${0} init
# generate gitstorage-config.sh for the first time for given directory (dir have to contain .git)
/bin/bash ${0} add [file|directory]
# register files from path in gitstorage-config.sh
/bin/bash ${0} lock [file|directory]
# add files given by path to .git/xxlockdir and register in gitstorage-config.sh
/bin/bash ${0} -c "gitstorage-config.sh"
# you can specify different config
/bin/bash ${0} state
# will look for each ${_CONFIG} anywhere in current PWD and check if files pointed by it are up to date
/bin/bash ${0} existing
# will look for all .git directories and try to determine if there is gitstorage-config.sh for it
# if yes then it will pull it with all tools around it
/bin/bash ${0} clean
# will look for all gitstorage-config.sh and remove them in current PWD
EOF
exit 0
fi
#GITSTORAGESOURCE="[email protected]:xxx/gitstorage.git"
exec 3<> /dev/null
function green {
printf "\e[32m${1}\e[0m\n"
}
function red {
printf "\e[31m${1}\e[0m\n"
}
function yellow {
printf "\e[33m${1}\e[0m\n"
}
trim() {
local var="${*}"
# remove leading whitespace characters
var="${var#"${var%%[![:space:]]*}"}"
# remove trailing whitespace characters
var="${var%"${var##*[![:space:]]}"}"
echo -n "${var}"
}
_CONFIG_FILE="gitstorage-config.sh"
_CONFIG_DIR=".git"
_CONFIG="${_CONFIG_DIR}/${_CONFIG_FILE}";
_FORCE="0"
_BACKUP="0"
_RESTORE="0"
PARAMS=""
REMOTE="origin";
GITSTORAGE_CORE_REPOSITORY="[email protected]:stopsopa/gitstorage.git"; # @substitute
while (( "${#}" )); do
case "${1}" in
-r|--remote)
if [ "${2}" = "" ]; then
{ red "$(basename "${0}") error: --remote value can't be empty"; } 2>&3
exit 1;
fi
REMOTE="${2}";
shift 2;
;;
-s|--core_repository)
if [ "${2}" = "" ]; then # PUT THIS CHECKING ALWAYS HERE IF YOU WAITING FOR VALUE
{ red "$(basename "${0}") error: --core_repository value can't be empty"; } 2>&3
exit 1; # optional
fi # optional
GITSTORAGE_CORE_REPOSITORY="${2}";
shift 2;
;;
--yes)
_FORCE="1";
shift;
;;
-c|--config)
_CONFIG="${2}";
shift 2;
;;
--backup)
_BACKUP="1";
shift;
;;
--restore)
_RESTORE="1";
shift;
;;
-*|--*=) # unsupported flags
{ red "$(basename "${0}") error: Unsupported flag ${1}"; } 2>&3
exit 1;
;;
*) # preserve positional arguments
if [ "${PARAMS}" = "" ]; then
PARAMS="\"${1}\""
else
PARAMS="${PARAMS} \"${1}\""
fi
shift;
;;
esac
done
_P="$(pwd)"
# set positional arguments in their proper place
eval set -- "${PARAMS}"
MODE="${1}"
shift;
function cloneTarget {
_TARGETGITDIR="";
while true
do
_TARGETGITDIR="$(openssl rand -hex 2)"
_TARGETGITDIR="${PWD}/${_TARGETGITDIR}"
echo "_TARGETGITDIR: ${_TARGETGITDIR}"
if ! [ -d "${_TARGETGITDIR}" ]; then
break;
fi
done
function cleanup {
{ yellow "\n$(basename "${0}") cleaning ${_TARGETGITDIR}"; } 2>&3
rm -rf "${_TARGETGITDIR}"
}
trap cleanup EXIT
mkdir -p "${_TARGETGITDIR}"
}
function init {
if [ ! -d ".git" ]; then
cat <<EEE
$(basename "${0}") error: you have to execute it in directory where .git exist
EEE
return 1
fi
GITSTORAGESCRIPT="gitstorage-core.sh"
GITSTORAGESCRIPT_WITH_RELATIVE_DIR=".git/${GITSTORAGESCRIPT}"
unlink "${GITSTORAGESCRIPT_WITH_RELATIVE_DIR}"
if [ -d "${REPO_DIR}" ] && [ -f "${REPO_DIR}/${GITSTORAGESCRIPT}" ]; then
{ yellow "linking ${GITSTORAGESCRIPT_WITH_RELATIVE_DIR}"; } 2>&3
(cd .git && ln -s "${REPO_DIR}/${GITSTORAGESCRIPT}" "${GITSTORAGESCRIPT}")
if [ ! -L "${GITSTORAGESCRIPT_WITH_RELATIVE_DIR}" ]; then
{ red "${0} error: can't download file ${GITSTORAGESCRIPT_WITH_RELATIVE_DIR}"; } 2>&3
exit 1
fi
else
{ yellow "downloading ${GITSTORAGESCRIPT_WITH_RELATIVE_DIR}"; } 2>&3
/bin/bash .git/wget.sh "${PROD}/${GITSTORAGESCRIPT}" "${GITSTORAGESCRIPT_WITH_RELATIVE_DIR}"
if [ ! -f "${GITSTORAGESCRIPT_WITH_RELATIVE_DIR}" ]; then
{ red "${0} error: can't download file ${GITSTORAGESCRIPT_WITH_RELATIVE_DIR}"; } 2>&3
exit 1
fi
fi
/bin/bash ".git/gitstorage-core.sh"
}
if [ "${MODE}" = "add" ]; then
if [ ! -f "${_CONFIG}" ]; then
cat <<EEE
file ${_CONFIG} doesn't exist
EEE
exit 1
fi
LIST="$(find "${1}" -type f)"
node "${_DIR}/gitstorage.cjs" "${_PWD}" "${_CONFIG}" "${LIST}"
exit 0
fi
if [ "${MODE}" = "lock" ]; then
set +x
if [ ! -d "${LOCKDIR}" ]; then
cat <<EEE
directory "${LOCKDIR}" doesn't exist
create it first:
mkdir -p "${LOCKDIR}"
EEE
exit 1
fi
LIST="$(find "${1}" -type f 2> /dev/null)"
if [ "${LIST}" = "" ]; then
cat <<EEE
directory "${LOCKDIR}" is empty
add some files to it first:
gits lock src/main/resources/application-local.yaml
and register them with gitstorage-config.sh
gits add "${LOCKDIR}"
EEE
else
while read -r FILE
do
PD="$(dirname "${FILE}")"
cat <<EEE
mkdir -p "${LOCKDIR}/${PD}"
cp "${FILE}" "${LOCKDIR}/${FILE}"
EEE
mkdir -p "${LOCKDIR}/${PD}"
cp "${FILE}" "${LOCKDIR}/${FILE}"
done <<< "${LIST}"
cat <<EEE
now run:
gits add "${LOCKDIR}"
EEE
fi
exit 0
fi
if [ "${MODE}" = "init" ]; then
init
exit 0
fi
if [ "${MODE}" = "state" ]; then
LIST="$(find . -type d -name 'node_modules' -prune -o -type f -name "${_CONFIG_FILE}" -print | grep "/.git/${_CONFIG_FILE}")"
# LIST="$(find SPECIAL_storage -type d -name 'node_modules' -prune -o -type f -name "gitstorage-config.sh" -print | grep "/.git/gitstorage-config.sh")"
LIST=$(cat <<END
${LIST}
./SPECIAL_storage/SPECIAL_storage/.git/${_CONFIG_FILE}
END
);
LIST="$(trim "${LIST}")"
LIST="$(echo "${LIST}" | sort -u)" # deduplicate
# echo ">${LIST}<"
# exit
LIST_FILTERED=();
while read -r xxx
do
if [ "${xxx}" != "" ]; then
# DIR="$(dirname "${xxx}")"
# CORE="${DIR}/gitstorage-core.sh"
# if [ -e "${CORE}" ]; then
LIST_FILTERED+=("${xxx}")
# fi
fi
done <<< "${LIST}"
if [ "${#LIST_FILTERED[@]}" = "0" ]; then
cat <<EEE
nothing found
EEE
else
{ green "===================================================================================================================="; } 2>&3
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
{ green "===================================================================================================================="; } 2>&3
cloneTarget
COUNT_SUCCESS="0"
COUNT_ERROR="0"
COUNT="${#LIST_FILTERED[@]}"
COUNT="$(trim "${COUNT}")"
I="0"
for xxx in "${LIST_FILTERED[@]}"
do
cd "$_P"
I="$(($I + 1))"
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ${I} : ${COUNT} >${xxx}<"
DIR="$(dirname "${xxx}")"
PROJECT_DIR="$(dirname "${DIR}")"
cat <<EEE
cd "$(realpath "${PROJECT_DIR}")"
gits diff
EEE
unset GITSTORAGELIST;
XXX_REAL="$(realpath "${xxx}")"
cd "${PROJECT_DIR}"
source "${XXX_REAL}";
cd "$_P"
_COUNT="${#GITSTORAGELIST[@]}";
if [ ${_COUNT} -lt 1 ] ; then
{ red "$(basename "${0}") error: list GITSTORAGELIST in config '${xxx}' shouldn't be empty"; } 2>&3
exit 1;
fi
checkIfGITSTORAGESOURCEdefined
GITSTORAGETARGETDIR="$(echo "${GITSTORAGESOURCE}"| sed -E 's/\//__/g')"
CLONED_TARGET_DIR="${_TARGETGITDIR}/${GITSTORAGETARGETDIR}"
if [ -d "${CLONED_TARGET_DIR}" ]; then
{ green "directory '${CLONED_TARGET_DIR}' for '${xxx}' already exist"; } 2>&3
else
mkdir -p "${CLONED_TARGET_DIR}"
{ green "cloning directory '${CLONED_TARGET_DIR}' for '${xxx}'"; } 2>&3
set -e
cd "${CLONED_TARGET_DIR}"
git clone "${GITSTORAGESOURCE}" .
cd "$_P"
set +e
echo cloned..
fi
cd "${CLONED_TARGET_DIR}"
echo before checkout vvv
git clean -df
git checkout .
echo after checkout ^^^
cd "$_P"
# ls -la "${CLONED_TARGET_DIR}/"
cd "${PROJECT_DIR}"
# echo "PROJECT_DIR: ${PROJECT_DIR}"
MISSING="0"
for index in "${GITSTORAGELIST[@]}"; do
_S="${index%%::*}"
_T="${index##*::}"
# we are working here form main directory of each project, so if path is not starting from absolute path (/) or home directory (~) then
# adjust it to project directory, because each path in GITSTORAGELIST should be declared relative to gitstorage-config.sh
if ! [[ ${_S} =~ ^~{0,1}/.* ]]; then
_S="${_CONFIG_DIR}/${_S}"
fi
_S="$(echo "${_S}" | sed -E 's/( )/\\\1/g')"
eval _S="${_S}"
if [ -f "${_S}" ]; then
_T="${CLONED_TARGET_DIR}/${_T}"
_TMPDIR="$(dirname "${_T}")"
mkdir -p "${_TMPDIR}";
cp "${_S}" "${_T}"
else
MISSING="1"
{ red "$(basename "${0}") error: source file (state) '${_S}' doesn't exist"; } 2>&3
fi
# echo "moving _S>${_S}< to _T>${_T}<"
done
if [ "${MISSING}" = "1" ]; then
{ red "\n files are not in sync - at least one source file is missing\n"; } 2>&3
else
cd "${CLONED_TARGET_DIR}"
DIFFSTATUS="$(git status -s)"
cd "$_P"
if [ "${DIFFSTATUS}" = "" ] ; then
{ green " files are in sync"; } 2>&3
COUNT_SUCCESS="$((${COUNT_SUCCESS} + 1))"
else
{ red " files are not in sync"; } 2>&3
COUNT_ERROR="$((${COUNT_ERROR} + 1))"
cd "${CLONED_TARGET_DIR}"
git status
cd "$_P"
fi
fi
done
echo ""
{ green "COUNT_SUCCESS : ${COUNT_SUCCESS}"; } 2>&3
{ red "COUNT_ERROR : ${COUNT_ERROR}"; } 2>&3
echo ""
cd "$_P"
fi
exit 0;
fi
if [ "${MODE}" = "existing" ]; then
git help > /dev/null;
if [ "${?}" != "0" ]; then
{ red "git is not installed"; } 2>&3
exit 3
fi
LIST="$(find . -type d -name 'node_modules' -prune -o -type d -name ".git" -print)"
LIST="$(trim "${LIST}")"
LIST_FILTERED=();
while read -r xxx
do
cd "${_P}"
if [ "${xxx}" != "" ]; then
TMP="${xxx}/config"
if [ -f "${TMP}" ]; then
DIR="$(dirname "${xxx}")"
cd "${DIR}"
REPOURL="$(git config --get "remote.${REMOTE}.url")"
if [ "${?}" = "0" ]; then
LIST_FILTERED+=("${xxx}")
else
{ red "git config --get \"remote.${REMOTE}.url\" - failed - tested for '${xxx}'"; } 2>&3
fi
else
{ red "${TMP} is not a file - tested for '${xxx}'"; } 2>&3
fi
fi
done <<< "${LIST}"
cd "${_P}"
if [ "${#LIST_FILTERED[@]}" = "0" ]; then
cat <<EEE
nothing found
EEE
else
cloneTarget
set -e
cd "${_TARGETGITDIR}"
git clone "${GITSTORAGE_CORE_REPOSITORY}" .
cd "$_P"
set +e
COUNT="${#LIST_FILTERED[@]}"
COUNT="$(trim "${COUNT}")"
I="0"
for xxx in "${LIST_FILTERED[@]}"
do
# echo "xxx: >${xxx}<"
cd "$_P"
I="$(($I + 1))"
echo -e ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ${I} : ${COUNT} >${xxx}<"
DIR="${xxx}"
PROJECT_DIR="$(dirname "${DIR}")"
cat <<EEE
cd "$(realpath "${PROJECT_DIR}")"
EEE
cd "${xxx}"
REPOURL="$(git config --get "remote.${REMOTE}.url")"
GITSTORAGETARGETDIR="$(echo "${REPOURL}"| sed -E 's/\//__/g')"
cd "$_P"
# echo "GITSTORAGETARGETDIR: >${GITSTORAGETARGETDIR}<"
GITDIR_CONFIGFILE="${xxx}/${_CONFIG_FILE}"
# echo "GITDIR_CONFIGFILE: >${GITDIR_CONFIGFILE}<"
if [ -f "${GITDIR_CONFIGFILE}" ]; then
{ green "${GITDIR_CONFIGFILE} is already present - initialization not needed"; } 2>&3
else
# { yellow "try to download ${GITDIR_CONFIGFILE}"; } 2>&3
_TARGETCONFIG="${_TARGETGITDIR}/${GITSTORAGETARGETDIR}/${_CONFIG_FILE}"
if [ -f "${_TARGETCONFIG}" ]; then
cp "${_TARGETCONFIG}" "${xxx}/"
cd "${PROJECT_DIR}"
init
cd "$_P"
{ green "${GITDIR_CONFIGFILE} cloned from ${GITSTORAGE_CORE_REPOSITORY}/${GITSTORAGETARGETDIR}"; } 2>&3
else
{ red "${_TARGETCONFIG} doesn't exist"; } 2>&3
fi
fi
done
fi
exit 0;
fi
if [ "${MODE}" = "clean" ]; then
set +e
LIST="$(find . -type d -name 'node_modules' -prune -o -type f -name "${_CONFIG_FILE}" -print | grep "/.git/${_CONFIG_FILE}")"
set -e
LIST="$(trim "${LIST}")"
if [ "${LIST}" = "" ]; then
cat <<EEE
none of ${_CONFIG_FILE} found
EEE
else
COUNT="$(echo $LIST | wc -l)"
COUNT="$(trim "${COUNT}")"
I="0"
while read -r xxx
do
cd "$_P"
I="$(($I + 1))"
echo -e "\n\n>>>> before >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ${I} : ${COUNT} >${xxx}<"
DIR="$(dirname "${xxx}")"
cd "${DIR}"
pwd
ls -la
rm "${_CONFIG_FILE}"
rm "gitstorage.sh"
rm "gitstorage-core.sh"
echo -e "\n\n>>>> after >>>>"
ls -la
done <<< "${LIST}"
fi
exit 0
fi
if [ "${_CONFIG}" = "" ]; then
{ red "$(basename "${0}") error: --config value can't be empty"; } 2>&3
exit 1;
fi
if ! [ -f "${_CONFIG}" ]; then
{ red "$(basename "${0}") error: --config file '${_CONFIG}' doesn't exist"; } 2>&3
exit 1;
fi
source "${_CONFIG}";
_GITHUB="^git@github"
checkIfGITSTORAGESOURCEdefined
if [[ ${GITSTORAGESOURCE} =~ ${_GITHUB} ]]; then
URL="$(echo "${GITSTORAGESOURCE}" | sed -E "s/^git@([^:]+):([^\/]+)\/([^\.]+).*/https:\/\/\1\/\2\/\3/")/tree/master/${GITSTORAGETARGETDIR}"
else
URL="$(echo "${GITSTORAGESOURCE}" | sed -E "s/^git@([^:]+):([^\/]+)\/([^\.]+).*/https:\/\/\1\/\2\/\3/")/raw/master/${GITSTORAGETARGETDIR}"
fi
# this will check if array exist and it will count it,
# if it doesn't exist and can't be counted then this will return 0
_COUNT="${#GITSTORAGELIST[@]}";
if [ ${_COUNT} -lt 1 ] ; then
{ red "$(basename "${0}") error: list GITSTORAGELIST in config '${_CONFIG}' shouldn't be empty"; } 2>&3
exit 1;
fi
TEST="^(url|isinsync|diff|pull|push|backup|restore|move|state)$"
if ! [[ ${MODE} =~ ${TEST} ]]; then
{ red "$(basename "${0}") error: mode ${MODE} don't match pattern ${TEST}"; } 2>&3
exit 1;
fi
TEST="^(backup|restore)$"
if [[ ${MODE} =~ ${TEST} ]]; then
if [ "${1}" = "" ]; then
{ red "$(basename "${0}") error: mode ${MODE} - directory not specified"; } 2>&3
fi
mkdir -p "${1}";
fi
if [ "${MODE}" = "url" ]; then
echo "final url ${URL}";
exit 0
fi
if [ "${MODE}" = "move" ]; then
function deslash {
echo "$(echo "${1}"| sed -E 's/\//__/g')"
}
echo "${URL}"
SOURCE="${1}"
FROM="$(deslash "${SOURCE}")"
shift;
TARGET="${1}"
TO="$(deslash "${TARGET}")"
shift;
_GITHUB="^git@github"
if [[ ${GITSTORAGESOURCE} =~ ${_GITHUB} ]]; then
BASEURL="$(echo "${GITSTORAGESOURCE}" | sed -E "s/^git@([^:]+):([^\/]+)\/([^\.]+).*/https:\/\/\1\/\2\/\3/")"
else
BASEURL="$(echo "${GITSTORAGESOURCE}" | sed -E "s/^git@([^:]+):([^\/]+)\/([^\.]+).*/https:\/\/\1\/\2\/\3/")"
fi
RENAMEDIRLINK="${BASEURL}/edit/master/${FROM}/${_CONFIG_FILE}"
EDITCONFIG="${BASEURL}/edit/master/${TO}/${_CONFIG_FILE}"
colorred=$(tput setaf 2)
coloryellow=$(tput setaf 3)
colorreset=$(tput sgr0)
cat <<DOC
Go to repository and file:
${colorred}${RENAMEDIRLINK}${colorreset}
rename directory:
${colorred}${FROM}${colorreset}
to:
${colorred}${TO}${colorreset}
manual how to do it via GitHub:
${coloryellow}https://github.blog/2013-03-15-moving-and-renaming-files-on-github${colorreset}
AND ALSO IN ONE MOVE, while you are on that page, change in gitstorage-config.sh itself variable:
GITSTORAGETARGETDIR="${FROM}"
to
GITSTORAGETARGETDIR="${TO}"
You can check everything if it's ok on this url after change:
${EDITCONFIG}
That would be all about config file and it's location, now we have to move all other files in directory ${FROM}.
In order to do that open terminal and paste:
rm -rf ____temporary_directory_remove_later
mkdir ____temporary_directory_remove_later
cd ____temporary_directory_remove_later
git clone "${GITSTORAGESOURCE}" .
mv ${FROM}/* ${TO}/
mv ${FROM}/.* ${TO}/
git add .
git commit -m "gits move ${SOURCE} ${TARGET}"
git push
cd ..
rm -rf ____temporary_directory_remove_later
and then visit again
${EDITCONFIG}
go one directory up and review all files, espcecially if there is gitstorage-config.sh and next to it other files
DOC
exit 0
fi
if [ "${MODE}" = "backup" ]; then
_TARGETGITDIR="${1}"
for index in "${GITSTORAGELIST[@]}"; do
_S="${index%%::*}"
if [ -f "${_S}" ]; then
_T="${_TARGETGITDIR}/${_S}"
_TMPDIR="$(dirname "${_T}")"
mkdir -p "${_TMPDIR}";
{ yellow "'${_S}' -> '${_T}'"; } 2>&3
cp "${_S}" "${_T}"
else
{ red "$(basename "${0}") error: source file1 '${_S}' doesn't exist"; } 2>&3
fi
done
{ green "\n files backup created\n"; } 2>&3
exit 0;
fi
if [ "${MODE}" = "restore" ]; then
_TARGETGITDIR="${1}"
for index in "${GITSTORAGELIST[@]}"; do
_T="${index%%::*}"
_S="${_TARGETGITDIR}/${_T}"
_TT="${_T}"
# we are working here form main directory of each project, so if path is not starting from absolute path (/) or home directory (~) then
# adjust it to project directory, because each path in GITSTORAGELIST should be declared relative to gitstorage-config.sh
if ! [[ ${_T} =~ ^~{0,1}/.* ]]; then
_TT="${_CONFIG_DIR}/${_T}"
fi
_TT="$(echo "${_TT}" | sed -E 's/( )/\\\1/g')"
eval _TT="${_TT}"
if [ -f "${_S}" ]; then
_TMPDIR="$(dirname "${_TT}")"
mkdir -p "${_TMPDIR}";
{ yellow "'${_S}' -> '${_T}'"; } 2>&3
cp "${_S}" "${_TT}"
else
{ red "file '${_S}' doesn't exist in backup directory"; } 2>&3
fi
done
{ green "\n files restored\n"; } 2>&3
exit 0;
fi
cloneTarget
if [ "${MODE}" = "isinsync" ]; then
checkIfGITSTORAGESOURCEdefined
set -e
cd "${_TARGETGITDIR}"