forked from yadm-dev/yadm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyadm
executable file
·1099 lines (894 loc) · 27.4 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-2017 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, version 3 of the License.
# 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 <http://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=1.12.0
YADM_WORK="$HOME"
YADM_DIR="$HOME/.yadm"
YADM_REPO="repo.git"
YADM_CONFIG="config"
YADM_ENCRYPT="encrypt"
YADM_ARCHIVE="files.gpg"
YADM_BOOTSTRAP="bootstrap"
HOOK_COMMAND=""
FULL_COMMAND=""
GPG_PROGRAM="gpg"
GIT_PROGRAM="git"
ENVTPL_PROGRAM="envtpl"
LSB_RELEASE_PROGRAM="lsb_release"
PROC_VERSION="/proc/version"
OPERATING_SYSTEM="Unknown"
ENCRYPT_INCLUDE_FILES="unparsed"
#; 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
FULL_COMMAND="$*"
#; 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|help|init|introspect|list|perms|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
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"
;;
-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
[ ! -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
}
#; ****** yadm Commands ******
function alt() {
require_repo
parse_encrypt
local_class="$(config local.class)"
if [ -z "$local_class" ] ; then
match_class="%"
else
match_class="$local_class"
fi
match_class="(%|$match_class)"
local_system="$(config local.os)"
if [ -z "$local_system" ] ; then
local_system="$OPERATING_SYSTEM"
fi
match_system="(%|$local_system)"
local_host="$(config local.hostname)"
if [ -z "$local_host" ] ; then
local_host=$(hostname)
local_host=${local_host%%.*} #; trim any domain from hostname
fi
match_host="(%|$local_host)"
local_user="$(config local.user)"
if [ -z "$local_user" ] ; then
local_user=$(id -u -n)
fi
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)$"
cd_work "Alternates" || return
#; only be noisy if the "alt" command was run directly
[ "$YADM_COMMAND" = "alt" ] && loud="YES"
#; decide if a copy should be done instead of a symbolic link
local do_copy=0
if [[ $OPERATING_SYSTEM == CYGWIN* ]] ; then
if [[ $(config --bool yadm.cygwin-copy) == "true" ]] ; then
do_copy=1
fi
fi
#; 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'
for tracked_file in $("$GIT_PROGRAM" ls-files | sort) "${ENCRYPT_INCLUDE_FILES[@]}"; do
tracked_file="$YADM_WORK/$tracked_file"
#; process both the path, and it's parent directory
for alt_path in "$tracked_file" "${tracked_file%/*}"; do
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 -nfs "$alt_path" "$new_link"
fi
last_linked="$alt_path"
fi
fi
fi
done
done
done
#; loop over all "tracked" files
#; for every file which is a *##yadm.j2 create a real file
local IFS=$'\n'
local match="^(.+)##yadm\\.j2$"
for tracked_file in $("$GIT_PROGRAM" ls-files | sort) "${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"
YADM_CLASS="$local_class" \
YADM_OS="$local_system" \
YADM_HOSTNAME="$local_host" \
YADM_USER="$local_user" \
YADM_DISTRO=$(query_distro) \
"$ENVTPL_PROGRAM" < "$tracked_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 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
clone_args=()
while [[ $# -gt 0 ]] ; do
key="$1"
case $key in
--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/master
debug "Adding remote to new repo"
"$GIT_PROGRAM" remote add origin "${clone_args[@]}"
debug "Configuring new repo to track origin/master"
"$GIT_PROGRAM" config branch.master.remote origin
"$GIT_PROGRAM" config branch.master.merge refs/heads/master
#; 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 "Determining if repo tracks private directories"
for private_dir in .ssh/ .gnupg/; do
found_log=$("$GIT_PROGRAM" log -n 1 origin/master -- "$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
[ -n "$DEBUG" ] && display_private_perms "pre-merge"
debug "Doing an initial merge of origin/master"
"$GIT_PROGRAM" merge origin/master || {
debug "Merge failed, doing a reset and stashing conflicts."
"$GIT_PROGRAM" reset origin/master
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/master failed.
As a result, yadm did 'reset origin/master', and then
stashed the conflicting data.
This likely happened because you had files in \$HOME
which conflicted with files tracked by origin/master.
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/master failed.
yadm did 'reset origin/master' 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/master'
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
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 config --local "$@"
CHANGES_POSSIBLE=1
else
#; operate on the yadm configuration file
git config --file="$(mixed_path "$YADM_CONFIG")" "$@"
fi
}
function decrypt() {
require_gpg
require_archive
YADM_WORK=$(unix_path "$("$GIT_PROGRAM" config core.worktree)")
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
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" "-r $GPG_KEY")
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 enter() {
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
echo "Entering yadm repo"
yadm_prompt="yadm shell ($YADM_REPO) $shell_path > "
PROMPT="$yadm_prompt" PS1="$yadm_prompt" "$SHELL" $shell_opts
echo "Leaving yadm repo"
}
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
auto_private_dirs=$(config --bool yadm.auto-private-dirs)
if [ "$auto_private_dirs" != "false" ] ; then
assert_private_dirs .gnupg/ .ssh/
fi
CHANGES_POSSIBLE=1
#; pass commands through to git
debug "Running git command $GIT_PROGRAM $*"
"$GIT_PROGRAM" "$@"
return "$?"
}
function help() {
cat << EOF
Usage: yadm <command> [options...]
Manage dotfiles maintained in a Git repository. Manage alternate files
for specific systems or hosts. Encrypt/decrypt private files.
Git Commands:
Any Git command or alias can be used as a <command>. It will operate
on yadm's repository and files in the work tree (usually \$HOME).
Commands:
yadm init [-f] - Initialize an empty repository
yadm clone <url> [-f] - Clone an existing repository
yadm config <name> <value> - Configure a setting
yadm list [-a] - List tracked files
yadm alt - Create links for alternates
yadm bootstrap - Execute \$HOME/.yadm/bootstrap
yadm encrypt - Encrypt files
yadm decrypt [-l] - Decrypt files
yadm perms - Fix perms for private files
Files:
\$HOME/.yadm/config - yadm's configuration file
\$HOME/.yadm/repo.git - yadm's Git repository
\$HOME/.yadm/encrypt - List of globs used for encrypt/decrypt
\$HOME/.yadm/files.gpg - Encrypted data stored here
Use "man yadm" for complete documentation.
EOF
exit_with_hook 1
}
function init() {
#; safety check, don't attempt to init 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 init to happen anyway
[ -d "$YADM_REPO" ] && {
debug "Removing existing repo prior to init"
rm -rf "$YADM_REPO"
}
#; init a new bare repo
debug "Init new repo"
"$GIT_PROGRAM" init --shared=0600 --bare "$(mixed_path "$YADM_REPO")" "$@"
configure_repo
CHANGES_POSSIBLE=1
}
function introspect() {
case "$1" in
commands|configs|repo|switches)
"introspect_$1"
;;
esac
}
function introspect_commands() {
cat <<-EOF
alt
bootstrap
clean
clone
config
decrypt
encrypt
enter
gitconfig
help
init
introspect
list
perms
version
EOF
}
function introspect_configs() {
cat << EOF
local.class
local.hostname
local.os
local.user
yadm.auto-alt
yadm.auto-perms
yadm.auto-private-dirs
yadm.cygwin-copy
yadm.git-program
yadm.gpg-perms
yadm.gpg-program
yadm.gpg-recipient
yadm.ssh-perms
EOF
}
function introspect_repo() {
echo "$YADM_REPO"
}
function introspect_switches() {
cat <<-EOF
--yadm-archive
--yadm-bootstrap
--yadm-config
--yadm-dir
--yadm-encrypt
--yadm-repo
-Y
EOF
}
function list() {
require_repo
#; process relative to YADM_WORK when --all is specified
if [ -n "$LIST_ALL" ] ; then
cd_work "List" || return
fi
#; list tracked files
"$GIT_PROGRAM" ls-files
}
function perms() {
parse_encrypt
#; TODO: prevent repeats in the files changed
cd_work "Perms" || return
GLOBS=()
#; include the archive created by "encrypt"
[ -f "$YADM_ARCHIVE" ] && GLOBS+=("$YADM_ARCHIVE")
#; include all .ssh files (unless disabled)
if [[ $(config --bool yadm.ssh-perms) != "false" ]] ; then
GLOBS+=(".ssh" ".ssh/*")
fi
#; include all gpg files (unless disabled)
if [[ $(config --bool yadm.gpg-perms) != "false" ]] ; then
GLOBS+=(".gnupg" ".gnupg/*")
fi
#; include any files we encrypt
GLOBS+=("${ENCRYPT_INCLUDE_FILES[@]}")
#; remove group/other permissions from collected globs
#shellcheck disable=SC2068
#(SC2068 is disabled because in this case, we desire globbing)
chmod -f go-rwx ${GLOBS[@]} >/dev/null 2>&1
#; TODO: detect and report changing permissions in a portable way
}
function version() {
echo "yadm $VERSION"
exit_with_hook 0
}
#; ****** Utility Functions ******
function query_distro() {
distro=""
if command -v "$LSB_RELEASE_PROGRAM" >/dev/null 2>&1; then
distro=$($LSB_RELEASE_PROGRAM -si 2>/dev/null)
fi
echo "$distro"
}
function process_global_args() {
#; global arguments are removed before the main processing is done
MAIN_ARGS=()
while [[ $# -gt 0 ]] ; do
key="$1"
case $key in
-Y|--yadm-dir) #; override the standard YADM_DIR
if [[ ! "$2" =~ ^/ ]] ; then
error_out "You must specify a fully qualified yadm directory"
fi
YADM_DIR="$2"
shift
;;
--yadm-repo) #; override the standard YADM_REPO
if [[ ! "$2" =~ ^/ ]] ; then
error_out "You must specify a fully qualified repo path"
fi
YADM_OVERRIDE_REPO="$2"
shift
;;
--yadm-config) #; override the standard YADM_CONFIG
if [[ ! "$2" =~ ^/ ]] ; then
error_out "You must specify a fully qualified config path"
fi
YADM_OVERRIDE_CONFIG="$2"
shift
;;
--yadm-encrypt) #; override the standard YADM_ENCRYPT
if [[ ! "$2" =~ ^/ ]] ; then
error_out "You must specify a fully qualified encrypt path"
fi
YADM_OVERRIDE_ENCRYPT="$2"
shift
;;
--yadm-archive) #; override the standard YADM_ARCHIVE
if [[ ! "$2" =~ ^/ ]] ; then
error_out "You must specify a fully qualified archive path"
fi
YADM_OVERRIDE_ARCHIVE="$2"
shift
;;
--yadm-bootstrap) #; override the standard YADM_BOOTSTRAP
if [[ ! "$2" =~ ^/ ]] ; then
error_out "You must specify a fully qualified bootstrap path"
fi
YADM_OVERRIDE_BOOTSTRAP="$2"
shift
;;
*) #; main arguments are kept intact
MAIN_ARGS+=("$1")
;;
esac
shift
done
}
function configure_paths() {
#; change all paths to be relative to YADM_DIR
YADM_REPO="$YADM_DIR/$YADM_REPO"
YADM_CONFIG="$YADM_DIR/$YADM_CONFIG"
YADM_ENCRYPT="$YADM_DIR/$YADM_ENCRYPT"
YADM_ARCHIVE="$YADM_DIR/$YADM_ARCHIVE"
YADM_BOOTSTRAP="$YADM_DIR/$YADM_BOOTSTRAP"
#; independent overrides for paths
if [ -n "$YADM_OVERRIDE_REPO" ]; then
YADM_REPO="$YADM_OVERRIDE_REPO"
fi
if [ -n "$YADM_OVERRIDE_CONFIG" ]; then
YADM_CONFIG="$YADM_OVERRIDE_CONFIG"
fi
if [ -n "$YADM_OVERRIDE_ENCRYPT" ]; then
YADM_ENCRYPT="$YADM_OVERRIDE_ENCRYPT"
fi
if [ -n "$YADM_OVERRIDE_ARCHIVE" ]; then
YADM_ARCHIVE="$YADM_OVERRIDE_ARCHIVE"
fi
if [ -n "$YADM_OVERRIDE_BOOTSTRAP" ]; then
YADM_BOOTSTRAP="$YADM_OVERRIDE_BOOTSTRAP"
fi
#; use the yadm repo for all git operations
GIT_DIR=$(mixed_path "$YADM_REPO")
export GIT_DIR
}
function configure_repo() {
debug "Configuring new repo"
#; change bare to false (there is a working directory)
"$GIT_PROGRAM" config core.bare 'false'
#; set the worktree for the yadm repo
"$GIT_PROGRAM" config core.worktree "$(mixed_path "$YADM_WORK")"
#; by default, do not show untracked files and directories
"$GIT_PROGRAM" config status.showUntrackedFiles no
#; possibly used later to ensure we're working on the yadm repo
"$GIT_PROGRAM" config yadm.managed 'true'
}
function set_operating_system() {
#; special detection of WSL (windows subsystem for linux)
local proc_version
proc_version=$(cat "$PROC_VERSION" 2>/dev/null)
if [[ "$proc_version" =~ Microsoft ]]; then
OPERATING_SYSTEM="WSL"
else
OPERATING_SYSTEM=$(uname -s)
fi
case "$OPERATING_SYSTEM" in
CYGWIN*)
git_version=$(git --version 2>/dev/null)
if [[ "$git_version" =~ windows ]] ; then
USE_CYGPATH=1
fi
;;
*)
;;
esac
}
function debug() {
[ -n "$DEBUG" ] && echo_e "DEBUG: $*"
}
function error_out() {
echo_e "ERROR: $*"
exit_with_hook 1
}
function exit_with_hook() {
invoke_hook "post" "$1"
exit "$1"
}
function invoke_hook() {
mode="$1"
exit_status="$2"
hook_command="$YADM_DIR/hooks/${mode}_$HOOK_COMMAND"
if [ -x "$hook_command" ] ; then
debug "Invoking hook: $hook_command"
#; expose some internal data to all hooks
work=$(unix_path "$("$GIT_PROGRAM" config core.worktree)")
YADM_HOOK_COMMAND=$HOOK_COMMAND
YADM_HOOK_EXIT=$exit_status
YADM_HOOK_FULL_COMMAND=$FULL_COMMAND
YADM_HOOK_REPO=$YADM_REPO
YADM_HOOK_WORK=$work
export YADM_HOOK_COMMAND
export YADM_HOOK_EXIT
export YADM_HOOK_FULL_COMMAND
export YADM_HOOK_REPO
export YADM_HOOK_WORK
"$hook_command"
hook_status=$?
#; failing "pre" hooks will prevent commands from being run
if [ "$mode" = "pre" ] && [ "$hook_status" -ne 0 ]; then
echo "Hook $hook_command was not successful"
echo "$HOOK_COMMAND will not be run"
exit "$hook_status"
fi
fi
}
function assert_private_dirs() {
work=$(unix_path "$("$GIT_PROGRAM" config core.worktree)")
for private_dir in "$@"; do
if [ ! -d "$work/$private_dir" ]; then
debug "Creating $work/$private_dir"
#shellcheck disable=SC2174
mkdir -m 0700 -p "$work/$private_dir" >/dev/null 2>&1
fi
done
}
function display_private_perms() {
when="$1"
for private_dir in .ssh .gnupg; do
if [ -d "$YADM_WORK/$private_dir" ]; then
private_perms=$(ls -ld "$YADM_WORK/$private_dir")
debug "$when" private dir perms "$private_perms"
fi
done
}
function cd_work() {
YADM_WORK=$(unix_path "$("$GIT_PROGRAM" config core.worktree)")
cd "$YADM_WORK" || {
debug "$1 not processed, unable to cd to $YADM_WORK"
return 1
}
return 0
}
function parse_encrypt() {
if [ "$ENCRYPT_INCLUDE_FILES" != "unparsed" ]; then
#shellcheck disable=SC2034
PARSE_ENCRYPT_SHORT="parse_encrypt() not reprocessed"
return
fi
ENCRYPT_INCLUDE_FILES=()
ENCRYPT_EXCLUDE_FILES=()
cd_work "Parsing encrypt" || return
exclude_pattern="^!(.+)"
if [ -f "$YADM_ENCRYPT" ] ; then
#; parse both included/excluded
while IFS='' read -r line || [ -n "$line" ]; do
if [[ ! $line =~ ^# && ! $line =~ ^[[:space:]]*$ ]] ; then
local IFS=$'\n'
for pattern in $line; do
if [[ "$pattern" =~ $exclude_pattern ]]; then
for ex_file in ${BASH_REMATCH[1]}; do
if [ -e "$ex_file" ]; then
ENCRYPT_EXCLUDE_FILES+=("$ex_file")
fi
done
else
for in_file in $pattern; do
if [ -e "$in_file" ]; then
ENCRYPT_INCLUDE_FILES+=("$in_file")
fi
done
fi
done
fi
done < "$YADM_ENCRYPT"
#; remove excludes from the includes
#(SC2068 is disabled because in this case, we desire globbing)
FINAL_INCLUDE=()
#shellcheck disable=SC2068
for included in "${ENCRYPT_INCLUDE_FILES[@]}"; do
skip=
#shellcheck disable=SC2068
for ex_file in ${ENCRYPT_EXCLUDE_FILES[@]}; do
[ "$included" == "$ex_file" ] && { skip=1; break; }
done
[ -n "$skip" ] || FINAL_INCLUDE+=("$included")
done
ENCRYPT_INCLUDE_FILES=("${FINAL_INCLUDE[@]}")
fi
}
#; ****** Auto Functions ******
function auto_alt() {
#; process alternates if there are possible changes
if [ "$CHANGES_POSSIBLE" = "1" ] ; then
auto_alt=$(config --bool yadm.auto-alt)
if [ "$auto_alt" != "false" ] ; then
[ -d "$YADM_REPO" ] && alt
fi
fi
}
function auto_perms() {
#; process permissions if there are possible changes
if [ "$CHANGES_POSSIBLE" = "1" ] ; then
auto_perms=$(config --bool yadm.auto-perms)
if [ "$auto_perms" != "false" ] ; then
[ -d "$YADM_REPO" ] && perms
fi
fi
}
function auto_bootstrap() {
bootstrap_available || return
[ "$DO_BOOTSTRAP" -eq 0 ] && return
[ "$DO_BOOTSTRAP" -eq 3 ] && return
[ "$DO_BOOTSTRAP" -eq 2 ] && bootstrap
if [ "$DO_BOOTSTRAP" -eq 1 ] ; then
echo "Found $YADM_BOOTSTRAP"
echo "It appears that a bootstrap program exists."
echo "Would you like to execute it now? (y/n)"
read -r answer < /dev/tty