-
Notifications
You must be signed in to change notification settings - Fork 179
/
yadm
executable file
·1975 lines (1645 loc) · 50.3 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-2020 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/>.
# execute script with bash (shebang line is /bin/sh for portability)
if [ -z "$BASH_VERSION" ]; then
[ "$YADM_TEST" != 1 ] && exec bash "$0" "$@"
fi
VERSION=2.4.0
YADM_WORK="$HOME"
YADM_DIR=
YADM_LEGACY_DIR="${HOME}/.yadm"
# these are the default paths relative to YADM_DIR
YADM_REPO="repo.git"
YADM_CONFIG="config"
YADM_ENCRYPT="encrypt"
YADM_ARCHIVE="files.gpg"
YADM_BOOTSTRAP="bootstrap"
YADM_HOOKS="hooks"
YADM_ALT="alt"
HOOK_COMMAND=""
FULL_COMMAND=""
GPG_PROGRAM="gpg"
GIT_PROGRAM="git"
AWK_PROGRAM=("gawk" "awk")
GIT_CRYPT_PROGRAM="git-crypt"
J2CLI_PROGRAM="j2"
ENVTPL_PROGRAM="envtpl"
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=()
# 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 if it doesn't exist yet
[ -d "$YADM_DIR" ] || mkdir -p "$YADM_DIR"
# parse command line arguments
local retval=0
internal_commands="^(alt|bootstrap|clean|clone|config|decrypt|encrypt|enter|git-crypt|help|init|introspect|list|perms|upgrade|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_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() and clone()
FORCE="YES"
;;
-l) # used by decrypt()
DO_LIST="YES"
[ "$YADM_COMMAND" = "config" ] && YADM_ARGS+=("$1")
;;
-w) # used by init() and clone()
if [[ ! "$2" =~ ^/ ]] ; then
error_out "You must specify a fully qualified work tree"
fi
YADM_WORK="$2"
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_WORK}/${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
score=$((score + 1000))
# default condition
if [[ "$label" =~ ^(default)$ ]]; then
score=$((score + 0))
# variable conditions
elif [[ "$label" =~ ^(o|os)$ ]]; then
if [ "$value" = "$local_system" ]; then
score=$((score + 1))
else
score=0
return
fi
elif [[ "$label" =~ ^(d|distro)$ ]]; then
if [ "$value" = "$local_distro" ]; then
score=$((score + 2))
else
score=0
return
fi
elif [[ "$label" =~ ^(c|class)$ ]]; then
if [ "$value" = "$local_class" ]; then
score=$((score + 4))
else
score=0
return
fi
elif [[ "$label" =~ ^(h|hostname)$ ]]; then
if [ "$value" = "$local_host" ]; then
score=$((score + 8))
else
score=0
return
fi
elif [[ "$label" =~ ^(u|user)$ ]]; then
if [ "$value" = "$local_user" ]; then
score=$((score + 16))
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
INVALID_ALT+=("$src")
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
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
# 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" = "j2cli" ] || [ "$kind" = "j2" ] && j2cli_available; then
echo "template_j2cli"
elif [ "$kind" = "envtpl" ] || [ "$kind" = "j2" ] && envtpl_available; then
echo "template_envtpl"
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:]]
awk_pgm=$(cat << "EOF"
# built-in default template processor
BEGIN {
blank = "[ ]"
c["class"] = class
c["os"] = os
c["hostname"] = host
c["user"] = user
c["distro"] = distro
c["source"] = source
vld = conditions()
ifs = "^{%" blank "*if"
els = "^{%" blank "*else" blank "*%}$"
end = "^{%" blank "*endif" blank "*%}$"
skp = "^{%" blank "*(if|else|endif)"
prt = 1
}
{ 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) print }
function replace_vars() {
for (label in c) {
gsub(("{{" blank "*yadm\\." label blank "*}}"), c[label])
}
}
function conditions() {
pattern = "^{%" blank "*if" blank "*("
for (label in c) {
value = c[label]
gsub(/[\\.^$(){}\[\]|*+?]/, "\\\\&", value)
pattern = sprintf("%syadm\\.%s" blank "*==" blank "*\"%s\"|", pattern, label, value)
}
sub(/\|$/,")",pattern)
return pattern
}
EOF
)
"${AWK_PROGRAM[0]}" \
-v class="$local_class" \
-v os="$local_system" \
-v host="$local_host" \
-v user="$local_user" \
-v distro="$local_distro" \
-v source="$input" \
"$awk_pgm" \
"$input" > "$temp_file"
[ -f "$temp_file" ] && mv -f "$temp_file" "$output"
}
function template_j2cli() {
input="$1"
output="$2"
temp_file="${output}.$$.$RANDOM"
YADM_CLASS="$local_class" \
YADM_OS="$local_system" \
YADM_HOSTNAME="$local_host" \
YADM_USER="$local_user" \
YADM_DISTRO="$local_distro" \
YADM_SOURCE="$input" \
"$J2CLI_PROGRAM" "$input" -o "$temp_file"
[ -f "$temp_file" ] && mv -f "$temp_file" "$output"
}
function template_envtpl() {
input="$1"
output="$2"
temp_file="${output}.$$.$RANDOM"
YADM_CLASS="$local_class" \
YADM_OS="$local_system" \
YADM_HOSTNAME="$local_host" \
YADM_USER="$local_user" \
YADM_DISTRO="$local_distro" \
YADM_SOURCE="$input" \
"$ENVTPL_PROGRAM" --keep-template "$input" -o "$temp_file"
[ -f "$temp_file" ] && mv -f "$temp_file" "$output"
}
# ****** yadm Commands ******
function alt() {
require_repo
parse_encrypt
# gather values for processing alternates
local local_class
local local_system
local local_host
local local_user
local local_distro
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
# deprecated yadm.cygwin-copy option (to be removed)
[ "$(config --bool yadm.cygwin-copy)" == "true" ] && do_copy=1
cd_work "Alternates" || return
# determine all tracked files
local tracked_files
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
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_WORK}/${base_alt}"
if [ "${yadm_alt#$YADM_ALT/}" != "${yadm_alt}" ]; then
base_alt="${yadm_alt#$YADM_ALT/}"
fi
possible_alts+=("$YADM_WORK/${base_alt}")
fi
done
local alt_linked
alt_linked=()
if [ "$YADM_COMPATIBILITY" = "1" ]; then
alt_past_linking
else
alt_future_linking
fi
remove_stale_links
report_invalid_alts
}
function report_invalid_alts() {
[ "$YADM_COMPATIBILITY" = "1" ] && return
[ "$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
cat <<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
}
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_class="$(config local.class)"
local_system="$(config local.os)"
if [ -z "$local_system" ] ; then
local_system="$OPERATING_SYSTEM"
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)"
}
function alt_future_linking() {
local alt_scores
local alt_targets
local alt_sources
local alt_template_cmds
alt_scores=()
alt_targets=()
alt_sources=()
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_WORK/$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 alt_past_linking() {
if [ -z "$local_class" ] ; then
match_class="%"
else
match_class="$local_class"
fi
match_class="(%|$match_class)"
match_system="(%|$local_system)"
match_host="(%|$local_host)"
match_user="(%|$local_user)"
# regex for matching "<file>##CLASS.SYSTEM.HOSTNAME.USER"
match1="^(.+)##(()|$match_system|$match_system\.$match_host|$match_system\.$match_host\.$match_user)$"
match2="^(.+)##($match_class|$match_class\.$match_system|$match_class\.$match_system\.$match_host|$match_class\.$match_system\.$match_host\.$match_user)$"
# loop over all "tracked" files
# for every file which matches the above regex, create a symlink
for match in $match1 $match2; do
last_linked=''
local IFS=$'\n'
# the alt_paths looped over here are a unique sorted list of both files and their immediate parent directory
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_WORK/$alt_path"
if [ -e "$alt_path" ] ; then
if [[ $alt_path =~ $match ]] ; then
if [ "$alt_path" != "$last_linked" ] ; then
new_link="${BASH_REMATCH[1]}"
debug "Linking $alt_path to $new_link"
[ -n "$loud" ] && echo "Linking $alt_path to $new_link"
if [ "$do_copy" -eq 1 ]; then
if [ -L "$new_link" ]; then
rm -f "$new_link"
fi
cp -f "$alt_path" "$new_link"
else
ln_relative "$alt_path" "$new_link"
fi
last_linked="$alt_path"
fi
fi
fi
done
done
# loop over all "tracked" files
# for every file which is a *##yadm.j2 create a real file
local match="^(.+)##yadm\\.j2$"
for tracked_file in "${tracked_files[@]}" "${ENCRYPT_INCLUDE_FILES[@]}"; do
tracked_file="$YADM_WORK/$tracked_file"
if [ -e "$tracked_file" ] ; then
if [[ $tracked_file =~ $match ]] ; then
real_file="${BASH_REMATCH[1]}"
if envtpl_available; then
debug "Creating $real_file from template $tracked_file"
[ -n "$loud" ] && echo "Creating $real_file from template $tracked_file"
temp_file="${real_file}.$$.$RANDOM"
YADM_CLASS="$local_class" \
YADM_OS="$local_system" \
YADM_HOSTNAME="$local_host" \
YADM_USER="$local_user" \
YADM_DISTRO="$local_distro" \
"$ENVTPL_PROGRAM" --keep-template "$tracked_file" -o "$temp_file"
[ -f "$temp_file" ] && mv -f "$temp_file" "$real_file"
else
debug "envtpl not available, not creating $real_file from template $tracked_file"
[ -n "$loud" ] && echo "envtpl not available, not creating $real_file from template $tracked_file"
fi
fi
fi
done
}
function ln_relative() {
local full_source full_target target_dir
full_source="$1"
full_target="$2"
target_dir="${full_target%/*}"
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
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 branch
branch="master"
clone_args=()
while [[ $# -gt 0 ]] ; do
key="$1"
case $key in
-b)
if ! is_valid_branch_name "$2"; then
error_out "You must provide a branch name when using '-b'"
fi
branch="$2"
shift
;;
--bootstrap) # force bootstrap, without prompt
DO_BOOTSTRAP=2
;;
--no-bootstrap) # prevent bootstrap, without prompt
DO_BOOTSTRAP=3
;;
*) # main arguments are kept intact
clone_args+=("$1")
;;
esac
shift
done
[ -n "$DEBUG" ] && display_private_perms "initial"
# clone will begin with a bare repo
local empty=
init $empty
# add the specified remote, and configure the repo to track origin/$branch
debug "Adding remote to new repo"
"$GIT_PROGRAM" remote add origin "${clone_args[@]}"
debug "Configuring new repo to track origin/${branch}"
"$GIT_PROGRAM" config "branch.${branch}.remote" origin
"$GIT_PROGRAM" config "branch.${branch}.merge" "refs/heads/${branch}"
# fetch / merge (and possibly fallback to reset)
debug "Doing an initial fetch of the origin"
"$GIT_PROGRAM" fetch origin || {
debug "Removing repo after failed clone"
rm -rf "$YADM_REPO"
error_out "Unable to fetch origin ${clone_args[0]}"
}
debug "Verifying '${branch}' is a valid branch to merge"
[ -f "${YADM_REPO}/refs/remotes/origin/${branch}" ] || {
debug "Removing repo after failed clone"
rm -rf "$YADM_REPO"
error_out "Clone failed, 'origin/${branch}' does not exist in ${clone_args[0]}"
}
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 "origin/${branch}" -- "$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
[ -n "$DEBUG" ] && display_private_perms "pre-merge"
debug "Doing an initial merge of origin/${branch}"
"$GIT_PROGRAM" merge "origin/${branch}" || {
debug "Merge failed, doing a reset and stashing conflicts."
"$GIT_PROGRAM" reset "origin/${branch}"
if cd "$YADM_WORK"; then # necessary because of a bug in Git
"$GIT_PROGRAM" -c user.name='yadm clone' -c user.email='yadm' stash save Conflicts preserved from yadm clone command 2>&1
cat <<EOF
**NOTE**
Merging origin/${branch} failed.
As a result, yadm did 'reset origin/${branch}', and then
stashed the conflicting data.
This likely happened because you had files in \$HOME
which conflicted with files tracked by origin/${branch}.
You can review the stashed conflicts with the
command 'yadm stash show -p' from within your
\$HOME directory. If you want to restore the
stashed data, you can run 'yadm stash apply' or
'yadm stash pop' and then handle the conflicts
in another way.
EOF
else
# skip auto_bootstrap if conflicts could not be stashed
DO_BOOTSTRAP=0
cat <<EOF
**NOTE**
Merging origin/${branch} failed.
yadm did 'reset origin/${branch}' instead.
yadm did not stash these conflicts beacuse it was unable
to change to the $YADM_WORK directory.
Please review and resolve any differences appropriately
If you know what you're doing, and want to overwrite the
tracked files, consider 'yadm reset --hard origin/${branch}'
EOF
fi
}
[ -n "$DEBUG" ] && display_private_perms "post-merge"
CHANGES_POSSIBLE=1
}
function config() {
use_repo_config=0
local_options="^local\.(class|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
cat << EOF
Please read the CONFIGURATION section in the man
page for more details about configurations, and
how to adjust them.
EOF
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
# operate on the yadm configuration file
"$GIT_PROGRAM" config --file="$(mixed_path "$YADM_CONFIG")" "$@"
fi
}
function decrypt() {
require_gpg
require_archive
[ -f "$YADM_ENCRYPT" ] && exclude_encrypted
if [ "$DO_LIST" = "YES" ] ; then
tar_option="t"
else
tar_option="x"
fi
# decrypt the archive
if ($GPG_PROGRAM -d "$YADM_ARCHIVE" || echo 1) | tar v${tar_option}f - -C "$YADM_WORK"; then
[ ! "$DO_LIST" = "YES" ] && echo "All files decrypted."
else
error_out "Unable to extract encrypted files."
fi
CHANGES_POSSIBLE=1
}
function encrypt() {
require_gpg
require_encrypt
exclude_encrypted
parse_encrypt
cd_work "Encryption" || return
# Build gpg options for gpg
GPG_KEY="$(config yadm.gpg-recipient)"
if [ "$GPG_KEY" = "ASK" ]; then
GPG_OPTS=("--no-default-recipient" "-e")
elif [ "$GPG_KEY" != "" ]; then
GPG_OPTS=("-e")
for key in $GPG_KEY; do
GPG_OPTS+=("-r $key")
done
else
GPG_OPTS=("-c")
fi
# report which files will be encrypted
echo "Encrypting the following files:"
printf '%s\n' "${ENCRYPT_INCLUDE_FILES[@]}"
echo
# encrypt all files which match the globs
if tar -f - -c "${ENCRYPT_INCLUDE_FILES[@]}" | $GPG_PROGRAM --yes "${GPG_OPTS[@]}" --output "$YADM_ARCHIVE"; then
echo "Wrote new file: $YADM_ARCHIVE"
else
error_out "Unable to write $YADM_ARCHIVE"
fi
# offer to add YADM_ARCHIVE if untracked
archive_status=$("$GIT_PROGRAM" status --porcelain -uall "$(mixed_path "$YADM_ARCHIVE")" 2>/dev/null)
archive_regex="^\?\?"
if [[ $archive_status =~ $archive_regex ]] ; then
echo "It appears that $YADM_ARCHIVE is not tracked by yadm's repository."
echo "Would you like to add it now? (y/n)"
read -r answer < /dev/tty
if [[ $answer =~ ^[yY]$ ]] ; then
"$GIT_PROGRAM" add "$(mixed_path "$YADM_ARCHIVE")"
fi
fi
CHANGES_POSSIBLE=1
}
function git_crypt() {
require_git_crypt
enter "${GIT_CRYPT_PROGRAM} $*"
}
function enter() {
command="$*"
require_shell
require_repo
shell_opts=""
shell_path=""
if [[ "$SHELL" =~ bash$ ]]; then
shell_opts="--norc"
shell_path="\w"
elif [[ "$SHELL" =~ [cz]sh$ ]]; then
shell_opts="-f"
shell_path="%~"
fi
shell_cmd=()
if [ -n "$command" ]; then
shell_cmd=('-c' "$*")
fi
GIT_WORK_TREE="$YADM_WORK"
export GIT_WORK_TREE
[ "${#shell_cmd[@]}" -eq 0 ] && echo "Entering yadm repo"
yadm_prompt="yadm shell ($YADM_REPO) $shell_path > "
PROMPT="$yadm_prompt" PS1="$yadm_prompt" "$SHELL" $shell_opts "${shell_cmd[@]}"
return_code="$?"
if [ "${#shell_cmd[@]}" -eq 0 ]; then
echo "Leaving yadm repo"
else
exit_with_hook "$return_code"
fi
}
function git_command() {
require_repo
# translate 'gitconfig' to 'config' -- 'config' is reserved for yadm
if [ "$1" = "gitconfig" ] ; then
set -- "config" "${@:2}"
fi
# ensure private .ssh and .gnupg directories exist first
# TODO: consider restricting this to only commands which modify the work-tree
if [ "$YADM_WORK" = "$HOME" ]; then
auto_private_dirs=$(config --bool yadm.auto-private-dirs)
if [ "$auto_private_dirs" != "false" ] ; then
for pdir in $(private_dirs all); do
assert_private_dirs "$pdir"
done
fi
fi
CHANGES_POSSIBLE=1
# pass commands through to git
debug "Running git command $GIT_PROGRAM $*"
"$GIT_PROGRAM" "$@"
return "$?"
}