forked from yadm-dev/yadm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yadm
executable file
·2344 lines (1966 loc) · 59.6 KB
/
yadm
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/sh
# yadm - Yet Another Dotfiles Manager
# Copyright (C) 2015-2023 Tim Byrne
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# shellcheck shell=bash
# execute script with bash (shebang line is /bin/sh for portability)
if [ -z "$BASH_VERSION" ]; then
[ "$YADM_TEST" != 1 ] && exec bash "$0" "$@"
fi
VERSION=3.2.2
YADM_WORK="$HOME"
YADM_DIR=
YADM_DATA=
YADM_LEGACY_DIR="${HOME}/.yadm"
YADM_LEGACY_ARCHIVE="files.gpg"
# these are the default paths relative to YADM_DIR
YADM_CONFIG="config"
YADM_ENCRYPT="encrypt"
YADM_BOOTSTRAP="bootstrap"
YADM_HOOKS="hooks"
YADM_ALT="alt"
# these are the default paths relative to YADM_DATA
YADM_REPO="repo.git"
YADM_ARCHIVE="archive"
HOOK_COMMAND=""
FULL_COMMAND=""
GPG_PROGRAM="gpg"
OPENSSL_PROGRAM="openssl"
GIT_PROGRAM="git"
AWK_PROGRAM=("gawk" "awk")
GIT_CRYPT_PROGRAM="git-crypt"
TRANSCRYPT_PROGRAM="transcrypt"
J2CLI_PROGRAM="j2"
ENVTPL_PROGRAM="envtpl"
MST_PROGRAM="mst"
ESH_PROGRAM="esh"
LSB_RELEASE_PROGRAM="lsb_release"
OS_RELEASE="/etc/os-release"
PROC_VERSION="/proc/version"
OPERATING_SYSTEM="Unknown"
ENCRYPT_INCLUDE_FILES="unparsed"
LEGACY_WARNING_ISSUED=0
INVALID_ALT=()
GPG_OPTS=()
OPENSSL_OPTS=()
# flag causing path translations with cygpath
USE_CYGPATH=0
# flag when something may have changes (which prompts auto actions to be performed)
CHANGES_POSSIBLE=0
# flag when a bootstrap should be performed after cloning
# 0: skip auto_bootstrap, 1: ask, 2: perform bootstrap, 3: prevent bootstrap
DO_BOOTSTRAP=0
function main() {
require_git
# capture full command, for passing to hooks
# the parameters will be space delimited and
# spaces, tabs, and backslashes will be escaped
_tab=$'\t'
for param in "$@"; do
param="${param//\\/\\\\}"
param="${param//$_tab/\\$_tab}"
param="${param// /\\ }"
_fc+=( "$param" )
done
FULL_COMMAND="${_fc[*]}"
# create the YADM_DIR & YADM_DATA if they doesn't exist yet
[ -d "$YADM_DIR" ] || mkdir -p "$YADM_DIR"
[ -d "$YADM_DATA" ] || mkdir -p "$YADM_DATA"
# parse command line arguments
local retval=0
internal_commands="^(alt|bootstrap|clean|clone|config|decrypt|encrypt|enter|git-crypt|help|--help|init|introspect|list|perms|transcrypt|upgrade|version|--version)$"
if [ -z "$*" ] ; then
# no argumnts will result in help()
help
elif [[ "$1" =~ $internal_commands ]] ; then
# for internal commands, process all of the arguments
YADM_COMMAND="${1//-/_}"
YADM_COMMAND="${YADM_COMMAND/__/}"
YADM_ARGS=()
shift
# commands listed below do not process any of the parameters
if [[ "$YADM_COMMAND" =~ ^(enter|git_crypt)$ ]] ; then
YADM_ARGS=("$@")
else
while [[ $# -gt 0 ]] ; do
key="$1"
case $key in
-a) # used by list()
LIST_ALL="YES"
;;
-d) # used by all commands
DEBUG="YES"
;;
-f) # used by init(), clone() and upgrade()
FORCE="YES"
;;
-l) # used by decrypt()
DO_LIST="YES"
[[ "$YADM_COMMAND" =~ ^(clone|config)$ ]] && YADM_ARGS+=("$1")
;;
-w) # used by init() and clone()
YADM_WORK="$(qualify_path "$2" "work tree")"
shift
;;
*) # any unhandled arguments
YADM_ARGS+=("$1")
;;
esac
shift
done
fi
[ ! -d "$YADM_WORK" ] && error_out "Work tree does not exist: [$YADM_WORK]"
HOOK_COMMAND="$YADM_COMMAND"
invoke_hook "pre"
$YADM_COMMAND "${YADM_ARGS[@]}"
else
# any other commands are simply passed through to git
HOOK_COMMAND="$1"
invoke_hook "pre"
git_command "$@"
retval="$?"
fi
# process automatic events
auto_alt
auto_perms
auto_bootstrap
exit_with_hook $retval
}
# ****** Alternate Processing ******
function score_file() {
src="$1"
tgt="${src%%##*}"
conditions="${src#*##}"
if [ "${tgt#"$YADM_ALT/"}" != "${tgt}" ]; then
tgt="${YADM_BASE}/${tgt#"$YADM_ALT/"}"
fi
score=0
IFS=',' read -ra fields <<< "$conditions"
for field in "${fields[@]}"; do
label=${field%%.*}
value=${field#*.}
[ "$field" = "$label" ] && value="" # when .value is omitted
# extension isn't a condition and doesn't affect the score
if [[ "$label" =~ ^(e|extension)$ ]]; then
continue
fi
score=$((score + 1000))
# default condition
if [[ "$label" =~ ^(default)$ ]]; then
score=$((score + 0))
# variable conditions
elif [[ "$label" =~ ^(a|arch)$ ]]; then
if [ "$value" = "$local_arch" ]; then
score=$((score + 1))
else
score=0
return
fi
elif [[ "$label" =~ ^(o|os)$ ]]; then
if [ "$value" = "$local_system" ]; then
score=$((score + 2))
else
score=0
return
fi
elif [[ "$label" =~ ^(d|distro)$ ]]; then
if [ "${value/\ /_}" = "${local_distro/\ /_}" ]; then
score=$((score + 4))
else
score=0
return
fi
elif [[ "$label" =~ ^(f|distro_family)$ ]]; then
if [ "${value/\ /_}" = "${local_distro_family/\ /_}" ]; then
score=$((score + 8))
else
score=0
return
fi
elif [[ "$label" =~ ^(c|class)$ ]]; then
if in_list "$value" "${local_classes[@]}"; then
score=$((score + 16))
else
score=0
return
fi
elif [[ "$label" =~ ^(h|hostname)$ ]]; then
if [ "$value" = "$local_host" ]; then
score=$((score + 32))
else
score=0
return
fi
elif [[ "$label" =~ ^(u|user)$ ]]; then
if [ "$value" = "$local_user" ]; then
score=$((score + 64))
else
score=0
return
fi
elif [[ "$label" =~ ^(a|arch)$ ]]; then
if [ "$value" = "$local_arch" ]; then
score=$((score + 32))
else
score=0
return
fi
# templates
elif [[ "$label" =~ ^(t|template|yadm)$ ]]; then
score=0
cmd=$(choose_template_cmd "$value")
if [ -n "$cmd" ]; then
record_template "$tgt" "$cmd" "$src"
else
debug "No supported template processor for template $src"
[ -n "$loud" ] && echo "No supported template processor for template $src"
fi
return 0
# unsupported values
else
if [[ "${src##*/}" =~ .\#\#. ]]; then
INVALID_ALT+=("$src")
fi
score=0
return
fi
done
record_score "$score" "$tgt" "$src"
}
function record_score() {
score="$1"
tgt="$2"
src="$3"
# record nothing if the score is zero
[ "$score" -eq 0 ] && return
# search for the index of this target, to see if we already are tracking it
index=-1
for search_index in "${!alt_targets[@]}"; do
if [ "${alt_targets[$search_index]}" = "$tgt" ]; then
index="$search_index"
break
fi
done
# if we don't find an existing index, create one by appending to the array
if [ "$index" -eq -1 ]; then
# $YADM_CONFIG must be processed first, in case other templates lookup yadm configurations
if [ "$tgt" = "$YADM_CONFIG" ]; then
alt_targets=("$tgt" "${alt_targets[@]}")
alt_sources=("$src" "${alt_sources[@]}")
alt_scores=(0 "${alt_scores[@]}")
index=0
# increase the index of any existing alt_template_cmds
new_cmds=()
for cmd_index in "${!alt_template_cmds[@]}"; do
new_cmds[$((cmd_index+1))]="${alt_template_cmds[$cmd_index]}"
done
alt_template_cmds=()
for cmd_index in "${!new_cmds[@]}"; do
alt_template_cmds[$cmd_index]="${new_cmds[$cmd_index]}"
done
else
alt_targets+=("$tgt")
# set index to the last index (newly created one)
for index in "${!alt_targets[@]}"; do :; done
# and set its initial score to zero
alt_scores[$index]=0
fi
fi
# record nothing if a template command is registered for this file
[ "${alt_template_cmds[$index]+isset}" ] && return
# record higher scoring sources
if [ "$score" -gt "${alt_scores[$index]}" ]; then
alt_scores[$index]="$score"
alt_sources[$index]="$src"
fi
}
function record_template() {
tgt="$1"
cmd="$2"
src="$3"
# search for the index of this target, to see if we already are tracking it
index=-1
for search_index in "${!alt_targets[@]}"; do
if [ "${alt_targets[$search_index]}" = "$tgt" ]; then
index="$search_index"
break
fi
done
# if we don't find an existing index, create one by appending to the array
if [ "$index" -eq -1 ]; then
alt_targets+=("$tgt")
# set index to the last index (newly created one)
for index in "${!alt_targets[@]}"; do :; done
fi
# record the template command, last one wins
alt_template_cmds[$index]="$cmd"
alt_sources[$index]="$src"
}
function choose_template_cmd() {
kind="$1"
if [ "$kind" = "default" ] || [ "$kind" = "" ] && awk_available; then
echo "template_default"
elif [ "$kind" = "esh" ] && esh_available; then
echo "template_esh"
elif [ "$kind" = "j2cli" ] || [ "$kind" = "j2" ] && j2cli_available; then
echo "template_j2cli"
elif [ "$kind" = "envtpl" ] || [ "$kind" = "j2" ] && envtpl_available; then
echo "template_envtpl"
elif [ "$kind" = "mst" ] && mst_available; then
echo "template_mst"
else
return # this "kind" of template is not supported
fi
}
# ****** Template Processors ******
function template_default() {
input="$1"
output="$2"
temp_file="${output}.$$.$RANDOM"
# the explicit "space + tab" character class used below is used because not
# all versions of awk seem to support the POSIX character classes [[:blank:]]
read -r -d '' awk_pgm << "EOF"
# built-in default template processor
BEGIN {
blank = "[ ]"
c["class"] = class
c["classes"] = classes
c["arch"] = arch
c["os"] = os
c["hostname"] = host
c["user"] = user
c["distro"] = distro
c["distro_family"] = distro_family
c["source"] = source
ifs = "^{%" blank "*if"
els = "^{%" blank "*else" blank "*%}$"
end = "^{%" blank "*endif" blank "*%}$"
skp = "^{%" blank "*(if|else|endif)"
vld = conditions()
inc_start = "^{%" blank "*include" blank "+\"?"
inc_end = "\"?" blank "*%}$"
inc = inc_start ".+" inc_end
prt = 1
err = 0
}
END { exit err }
{ replace_vars() } # variable replacements
$0 ~ vld, $0 ~ end {
if ($0 ~ vld || $0 ~ end) prt=1;
if ($0 ~ els) prt=0;
if ($0 ~ skp) next;
}
($0 ~ ifs && $0 !~ vld), $0 ~ end {
if ($0 ~ ifs && $0 !~ vld) prt=0;
if ($0 ~ els || $0 ~ end) prt=1;
if ($0 ~ skp) next;
}
{ if (!prt) next }
$0 ~ inc {
file = $0
sub(inc_start, "", file)
sub(inc_end, "", file)
sub(/^[^\/].*$/, source_dir "/&", file)
while ((res = getline <file) > 0) {
replace_vars()
print
}
if (res < 0) {
printf "%s:%d: error: could not read '%s'\n", FILENAME, NR, file | "cat 1>&2"
err = 1
}
close(file)
next
}
{ print }
function replace_vars() {
for (label in c) {
gsub(("{{" blank "*yadm\\." label blank "*}}"), c[label])
}
for (label in ENVIRON) {
gsub(("{{" blank "*env\\." label blank "*}}"), ENVIRON[label])
}
}
function condition_helper(label, value) {
gsub(/[\\.^$(){}\[\]|*+?]/, "\\\\&", value)
return sprintf("yadm\\.%s" blank "*==" blank "*\"%s\"", label, value)
}
function conditions() {
pattern = ifs blank "+("
for (label in c) {
if (label != "class") {
value = c[label]
pattern = sprintf("%s%s|", pattern, condition_helper(label, value));
}
}
split(classes, cls_array, "\n")
for (idx in cls_array) {
value = cls_array[idx]
pattern = sprintf("%s%s|", pattern, condition_helper("class", value));
}
sub(/\|$/, ")" blank "*%}$", pattern)
return pattern
}
EOF
"${AWK_PROGRAM[0]}" \
-v class="$local_class" \
-v arch="$local_arch" \
-v os="$local_system" \
-v arch="$local_arch" \
-v host="$local_host" \
-v user="$local_user" \
-v distro="$local_distro" \
-v distro_family="$local_distro_family" \
-v source="$input" \
-v source_dir="$(dirname "$input")" \
-v classes="$(join_string $'\n' "${local_classes[@]}")" \
"$awk_pgm" \
"$input" > "$temp_file" || rm -f "$temp_file"
move_file "$input" "$output" "$temp_file"
}
function template_j2cli() {
input="$1"
output="$2"
temp_file="${output}.$$.$RANDOM"
YADM_CLASS="$local_class" \
YADM_ARCH="$local_arch" \
YADM_OS="$local_system" \
YADM_ARCH="$local_arch" \
YADM_HOSTNAME="$local_host" \
YADM_USER="$local_user" \
YADM_DISTRO="$local_distro" \
YADM_DISTRO_FAMILY="$local_distro_family" \
YADM_SOURCE="$input" \
YADM_CLASSES="$(join_string $'\n' "${local_classes[@]}")" \
YADM_TARGET="$output" \
"$J2CLI_PROGRAM" "$input" -o "$temp_file"
move_file "$input" "$output" "$temp_file"
}
function template_envtpl() {
input="$1"
output="$2"
temp_file="${output}.$$.$RANDOM"
YADM_CLASS="$local_class" \
YADM_ARCH="$local_arch" \
YADM_OS="$local_system" \
YADM_ARCH="$local_arch" \
YADM_HOSTNAME="$local_host" \
YADM_USER="$local_user" \
YADM_DISTRO="$local_distro" \
YADM_DISTRO_FAMILY="$local_distro_family" \
YADM_SOURCE="$input" \
YADM_CLASSES="$(join_string $'\n' "${local_classes[@]}")" \
YADM_TARGET="$output" \
"$ENVTPL_PROGRAM" --keep-template "$input" -o "$temp_file"
move_file "$input" "$output" "$temp_file"
}
function template_mst() {
input="$1"
output="$2"
temp_file="${output}.$$.$RANDOM"
"${MST_PROGRAM}" "${input}" \
YADM_SOURCE "${input}" \
"input_${input}" "true" \
YADM_TARGET "${output}" \
"output_${output}" "true" \
YADM_CLASS "${local_class}" \
"class_${local_class//[^[:alnum:]]/_}" "true" \
"os_${local_system//[^[:alnum:]]/_}" "true" \
YADM_OS "${local_system}" \
"hostname_${local_host//[^[:alnum:]]/_}" "true" \
YADM_ARCH "${local_arch}" \
"arch_${local_arch//[^[:alnum:]]/_}" "true" \
YADM_HOSTNAME "${local_host}" \
"user_${local_user//[^[:alnum:]]/_}" "true" \
YADM_USER "${local_user}" \
"distro_${local_distro//[^[:alnum:]]/_}" "true" \
YADM_DISTRO "${local_distro}" \
> "$temp_file"
if [ -f "$temp_file" ] ; then
if cmp --silent "$temp_file" "$output"; then
debug "$temp_file has same content as $output"
[ -n "$loud" ] && echo "$temp_file has same content as $output"
rm "$temp_file"
else
debug "$temp_file has updated content for $output, copying"
[ -n "$loud" ] && echo "$temp_file has updated content for $output, copying"
copy_perms "$input" "$temp_file"
mv -f "$temp_file" "$output"
fi
fi
}
function template_esh() {
input="$1"
output="$2"
temp_file="${output}.$$.$RANDOM"
YADM_CLASSES="$(join_string $'\n' "${local_classes[@]}")" \
"$ESH_PROGRAM" -o "$temp_file" "$input" \
YADM_CLASS="$local_class" \
YADM_ARCH="$local_arch" \
YADM_OS="$local_system" \
YADM_ARCH="$local_arch" \
YADM_HOSTNAME="$local_host" \
YADM_USER="$local_user" \
YADM_DISTRO="$local_distro" \
YADM_DISTRO_FAMILY="$local_distro_family" \
YADM_SOURCE="$input" \
YADM_TARGET="$output"
move_file "$input" "$output" "$temp_file"
}
function move_file() {
local input=$1
local output=$2
local temp_file=$3
[ ! -f "$temp_file" ] && return
# if the output files already exists as read-only, change it to be writable.
# there are some environments in which a read-only file will prevent the move
# from being successful.
[[ -e "$output" && ! -w "$output" ]] && chmod u+w "$output"
mv -f "$temp_file" "$output"
copy_perms "$input" "$output"
}
# ****** yadm Commands ******
function alt() {
require_repo
parse_encrypt
# gather values for processing alternates
local local_class
local -a local_classes
local local_arch
local local_system
local local_arch
local local_host
local local_user
local local_distro
local local_distro_family
set_local_alt_values
# only be noisy if the "alt" command was run directly
local loud=
[ "$YADM_COMMAND" = "alt" ] && loud="YES"
# decide if a copy should be done instead of a symbolic link
local do_copy=0
[ "$(config --bool yadm.alt-copy)" == "true" ] && do_copy=1
cd_work "Alternates" || return
# determine all tracked files
local tracked_files=()
local IFS=$'\n'
for tracked_file in $("$GIT_PROGRAM" ls-files | LC_ALL=C sort); do
tracked_files+=("$tracked_file")
done
# generate data for removing stale links
local possible_alts=()
local IFS=$'\n'
for possible_alt in "${tracked_files[@]}" "${ENCRYPT_INCLUDE_FILES[@]}"; do
if [[ $possible_alt =~ .\#\#. ]]; then
base_alt="${possible_alt%%##*}"
yadm_alt="${YADM_BASE}/${base_alt}"
if [ "${yadm_alt#"$YADM_ALT/"}" != "${yadm_alt}" ]; then
base_alt="${yadm_alt#"$YADM_ALT/"}"
fi
possible_alts+=("$YADM_BASE/${base_alt}")
fi
done
local alt_linked=()
alt_linking
remove_stale_links
report_invalid_alts
}
function report_invalid_alts() {
[ "$LEGACY_WARNING_ISSUED" = "1" ] && return
[ "${#INVALID_ALT[@]}" = "0" ] && return
local path_list
for invalid in "${INVALID_ALT[@]}"; do
path_list="$path_list * $invalid"$'\n'
done
local msg
IFS='' read -r -d '' msg <<EOF
**WARNING**
Invalid alternates have been detected.
Beginning with version 2.0.0, yadm uses a new naming convention for alternate
files. Read more about this change here:
https://yadm.io/docs/upgrade_from_1
Or to learn more about alternates in general, read:
https://yadm.io/docs/alternates
To rename the invalid alternates run:
yadm mv <old name> <new name>
Invalid alternates detected:
${path_list}
***********
EOF
printf '%s\n' "$msg" >&2
}
function remove_stale_links() {
# review alternate candidates for stale links
# if a possible alt IS linked, but it's source is not part of alt_linked,
# remove it.
if readlink_available; then
for stale_candidate in "${possible_alts[@]}"; do
if [ -L "$stale_candidate" ]; then
src=$(readlink "$stale_candidate" 2>/dev/null)
if [ -n "$src" ]; then
for review_link in "${alt_linked[@]}"; do
[ "$src" = "$review_link" ] && continue 2
done
rm -f "$stale_candidate"
fi
fi
done
fi
}
function set_local_alt_values() {
local -a all_classes
all_classes=$(config --get-all local.class)
while IFS='' read -r class; do
local_classes+=("$class")
local_class="$class"
done <<< "$all_classes"
local_arch="$(config local.arch)"
if [ -z "$local_arch" ] ; then
local_arch=$(uname -m)
fi
local_system="$(config local.os)"
if [ -z "$local_system" ] ; then
local_system="$OPERATING_SYSTEM"
fi
local_arch="$(config local.arch)"
if [ -z "$local_arch" ] ; then
local_arch="$(uname -m)"
fi
local_host="$(config local.hostname)"
if [ -z "$local_host" ] ; then
local_host=$(uname -n)
local_host=${local_host%%.*} # trim any domain from hostname
fi
local_user="$(config local.user)"
if [ -z "$local_user" ] ; then
local_user=$(id -u -n)
fi
local_distro="$(query_distro)"
local_distro_family="$(query_distro_family)"
}
function alt_linking() {
local alt_scores=()
local alt_targets=()
local alt_sources=()
local alt_template_cmds=()
for alt_path in $(for tracked in "${tracked_files[@]}"; do printf "%s\n" "$tracked" "${tracked%/*}"; done | LC_ALL=C sort -u) "${ENCRYPT_INCLUDE_FILES[@]}"; do
alt_path="$YADM_BASE/$alt_path"
if [[ "$alt_path" =~ .\#\#. ]]; then
if [ -e "$alt_path" ] ; then
score_file "$alt_path"
fi
fi
done
for index in "${!alt_targets[@]}"; do
tgt="${alt_targets[$index]}"
src="${alt_sources[$index]}"
template_cmd="${alt_template_cmds[$index]}"
if [ -n "$template_cmd" ]; then
# a template is defined, process the template
debug "Creating $tgt from template $src"
[ -n "$loud" ] && echo "Creating $tgt from template $src"
# ensure the destination path exists
assert_parent "$tgt"
# remove any existing symlink before processing template
[ -L "$tgt" ] && rm -f "$tgt"
"$template_cmd" "$src" "$tgt"
elif [ -n "$src" ]; then
# a link source is defined, create symlink
debug "Linking $src to $tgt"
[ -n "$loud" ] && echo "Linking $src to $tgt"
# ensure the destination path exists
assert_parent "$tgt"
if [ "$do_copy" -eq 1 ]; then
# remove any existing symlink before copying
[ -L "$tgt" ] && rm -f "$tgt"
cp -f "$src" "$tgt"
else
ln_relative "$src" "$tgt"
fi
fi
done
}
function ln_relative() {
local full_source full_target target_dir
local full_source="$1"
local full_target="$2"
local target_dir="${full_target%/*}"
if [ "$target_dir" == "" ]; then
target_dir="/"
fi
local rel_source
rel_source=$(relative_path "$target_dir" "$full_source")
ln -nfs "$rel_source" "$full_target"
alt_linked+=("$rel_source")
}
function bootstrap() {
bootstrap_available || error_out "Cannot execute bootstrap\n'$YADM_BOOTSTRAP' is not an executable program."
# GIT_DIR should not be set for user's bootstrap code
unset GIT_DIR
local local_class
local local_system
local local_arch
local local_host
local local_user
local local_distro
set_local_alt_values
export YADM_CLASS="$local_class"
export YADM_OS="$local_system"
export YADM_ARCH="$local_arch"
export YADM_HOSTNAME="$local_host"
export YADM_USER="$local_user"
export YADM_DISTRO="$local_distro"
echo "Executing $YADM_BOOTSTRAP"
exec "$YADM_BOOTSTRAP"
}
function clean() {
error_out "\"git clean\" has been disabled for safety. You could end up removing all unmanaged files."
}
function clone() {
DO_BOOTSTRAP=1
local -a args
local -i do_checkout=1
while [[ $# -gt 0 ]] ; do
case "$1" in
--bootstrap) # force bootstrap, without prompt
DO_BOOTSTRAP=2
;;
--no-bootstrap) # prevent bootstrap, without prompt
DO_BOOTSTRAP=3
;;
--checkout)
do_checkout=1
;;
-n|--no-checkout)
do_checkout=0
;;
--bare|--mirror|--recurse-submodules*|--recursive|--separate-git-dir=*)
# ignore arguments without separate parameter
;;
--separate-git-dir)
# ignore arguments with separate parameter
shift
;;
*)
args+=("$1")
;;
esac
shift
done
[ -n "$DEBUG" ] && display_private_perms "initial"
# safety check, don't attempt to clone when the repo is already present
[ -d "$YADM_REPO" ] && [ -z "$FORCE" ] &&
error_out "Git repo already exists. [$YADM_REPO]\nUse '-f' if you want to force it to be overwritten."
# remove existing if forcing the clone to happen anyway
[ -d "$YADM_REPO" ] && {
debug "Removing existing repo prior to clone"
"$GIT_PROGRAM" -C "$YADM_WORK" submodule deinit -f --all
rm -rf "$YADM_REPO"
}
local wc
wc="$(mk_tmp_dir)"
[ -d "$wc" ] || error_out "Unable to create temporary directory"
# first clone without checkout
debug "Doing an initial clone of the repository"
(cd "$wc" &&
"$GIT_PROGRAM" -c core.sharedrepository=0600 clone --no-checkout \
--separate-git-dir="$YADM_REPO" "${args[@]}" repo.git) || {
debug "Removing repo after failed clone"
rm -rf "$YADM_REPO" "$wc"
error_out "Unable to clone the repository"
}
configure_repo
rm -rf "$wc"
# then reset the index as the --no-checkout flag makes the index empty
"$GIT_PROGRAM" reset --quiet -- .
if [ "$YADM_WORK" = "$HOME" ]; then
debug "Determining if repo tracks private directories"
for private_dir in $(private_dirs all); do
found_log=$("$GIT_PROGRAM" log -n 1 -- "$private_dir" 2>/dev/null)
if [ -n "$found_log" ]; then
debug "Private directory $private_dir is tracked by repo"
assert_private_dirs "$private_dir"
fi
done
fi
# finally check out (unless instructed not to) all files that don't exist in $YADM_WORK
if [[ $do_checkout -ne 0 ]]; then
[ -n "$DEBUG" ] && display_private_perms "pre-checkout"
cd_work "Clone" || return
"$GIT_PROGRAM" ls-files --deleted | while IFS= read -r file; do
"$GIT_PROGRAM" checkout -- ":/$file"
done
if [ -n "$("$GIT_PROGRAM" ls-files --modified)" ]; then
local msg
IFS='' read -r -d '' msg <<EOF
**NOTE**
Local files with content that differs from the ones just
cloned were found in $YADM_WORK. They have been left
unmodified.
Please review and resolve any differences appropriately.
If you know what you're doing, and want to overwrite the
tracked files, consider 'yadm checkout "$YADM_WORK"'.
EOF
printf '%s\n' "$msg"
fi
[ -n "$DEBUG" ] && display_private_perms "post-checkout"
CHANGES_POSSIBLE=1
fi
}
function config() {
use_repo_config=0
local_options="^local\.(class|arch|os|hostname|user)$"
for option in "$@"; do
[[ "$option" =~ $local_options ]] && use_repo_config=1
done
if [ -z "$*" ] ; then
# with no parameters, provide some helpful documentation
echo "yadm supports the following configurations:"
echo
local IFS=$'\n'
for supported_config in $(introspect_configs); do
echo " ${supported_config}"
done
echo
local msg
read -r -d '' msg << EOF
Please read the CONFIGURATION section in the man
page for more details about configurations, and
how to adjust them.
EOF
printf '%s\n' "$msg"
elif [ "$use_repo_config" -eq 1 ]; then
require_repo
# operate on the yadm repo's configuration file
# this is always local to the machine
"$GIT_PROGRAM" config "$@"
CHANGES_POSSIBLE=1
else
# make sure parent folder of config file exists
assert_parent "$YADM_CONFIG"
# operate on the yadm configuration file
"$GIT_PROGRAM" config --file="$(mixed_path "$YADM_CONFIG")" "$@"
fi
}
function _set_gpg_options() {
gpg_key="$(config yadm.gpg-recipient)"
if [ "$gpg_key" = "ASK" ]; then
GPG_OPTS=("--no-default-recipient" "-e")
elif [ "$gpg_key" != "" ]; then
GPG_OPTS=("-e")