| 
									
										
										
										
											2016-06-18 15:33:49 +00:00
										 |  |  | #!/bin/sh | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | # yadm - Yet Another Dotfiles Manager | 
					
						
							| 
									
										
										
										
											2024-11-08 19:16:29 +00:00
										 |  |  | # Copyright (C) 2015-2024 Tim Byrne | 
					
						
							| 
									
										
										
										
											2025-02-09 21:03:16 +00:00
										 |  |  | # Copyright (C) 2025 Erik Flodin | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # 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 | 
					
						
							| 
									
										
										
										
											2019-03-14 23:14:10 +00:00
										 |  |  | # the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | # (at your option) any later version. | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | # 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. | 
					
						
							| 
									
										
										
										
											2019-03-14 23:14:10 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							| 
									
										
										
										
											2019-03-14 23:14:10 +00:00
										 |  |  | # along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-15 18:39:27 +00:00
										 |  |  | # shellcheck shell=bash | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  | # execute script with bash (shebang line is /bin/sh for portability) | 
					
						
							| 
									
										
										
										
											2016-06-18 15:33:49 +00:00
										 |  |  | if [ -z "$BASH_VERSION" ]; then | 
					
						
							|  |  |  |   [ "$YADM_TEST" != 1 ] && exec bash "$0" "$@" | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 23:01:45 +00:00
										 |  |  | VERSION=3.5.0 | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | YADM_WORK="$HOME" | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:45 +00:00
										 |  |  | YADM_DIR= | 
					
						
							| 
									
										
										
										
											2020-11-15 20:23:49 +00:00
										 |  |  | YADM_DATA= | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | YADM_LEGACY_DIR="${HOME}/.yadm" | 
					
						
							| 
									
										
										
										
											2020-11-18 05:01:45 +00:00
										 |  |  | YADM_LEGACY_ARCHIVE="files.gpg" | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:45 +00:00
										 |  |  | # these are the default paths relative to YADM_DIR | 
					
						
							| 
									
										
										
										
											2016-03-23 07:41:12 +00:00
										 |  |  | YADM_CONFIG="config" | 
					
						
							|  |  |  | YADM_ENCRYPT="encrypt" | 
					
						
							| 
									
										
										
										
											2017-01-23 23:23:06 +00:00
										 |  |  | YADM_BOOTSTRAP="bootstrap" | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:01 +00:00
										 |  |  | YADM_HOOKS="hooks" | 
					
						
							| 
									
										
										
										
											2019-10-10 13:23:36 +00:00
										 |  |  | YADM_ALT="alt" | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-15 20:23:49 +00:00
										 |  |  | # these are the default paths relative to YADM_DATA | 
					
						
							|  |  |  | YADM_REPO="repo.git" | 
					
						
							|  |  |  | YADM_ARCHIVE="archive" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-03 21:25:23 +00:00
										 |  |  | HOOK_COMMAND="" | 
					
						
							| 
									
										
										
										
											2017-07-05 12:58:40 +00:00
										 |  |  | FULL_COMMAND="" | 
					
						
							| 
									
										
										
										
											2017-07-03 21:25:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-13 22:17:16 +00:00
										 |  |  | GPG_PROGRAM="gpg" | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  | OPENSSL_PROGRAM="openssl" | 
					
						
							| 
									
										
										
										
											2017-01-08 01:44:43 +00:00
										 |  |  | GIT_PROGRAM="git" | 
					
						
							| 
									
										
										
										
											2019-12-05 04:18:22 +00:00
										 |  |  | AWK_PROGRAM=("gawk" "awk") | 
					
						
							| 
									
										
										
										
											2019-12-16 00:42:21 +00:00
										 |  |  | GIT_CRYPT_PROGRAM="git-crypt" | 
					
						
							| 
									
										
										
										
											2020-02-21 14:05:56 +00:00
										 |  |  | TRANSCRYPT_PROGRAM="transcrypt" | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | J2CLI_PROGRAM="j2" | 
					
						
							| 
									
										
										
										
											2017-03-31 04:51:23 +00:00
										 |  |  | ENVTPL_PROGRAM="envtpl" | 
					
						
							| 
									
										
										
										
											2020-05-20 02:27:14 +00:00
										 |  |  | ESH_PROGRAM="esh" | 
					
						
							| 
									
										
										
										
											2017-07-03 21:21:27 +00:00
										 |  |  | LSB_RELEASE_PROGRAM="lsb_release" | 
					
						
							| 
									
										
										
										
											2016-08-13 22:17:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-30 16:52:18 +00:00
										 |  |  | OS_RELEASE="/etc/os-release" | 
					
						
							| 
									
										
										
										
											2017-04-09 12:56:27 +00:00
										 |  |  | PROC_VERSION="/proc/version" | 
					
						
							|  |  |  | OPERATING_SYSTEM="Unknown" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-18 12:39:10 +00:00
										 |  |  | ENCRYPT_INCLUDE_FILES="unparsed" | 
					
						
							| 
									
										
										
										
											2025-02-10 19:58:01 +00:00
										 |  |  | NO_ENCRYPT_TRACKED_FILES=() | 
					
						
							| 
									
										
										
										
											2017-09-18 12:39:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  | LEGACY_WARNING_ISSUED=0 | 
					
						
							|  |  |  | INVALID_ALT=() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 06:20:44 +00:00
										 |  |  | GPG_OPTS=() | 
					
						
							| 
									
										
										
										
											2020-09-25 14:25:38 +00:00
										 |  |  | OPENSSL_OPTS=() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  | # flag causing path translations with cygpath | 
					
						
							| 
									
										
										
										
											2017-01-09 01:04:40 +00:00
										 |  |  | USE_CYGPATH=0 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  | # flag when something may have changes (which prompts auto actions to be performed) | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | CHANGES_POSSIBLE=0 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  | # flag when a bootstrap should be performed after cloning | 
					
						
							|  |  |  | # 0: skip auto_bootstrap, 1: ask, 2: perform bootstrap, 3: prevent bootstrap | 
					
						
							| 
									
										
										
										
											2017-01-25 07:07:07 +00:00
										 |  |  | DO_BOOTSTRAP=0 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | function main() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   require_git | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # capture full command, for passing to hooks | 
					
						
							| 
									
										
										
										
											2020-01-23 14:11:19 +00:00
										 |  |  |   # 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// /\\ }" | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     _fc+=("$param") | 
					
						
							| 
									
										
										
										
											2020-01-23 14:11:19 +00:00
										 |  |  |   done | 
					
						
							|  |  |  |   FULL_COMMAND="${_fc[*]}" | 
					
						
							| 
									
										
										
										
											2017-07-05 12:58:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-15 20:23:49 +00:00
										 |  |  |   # create the YADM_DIR & YADM_DATA if they doesn't exist yet | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   [ -d "$YADM_DIR" ] || mkdir -p "$YADM_DIR" | 
					
						
							| 
									
										
										
										
											2020-11-15 20:23:49 +00:00
										 |  |  |   [ -d "$YADM_DATA" ] || mkdir -p "$YADM_DATA" | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # parse command line arguments | 
					
						
							| 
									
										
										
										
											2017-01-06 22:27:24 +00:00
										 |  |  |   local retval=0 | 
					
						
							| 
									
										
										
										
											2020-12-21 22:53:44 +00:00
										 |  |  |   internal_commands="^(alt|bootstrap|clean|clone|config|decrypt|encrypt|enter|git-crypt|help|--help|init|introspect|list|perms|transcrypt|upgrade|version|--version)$" | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if [ -z "$*" ]; then | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |     # no argumnts will result in help() | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |     help | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   elif [[ "$1" =~ $internal_commands ]]; then | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |     # for internal commands, process all of the arguments | 
					
						
							| 
									
										
										
										
											2020-12-21 22:53:44 +00:00
										 |  |  |     YADM_COMMAND="${1//-/_}" | 
					
						
							|  |  |  |     YADM_COMMAND="${YADM_COMMAND/__/}" | 
					
						
							| 
									
										
										
										
											2016-04-05 13:52:21 +00:00
										 |  |  |     YADM_ARGS=() | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |     shift | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-16 00:42:21 +00:00
										 |  |  |     # commands listed below do not process any of the parameters | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     if [[ "$YADM_COMMAND" =~ ^(enter|git_crypt)$ ]]; then | 
					
						
							| 
									
										
										
										
											2019-12-16 00:42:21 +00:00
										 |  |  |       YADM_ARGS=("$@") | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |       while [[ $# -gt 0 ]]; do | 
					
						
							| 
									
										
										
										
											2019-12-16 00:42:21 +00:00
										 |  |  |         key="$1" | 
					
						
							|  |  |  |         case $key in | 
					
						
							|  |  |  |           -a) # used by list() | 
					
						
							|  |  |  |             LIST_ALL="YES" | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |             ;; | 
					
						
							| 
									
										
										
										
											2019-12-16 00:42:21 +00:00
										 |  |  |           -d) # used by all commands | 
					
						
							|  |  |  |             DEBUG="YES" | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |             ;; | 
					
						
							| 
									
										
										
										
											2021-01-03 23:33:38 +00:00
										 |  |  |           -f) # used by init(), clone() and upgrade() | 
					
						
							| 
									
										
										
										
											2019-12-16 00:42:21 +00:00
										 |  |  |             FORCE="YES" | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |             ;; | 
					
						
							| 
									
										
										
										
											2019-12-16 00:42:21 +00:00
										 |  |  |           -l) # used by decrypt() | 
					
						
							|  |  |  |             DO_LIST="YES" | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  |             [[ "$YADM_COMMAND" =~ ^(clone|config)$ ]] && YADM_ARGS+=("$1") | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |             ;; | 
					
						
							| 
									
										
										
										
											2019-12-16 00:42:21 +00:00
										 |  |  |           -w) # used by init() and clone() | 
					
						
							| 
									
										
										
										
											2021-01-07 17:59:41 +00:00
										 |  |  |             YADM_WORK="$(qualify_path "$2" "work tree")" | 
					
						
							| 
									
										
										
										
											2019-12-16 00:42:21 +00:00
										 |  |  |             shift | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |             ;; | 
					
						
							| 
									
										
										
										
											2019-12-16 00:42:21 +00:00
										 |  |  |           *) # any unhandled arguments | 
					
						
							|  |  |  |             YADM_ARGS+=("$1") | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |             ;; | 
					
						
							| 
									
										
										
										
											2019-12-16 00:42:21 +00:00
										 |  |  |         esac | 
					
						
							|  |  |  |         shift | 
					
						
							|  |  |  |       done | 
					
						
							|  |  |  |     fi | 
					
						
							| 
									
										
										
										
											2016-04-05 13:52:21 +00:00
										 |  |  |     [ ! -d "$YADM_WORK" ] && error_out "Work tree does not exist: [$YADM_WORK]" | 
					
						
							| 
									
										
										
										
											2017-07-03 21:25:23 +00:00
										 |  |  |     HOOK_COMMAND="$YADM_COMMAND" | 
					
						
							|  |  |  |     invoke_hook "pre" | 
					
						
							| 
									
										
										
										
											2016-04-05 13:52:21 +00:00
										 |  |  |     $YADM_COMMAND "${YADM_ARGS[@]}" | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |     # any other commands are simply passed through to git | 
					
						
							| 
									
										
										
										
											2017-07-03 21:25:23 +00:00
										 |  |  |     HOOK_COMMAND="$1" | 
					
						
							|  |  |  |     invoke_hook "pre" | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |     git_command "$@" | 
					
						
							| 
									
										
										
										
											2017-01-06 22:27:24 +00:00
										 |  |  |     retval="$?" | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # process automatic events | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |   auto_alt | 
					
						
							|  |  |  |   auto_perms | 
					
						
							| 
									
										
										
										
											2017-01-25 07:07:07 +00:00
										 |  |  |   auto_bootstrap | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-03 21:25:23 +00:00
										 |  |  |   exit_with_hook $retval | 
					
						
							| 
									
										
										
										
											2016-03-24 00:13:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | # ****** Alternate Processing ****** | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function score_file() { | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |   local source="$1" | 
					
						
							|  |  |  |   local target="$2" | 
					
						
							|  |  |  |   local conditions="${source#*##}" | 
					
						
							| 
									
										
										
										
											2019-10-10 13:23:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  |   score=0 | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |   local template_processor="" | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   IFS=',' read -ra fields <<<"$conditions" | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  |   for field in "${fields[@]}"; do | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |     local label=${field%%.*} | 
					
						
							|  |  |  |     local value=${field#*.} | 
					
						
							| 
									
										
										
										
											2019-10-12 14:14:45 +00:00
										 |  |  |     [ "$field" = "$label" ] && value="" # when .value is omitted | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-02 21:05:15 +00:00
										 |  |  |     # Check for negative condition prefix (e.g., "~<label>") | 
					
						
							|  |  |  |     local negate=0 | 
					
						
							|  |  |  |     if [ "${label:0:1}" = "~" ]; then | 
					
						
							|  |  |  |       negate=1 | 
					
						
							|  |  |  |       label="${label:1}" | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-09 22:50:49 +00:00
										 |  |  |     shopt -s nocasematch | 
					
						
							| 
									
										
										
										
											2025-03-02 21:05:15 +00:00
										 |  |  |     local -i delta=$((negate ? 1 : -1)) | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |     case "$label" in | 
					
						
							|  |  |  |       default) | 
					
						
							| 
									
										
										
										
											2025-03-02 21:18:09 +00:00
										 |  |  |         if ((negate)); then | 
					
						
							|  |  |  |           INVALID_ALT+=("$source") | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           delta=0 | 
					
						
							|  |  |  |         fi | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |       a | arch) | 
					
						
							| 
									
										
										
										
											2025-03-02 21:05:15 +00:00
										 |  |  |         [[ "$value" = "$local_arch" ]] && delta=1 || delta=-1 | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |       o | os) | 
					
						
							| 
									
										
										
										
											2025-03-02 21:05:15 +00:00
										 |  |  |         [[ "$value" = "$local_system" ]] && delta=2 || delta=-2 | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |       d | distro) | 
					
						
							| 
									
										
										
										
											2025-03-02 21:05:15 +00:00
										 |  |  |         [[ "${value// /_}" = "${local_distro// /_}" ]] && delta=4 || delta=-4 | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |       f | distro_family) | 
					
						
							| 
									
										
										
										
											2025-03-02 21:05:15 +00:00
										 |  |  |         [[ "${value// /_}" = "${local_distro_family// /_}" ]] && delta=8 || delta=-8 | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |       c | class) | 
					
						
							| 
									
										
										
										
											2025-03-02 21:05:15 +00:00
										 |  |  |         in_list "$value" "${local_classes[@]}" && delta=16 || delta=-16 | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |       h | hostname) | 
					
						
							| 
									
										
										
										
											2025-03-02 21:05:15 +00:00
										 |  |  |         [[ "$value" = "$local_host" ]] && delta=32 || delta=-32 | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |       u | user) | 
					
						
							| 
									
										
										
										
											2025-03-02 21:05:15 +00:00
										 |  |  |         [[ "$value" = "$local_user" ]] && delta=64 || delta=-64 | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |       e | extension) | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |         # extension isn't a condition and doesn't affect the score | 
					
						
							|  |  |  |         continue | 
					
						
							|  |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |       t | template | yadm) | 
					
						
							| 
									
										
										
										
											2025-03-02 21:18:09 +00:00
										 |  |  |         if [ -d "$source" ] || ((negate)); then | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |           INVALID_ALT+=("$source") | 
					
						
							|  |  |  |         else | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |           template_processor=$(choose_template_processor "$value") | 
					
						
							|  |  |  |           if [ -n "$template_processor" ]; then | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |             delta=0 | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |           elif [ -n "$loud" ]; then | 
					
						
							|  |  |  |             echo "No supported template processor for template $source" | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |           else | 
					
						
							|  |  |  |             debug "No supported template processor for template $source" | 
					
						
							|  |  |  |           fi | 
					
						
							|  |  |  |         fi | 
					
						
							|  |  |  |         ;; | 
					
						
							|  |  |  |       *) | 
					
						
							|  |  |  |         INVALID_ALT+=("$source") | 
					
						
							|  |  |  |         ;; | 
					
						
							|  |  |  |     esac | 
					
						
							| 
									
										
										
										
											2024-12-09 22:50:49 +00:00
										 |  |  |     shopt -u nocasematch | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-02 21:05:15 +00:00
										 |  |  |     ((negate)) && delta=$((-delta)) | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     if ((delta < 0)); then | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  |       score=0 | 
					
						
							|  |  |  |       return | 
					
						
							|  |  |  |     fi | 
					
						
							| 
									
										
										
										
											2025-03-02 21:18:09 +00:00
										 |  |  |     score=$((score + delta + (negate ? 0 : 1000))) | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  |   done | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |   record_score "$score" "$target" "$source" "$template_processor" | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function record_score() { | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |   local score="$1" | 
					
						
							|  |  |  |   local target="$2" | 
					
						
							|  |  |  |   local source="$3" | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |   local template_processor="$4" | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # record nothing if the score is zero | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |   [ "$score" -eq 0 ] && [ -z "$template_processor" ] && return | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-25 13:44:44 +00:00
										 |  |  |   # search for the index of this target, to see if we already are tracking it | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |   local -i index=$((${#alt_targets[@]} - 1)) | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   for (( ; index >= 0; --index)); do | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |     if [ "${alt_targets[$index]}" = "$target" ]; then | 
					
						
							|  |  |  |       break | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  |     fi | 
					
						
							|  |  |  |   done | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if [ $index -lt 0 ]; then | 
					
						
							| 
									
										
										
										
											2020-07-08 07:03:52 +00:00
										 |  |  |     # $YADM_CONFIG must be processed first, in case other templates lookup yadm configurations | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |     if [ "$target" = "$YADM_CONFIG" ]; then | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |       alt_targets=("$target" "${alt_targets[@]}") | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |       alt_sources=("$source" "${alt_sources[@]}") | 
					
						
							|  |  |  |       alt_scores=("$score" "${alt_scores[@]}") | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |       alt_template_processors=("$template_processor" "${alt_template_processors[@]}") | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |     else | 
					
						
							|  |  |  |       alt_targets+=("$target") | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |       alt_sources+=("$source") | 
					
						
							|  |  |  |       alt_scores+=("$score") | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |       alt_template_processors+=("$template_processor") | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |     fi | 
					
						
							|  |  |  |     return | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |   if [[ -n "${alt_template_processors[$index]}" ]]; then | 
					
						
							|  |  |  |     if [[ -z "$template_processor" || "$score" -lt "${alt_scores[$index]}" ]]; then | 
					
						
							|  |  |  |       # Not template, or template but lower score | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |       return | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  |     fi | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |   elif [[ -z "$template_processor" && "$score" -le "${alt_scores[$index]}" ]]; then | 
					
						
							|  |  |  |     # Not template and too low score | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |     return | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |   # Record new alt | 
					
						
							|  |  |  |   alt_sources[index]="$source" | 
					
						
							|  |  |  |   alt_scores[index]="$score" | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |   alt_template_processors[index]="$template_processor" | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  | function choose_template_processor() { | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |   local kind="$1" | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-09 22:50:49 +00:00
										 |  |  |   if [[ "${kind:-default}" = "default" ]]; then | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |     awk_available && echo "default" | 
					
						
							| 
									
										
										
										
											2024-12-09 22:50:49 +00:00
										 |  |  |   elif [[ "$kind" = "esh" ]]; then | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |     esh_available && echo "esh" | 
					
						
							| 
									
										
										
										
											2024-12-09 22:50:49 +00:00
										 |  |  |   elif [[ "$kind" = "j2cli" || "$kind" = "j2" ]] && j2cli_available; then | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |     echo "j2cli" | 
					
						
							| 
									
										
										
										
											2024-12-09 22:50:49 +00:00
										 |  |  |   elif [[ "$kind" = "envtpl" || "$kind" = "j2" ]] && envtpl_available; then | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |     echo "envtpl" | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # ****** Template Processors ****** | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  | function template() { | 
					
						
							|  |  |  |   local processor="$1" | 
					
						
							|  |  |  |   local input="$2" | 
					
						
							|  |  |  |   local output="$3" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   local content | 
					
						
							|  |  |  |   if ! content=$("template_$processor" "$input"); then | 
					
						
							|  |  |  |     echo "Error: failed to process template '$input'" >&2 | 
					
						
							|  |  |  |     return | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if [ -r "$output" ] && [ "$content" = "$(<"$output")" ]; then | 
					
						
							|  |  |  |     debug "Template output '$output' is unchanged" | 
					
						
							|  |  |  |     return | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # If the output file 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. | 
					
						
							|  |  |  |   if [ ! -w "$output" ] && [ -e "$output" ]; then | 
					
						
							|  |  |  |     chmod u+w "$output" | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if [ -n "$loud" ]; then | 
					
						
							|  |  |  |     echo "Creating $output from template $input" | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     debug "Creating $output from template $input" | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   local temp_file="${output}.$$.$RANDOM" | 
					
						
							|  |  |  |   if cat >"$temp_file" <<<"$content" && mv -f "$temp_file" "$output"; then | 
					
						
							|  |  |  |     copy_perms "$input" "$output" | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     echo "Error: failed to create template output '$output'" | 
					
						
							|  |  |  |     rm -f "$temp_file" | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-30 22:29:17 +00:00
										 |  |  | function template_default() { | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |   local input="$1" | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-01 13:17:28 +00:00
										 |  |  |   # the explicit "space + tab" character class used below is used because not | 
					
						
							|  |  |  |   # all versions of awk seem to support the POSIX character classes [[:blank:]] | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |   local awk_pgm | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   read -r -d '' awk_pgm <<"EOF" | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | BEGIN { | 
					
						
							| 
									
										
										
										
											2024-11-08 18:51:25 +00:00
										 |  |  |   classes = ARGV[2] | 
					
						
							|  |  |  |   for (i = 3; i < ARGC; ++i) { | 
					
						
							|  |  |  |     classes = classes "\n" ARGV[i] | 
					
						
							| 
									
										
										
										
											2020-10-09 13:36:58 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-10-27 12:38:12 +00:00
										 |  |  |   yadm["class"] = class | 
					
						
							|  |  |  |   yadm["classes"] = classes | 
					
						
							|  |  |  |   yadm["arch"] = arch | 
					
						
							|  |  |  |   yadm["os"] = os | 
					
						
							|  |  |  |   yadm["hostname"] = host | 
					
						
							|  |  |  |   yadm["user"] = user | 
					
						
							|  |  |  |   yadm["distro"] = distro | 
					
						
							|  |  |  |   yadm["distro_family"] = distro_family | 
					
						
							| 
									
										
										
										
											2025-02-25 22:47:08 +00:00
										 |  |  |   yadm["source"] = ARGV[1] | 
					
						
							| 
									
										
										
										
											2024-10-27 12:38:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   VARIABLE = "(env|yadm)\\.[a-zA-Z0-9_]+" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   current = 0 | 
					
						
							|  |  |  |   filename[current] = ARGV[1] | 
					
						
							|  |  |  |   line[current] = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   level = 0 | 
					
						
							|  |  |  |   skip[level] = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (; current >= 0; --current) { | 
					
						
							|  |  |  |     while ((res = getline <filename[current]) > 0) { | 
					
						
							|  |  |  |       ++line[current] | 
					
						
							|  |  |  |       if ($0 ~ "^[ \t]*\\{%[ \t]*if[ \t]+" VARIABLE "[ \t]*[!=]=[ \t]*\".*\"[ \t]*%\\}$") { | 
					
						
							|  |  |  |         if (skip[level]) { skip[++level] = 1; continue } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         match($0, VARIABLE) | 
					
						
							|  |  |  |         lhs = substr($0, RSTART, RLENGTH) | 
					
						
							|  |  |  |         match($0, /[!=]=/) | 
					
						
							|  |  |  |         op = substr($0, RSTART, RLENGTH) | 
					
						
							|  |  |  |         match($0, /".*"/) | 
					
						
							| 
									
										
										
										
											2024-12-09 22:50:49 +00:00
										 |  |  |         rhs = tolower(replace_vars(substr($0, RSTART + 1, RLENGTH - 2))) | 
					
						
							| 
									
										
										
										
											2024-10-27 12:38:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (lhs == "yadm.class") { | 
					
						
							|  |  |  |           lhs = "not" rhs | 
					
						
							|  |  |  |           split(classes, cls_array, "\n") | 
					
						
							|  |  |  |           for (idx in cls_array) { | 
					
						
							| 
									
										
										
										
											2024-12-09 22:50:49 +00:00
										 |  |  |             if (rhs == tolower(cls_array[idx])) { lhs = rhs; break } | 
					
						
							| 
									
										
										
										
											2024-10-27 12:38:12 +00:00
										 |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2024-12-09 22:50:49 +00:00
										 |  |  |           lhs = tolower(replace_vars("{{" lhs "}}")) | 
					
						
							| 
									
										
										
										
											2024-10-27 12:38:12 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (op == "==") { skip[++level] = lhs != rhs } | 
					
						
							|  |  |  |         else { skip[++level] = lhs == rhs } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       else if (/^[ \t]*\{%[ \t]*else[ \t]*%\}$/) { | 
					
						
							|  |  |  |         if (level == 0 || skip[level] < 0) { error("else without matching if") } | 
					
						
							|  |  |  |         skip[level] = skip[level] ? skip[level - 1] : -1 | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       else if (/^[ \t]*\{%[ \t]*endif[ \t]*%\}$/) { | 
					
						
							|  |  |  |         if (--level < 0) { error("endif without matching if") } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       else if (!skip[level]) { | 
					
						
							|  |  |  |         $0 = replace_vars($0) | 
					
						
							|  |  |  |         if (match($0, /^[ \t]*\{%[ \t]*include[ \t]+("[^"]+"|[^"]+)[ \t]*%\}$/)) { | 
					
						
							|  |  |  |           include = $0 | 
					
						
							|  |  |  |           sub(/^[ \t]*\{%[ \t]*include[ \t]+"?/, "", include) | 
					
						
							|  |  |  |           sub(/"?[ \t]*%\}$/, "", include) | 
					
						
							|  |  |  |           if (index(include, "/") != 1) { | 
					
						
							|  |  |  |             include = source_dir "/" include | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           filename[++current] = include | 
					
						
							|  |  |  |           line[current] = 0 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { print } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (res >= 0) { close(filename[current]) } | 
					
						
							|  |  |  |     else if (current == 0) { error("could not read input file") } | 
					
						
							|  |  |  |     else { --current; error("could not read include file '" filename[current + 1] "'") } | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-10-27 12:38:12 +00:00
										 |  |  |   if (level > 0) { | 
					
						
							|  |  |  |     current = 0 | 
					
						
							|  |  |  |     error("unterminated if") | 
					
						
							| 
									
										
										
										
											2021-06-06 17:37:14 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-10-27 12:38:12 +00:00
										 |  |  |   exit 0 | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2024-10-27 12:38:12 +00:00
										 |  |  | function error(text) { | 
					
						
							|  |  |  |   printf "%s:%d: error: %s\n", | 
					
						
							|  |  |  |     filename[current], line[current], text > "/dev/stderr" | 
					
						
							|  |  |  |   exit 1 | 
					
						
							| 
									
										
										
										
											2021-01-10 21:38:42 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2024-10-27 12:38:12 +00:00
										 |  |  | function replace_vars(input) { | 
					
						
							|  |  |  |   output = "" | 
					
						
							|  |  |  |   while (match(input, "\\{\\{[ \t]*" VARIABLE "[ \t]*\\}\\}")) { | 
					
						
							|  |  |  |     if (RSTART > 1) { | 
					
						
							|  |  |  |       output = output substr(input, 0, RSTART - 1) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     data = substr(input, RSTART + 2, RLENGTH - 4) | 
					
						
							|  |  |  |     input = substr(input, RSTART + RLENGTH) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     gsub(/[ \t]+/, "", data) | 
					
						
							|  |  |  |     split(data, fields, /\./) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (fields[1] == "env") { | 
					
						
							|  |  |  |       output = output ENVIRON[fields[2]] | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-25 22:47:08 +00:00
										 |  |  |     else if (fields[2] == "filename") { | 
					
						
							|  |  |  |       output = output filename[current] | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-10-27 12:38:12 +00:00
										 |  |  |     else { | 
					
						
							|  |  |  |       output = output yadm[fields[2]] | 
					
						
							| 
									
										
										
										
											2021-01-10 21:38:42 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-10-27 12:38:12 +00:00
										 |  |  |   return output input | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | } | 
					
						
							|  |  |  | EOF | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |   "${AWK_PROGRAM[0]}" \ | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  |     -v class="$local_class" \ | 
					
						
							| 
									
										
										
										
											2020-02-22 01:10:39 +00:00
										 |  |  |     -v arch="$local_arch" \ | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  |     -v os="$local_system" \ | 
					
						
							|  |  |  |     -v host="$local_host" \ | 
					
						
							|  |  |  |     -v user="$local_user" \ | 
					
						
							|  |  |  |     -v distro="$local_distro" \ | 
					
						
							| 
									
										
										
										
											2022-01-17 19:46:31 +00:00
										 |  |  |     -v distro_family="$local_distro_family" \ | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |     -v source_dir="$(builtin_dirname "$input")" \ | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  |     "$awk_pgm" \ | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |     "$input" "${local_classes[@]}" | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function template_j2cli() { | 
					
						
							| 
									
										
										
										
											2023-10-11 21:10:21 +00:00
										 |  |  |   local input="$1" | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |   YADM_CLASS="$local_class" \ | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     YADM_ARCH="$local_arch" \ | 
					
						
							|  |  |  |     YADM_OS="$local_system" \ | 
					
						
							|  |  |  |     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[@]}")" \ | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |     "$J2CLI_PROGRAM" "$input" | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function template_envtpl() { | 
					
						
							| 
									
										
										
										
											2023-10-11 21:10:21 +00:00
										 |  |  |   local input="$1" | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |   YADM_CLASS="$local_class" \ | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     YADM_ARCH="$local_arch" \ | 
					
						
							|  |  |  |     YADM_OS="$local_system" \ | 
					
						
							|  |  |  |     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[@]}")" \ | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |     "$ENVTPL_PROGRAM" -o - --keep-template "$input" | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-20 02:27:14 +00:00
										 |  |  | function template_esh() { | 
					
						
							| 
									
										
										
										
											2023-10-11 21:10:21 +00:00
										 |  |  |   local input="$1" | 
					
						
							| 
									
										
										
										
											2020-05-20 02:27:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |   YADM_CLASSES="$(join_string $'\n' "${local_classes[@]}")" \ | 
					
						
							| 
									
										
										
										
											2024-12-15 15:14:50 +00:00
										 |  |  |     "$ESH_PROGRAM" "$input" \ | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     YADM_CLASS="$local_class" \ | 
					
						
							|  |  |  |     YADM_ARCH="$local_arch" \ | 
					
						
							|  |  |  |     YADM_OS="$local_system" \ | 
					
						
							|  |  |  |     YADM_HOSTNAME="$local_host" \ | 
					
						
							|  |  |  |     YADM_USER="$local_user" \ | 
					
						
							|  |  |  |     YADM_DISTRO="$local_distro" \ | 
					
						
							|  |  |  |     YADM_DISTRO_FAMILY="$local_distro_family" \ | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |     YADM_SOURCE="$input" | 
					
						
							| 
									
										
										
										
											2020-05-20 02:27:14 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  | # ****** yadm Commands ****** | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | function alt() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   require_repo | 
					
						
							| 
									
										
										
										
											2017-09-18 12:39:10 +00:00
										 |  |  |   parse_encrypt | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-17 20:20:56 +00:00
										 |  |  |   # gather values for processing alternates | 
					
						
							| 
									
										
										
										
											2019-08-16 13:19:11 +00:00
										 |  |  |   local local_class | 
					
						
							| 
									
										
										
										
											2021-01-10 21:38:42 +00:00
										 |  |  |   local -a local_classes | 
					
						
							| 
									
										
										
										
											2020-02-22 01:10:39 +00:00
										 |  |  |   local local_arch | 
					
						
							| 
									
										
										
										
											2019-08-17 20:20:56 +00:00
										 |  |  |   local local_system | 
					
						
							|  |  |  |   local local_host | 
					
						
							|  |  |  |   local local_user | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  |   local local_distro | 
					
						
							| 
									
										
										
										
											2022-01-17 19:46:31 +00:00
										 |  |  |   local local_distro_family | 
					
						
							| 
									
										
										
										
											2019-08-17 20:20:56 +00:00
										 |  |  |   set_local_alt_values | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 17:51:53 +00:00
										 |  |  |   # 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 | 
					
						
							| 
									
										
										
										
											2019-10-09 13:25:02 +00:00
										 |  |  |   [ "$(config --bool yadm.alt-copy)" == "true" ] && do_copy=1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 17:51:53 +00:00
										 |  |  |   cd_work "Alternates" || return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # determine all tracked files | 
					
						
							| 
									
										
										
										
											2020-12-21 21:51:48 +00:00
										 |  |  |   local tracked_files=() | 
					
						
							| 
									
										
										
										
											2019-08-18 17:51:53 +00:00
										 |  |  |   local IFS=$'\n' | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |   for tracked_file in $("$GIT_PROGRAM" ls-files -- '*##*'); do | 
					
						
							| 
									
										
										
										
											2019-08-18 17:51:53 +00:00
										 |  |  |     tracked_files+=("$tracked_file") | 
					
						
							|  |  |  |   done | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |   local alt_targets=() | 
					
						
							|  |  |  |   local alt_sources=() | 
					
						
							|  |  |  |   local alt_scores=() | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |   local alt_template_processors=() | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # For removing stale links | 
					
						
							|  |  |  |   local possible_alt_targets=() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   local alt_source | 
					
						
							|  |  |  |   for alt_source in "${tracked_files[@]}" "${ENCRYPT_INCLUDE_FILES[@]}"; do | 
					
						
							|  |  |  |     local conditions="${alt_source#*##}" | 
					
						
							|  |  |  |     if [ "$alt_source" = "$conditions" ]; then | 
					
						
							|  |  |  |       continue | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     local target_base="${alt_source%%##*}" | 
					
						
							|  |  |  |     alt_source="${YADM_BASE}/${target_base}##${conditions%%/*}" | 
					
						
							|  |  |  |     local alt_target="${YADM_BASE}/${target_base}" | 
					
						
							|  |  |  |     if [ "${alt_target#"$YADM_ALT/"}" != "$alt_target" ]; then | 
					
						
							|  |  |  |       target_base="${alt_target#"$YADM_ALT/"}" | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  |     alt_target="${YADM_BASE}/${target_base}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ! in_list "$alt_target" "${possible_alt_targets[@]}"; then | 
					
						
							|  |  |  |       possible_alt_targets+=("$alt_target") | 
					
						
							| 
									
										
										
										
											2019-08-18 17:51:53 +00:00
										 |  |  |     fi | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     score_file "$alt_source" "$alt_target" | 
					
						
							| 
									
										
										
										
											2019-08-18 17:51:53 +00:00
										 |  |  |   done | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-21 21:51:48 +00:00
										 |  |  |   local alt_linked=() | 
					
						
							| 
									
										
										
										
											2019-08-18 17:51:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-17 22:40:43 +00:00
										 |  |  |   alt_linking | 
					
						
							| 
									
										
										
										
											2019-09-30 13:44:41 +00:00
										 |  |  |   remove_stale_links | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  |   report_invalid_alts | 
					
						
							| 
									
										
										
										
											2019-12-03 14:30:51 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  | 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 | 
					
						
							| 
									
										
										
										
											2021-01-26 03:16:37 +00:00
										 |  |  |   local msg | 
					
						
							|  |  |  |   IFS='' read -r -d '' msg <<EOF | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | **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 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   printf '%s\n' "$msg" >&2 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-30 13:44:41 +00:00
										 |  |  | function remove_stale_links() { | 
					
						
							| 
									
										
										
										
											2019-08-18 17:51:53 +00:00
										 |  |  |   # review alternate candidates for stale links | 
					
						
							| 
									
										
										
										
											2019-11-25 13:44:44 +00:00
										 |  |  |   # if a possible alt IS linked, but it's source is not part of alt_linked, | 
					
						
							| 
									
										
										
										
											2019-08-18 17:51:53 +00:00
										 |  |  |   # remove it. | 
					
						
							|  |  |  |   if readlink_available; then | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |     for stale_candidate in "${possible_alt_targets[@]}"; do | 
					
						
							| 
									
										
										
										
											2019-08-18 17:51:53 +00:00
										 |  |  |       if [ -L "$stale_candidate" ]; then | 
					
						
							| 
									
										
										
										
											2019-11-25 13:44:44 +00:00
										 |  |  |         src=$(readlink "$stale_candidate" 2>/dev/null) | 
					
						
							|  |  |  |         if [ -n "$src" ]; then | 
					
						
							| 
									
										
										
										
											2019-08-18 17:51:53 +00:00
										 |  |  |           for review_link in "${alt_linked[@]}"; do | 
					
						
							| 
									
										
										
										
											2019-11-25 13:44:44 +00:00
										 |  |  |             [ "$src" = "$review_link" ] && continue 2 | 
					
						
							| 
									
										
										
										
											2019-08-18 17:51:53 +00:00
										 |  |  |           done | 
					
						
							| 
									
										
										
										
											2019-11-13 16:17:06 +00:00
										 |  |  |           rm -f "$stale_candidate" | 
					
						
							| 
									
										
										
										
											2019-08-18 17:51:53 +00:00
										 |  |  |         fi | 
					
						
							|  |  |  |       fi | 
					
						
							|  |  |  |     done | 
					
						
							| 
									
										
										
										
											2019-08-17 20:20:56 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function set_local_alt_values() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-10 21:38:42 +00:00
										 |  |  |   local -a all_classes | 
					
						
							|  |  |  |   all_classes=$(config --get-all local.class) | 
					
						
							| 
									
										
										
										
											2022-03-17 23:43:10 +00:00
										 |  |  |   while IFS='' read -r class; do | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |     local_classes+=("$class") | 
					
						
							|  |  |  |     local_class="$class" | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   done <<<"$all_classes" | 
					
						
							| 
									
										
										
										
											2017-01-20 06:14:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-22 01:10:39 +00:00
										 |  |  |   local_arch="$(config local.arch)" | 
					
						
							| 
									
										
										
										
											2024-12-10 17:35:26 +00:00
										 |  |  |   if [[ -z "$local_arch" ]]; then | 
					
						
							| 
									
										
										
										
											2020-02-22 01:10:39 +00:00
										 |  |  |     local_arch=$(uname -m) | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-25 18:26:10 +00:00
										 |  |  |   local_system="$(config local.os)" | 
					
						
							| 
									
										
										
										
											2024-12-10 17:35:26 +00:00
										 |  |  |   if [[ -z "$local_system" ]]; then | 
					
						
							| 
									
										
										
										
											2017-04-09 12:56:27 +00:00
										 |  |  |     local_system="$OPERATING_SYSTEM" | 
					
						
							| 
									
										
										
										
											2017-01-19 01:51:28 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-31 12:55:43 +00:00
										 |  |  |   local_host="$(config local.hostname)" | 
					
						
							| 
									
										
										
										
											2024-12-10 17:35:26 +00:00
										 |  |  |   if [[ -z "$local_host" ]]; then | 
					
						
							| 
									
										
										
										
											2019-11-30 16:27:28 +00:00
										 |  |  |     local_host=$(uname -n) | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |     local_host=${local_host%%.*} # trim any domain from hostname | 
					
						
							| 
									
										
										
										
											2017-01-19 01:51:28 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-25 18:26:10 +00:00
										 |  |  |   local_user="$(config local.user)" | 
					
						
							| 
									
										
										
										
											2024-12-10 17:35:26 +00:00
										 |  |  |   if [[ -z "$local_user" ]]; then | 
					
						
							| 
									
										
										
										
											2017-03-25 18:26:10 +00:00
										 |  |  |     local_user=$(id -u -n) | 
					
						
							| 
									
										
										
										
											2017-01-19 01:51:28 +00:00
										 |  |  |   fi | 
					
						
							| 
									
										
										
										
											2019-08-16 13:19:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-10 17:35:26 +00:00
										 |  |  |   local_distro="$(config local.distro)" | 
					
						
							|  |  |  |   if [[ -z "$local_distro" ]]; then | 
					
						
							|  |  |  |     local_distro="$(query_distro)" | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   local_distro_family="$(config local.distro-family)" | 
					
						
							|  |  |  |   if [[ -z "$local_distro_family" ]]; then | 
					
						
							|  |  |  |     local_distro_family="$(query_distro_family)" | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 13:19:11 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-17 22:40:43 +00:00
										 |  |  | function alt_linking() { | 
					
						
							| 
									
										
										
										
											2025-02-27 20:18:07 +00:00
										 |  |  |   local -a exclude=() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   local log="debug" | 
					
						
							|  |  |  |   [ -n "$loud" ] && log="echo" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |   local -i index | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   for ((index = 0; index < ${#alt_targets[@]}; ++index)); do | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |     local target="${alt_targets[$index]}" | 
					
						
							|  |  |  |     local source="${alt_sources[$index]}" | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |     local template_processor="${alt_template_processors[$index]}" | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if [[ -L "$target" ]]; then | 
					
						
							|  |  |  |       rm -f "$target" | 
					
						
							|  |  |  |     elif [[ -d "$target" ]]; then | 
					
						
							|  |  |  |       echo "Skipping alt $source as $target is a directory" | 
					
						
							|  |  |  |       continue | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       assert_parent "$target" | 
					
						
							|  |  |  |     fi | 
					
						
							| 
									
										
										
										
											2019-08-18 17:51:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |     if [[ -n "$template_processor" ]]; then | 
					
						
							|  |  |  |       template "$template_processor" "$source" "$target" | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |     elif [[ "$do_copy" -eq 1 ]]; then | 
					
						
							| 
									
										
										
										
											2025-02-27 20:18:07 +00:00
										 |  |  |       $log "Copying $source to $target" | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |       cp -f "$source" "$target" | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2025-02-27 20:18:07 +00:00
										 |  |  |       $log "Linking $source to $target" | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |       ln_relative "$source" "$target" | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  |     fi | 
					
						
							| 
									
										
										
										
											2025-02-27 20:18:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     exclude+=("${target#"$YADM_WORK"}") | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  |   done | 
					
						
							| 
									
										
										
										
											2025-02-27 20:18:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   update_exclude alt "${exclude[@]}" | 
					
						
							| 
									
										
										
										
											2019-08-16 13:19:11 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-25 13:44:44 +00:00
										 |  |  | function ln_relative() { | 
					
						
							| 
									
										
										
										
											2024-12-03 21:52:58 +00:00
										 |  |  |   local source="$1" | 
					
						
							|  |  |  |   local target="$2" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-21 21:51:48 +00:00
										 |  |  |   local rel_source | 
					
						
							| 
									
										
										
										
											2024-12-03 21:52:58 +00:00
										 |  |  |   rel_source=$(relative_path "$(builtin_dirname "$target")" "$source") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-28 22:28:32 +00:00
										 |  |  |   ln -fs "$rel_source" "$target" | 
					
						
							| 
									
										
										
										
											2019-11-17 19:07:29 +00:00
										 |  |  |   alt_linked+=("$rel_source") | 
					
						
							| 
									
										
										
										
											2019-11-13 16:17:06 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 23:23:06 +00:00
										 |  |  | function bootstrap() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   bootstrap_available || error_out "Cannot execute bootstrap\n'$YADM_BOOTSTRAP' is not an executable program." | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-31 03:20:37 +00:00
										 |  |  |   # GIT_DIR should not be set for user's bootstrap code | 
					
						
							|  |  |  |   unset GIT_DIR | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 23:23:06 +00:00
										 |  |  |   echo "Executing $YADM_BOOTSTRAP" | 
					
						
							|  |  |  |   exec "$YADM_BOOTSTRAP" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | function clean() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   error_out "\"git clean\" has been disabled for safety. You could end up removing all unmanaged files." | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function clone() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-25 07:07:07 +00:00
										 |  |  |   DO_BOOTSTRAP=1 | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  |   local -a args | 
					
						
							|  |  |  |   local -i do_checkout=1 | 
					
						
							| 
									
										
										
										
											2025-01-19 22:13:37 +00:00
										 |  |  |   local -a submodules | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   while [[ $# -gt 0 ]]; do | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  |     case "$1" in | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |       --bootstrap) # force bootstrap, without prompt | 
					
						
							| 
									
										
										
										
											2017-01-25 07:07:07 +00:00
										 |  |  |         DO_BOOTSTRAP=2 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |       --no-bootstrap) # prevent bootstrap, without prompt | 
					
						
							| 
									
										
										
										
											2017-01-25 07:07:07 +00:00
										 |  |  |         DO_BOOTSTRAP=3 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  |       --checkout) | 
					
						
							|  |  |  |         do_checkout=1 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |         ;; | 
					
						
							|  |  |  |       -n | --no-checkout) | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  |         do_checkout=0 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2025-01-19 22:13:37 +00:00
										 |  |  |       --recursive | --recurse-submodules) | 
					
						
							|  |  |  |         submodules+=(":/") | 
					
						
							| 
									
										
										
										
											2021-04-25 09:02:26 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2025-01-19 22:13:37 +00:00
										 |  |  |       --recurse-submodules=*) | 
					
						
							|  |  |  |         submodules+=(":/${1#*=}") | 
					
						
							|  |  |  |         ;; | 
					
						
							|  |  |  |       --bare | --mirror | --separate-git-dir=*) | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  |         # ignore arguments without separate parameter | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  |       --separate-git-dir) | 
					
						
							|  |  |  |         # ignore arguments with separate parameter | 
					
						
							|  |  |  |         shift | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  |       *) | 
					
						
							|  |  |  |         args+=("$1") | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2017-01-25 07:07:07 +00:00
										 |  |  |     esac | 
					
						
							|  |  |  |     shift | 
					
						
							|  |  |  |   done | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-17 13:52:06 +00:00
										 |  |  |   [ -n "$DEBUG" ] && display_private_perms "initial" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  |   # 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." | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  |   # remove existing if forcing the clone to happen anyway | 
					
						
							|  |  |  |   [ -d "$YADM_REPO" ] && { | 
					
						
							|  |  |  |     debug "Removing existing repo prior to clone" | 
					
						
							| 
									
										
										
										
											2021-01-03 22:02:25 +00:00
										 |  |  |     "$GIT_PROGRAM" -C "$YADM_WORK" submodule deinit -f --all | 
					
						
							| 
									
										
										
										
											2016-03-30 08:31:21 +00:00
										 |  |  |     rm -rf "$YADM_REPO" | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   local wc | 
					
						
							| 
									
										
										
										
											2021-02-04 04:46:48 +00:00
										 |  |  |   wc="$(mk_tmp_dir)" | 
					
						
							|  |  |  |   [ -d "$wc" ] || error_out "Unable to create temporary directory" | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # first clone without checkout | 
					
						
							|  |  |  |   debug "Doing an initial clone of the repository" | 
					
						
							|  |  |  |   (cd "$wc" && | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     "$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" | 
					
						
							| 
									
										
										
										
											2019-10-07 13:36:32 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  |   configure_repo | 
					
						
							| 
									
										
										
										
											2021-08-23 13:11:38 +00:00
										 |  |  |   rm -rf "$wc" | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # then reset the index as the --no-checkout flag makes the index empty | 
					
						
							| 
									
										
										
										
											2024-11-26 12:22:18 +00:00
										 |  |  |   "$GIT_PROGRAM" reset --quiet -- ":/" | 
					
						
							| 
									
										
										
										
											2019-12-12 14:09:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if [ "$YADM_WORK" = "$HOME" ]; then | 
					
						
							|  |  |  |     debug "Determining if repo tracks private directories" | 
					
						
							| 
									
										
										
										
											2019-12-13 14:37:34 +00:00
										 |  |  |     for private_dir in $(private_dirs all); do | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  |       found_log=$("$GIT_PROGRAM" log -n 1 -- "$private_dir" 2>/dev/null) | 
					
						
							| 
									
										
										
										
											2019-12-12 14:09:00 +00:00
										 |  |  |       if [ -n "$found_log" ]; then | 
					
						
							|  |  |  |         debug "Private directory $private_dir is tracked by repo" | 
					
						
							|  |  |  |         assert_private_dirs "$private_dir" | 
					
						
							|  |  |  |       fi | 
					
						
							|  |  |  |     done | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  |   # finally check out (unless instructed not to) all files that don't exist in $YADM_WORK | 
					
						
							|  |  |  |   if [[ $do_checkout -ne 0 ]]; then | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     [ -n "$DEBUG" ] && display_private_perms "pre-checkout" | 
					
						
							| 
									
										
										
										
											2017-01-20 23:11:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     cd_work "Clone" || return | 
					
						
							| 
									
										
										
										
											2017-01-20 23:11:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     "$GIT_PROGRAM" ls-files --deleted | while IFS= read -r file; do | 
					
						
							|  |  |  |       "$GIT_PROGRAM" checkout -- ":/$file" | 
					
						
							|  |  |  |     done | 
					
						
							| 
									
										
										
										
											2017-01-20 23:11:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-19 22:13:37 +00:00
										 |  |  |     if [ ${#submodules[@]} -gt 0 ]; then | 
					
						
							|  |  |  |       "$GIT_PROGRAM" submodule update --init --recursive -- "${submodules[@]}" | 
					
						
							| 
									
										
										
										
											2021-04-25 09:02:26 +00:00
										 |  |  |     fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     if [ -n "$("$GIT_PROGRAM" ls-files --modified)" ]; then | 
					
						
							|  |  |  |       local msg | 
					
						
							|  |  |  |       IFS='' read -r -d '' msg <<EOF | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | **NOTE** | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  |   Local files with content that differs from the ones just | 
					
						
							|  |  |  |   cloned were found in $YADM_WORK. They have been left | 
					
						
							|  |  |  |   unmodified. | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  |   Please review and resolve any differences appropriately. | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |   If you know what you're doing, and want to overwrite the | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  |   tracked files, consider 'yadm checkout "$YADM_WORK"'. | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | EOF | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |       printf '%s\n' "$msg" | 
					
						
							|  |  |  |     fi | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     [ -n "$DEBUG" ] && display_private_perms "post-checkout" | 
					
						
							| 
									
										
										
										
											2017-07-17 13:52:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     CHANGES_POSSIBLE=1 | 
					
						
							| 
									
										
										
										
											2021-01-01 23:24:45 +00:00
										 |  |  |   fi | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function config() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-26 14:29:51 +00:00
										 |  |  |   use_repo_config=0 | 
					
						
							| 
									
										
										
										
											2024-12-10 17:35:26 +00:00
										 |  |  |   local_options="^local\.(class|arch|os|hostname|user|distro|distro-family)$" | 
					
						
							| 
									
										
										
										
											2017-01-26 14:29:51 +00:00
										 |  |  |   for option in "$@"; do | 
					
						
							|  |  |  |     [[ "$option" =~ $local_options ]] && use_repo_config=1 | 
					
						
							|  |  |  |   done | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if [ -z "$*" ]; then | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |     # with no parameters, provide some helpful documentation | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  |     echo "yadm supports the following configurations:" | 
					
						
							|  |  |  |     echo | 
					
						
							| 
									
										
										
										
											2019-11-03 20:13:46 +00:00
										 |  |  |     local IFS=$'\n' | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  |     for supported_config in $(introspect_configs); do | 
					
						
							|  |  |  |       echo "  ${supported_config}" | 
					
						
							|  |  |  |     done | 
					
						
							|  |  |  |     echo | 
					
						
							| 
									
										
										
										
											2021-01-26 03:16:37 +00:00
										 |  |  |     local msg | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     read -r -d '' msg <<EOF | 
					
						
							| 
									
										
										
										
											2017-01-25 23:28:55 +00:00
										 |  |  | Please read the CONFIGURATION section in the man | 
					
						
							|  |  |  | page for more details about configurations, and | 
					
						
							|  |  |  | how to adjust them. | 
					
						
							|  |  |  | EOF | 
					
						
							| 
									
										
										
										
											2021-01-26 03:16:37 +00:00
										 |  |  |     printf '%s\n' "$msg" | 
					
						
							| 
									
										
										
										
											2017-01-26 14:29:51 +00:00
										 |  |  |   elif [ "$use_repo_config" -eq 1 ]; then | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     require_repo | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |     # operate on the yadm repo's configuration file | 
					
						
							|  |  |  |     # this is always local to the machine | 
					
						
							| 
									
										
										
										
											2019-11-03 19:35:44 +00:00
										 |  |  |     "$GIT_PROGRAM" config "$@" | 
					
						
							| 
									
										
										
										
											2017-01-26 14:29:51 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     CHANGES_POSSIBLE=1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2020-06-01 17:37:43 +00:00
										 |  |  |     # make sure parent folder of config file exists | 
					
						
							|  |  |  |     assert_parent "$YADM_CONFIG" | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |     # operate on the yadm configuration file | 
					
						
							| 
									
										
										
										
											2019-07-29 13:00:09 +00:00
										 |  |  |     "$GIT_PROGRAM" config --file="$(mixed_path "$YADM_CONFIG")" "$@" | 
					
						
							| 
									
										
										
										
											2017-01-21 17:41:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 06:20:44 +00:00
										 |  |  | 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 | 
					
						
							| 
									
										
										
										
											2021-05-17 20:42:35 +00:00
										 |  |  |     GPG_OPTS=("-e") | 
					
						
							|  |  |  |     for key in $gpg_key; do | 
					
						
							|  |  |  |       GPG_OPTS+=("-r $key") | 
					
						
							|  |  |  |     done | 
					
						
							| 
									
										
										
										
											2020-10-07 06:20:44 +00:00
										 |  |  |   else | 
					
						
							|  |  |  |     GPG_OPTS=("-c") | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-09 11:55:25 +00:00
										 |  |  | function _get_openssl_ciphername() { | 
					
						
							| 
									
										
										
										
											2020-07-17 19:42:58 +00:00
										 |  |  |   OPENSSL_CIPHERNAME="$(config yadm.openssl-ciphername)" | 
					
						
							|  |  |  |   if [ -z "$OPENSSL_CIPHERNAME" ]; then | 
					
						
							|  |  |  |     OPENSSL_CIPHERNAME="aes-256-cbc" | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  |   echo "$OPENSSL_CIPHERNAME" | 
					
						
							| 
									
										
										
										
											2019-01-09 11:55:25 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-25 14:25:38 +00:00
										 |  |  | function _set_openssl_options() { | 
					
						
							| 
									
										
										
										
											2020-10-07 06:20:44 +00:00
										 |  |  |   cipher_name="$(_get_openssl_ciphername)" | 
					
						
							|  |  |  |   OPENSSL_OPTS=("-${cipher_name}" -salt) | 
					
						
							| 
									
										
										
										
											2020-09-25 14:25:38 +00:00
										 |  |  |   if [ "$(config --bool yadm.openssl-old)" == "true" ]; then | 
					
						
							| 
									
										
										
										
											2020-10-07 06:20:44 +00:00
										 |  |  |     OPENSSL_OPTS+=(-md md5) | 
					
						
							| 
									
										
										
										
											2020-09-25 14:25:38 +00:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2020-10-07 06:20:44 +00:00
										 |  |  |     OPENSSL_OPTS+=(-pbkdf2 -iter 100000 -md sha512) | 
					
						
							| 
									
										
										
										
											2020-09-25 14:25:38 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-17 19:43:29 +00:00
										 |  |  | function _get_cipher() { | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  |   output_archive="$1" | 
					
						
							| 
									
										
										
										
											2020-02-17 10:53:29 +00:00
										 |  |  |   yadm_cipher="$(config yadm.cipher)" | 
					
						
							|  |  |  |   if [ -z "$yadm_cipher" ]; then | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     yadm_cipher="gpg" | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  |   fi | 
					
						
							| 
									
										
										
										
											2020-07-17 19:43:29 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function _decrypt_from() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   local output_archive | 
					
						
							|  |  |  |   local yadm_cipher | 
					
						
							|  |  |  |   _get_cipher "$1" | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 10:53:29 +00:00
										 |  |  |   case "$yadm_cipher" in | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  |     gpg) | 
					
						
							|  |  |  |       require_gpg | 
					
						
							|  |  |  |       $GPG_PROGRAM -d "$output_archive" | 
					
						
							|  |  |  |       ;; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     openssl) | 
					
						
							|  |  |  |       require_openssl | 
					
						
							| 
									
										
										
										
											2020-09-25 14:25:38 +00:00
										 |  |  |       _set_openssl_options | 
					
						
							| 
									
										
										
										
											2020-10-07 06:20:44 +00:00
										 |  |  |       $OPENSSL_PROGRAM enc -d "${OPENSSL_OPTS[@]}" -in "$output_archive" | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  |       ;; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     *) | 
					
						
							| 
									
										
										
										
											2020-02-17 10:53:29 +00:00
										 |  |  |       error_out "Unknown cipher '$yadm_cipher'" | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  |       ;; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   esac | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function _encrypt_to() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   local output_archive | 
					
						
							| 
									
										
										
										
											2020-02-17 10:53:29 +00:00
										 |  |  |   local yadm_cipher | 
					
						
							| 
									
										
										
										
											2020-07-17 19:43:29 +00:00
										 |  |  |   _get_cipher "$1" | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 10:53:29 +00:00
										 |  |  |   case "$yadm_cipher" in | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  |     gpg) | 
					
						
							|  |  |  |       require_gpg | 
					
						
							| 
									
										
										
										
											2020-10-07 06:20:44 +00:00
										 |  |  |       _set_gpg_options | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  |       $GPG_PROGRAM --yes "${GPG_OPTS[@]}" --output "$output_archive" | 
					
						
							|  |  |  |       ;; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     openssl) | 
					
						
							|  |  |  |       require_openssl | 
					
						
							| 
									
										
										
										
											2020-10-06 03:32:18 +00:00
										 |  |  |       _set_openssl_options | 
					
						
							| 
									
										
										
										
											2020-10-07 06:20:44 +00:00
										 |  |  |       $OPENSSL_PROGRAM enc -e "${OPENSSL_OPTS[@]}" -out "$output_archive" | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  |       ;; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     *) | 
					
						
							| 
									
										
										
										
											2020-02-17 10:53:29 +00:00
										 |  |  |       error_out "Unknown cipher '$yadm_cipher'" | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  |       ;; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   esac | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | function decrypt() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   require_archive | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-15 14:17:27 +00:00
										 |  |  |   [ -f "$YADM_ENCRYPT" ] && exclude_encrypted | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if [ "$DO_LIST" = "YES" ]; then | 
					
						
							| 
									
										
										
										
											2015-07-17 01:57:53 +00:00
										 |  |  |     tar_option="t" | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     tar_option="x" | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # decrypt the archive | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  |   if (_decrypt_from "$YADM_ARCHIVE" || echo 1) | tar v${tar_option}f - -C "$YADM_WORK"; then | 
					
						
							| 
									
										
										
										
											2015-07-17 21:21:47 +00:00
										 |  |  |     [ ! "$DO_LIST" = "YES" ] && echo "All files decrypted." | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |   else | 
					
						
							|  |  |  |     error_out "Unable to extract encrypted files." | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   CHANGES_POSSIBLE=1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function encrypt() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   require_encrypt | 
					
						
							| 
									
										
										
										
											2019-10-15 12:17:38 +00:00
										 |  |  |   exclude_encrypted | 
					
						
							| 
									
										
										
										
											2017-09-18 12:39:10 +00:00
										 |  |  |   parse_encrypt | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-18 12:51:08 +00:00
										 |  |  |   cd_work "Encryption" || return | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # report which files will be encrypted | 
					
						
							| 
									
										
										
										
											2016-04-21 17:13:42 +00:00
										 |  |  |   echo "Encrypting the following files:" | 
					
						
							| 
									
										
										
										
											2017-09-18 12:39:10 +00:00
										 |  |  |   printf '%s\n' "${ENCRYPT_INCLUDE_FILES[@]}" | 
					
						
							| 
									
										
										
										
											2016-04-21 17:13:42 +00:00
										 |  |  |   echo | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-10 19:58:01 +00:00
										 |  |  |   if [ ${#NO_ENCRYPT_TRACKED_FILES[@]} -gt 0 ]; then | 
					
						
							|  |  |  |     echo "Warning: The following files are tracked and will NOT be encrypted:" | 
					
						
							|  |  |  |     printf '%s\n' "${NO_ENCRYPT_TRACKED_FILES[@]}" | 
					
						
							|  |  |  |     echo | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # encrypt all files which match the globs | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  |   if tar -f - -c "${ENCRYPT_INCLUDE_FILES[@]}" | _encrypt_to "$YADM_ARCHIVE"; then | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |     echo "Wrote new file: $YADM_ARCHIVE" | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     error_out "Unable to write $YADM_ARCHIVE" | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # offer to add YADM_ARCHIVE if untracked | 
					
						
							| 
									
										
										
										
											2017-01-09 01:04:40 +00:00
										 |  |  |   archive_status=$("$GIT_PROGRAM" status --porcelain -uall "$(mixed_path "$YADM_ARCHIVE")" 2>/dev/null) | 
					
						
							| 
									
										
										
										
											2015-07-17 02:33:25 +00:00
										 |  |  |   archive_regex="^\?\?" | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if [[ $archive_status =~ $archive_regex ]]; then | 
					
						
							| 
									
										
										
										
											2015-07-17 02:33:25 +00:00
										 |  |  |     echo "It appears that $YADM_ARCHIVE is not tracked by yadm's repository." | 
					
						
							|  |  |  |     echo "Would you like to add it now? (y/n)" | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     read -r answer </dev/tty | 
					
						
							|  |  |  |     if [[ $answer =~ ^[yY]$ ]]; then | 
					
						
							| 
									
										
										
										
											2017-01-09 01:04:40 +00:00
										 |  |  |       "$GIT_PROGRAM" add "$(mixed_path "$YADM_ARCHIVE")" | 
					
						
							| 
									
										
										
										
											2015-07-17 02:33:25 +00:00
										 |  |  |     fi | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |   CHANGES_POSSIBLE=1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-16 00:42:21 +00:00
										 |  |  | function git_crypt() { | 
					
						
							|  |  |  |   require_git_crypt | 
					
						
							|  |  |  |   enter "${GIT_CRYPT_PROGRAM} $*" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-21 14:05:56 +00:00
										 |  |  | function transcrypt() { | 
					
						
							|  |  |  |   require_transcrypt | 
					
						
							|  |  |  |   enter "${TRANSCRYPT_PROGRAM} $*" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-21 10:53:13 +00:00
										 |  |  | function enter() { | 
					
						
							| 
									
										
										
										
											2019-12-08 20:39:03 +00:00
										 |  |  |   command="$*" | 
					
						
							| 
									
										
										
										
											2017-03-30 21:30:22 +00:00
										 |  |  |   require_shell | 
					
						
							|  |  |  |   require_repo | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 22:58:05 +00:00
										 |  |  |   local -a shell_opts | 
					
						
							|  |  |  |   local shell_path="" | 
					
						
							| 
									
										
										
										
											2017-03-30 21:30:22 +00:00
										 |  |  |   if [[ "$SHELL" =~ bash$ ]]; then | 
					
						
							| 
									
										
										
										
											2020-12-29 22:58:05 +00:00
										 |  |  |     shell_opts=("--norc") | 
					
						
							| 
									
										
										
										
											2017-03-30 21:30:22 +00:00
										 |  |  |     shell_path="\w" | 
					
						
							|  |  |  |   elif [[ "$SHELL" =~ [cz]sh$ ]]; then | 
					
						
							| 
									
										
										
										
											2020-12-29 22:58:05 +00:00
										 |  |  |     shell_opts=("-f") | 
					
						
							|  |  |  |     if [[ "$SHELL" =~ zsh$ && "$TERM" = "dumb" ]]; then | 
					
						
							|  |  |  |       # Disable ZLE for tramp | 
					
						
							|  |  |  |       shell_opts+=("--no-zle") | 
					
						
							|  |  |  |     fi | 
					
						
							| 
									
										
										
										
											2017-03-30 21:30:22 +00:00
										 |  |  |     shell_path="%~" | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-08 20:39:03 +00:00
										 |  |  |   shell_cmd=() | 
					
						
							|  |  |  |   if [ -n "$command" ]; then | 
					
						
							|  |  |  |     shell_cmd=('-c' "$*") | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-12 14:00:10 +00:00
										 |  |  |   GIT_WORK_TREE="$YADM_WORK" | 
					
						
							| 
									
										
										
										
											2019-12-08 20:42:04 +00:00
										 |  |  |   export GIT_WORK_TREE | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-08 20:39:03 +00:00
										 |  |  |   [ "${#shell_cmd[@]}" -eq 0 ] && echo "Entering yadm repo" | 
					
						
							| 
									
										
										
										
											2017-03-30 21:30:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   yadm_prompt="yadm shell ($YADM_REPO) $shell_path > " | 
					
						
							| 
									
										
										
										
											2020-12-29 22:58:05 +00:00
										 |  |  |   PROMPT="$yadm_prompt" PS1="$yadm_prompt" "$SHELL" "${shell_opts[@]}" "${shell_cmd[@]}" | 
					
						
							| 
									
										
										
										
											2019-12-16 14:36:48 +00:00
										 |  |  |   return_code="$?" | 
					
						
							| 
									
										
										
										
											2017-03-30 21:30:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-16 14:36:48 +00:00
										 |  |  |   if [ "${#shell_cmd[@]}" -eq 0 ]; then | 
					
						
							|  |  |  |     echo "Leaving yadm repo" | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     exit_with_hook "$return_code" | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2017-03-21 10:53:13 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | function git_command() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   require_repo | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # translate 'gitconfig' to 'config' -- 'config' is reserved for yadm | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if [ "$1" = "gitconfig" ]; then | 
					
						
							| 
									
										
										
										
											2016-02-13 23:30:33 +00:00
										 |  |  |     set -- "config" "${@:2}" | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # ensure private .ssh and .gnupg directories exist first | 
					
						
							|  |  |  |   # TODO: consider restricting this to only commands which modify the work-tree | 
					
						
							| 
									
										
										
										
											2017-07-17 13:52:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-12 14:09:00 +00:00
										 |  |  |   if [ "$YADM_WORK" = "$HOME" ]; then | 
					
						
							|  |  |  |     auto_private_dirs=$(config --bool yadm.auto-private-dirs) | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     if [ "$auto_private_dirs" != "false" ]; then | 
					
						
							| 
									
										
										
										
											2019-12-13 14:37:34 +00:00
										 |  |  |       for pdir in $(private_dirs all); do | 
					
						
							|  |  |  |         assert_private_dirs "$pdir" | 
					
						
							|  |  |  |       done | 
					
						
							| 
									
										
										
										
											2019-12-12 14:09:00 +00:00
										 |  |  |     fi | 
					
						
							| 
									
										
										
										
											2017-07-17 13:52:06 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |   CHANGES_POSSIBLE=1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # pass commands through to git | 
					
						
							| 
									
										
										
										
											2017-07-17 13:52:06 +00:00
										 |  |  |   debug "Running git command $GIT_PROGRAM $*" | 
					
						
							| 
									
										
										
										
											2017-01-08 01:44:43 +00:00
										 |  |  |   "$GIT_PROGRAM" "$@" | 
					
						
							| 
									
										
										
										
											2017-01-06 22:27:24 +00:00
										 |  |  |   return "$?" | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function help() { | 
					
						
							| 
									
										
										
										
											2024-12-05 23:07:45 +00:00
										 |  |  |   readonly config="${YADM_CONFIG/$HOME/\$HOME}" | 
					
						
							|  |  |  |   readonly encrypt="${YADM_ENCRYPT/$HOME/\$HOME}" | 
					
						
							|  |  |  |   readonly bootstrap="${YADM_BOOTSTRAP/$HOME/\$HOME}" | 
					
						
							|  |  |  |   readonly repo="${YADM_REPO/$HOME/\$HOME}" | 
					
						
							|  |  |  |   readonly archive="${YADM_ARCHIVE/$HOME/\$HOME}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   readonly padding="                                " | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 03:16:37 +00:00
										 |  |  |   local msg | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   IFS='' read -r -d '' msg <<EOF | 
					
						
							| 
									
										
										
										
											2015-07-14 21:39:52 +00:00
										 |  |  | Usage: yadm <command> [options...] | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Manage dotfiles maintained in a Git repository. Manage alternate files | 
					
						
							|  |  |  | for specific systems or hosts. Encrypt/decrypt private files. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Git Commands: | 
					
						
							| 
									
										
										
										
											2015-07-14 21:39:52 +00:00
										 |  |  | 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). | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Commands: | 
					
						
							| 
									
										
										
										
											2015-07-14 21:39:52 +00:00
										 |  |  |   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 | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:45 +00:00
										 |  |  |   yadm bootstrap             - Execute \$HOME/.config/yadm/bootstrap | 
					
						
							| 
									
										
										
										
											2015-07-14 21:39:52 +00:00
										 |  |  |   yadm encrypt               - Encrypt files | 
					
						
							| 
									
										
										
										
											2015-07-17 01:57:53 +00:00
										 |  |  |   yadm decrypt [-l]          - Decrypt files | 
					
						
							| 
									
										
										
										
											2015-07-14 21:39:52 +00:00
										 |  |  |   yadm perms                 - Fix perms for private files | 
					
						
							| 
									
										
										
										
											2019-12-16 00:42:21 +00:00
										 |  |  |   yadm enter [COMMAND]       - Run sub-shell with GIT variables set | 
					
						
							|  |  |  |   yadm git-crypt [OPTIONS]   - Run git-crypt commands for the yadm repo | 
					
						
							| 
									
										
										
										
											2020-02-21 14:05:56 +00:00
										 |  |  |   yadm transcrypt [OPTIONS]  - Run transcrypt commands for the yadm repo | 
					
						
							| 
									
										
										
										
											2015-07-14 21:39:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Files: | 
					
						
							| 
									
										
										
										
											2024-12-05 23:07:45 +00:00
										 |  |  |   $config${padding:${#config}} - yadm's configuration file | 
					
						
							|  |  |  |   $encrypt${padding:${#encrypt}} - List of globs to encrypt/decrypt | 
					
						
							|  |  |  |   $bootstrap${padding:${#bootstrap}} - Script run via: yadm bootstrap | 
					
						
							|  |  |  |   $repo${padding:${#repo}} - yadm's Git repository | 
					
						
							|  |  |  |   $archive${padding:${#archive}} - Encrypted data stored here | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Use "man yadm" for complete documentation. | 
					
						
							|  |  |  | EOF | 
					
						
							| 
									
										
										
										
											2021-01-26 03:16:37 +00:00
										 |  |  |   printf '%s\n' "$msg" | 
					
						
							| 
									
										
										
										
											2017-07-03 21:25:23 +00:00
										 |  |  |   exit_with_hook 1 | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-28 17:23:46 +00:00
										 |  |  | # shellcheck disable=SC2120 | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | function init() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # safety check, don't attempt to init when the repo is already present | 
					
						
							| 
									
										
										
										
											2019-08-09 12:52:41 +00:00
										 |  |  |   [ -d "$YADM_REPO" ] && [ -z "$FORCE" ] && | 
					
						
							| 
									
										
										
										
											2016-03-24 00:14:25 +00:00
										 |  |  |     error_out "Git repo already exists. [$YADM_REPO]\nUse '-f' if you want to force it to be overwritten." | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # remove existing if forcing the init to happen anyway | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |   [ -d "$YADM_REPO" ] && { | 
					
						
							|  |  |  |     debug "Removing existing repo prior to init" | 
					
						
							| 
									
										
										
										
											2021-01-03 22:02:25 +00:00
										 |  |  |     "$GIT_PROGRAM" -C "$YADM_WORK" submodule deinit -f --all | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |     rm -rf "$YADM_REPO" | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # init a new bare repo | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |   debug "Init new repo" | 
					
						
							| 
									
										
										
										
											2017-01-09 01:04:40 +00:00
										 |  |  |   "$GIT_PROGRAM" init --shared=0600 --bare "$(mixed_path "$YADM_REPO")" "$@" | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |   configure_repo | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   CHANGES_POSSIBLE=1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  | function introspect() { | 
					
						
							|  |  |  |   case "$1" in | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     commands | configs | repo | switches) | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  |       "introspect_$1" | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |       ;; | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  |   esac | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function introspect_commands() { | 
					
						
							| 
									
										
										
										
											2021-01-26 03:16:37 +00:00
										 |  |  |   local msg | 
					
						
							|  |  |  |   read -r -d '' msg <<-EOF | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  | alt | 
					
						
							|  |  |  | bootstrap | 
					
						
							|  |  |  | clean | 
					
						
							|  |  |  | clone | 
					
						
							|  |  |  | config | 
					
						
							|  |  |  | decrypt | 
					
						
							|  |  |  | encrypt | 
					
						
							|  |  |  | enter | 
					
						
							| 
									
										
										
										
											2019-12-16 00:42:21 +00:00
										 |  |  | git-crypt | 
					
						
							| 
									
										
										
										
											2020-02-21 13:55:58 +00:00
										 |  |  | gitconfig | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  | help | 
					
						
							|  |  |  | init | 
					
						
							|  |  |  | introspect | 
					
						
							|  |  |  | list | 
					
						
							|  |  |  | perms | 
					
						
							| 
									
										
										
										
											2020-02-21 14:05:56 +00:00
										 |  |  | transcrypt | 
					
						
							| 
									
										
										
										
											2019-10-22 22:47:41 +00:00
										 |  |  | upgrade | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  | version | 
					
						
							|  |  |  | EOF | 
					
						
							| 
									
										
										
										
											2021-01-26 03:16:37 +00:00
										 |  |  |   printf '%s' "$msg" | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function introspect_configs() { | 
					
						
							| 
									
										
										
										
											2021-01-26 03:16:37 +00:00
										 |  |  |   local msg | 
					
						
							|  |  |  |   read -r -d '' msg <<-EOF | 
					
						
							| 
									
										
										
										
											2022-02-21 20:09:57 +00:00
										 |  |  | local.arch | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  | local.class | 
					
						
							| 
									
										
										
										
											2024-12-10 17:35:26 +00:00
										 |  |  | local.distro | 
					
						
							|  |  |  | local.distro-family | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  | local.hostname | 
					
						
							|  |  |  | local.os | 
					
						
							|  |  |  | local.user | 
					
						
							| 
									
										
										
										
											2019-10-09 13:25:02 +00:00
										 |  |  | yadm.alt-copy | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  | yadm.auto-alt | 
					
						
							| 
									
										
										
										
											2019-10-15 12:17:38 +00:00
										 |  |  | yadm.auto-exclude | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  | yadm.auto-perms | 
					
						
							| 
									
										
										
										
											2017-07-17 13:52:06 +00:00
										 |  |  | yadm.auto-private-dirs | 
					
						
							| 
									
										
										
										
											2020-02-17 10:53:29 +00:00
										 |  |  | yadm.cipher | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  | yadm.git-program | 
					
						
							|  |  |  | yadm.gpg-perms | 
					
						
							|  |  |  | yadm.gpg-program | 
					
						
							|  |  |  | yadm.gpg-recipient | 
					
						
							| 
									
										
										
										
											2019-01-10 13:50:51 +00:00
										 |  |  | yadm.openssl-ciphername | 
					
						
							| 
									
										
										
										
											2020-09-25 14:25:38 +00:00
										 |  |  | yadm.openssl-old | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  | yadm.openssl-program | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  | yadm.ssh-perms | 
					
						
							|  |  |  | EOF | 
					
						
							| 
									
										
										
										
											2021-01-26 03:16:37 +00:00
										 |  |  |   printf '%s' "$msg" | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function introspect_repo() { | 
					
						
							|  |  |  |   echo "$YADM_REPO" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function introspect_switches() { | 
					
						
							| 
									
										
										
										
											2021-01-26 03:16:37 +00:00
										 |  |  |   local msg | 
					
						
							|  |  |  |   read -r -d '' msg <<-EOF | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  | --yadm-archive | 
					
						
							|  |  |  | --yadm-bootstrap | 
					
						
							|  |  |  | --yadm-config | 
					
						
							| 
									
										
										
										
											2020-11-15 20:23:49 +00:00
										 |  |  | --yadm-data | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  | --yadm-dir | 
					
						
							|  |  |  | --yadm-encrypt | 
					
						
							|  |  |  | --yadm-repo | 
					
						
							|  |  |  | -Y | 
					
						
							|  |  |  | EOF | 
					
						
							| 
									
										
										
										
											2021-01-26 03:16:37 +00:00
										 |  |  |   printf '%s' "$msg" | 
					
						
							| 
									
										
										
										
											2017-04-06 12:30:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | function list() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   require_repo | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # process relative to YADM_WORK when --all is specified | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if [ -n "$LIST_ALL" ]; then | 
					
						
							| 
									
										
										
										
											2017-09-18 12:51:08 +00:00
										 |  |  |     cd_work "List" || return | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # list tracked files | 
					
						
							| 
									
										
										
										
											2017-01-08 01:44:43 +00:00
										 |  |  |   "$GIT_PROGRAM" ls-files | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function perms() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-18 12:39:10 +00:00
										 |  |  |   parse_encrypt | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # TODO: prevent repeats in the files changed | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-18 12:51:08 +00:00
										 |  |  |   cd_work "Perms" || return | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   GLOBS=() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # include the archive created by "encrypt" | 
					
						
							| 
									
										
										
										
											2017-09-18 12:39:10 +00:00
										 |  |  |   [ -f "$YADM_ARCHIVE" ] && GLOBS+=("$YADM_ARCHIVE") | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-13 13:34:06 +00:00
										 |  |  |   # only include private globs if using HOME as worktree | 
					
						
							|  |  |  |   if [ "$YADM_WORK" = "$HOME" ]; then | 
					
						
							|  |  |  |     # include all .ssh files (unless disabled) | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     if [[ $(config --bool yadm.ssh-perms) != "false" ]]; then | 
					
						
							| 
									
										
										
										
											2019-12-13 13:34:06 +00:00
										 |  |  |       GLOBS+=(".ssh" ".ssh/*" ".ssh/.[!.]*") | 
					
						
							|  |  |  |     fi | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-13 13:34:06 +00:00
										 |  |  |     # include all gpg files (unless disabled) | 
					
						
							| 
									
										
										
										
											2019-12-13 14:37:34 +00:00
										 |  |  |     gnupghome="$(private_dirs gnupg)" | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     if [[ $(config --bool yadm.gpg-perms) != "false" ]]; then | 
					
						
							| 
									
										
										
										
											2019-12-13 14:37:34 +00:00
										 |  |  |       GLOBS+=("${gnupghome}" "${gnupghome}/*" "${gnupghome}/.[!.]*") | 
					
						
							| 
									
										
										
										
											2019-12-13 13:34:06 +00:00
										 |  |  |     fi | 
					
						
							| 
									
										
										
										
											2015-08-07 12:18:22 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # include any files we encrypt | 
					
						
							| 
									
										
										
										
											2017-09-18 12:39:10 +00:00
										 |  |  |   GLOBS+=("${ENCRYPT_INCLUDE_FILES[@]}") | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # remove group/other permissions from collected globs | 
					
						
							| 
									
										
										
										
											2016-04-05 13:52:21 +00:00
										 |  |  |   #shellcheck disable=SC2068 | 
					
						
							|  |  |  |   #(SC2068 is disabled because in this case, we desire globbing) | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   chmod -f go-rwx ${GLOBS[@]} &>/dev/null | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # TODO: detect and report changing permissions in a portable way | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-22 22:47:41 +00:00
										 |  |  | function upgrade() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-21 21:51:48 +00:00
										 |  |  |   local actions_performed=0 | 
					
						
							| 
									
										
										
										
											2021-01-03 23:33:38 +00:00
										 |  |  |   local -a submodules | 
					
						
							| 
									
										
										
										
											2020-12-21 21:51:48 +00:00
										 |  |  |   local repo_updates=0 | 
					
						
							| 
									
										
										
										
											2019-10-22 22:47:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   [[ -n "${YADM_OVERRIDE_REPO}${YADM_OVERRIDE_ARCHIVE}" || "$YADM_DATA" = "$YADM_DIR" ]] && | 
					
						
							| 
									
										
										
										
											2020-11-18 05:01:45 +00:00
										 |  |  |     error_out "Unable to upgrade. Paths have been overridden with command line options" | 
					
						
							| 
									
										
										
										
											2019-10-22 22:47:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  |   # choose a legacy repo, the version 2 location will be favored | 
					
						
							|  |  |  |   local LEGACY_REPO= | 
					
						
							|  |  |  |   [ -d "$YADM_LEGACY_DIR/repo.git" ] && LEGACY_REPO="$YADM_LEGACY_DIR/repo.git" | 
					
						
							|  |  |  |   [ -d "$YADM_DIR/repo.git" ] && LEGACY_REPO="$YADM_DIR/repo.git" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-22 22:47:41 +00:00
										 |  |  |   # handle legacy repo | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  |   if [ -d "$LEGACY_REPO" ]; then | 
					
						
							|  |  |  |     # choose | 
					
						
							| 
									
										
										
										
											2019-10-22 22:47:41 +00:00
										 |  |  |     # legacy repo detected, it must be moved to YADM_REPO | 
					
						
							|  |  |  |     if [ -e "$YADM_REPO" ]; then | 
					
						
							|  |  |  |       error_out "Unable to upgrade. '$YADM_REPO' already exists. Refusing to overwrite it." | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       actions_performed=1 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  |       echo "Moving $LEGACY_REPO to $YADM_REPO" | 
					
						
							| 
									
										
										
										
											2021-01-03 23:33:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       export GIT_DIR="$LEGACY_REPO" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # Must absorb git dirs, otherwise deinit below will fail for modules that have | 
					
						
							|  |  |  |       # been cloned first and then added as a submodule. | 
					
						
							|  |  |  |       "$GIT_PROGRAM" submodule absorbgitdirs | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-07 16:46:06 +00:00
										 |  |  |       local submodule_status | 
					
						
							|  |  |  |       submodule_status=$("$GIT_PROGRAM" -C "$YADM_WORK" submodule status) | 
					
						
							| 
									
										
										
										
											2021-01-03 23:33:38 +00:00
										 |  |  |       while read -r sha submodule rest; do | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |         [ "$submodule" == "" ] && continue | 
					
						
							|  |  |  |         if [[ "$sha" = -* ]]; then | 
					
						
							|  |  |  |           continue | 
					
						
							|  |  |  |         fi | 
					
						
							|  |  |  |         "$GIT_PROGRAM" -C "$YADM_WORK" submodule deinit ${FORCE:+-f} -- "$submodule" || { | 
					
						
							|  |  |  |           for other in "${submodules[@]}"; do | 
					
						
							|  |  |  |             "$GIT_PROGRAM" -C "$YADM_WORK" submodule update --init --recursive -- "$other" | 
					
						
							|  |  |  |           done | 
					
						
							|  |  |  |           error_out "Unable to upgrade. Could not deinit submodule $submodule" | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         submodules+=("$submodule") | 
					
						
							|  |  |  |       done <<<"$submodule_status" | 
					
						
							| 
									
										
										
										
											2021-01-03 23:33:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-22 22:47:41 +00:00
										 |  |  |       assert_parent "$YADM_REPO" | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  |       mv "$LEGACY_REPO" "$YADM_REPO" | 
					
						
							| 
									
										
										
										
											2019-10-22 22:47:41 +00:00
										 |  |  |     fi | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2019-11-05 22:36:05 +00:00
										 |  |  |   GIT_DIR="$YADM_REPO" | 
					
						
							|  |  |  |   export GIT_DIR | 
					
						
							| 
									
										
										
										
											2020-11-18 05:01:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  |   # choose a legacy archive, the version 2 location will be favored | 
					
						
							|  |  |  |   local LEGACY_ARCHIVE= | 
					
						
							|  |  |  |   [ -e "$YADM_LEGACY_DIR/$YADM_LEGACY_ARCHIVE" ] && LEGACY_ARCHIVE="$YADM_LEGACY_DIR/$YADM_LEGACY_ARCHIVE" | 
					
						
							|  |  |  |   [ -e "$YADM_DIR/$YADM_LEGACY_ARCHIVE" ] && LEGACY_ARCHIVE="$YADM_DIR/$YADM_LEGACY_ARCHIVE" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-18 05:01:45 +00:00
										 |  |  |   # handle legacy archive | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  |   if [ -e "$LEGACY_ARCHIVE" ]; then | 
					
						
							| 
									
										
										
										
											2020-11-18 05:01:45 +00:00
										 |  |  |     actions_performed=1 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  |     echo "Moving $LEGACY_ARCHIVE to $YADM_ARCHIVE" | 
					
						
							| 
									
										
										
										
											2020-11-18 05:01:45 +00:00
										 |  |  |     assert_parent "$YADM_ARCHIVE" | 
					
						
							|  |  |  |     # test to see if path is "tracked" in repo, if so 'git mv' must be used | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     if "$GIT_PROGRAM" ls-files --error-unmatch "$LEGACY_ARCHIVE" &>/dev/null; then | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  |       "$GIT_PROGRAM" mv "$LEGACY_ARCHIVE" "$YADM_ARCHIVE" && repo_updates=1 | 
					
						
							| 
									
										
										
										
											2020-11-18 05:01:45 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  |       mv -i "$LEGACY_ARCHIVE" "$YADM_ARCHIVE" | 
					
						
							| 
									
										
										
										
											2019-10-22 22:47:41 +00:00
										 |  |  |     fi | 
					
						
							| 
									
										
										
										
											2020-11-18 05:01:45 +00:00
										 |  |  |   fi | 
					
						
							| 
									
										
										
										
											2019-10-22 22:47:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  |   # handle any remaining version 1 paths | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   for legacy_path in \ | 
					
						
							|  |  |  |     "$YADM_LEGACY_DIR/config" \ | 
					
						
							|  |  |  |     "$YADM_LEGACY_DIR/encrypt" \ | 
					
						
							|  |  |  |     "$YADM_LEGACY_DIR/bootstrap" \ | 
					
						
							|  |  |  |     "$YADM_LEGACY_DIR"/hooks/{pre,post}_*; do | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  |     if [ -e "$legacy_path" ]; then | 
					
						
							| 
									
										
										
										
											2021-12-29 20:55:14 +00:00
										 |  |  |       new_filename="${legacy_path#"$YADM_LEGACY_DIR/"}" | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  |       new_filename="$YADM_DIR/$new_filename" | 
					
						
							|  |  |  |       actions_performed=1 | 
					
						
							|  |  |  |       echo "Moving $legacy_path to $new_filename" | 
					
						
							|  |  |  |       assert_parent "$new_filename" | 
					
						
							|  |  |  |       # test to see if path is "tracked" in repo, if so 'git mv' must be used | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |       if "$GIT_PROGRAM" ls-files --error-unmatch "$legacy_path" &>/dev/null; then | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  |         "$GIT_PROGRAM" mv "$legacy_path" "$new_filename" && repo_updates=1 | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         mv -i "$legacy_path" "$new_filename" | 
					
						
							|  |  |  |       fi | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  |   done | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-05 22:36:05 +00:00
										 |  |  |   # handle submodules, which need to be reinitialized | 
					
						
							| 
									
										
										
										
											2021-01-03 23:33:38 +00:00
										 |  |  |   for submodule in "${submodules[@]}"; do | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     "$GIT_PROGRAM" -C "$YADM_WORK" submodule update --init --recursive -- "$submodule" | 
					
						
							| 
									
										
										
										
											2021-01-03 23:33:38 +00:00
										 |  |  |   done | 
					
						
							| 
									
										
										
										
											2019-11-05 22:36:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   [ "$actions_performed" -eq 0 ] && | 
					
						
							| 
									
										
										
										
											2019-10-22 22:47:41 +00:00
										 |  |  |     echo "No legacy paths found. Upgrade is not necessary" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   [ "$repo_updates" -eq 1 ] && | 
					
						
							| 
									
										
										
										
											2020-12-23 16:40:36 +00:00
										 |  |  |     echo "Some files tracked by yadm have been renamed. These changes should probably be commited now." | 
					
						
							| 
									
										
										
										
											2019-10-22 22:47:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   exit 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | function version() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-17 17:45:09 +00:00
										 |  |  |   echo "bash version $BASH_VERSION" | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   printf " " | 
					
						
							|  |  |  |   "$GIT_PROGRAM" --version | 
					
						
							| 
									
										
										
										
											2022-01-17 15:12:41 +00:00
										 |  |  |   echo "yadm version $VERSION" | 
					
						
							| 
									
										
										
										
											2017-07-03 21:25:23 +00:00
										 |  |  |   exit_with_hook 0 | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  | # ****** Utility Functions ****** | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-27 20:18:07 +00:00
										 |  |  | function update_exclude() { | 
					
						
							| 
									
										
										
										
											2019-10-15 12:17:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-10 19:58:01 +00:00
										 |  |  |   local auto_exclude | 
					
						
							| 
									
										
										
										
											2019-10-15 12:17:38 +00:00
										 |  |  |   auto_exclude=$(config --bool yadm.auto-exclude) | 
					
						
							|  |  |  |   [ "$auto_exclude" == "false" ] && return 0 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-27 20:18:07 +00:00
										 |  |  |   local exclude_path="${YADM_REPO}/info/exclude" | 
					
						
							|  |  |  |   local newline=$'\n' | 
					
						
							| 
									
										
										
										
											2025-02-10 19:58:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-27 20:18:07 +00:00
										 |  |  |   local part_path="$exclude_path.yadm-$1" | 
					
						
							|  |  |  |   local part_str | 
					
						
							|  |  |  |   part_str=$(join_string "$newline" "${@:2}") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if [ -e "$part_path" ]; then | 
					
						
							|  |  |  |     if [ "$part_str" = "$(<"$part_path")" ]; then | 
					
						
							|  |  |  |       return | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     rm -f "$part_path" | 
					
						
							|  |  |  |   elif [ -z "$part_str" ]; then | 
					
						
							|  |  |  |     return | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if [ -n "$part_str" ]; then | 
					
						
							|  |  |  |     assert_parent "$part_path" | 
					
						
							|  |  |  |     cat >"$part_path" <<<"$part_str" | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   local exclude_flag="# yadm-auto-excludes" | 
					
						
							| 
									
										
										
										
											2025-02-10 19:58:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   local exclude_header="${exclude_flag}${newline}" | 
					
						
							| 
									
										
										
										
											2019-10-15 12:17:38 +00:00
										 |  |  |   exclude_header="${exclude_header}# This section is managed by yadm." | 
					
						
							|  |  |  |   exclude_header="${exclude_header}${newline}" | 
					
						
							|  |  |  |   exclude_header="${exclude_header}# Any edits below will be lost." | 
					
						
							|  |  |  |   exclude_header="${exclude_header}${newline}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # read info/exclude | 
					
						
							| 
									
										
										
										
											2025-02-10 19:58:01 +00:00
										 |  |  |   local unmanaged="" | 
					
						
							|  |  |  |   local managed="" | 
					
						
							| 
									
										
										
										
											2019-10-15 12:17:38 +00:00
										 |  |  |   if [ -e "$exclude_path" ]; then | 
					
						
							| 
									
										
										
										
											2025-02-10 19:58:01 +00:00
										 |  |  |     local -i flag_seen=0 | 
					
						
							|  |  |  |     local line | 
					
						
							| 
									
										
										
										
											2019-10-15 12:17:38 +00:00
										 |  |  |     while IFS='' read -r line || [ -n "$line" ]; do | 
					
						
							|  |  |  |       [ "$line" = "$exclude_flag" ] && flag_seen=1 | 
					
						
							| 
									
										
										
										
											2025-02-10 19:58:01 +00:00
										 |  |  |       if ((flag_seen)); then | 
					
						
							| 
									
										
										
										
											2019-10-15 12:17:38 +00:00
										 |  |  |         managed="${managed}${line}${newline}" | 
					
						
							| 
									
										
										
										
											2025-02-10 19:58:01 +00:00
										 |  |  |       else | 
					
						
							|  |  |  |         unmanaged="${unmanaged}${line}${newline}" | 
					
						
							| 
									
										
										
										
											2019-10-15 12:17:38 +00:00
										 |  |  |       fi | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     done <"$exclude_path" | 
					
						
							| 
									
										
										
										
											2019-10-15 12:17:38 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-27 20:18:07 +00:00
										 |  |  |   local exclude_str="" | 
					
						
							|  |  |  |   for suffix in alt encrypt; do | 
					
						
							|  |  |  |     if [ -e "${exclude_path}.yadm-$suffix" ]; then | 
					
						
							|  |  |  |       local header="# yadm $suffix$newline" | 
					
						
							|  |  |  |       exclude_str="$exclude_str$header$(<"$exclude_path".yadm-"$suffix")" | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  |   done | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if [ "${exclude_header}${exclude_str}${newline}" != "$managed" ]; then | 
					
						
							| 
									
										
										
										
											2019-10-15 12:17:38 +00:00
										 |  |  |     debug "Updating ${exclude_path}" | 
					
						
							| 
									
										
										
										
											2025-02-27 20:18:07 +00:00
										 |  |  |     cat >"$exclude_path" <<<"${unmanaged}${exclude_header}${exclude_str}" | 
					
						
							| 
									
										
										
										
											2019-10-15 12:17:38 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 0 | 
					
						
							| 
									
										
										
										
											2025-02-27 20:18:07 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-15 12:17:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-27 20:18:07 +00:00
										 |  |  | function exclude_encrypted() { | 
					
						
							|  |  |  |   local -a exclude=() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if [ -r "$YADM_ENCRYPT" ]; then | 
					
						
							|  |  |  |     local pattern | 
					
						
							|  |  |  |     while IFS='' read -r pattern || [ -n "$pattern" ]; do | 
					
						
							|  |  |  |       # Prepend / to the pattern so that it matches the same files as in | 
					
						
							|  |  |  |       # parse_encrypt (i.e. only from the root) | 
					
						
							|  |  |  |       if [ "${pattern:0:1}" = "!" ]; then | 
					
						
							|  |  |  |         exclude+=("!/${pattern:1}") | 
					
						
							|  |  |  |       elif ! [[ $pattern =~ ^[[:blank:]]*(#|$) ]]; then | 
					
						
							|  |  |  |         exclude+=("/$pattern") | 
					
						
							|  |  |  |       fi | 
					
						
							|  |  |  |     done <"$YADM_ENCRYPT" | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   update_exclude encrypt "${exclude[@]}" | 
					
						
							| 
									
										
										
										
											2019-10-15 12:17:38 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-03 21:21:27 +00:00
										 |  |  | function query_distro() { | 
					
						
							| 
									
										
										
										
											2024-12-09 22:10:35 +00:00
										 |  |  |   local distro="" | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if command -v "$LSB_RELEASE_PROGRAM" &>/dev/null; then | 
					
						
							| 
									
										
										
										
											2017-07-03 21:21:27 +00:00
										 |  |  |     distro=$($LSB_RELEASE_PROGRAM -si 2>/dev/null) | 
					
						
							| 
									
										
										
										
											2019-11-30 16:52:18 +00:00
										 |  |  |   elif [ -f "$OS_RELEASE" ]; then | 
					
						
							|  |  |  |     while IFS='' read -r line || [ -n "$line" ]; do | 
					
						
							|  |  |  |       if [[ "$line" = ID=* ]]; then | 
					
						
							|  |  |  |         distro="${line#ID=}" | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |         distro="${distro//\"/}" | 
					
						
							| 
									
										
										
										
											2019-11-30 16:52:18 +00:00
										 |  |  |         break | 
					
						
							|  |  |  |       fi | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     done <"$OS_RELEASE" | 
					
						
							| 
									
										
										
										
											2017-07-03 21:21:27 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  |   echo "$distro" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-17 19:46:31 +00:00
										 |  |  | function query_distro_family() { | 
					
						
							| 
									
										
										
										
											2024-12-09 22:10:35 +00:00
										 |  |  |   local family="" | 
					
						
							| 
									
										
										
										
											2022-01-17 19:46:31 +00:00
										 |  |  |   if [ -f "$OS_RELEASE" ]; then | 
					
						
							|  |  |  |     while IFS='' read -r line || [ -n "$line" ]; do | 
					
						
							|  |  |  |       if [[ "$line" = ID_LIKE=* ]]; then | 
					
						
							|  |  |  |         family="${line#ID_LIKE=}" | 
					
						
							|  |  |  |         break | 
					
						
							| 
									
										
										
										
											2024-12-09 22:10:35 +00:00
										 |  |  |       elif [[ "$line" = ID=* ]]; then | 
					
						
							|  |  |  |         family="${line#ID=}" | 
					
						
							|  |  |  |         # No break, only used as fallback in case ID_LIKE isn't found | 
					
						
							| 
									
										
										
										
											2022-01-17 19:46:31 +00:00
										 |  |  |       fi | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     done <"$OS_RELEASE" | 
					
						
							| 
									
										
										
										
											2022-01-17 19:46:31 +00:00
										 |  |  |   fi | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   echo "${family//\"/}" | 
					
						
							| 
									
										
										
										
											2022-01-17 19:46:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-23 07:41:12 +00:00
										 |  |  | function process_global_args() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # global arguments are removed before the main processing is done | 
					
						
							| 
									
										
										
										
											2016-03-23 07:41:12 +00:00
										 |  |  |   MAIN_ARGS=() | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   while [[ $# -gt 0 ]]; do | 
					
						
							| 
									
										
										
										
											2016-03-23 07:41:12 +00:00
										 |  |  |     key="$1" | 
					
						
							|  |  |  |     case $key in | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |       -Y | --yadm-dir) # override the standard YADM_DIR | 
					
						
							| 
									
										
										
										
											2021-01-07 17:59:41 +00:00
										 |  |  |         YADM_DIR="$(qualify_path "$2" "yadm")" | 
					
						
							| 
									
										
										
										
											2016-03-23 07:41:12 +00:00
										 |  |  |         shift | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2020-11-15 20:23:49 +00:00
										 |  |  |       --yadm-data) # override the standard YADM_DATA | 
					
						
							| 
									
										
										
										
											2021-01-07 17:59:41 +00:00
										 |  |  |         YADM_DATA="$(qualify_path "$2" "data")" | 
					
						
							| 
									
										
										
										
											2020-11-15 20:23:49 +00:00
										 |  |  |         shift | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |       --yadm-repo) # override the standard YADM_REPO | 
					
						
							| 
									
										
										
										
											2021-01-07 17:59:41 +00:00
										 |  |  |         YADM_OVERRIDE_REPO="$(qualify_path "$2" "repo")" | 
					
						
							| 
									
										
										
										
											2016-04-08 22:21:38 +00:00
										 |  |  |         shift | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |       --yadm-config) # override the standard YADM_CONFIG | 
					
						
							| 
									
										
										
										
											2021-01-07 17:59:41 +00:00
										 |  |  |         YADM_OVERRIDE_CONFIG="$(qualify_path "$2" "config")" | 
					
						
							| 
									
										
										
										
											2016-04-08 22:21:38 +00:00
										 |  |  |         shift | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |       --yadm-encrypt) # override the standard YADM_ENCRYPT | 
					
						
							| 
									
										
										
										
											2021-01-07 17:59:41 +00:00
										 |  |  |         YADM_OVERRIDE_ENCRYPT="$(qualify_path "$2" "encrypt")" | 
					
						
							| 
									
										
										
										
											2016-04-08 22:21:38 +00:00
										 |  |  |         shift | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |       --yadm-archive) # override the standard YADM_ARCHIVE | 
					
						
							| 
									
										
										
										
											2021-01-07 17:59:41 +00:00
										 |  |  |         YADM_OVERRIDE_ARCHIVE="$(qualify_path "$2" "archive")" | 
					
						
							| 
									
										
										
										
											2016-04-08 22:21:38 +00:00
										 |  |  |         shift | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |       --yadm-bootstrap) # override the standard YADM_BOOTSTRAP | 
					
						
							| 
									
										
										
										
											2021-01-07 17:59:41 +00:00
										 |  |  |         YADM_OVERRIDE_BOOTSTRAP="$(qualify_path "$2" "bootstrap")" | 
					
						
							| 
									
										
										
										
											2017-01-23 23:23:06 +00:00
										 |  |  |         shift | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |       *) # main arguments are kept intact | 
					
						
							| 
									
										
										
										
											2016-03-23 07:41:12 +00:00
										 |  |  |         MAIN_ARGS+=("$1") | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2016-03-23 07:41:12 +00:00
										 |  |  |     esac | 
					
						
							|  |  |  |     shift | 
					
						
							|  |  |  |   done | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-07 17:59:41 +00:00
										 |  |  | function qualify_path() { | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   local path="$1" | 
					
						
							|  |  |  |   if [ -z "$path" ]; then | 
					
						
							|  |  |  |     error_out "You can't specify an empty $2 path" | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2021-01-07 17:59:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if [ "$path" = "." ]; then | 
					
						
							|  |  |  |     path="$PWD" | 
					
						
							|  |  |  |   elif [[ "$path" != /* ]]; then | 
					
						
							|  |  |  |     path="$PWD/${path#./}" | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  |   echo "$path" | 
					
						
							| 
									
										
										
										
											2021-01-07 17:59:41 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-15 20:23:49 +00:00
										 |  |  | function set_yadm_dirs() { | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-15 20:23:49 +00:00
										 |  |  |   # only resolve YADM_DATA if it hasn't been provided already | 
					
						
							|  |  |  |   if [ -z "$YADM_DATA" ]; then | 
					
						
							| 
									
										
										
										
											2020-12-21 21:51:48 +00:00
										 |  |  |     local base_yadm_data="$XDG_DATA_HOME" | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     if [[ ! "$base_yadm_data" =~ ^/ ]]; then | 
					
						
							| 
									
										
										
										
											2020-11-15 20:23:49 +00:00
										 |  |  |       base_yadm_data="${HOME}/.local/share" | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  |     YADM_DATA="${base_yadm_data}/yadm" | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-15 20:23:49 +00:00
										 |  |  |   # only resolve YADM_DIR if it hasn't been provided already | 
					
						
							|  |  |  |   if [ -z "$YADM_DIR" ]; then | 
					
						
							| 
									
										
										
										
											2020-12-21 21:51:48 +00:00
										 |  |  |     local base_yadm_dir="$XDG_CONFIG_HOME" | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     if [[ ! "$base_yadm_dir" =~ ^/ ]]; then | 
					
						
							| 
									
										
										
										
											2020-11-15 20:23:49 +00:00
										 |  |  |       base_yadm_dir="${HOME}/.config" | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  |     YADM_DIR="${base_yadm_dir}/yadm" | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:45 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   issue_legacy_path_warning | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function issue_legacy_path_warning() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-22 22:47:41 +00:00
										 |  |  |   # no warnings during upgrade | 
					
						
							|  |  |  |   [[ "${MAIN_ARGS[*]}" =~ upgrade ]] && return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  |   # no warnings if YADM_DIR is resolved as the leacy path | 
					
						
							|  |  |  |   [ "$YADM_DIR" = "$YADM_LEGACY_DIR" ] && return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-18 05:01:45 +00:00
										 |  |  |   # no warnings if overrides have been provided | 
					
						
							|  |  |  |   [[ -n "${YADM_OVERRIDE_REPO}${YADM_OVERRIDE_ARCHIVE}" || "$YADM_DATA" = "$YADM_DIR" ]] && return | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # test for legacy paths | 
					
						
							| 
									
										
										
										
											2020-12-21 21:51:48 +00:00
										 |  |  |   local legacy_found=() | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:45 +00:00
										 |  |  |   # this is ordered by importance | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   for legacy_path in \ | 
					
						
							|  |  |  |     "$YADM_DIR/$YADM_REPO" \ | 
					
						
							|  |  |  |     "$YADM_DIR/$YADM_LEGACY_ARCHIVE" \ | 
					
						
							|  |  |  |     "$YADM_LEGACY_DIR/$YADM_REPO" \ | 
					
						
							|  |  |  |     "$YADM_LEGACY_DIR/$YADM_BOOTSTRAP" \ | 
					
						
							|  |  |  |     "$YADM_LEGACY_DIR/$YADM_CONFIG" \ | 
					
						
							|  |  |  |     "$YADM_LEGACY_DIR/$YADM_ENCRYPT" \ | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  |     "$YADM_LEGACY_DIR/$YADM_HOOKS"/{pre,post}_* \ | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     "$YADM_LEGACY_DIR/$YADM_LEGACY_ARCHIVE"; do | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:45 +00:00
										 |  |  |     [ -e "$legacy_path" ] && legacy_found+=("$legacy_path") | 
					
						
							|  |  |  |   done | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   [ ${#legacy_found[@]} -eq 0 ] && return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   local path_list | 
					
						
							|  |  |  |   for legacy_path in "${legacy_found[@]}"; do | 
					
						
							|  |  |  |     path_list="$path_list    * $legacy_path"$'\n' | 
					
						
							|  |  |  |   done | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 03:16:37 +00:00
										 |  |  |   local msg | 
					
						
							|  |  |  |   IFS='' read -r -d '' msg <<EOF | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | **WARNING** | 
					
						
							| 
									
										
										
										
											2020-11-18 05:01:45 +00:00
										 |  |  |   Legacy paths have been detected. | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-18 05:01:45 +00:00
										 |  |  |   With version 3.0.0, yadm uses the XDG Base Directory Specification | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  |   to find its configurations and data. Read more about these changes here: | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-18 05:01:45 +00:00
										 |  |  |     https://yadm.io/docs/upgrade_from_2 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  |     https://yadm.io/docs/upgrade_from_1 | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-18 05:01:45 +00:00
										 |  |  |   In your environment, the data directory has been resolved to: | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-18 05:01:45 +00:00
										 |  |  |     $YADM_DATA | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   To remove this warning do one of the following: | 
					
						
							| 
									
										
										
										
											2020-11-18 05:01:45 +00:00
										 |  |  |     * Run "yadm upgrade" to move the yadm data to the new paths. (RECOMMENDED) | 
					
						
							| 
									
										
										
										
											2021-01-03 23:33:38 +00:00
										 |  |  |     * Manually move yadm data to new default paths and reinit any submodules. | 
					
						
							| 
									
										
										
										
											2020-11-18 05:01:45 +00:00
										 |  |  |     * Specify your preferred paths with --yadm-data and --yadm-archive each execution. | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Legacy paths detected: | 
					
						
							|  |  |  | ${path_list} | 
					
						
							|  |  |  | *********** | 
					
						
							|  |  |  | EOF | 
					
						
							| 
									
										
										
										
											2021-01-26 03:16:37 +00:00
										 |  |  |   printf '%s\n' "$msg" >&2 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   LEGACY_WARNING_ISSUED=1 | 
					
						
							| 
									
										
										
										
											2020-12-29 17:10:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-23 07:41:12 +00:00
										 |  |  | function configure_paths() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-15 20:23:49 +00:00
										 |  |  |   # change paths to be relative to YADM_DIR | 
					
						
							| 
									
										
										
										
											2016-03-23 07:41:12 +00:00
										 |  |  |   YADM_CONFIG="$YADM_DIR/$YADM_CONFIG" | 
					
						
							|  |  |  |   YADM_ENCRYPT="$YADM_DIR/$YADM_ENCRYPT" | 
					
						
							| 
									
										
										
										
											2017-01-23 23:23:06 +00:00
										 |  |  |   YADM_BOOTSTRAP="$YADM_DIR/$YADM_BOOTSTRAP" | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:01 +00:00
										 |  |  |   YADM_HOOKS="$YADM_DIR/$YADM_HOOKS" | 
					
						
							| 
									
										
										
										
											2019-10-10 13:23:36 +00:00
										 |  |  |   YADM_ALT="$YADM_DIR/$YADM_ALT" | 
					
						
							| 
									
										
										
										
											2016-03-23 07:41:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-15 20:23:49 +00:00
										 |  |  |   # change paths to be relative to YADM_DATA | 
					
						
							|  |  |  |   YADM_REPO="$YADM_DATA/$YADM_REPO" | 
					
						
							|  |  |  |   YADM_ARCHIVE="$YADM_DATA/$YADM_ARCHIVE" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # independent overrides for paths | 
					
						
							| 
									
										
										
										
											2016-04-08 22:21:38 +00:00
										 |  |  |   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 | 
					
						
							| 
									
										
										
										
											2017-01-23 23:23:06 +00:00
										 |  |  |   if [ -n "$YADM_OVERRIDE_BOOTSTRAP" ]; then | 
					
						
							|  |  |  |     YADM_BOOTSTRAP="$YADM_OVERRIDE_BOOTSTRAP" | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2016-04-08 22:21:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # use the yadm repo for all git operations | 
					
						
							| 
									
										
										
										
											2017-01-09 01:04:40 +00:00
										 |  |  |   GIT_DIR=$(mixed_path "$YADM_REPO") | 
					
						
							| 
									
										
										
										
											2016-10-09 17:24:31 +00:00
										 |  |  |   export GIT_DIR | 
					
						
							| 
									
										
										
										
											2016-03-23 07:41:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-12 14:00:10 +00:00
										 |  |  |   # obtain YADM_WORK from repo if it exists | 
					
						
							|  |  |  |   if [ -d "$GIT_DIR" ]; then | 
					
						
							|  |  |  |     local work | 
					
						
							|  |  |  |     work=$(unix_path "$("$GIT_PROGRAM" config core.worktree)") | 
					
						
							|  |  |  |     [ -n "$work" ] && YADM_WORK="$work" | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-17 15:08:58 +00:00
										 |  |  |   # YADM_BASE is used for manipulating the base worktree path for much of the | 
					
						
							|  |  |  |   # alternate file processing | 
					
						
							|  |  |  |   if [ "$YADM_WORK" == "/" ]; then | 
					
						
							|  |  |  |     YADM_BASE="" | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     YADM_BASE="$YADM_WORK" | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-23 07:41:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | function configure_repo() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   debug "Configuring new repo" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # change bare to false (there is a working directory) | 
					
						
							| 
									
										
										
										
											2017-01-08 01:44:43 +00:00
										 |  |  |   "$GIT_PROGRAM" config core.bare 'false' | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # set the worktree for the yadm repo | 
					
						
							| 
									
										
										
										
											2017-01-09 01:04:40 +00:00
										 |  |  |   "$GIT_PROGRAM" config core.worktree "$(mixed_path "$YADM_WORK")" | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # by default, do not show untracked files and directories | 
					
						
							| 
									
										
										
										
											2017-01-08 01:44:43 +00:00
										 |  |  |   "$GIT_PROGRAM" config status.showUntrackedFiles no | 
					
						
							| 
									
										
										
										
											2015-07-26 15:59:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # possibly used later to ensure we're working on the yadm repo | 
					
						
							| 
									
										
										
										
											2017-01-08 01:44:43 +00:00
										 |  |  |   "$GIT_PROGRAM" config yadm.managed 'true' | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-09 12:56:27 +00:00
										 |  |  | function set_operating_system() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 03:16:37 +00:00
										 |  |  |   if [[ "$(<$PROC_VERSION)" =~ [Mm]icrosoft ]]; then | 
					
						
							| 
									
										
										
										
											2017-04-09 12:56:27 +00:00
										 |  |  |     OPERATING_SYSTEM="WSL" | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     OPERATING_SYSTEM=$(uname -s) | 
					
						
							| 
									
										
										
										
											2021-01-26 03:16:37 +00:00
										 |  |  |   fi 2>/dev/null | 
					
						
							| 
									
										
										
										
											2017-04-09 12:56:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   case "$OPERATING_SYSTEM" in | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     CYGWIN* | MINGW* | MSYS*) | 
					
						
							| 
									
										
										
										
											2019-07-29 13:00:09 +00:00
										 |  |  |       git_version="$("$GIT_PROGRAM" --version 2>/dev/null)" | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |       if [[ "$git_version" =~ windows ]]; then | 
					
						
							|  |  |  |         USE_CYGPATH=1 | 
					
						
							| 
									
										
										
										
											2017-04-09 12:56:27 +00:00
										 |  |  |       fi | 
					
						
							| 
									
										
										
										
											2018-03-04 04:02:45 +00:00
										 |  |  |       OPERATING_SYSTEM=$(uname -o) | 
					
						
							| 
									
										
										
										
											2017-04-09 12:56:27 +00:00
										 |  |  |       ;; | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     *) ;; | 
					
						
							| 
									
										
										
										
											2017-04-09 12:56:27 +00:00
										 |  |  |   esac | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-05 04:18:22 +00:00
										 |  |  | function set_awk() { | 
					
						
							|  |  |  |   local pgm | 
					
						
							|  |  |  |   for pgm in "${AWK_PROGRAM[@]}"; do | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     command -v "$pgm" &>/dev/null && AWK_PROGRAM=("$pgm") && return | 
					
						
							| 
									
										
										
										
											2019-12-05 04:18:22 +00:00
										 |  |  |   done | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | function debug() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-15 23:35:41 +00:00
										 |  |  |   [ -n "$DEBUG" ] && echo_e "DEBUG: $*" | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function error_out() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-05 20:57:32 +00:00
										 |  |  |   echo_e "ERROR: $*" >&2 | 
					
						
							| 
									
										
										
										
											2017-07-03 21:25:23 +00:00
										 |  |  |   exit_with_hook 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function exit_with_hook() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-05 12:58:40 +00:00
										 |  |  |   invoke_hook "post" "$1" | 
					
						
							| 
									
										
										
										
											2017-07-03 21:25:23 +00:00
										 |  |  |   exit "$1" | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-22 23:32:16 +00:00
										 |  |  | function invoke_hook() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-03 21:25:23 +00:00
										 |  |  |   mode="$1" | 
					
						
							| 
									
										
										
										
											2017-07-05 12:58:40 +00:00
										 |  |  |   exit_status="$2" | 
					
						
							| 
									
										
										
										
											2019-08-06 13:19:01 +00:00
										 |  |  |   hook_command="${YADM_HOOKS}/${mode}_$HOOK_COMMAND" | 
					
						
							| 
									
										
										
										
											2017-07-03 21:25:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if [ -x "$hook_command" ] || | 
					
						
							|  |  |  |     { [[ $OPERATING_SYSTEM == MINGW* ]] && [ -f "$hook_command" ]; }; then | 
					
						
							| 
									
										
										
										
											2017-07-03 21:25:23 +00:00
										 |  |  |     debug "Invoking hook: $hook_command" | 
					
						
							| 
									
										
										
										
											2017-07-05 12:58:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |     # expose some internal data to all hooks | 
					
						
							| 
									
										
										
										
											2017-07-05 12:58:40 +00:00
										 |  |  |     YADM_HOOK_COMMAND=$HOOK_COMMAND | 
					
						
							| 
									
										
										
										
											2019-12-29 11:51:29 +00:00
										 |  |  |     YADM_HOOK_DIR=$YADM_DIR | 
					
						
							| 
									
										
										
										
											2020-11-15 20:23:49 +00:00
										 |  |  |     YADM_HOOK_DATA=$YADM_DATA | 
					
						
							| 
									
										
										
										
											2017-07-05 12:58:40 +00:00
										 |  |  |     YADM_HOOK_EXIT=$exit_status | 
					
						
							|  |  |  |     YADM_HOOK_FULL_COMMAND=$FULL_COMMAND | 
					
						
							|  |  |  |     YADM_HOOK_REPO=$YADM_REPO | 
					
						
							| 
									
										
										
										
											2019-12-12 14:00:10 +00:00
										 |  |  |     YADM_HOOK_WORK=$YADM_WORK | 
					
						
							| 
									
										
										
										
											2019-12-28 15:09:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # pack array to export it; filenames including a newline character (\n) | 
					
						
							|  |  |  |     # are NOT supported | 
					
						
							|  |  |  |     YADM_ENCRYPT_INCLUDE_FILES=$(join_string $'\n' "${ENCRYPT_INCLUDE_FILES[@]}") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-05 12:58:40 +00:00
										 |  |  |     export YADM_HOOK_COMMAND | 
					
						
							| 
									
										
										
										
											2019-12-29 11:51:29 +00:00
										 |  |  |     export YADM_HOOK_DIR | 
					
						
							| 
									
										
										
										
											2020-11-15 20:23:49 +00:00
										 |  |  |     export YADM_HOOK_DATA | 
					
						
							| 
									
										
										
										
											2017-07-05 12:58:40 +00:00
										 |  |  |     export YADM_HOOK_EXIT | 
					
						
							|  |  |  |     export YADM_HOOK_FULL_COMMAND | 
					
						
							|  |  |  |     export YADM_HOOK_REPO | 
					
						
							|  |  |  |     export YADM_HOOK_WORK | 
					
						
							| 
									
										
										
										
											2019-12-27 16:13:25 +00:00
										 |  |  |     export YADM_ENCRYPT_INCLUDE_FILES | 
					
						
							| 
									
										
										
										
											2017-07-05 12:58:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-29 14:21:55 +00:00
										 |  |  |     # export helper functions | 
					
						
							|  |  |  |     export -f builtin_dirname | 
					
						
							|  |  |  |     export -f relative_path | 
					
						
							|  |  |  |     export -f unix_path | 
					
						
							|  |  |  |     export -f mixed_path | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-03 21:25:23 +00:00
										 |  |  |     "$hook_command" | 
					
						
							| 
									
										
										
										
											2017-07-05 13:11:50 +00:00
										 |  |  |     hook_status=$? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |     # failing "pre" hooks will prevent commands from being run | 
					
						
							| 
									
										
										
										
											2017-07-05 13:11:50 +00:00
										 |  |  |     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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-22 23:32:16 +00:00
										 |  |  |   fi | 
					
						
							| 
									
										
										
										
											2017-07-03 21:25:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-22 23:32:16 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-13 14:37:34 +00:00
										 |  |  | function private_dirs() { | 
					
						
							|  |  |  |   fetch="$1" | 
					
						
							|  |  |  |   pdirs=(.ssh) | 
					
						
							|  |  |  |   if [ -z "${GNUPGHOME:-}" ]; then | 
					
						
							|  |  |  |     pdirs+=(.gnupg) | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     pdirs+=("$(relative_path "$YADM_WORK" "$GNUPGHOME")") | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  |   if [ "$fetch" = "all" ]; then | 
					
						
							|  |  |  |     echo "${pdirs[@]}" | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     echo "${pdirs[1]}" | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-17 13:52:06 +00:00
										 |  |  | function assert_private_dirs() { | 
					
						
							|  |  |  |   for private_dir in "$@"; do | 
					
						
							| 
									
										
										
										
											2019-12-12 14:00:10 +00:00
										 |  |  |     if [ ! -d "$YADM_WORK/$private_dir" ]; then | 
					
						
							|  |  |  |       debug "Creating $YADM_WORK/$private_dir" | 
					
						
							| 
									
										
										
										
											2017-07-17 13:52:06 +00:00
										 |  |  |       #shellcheck disable=SC2174 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |       mkdir -m 0700 -p "$YADM_WORK/$private_dir" &>/dev/null | 
					
						
							| 
									
										
										
										
											2017-07-17 13:52:06 +00:00
										 |  |  |     fi | 
					
						
							|  |  |  |   done | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-22 22:47:41 +00:00
										 |  |  | function assert_parent() { | 
					
						
							|  |  |  |   basedir=${1%/*} | 
					
						
							| 
									
										
										
										
											2020-07-17 15:08:58 +00:00
										 |  |  |   if [ -n "$basedir" ]; then | 
					
						
							|  |  |  |     [ -e "$basedir" ] || mkdir -p "$basedir" | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2019-10-22 22:47:41 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-17 13:52:06 +00:00
										 |  |  | function display_private_perms() { | 
					
						
							|  |  |  |   when="$1" | 
					
						
							| 
									
										
										
										
											2019-12-13 14:37:34 +00:00
										 |  |  |   for private_dir in $(private_dirs all); do | 
					
						
							| 
									
										
										
										
											2017-07-17 13:52:06 +00:00
										 |  |  |     if [ -d "$YADM_WORK/$private_dir" ]; then | 
					
						
							|  |  |  |       private_perms=$(ls -ld "$YADM_WORK/$private_dir") | 
					
						
							|  |  |  |       debug "$when" private dir perms "$private_perms" | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  |   done | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-18 12:51:08 +00:00
										 |  |  | function cd_work() { | 
					
						
							|  |  |  |   cd "$YADM_WORK" || { | 
					
						
							|  |  |  |     debug "$1 not processed, unable to cd to $YADM_WORK" | 
					
						
							|  |  |  |     return 1 | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return 0 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-18 12:39:10 +00:00
										 |  |  | function parse_encrypt() { | 
					
						
							|  |  |  |   if [ "$ENCRYPT_INCLUDE_FILES" != "unparsed" ]; then | 
					
						
							|  |  |  |     #shellcheck disable=SC2034 | 
					
						
							|  |  |  |     PARSE_ENCRYPT_SHORT="parse_encrypt() not reprocessed" | 
					
						
							|  |  |  |     return | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ENCRYPT_INCLUDE_FILES=() | 
					
						
							| 
									
										
										
										
											2021-01-11 03:07:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-15 18:51:33 +00:00
										 |  |  |   [ -f "$YADM_ENCRYPT" ] || return | 
					
						
							| 
									
										
										
										
											2017-09-18 12:39:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-18 12:51:08 +00:00
										 |  |  |   cd_work "Parsing encrypt" || return | 
					
						
							| 
									
										
										
										
											2017-09-18 12:39:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-24 21:56:07 +00:00
										 |  |  |   local -a exclude | 
					
						
							|  |  |  |   local -a include | 
					
						
							| 
									
										
										
										
											2019-11-26 22:24:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-10 19:58:01 +00:00
										 |  |  |   local pattern | 
					
						
							|  |  |  |   while IFS='' read -r pattern || [ -n "$pattern" ]; do | 
					
						
							| 
									
										
										
										
											2025-02-27 20:18:07 +00:00
										 |  |  |     if [ "${pattern:0:1}" = "!" ]; then | 
					
						
							|  |  |  |       exclude+=("--exclude=/${pattern:1}") | 
					
						
							|  |  |  |     elif ! [[ $pattern =~ ^[[:blank:]]*(#|$) ]]; then | 
					
						
							|  |  |  |       include+=("$pattern") | 
					
						
							|  |  |  |     fi | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   done <"$YADM_ENCRYPT" | 
					
						
							| 
									
										
										
										
											2017-09-18 12:39:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-10 19:58:01 +00:00
										 |  |  |   if [ ${#include[@]} -gt 0 ]; then | 
					
						
							|  |  |  |     while IFS='' read -r filename; do | 
					
						
							|  |  |  |       if [ -n "$filename" ]; then | 
					
						
							|  |  |  |         ENCRYPT_INCLUDE_FILES+=("${filename%/}") | 
					
						
							|  |  |  |       fi | 
					
						
							|  |  |  |     done <<<"$( | 
					
						
							|  |  |  |       "$GIT_PROGRAM" --glob-pathspecs ls-files --others \ | 
					
						
							|  |  |  |         "${exclude[@]}" -- "${include[@]}" 2>/dev/null | 
					
						
							|  |  |  |     )" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     [ "$YADM_COMMAND" = "encrypt" ] || return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # List files that matches encryption pattern but is tracked | 
					
						
							|  |  |  |     while IFS='' read -r filename; do | 
					
						
							|  |  |  |       if [ -n "$filename" ]; then | 
					
						
							|  |  |  |         NO_ENCRYPT_TRACKED_FILES+=("${filename%/}") | 
					
						
							|  |  |  |       fi | 
					
						
							|  |  |  |     done <<<"$( | 
					
						
							|  |  |  |       "$GIT_PROGRAM" --glob-pathspecs ls-files \ | 
					
						
							|  |  |  |         "${exclude[@]}" -- "${include[@]}" | 
					
						
							|  |  |  |     )" | 
					
						
							| 
									
										
										
										
											2019-11-26 22:24:51 +00:00
										 |  |  |   fi | 
					
						
							| 
									
										
										
										
											2017-09-18 12:39:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-17 18:58:49 +00:00
										 |  |  | function builtin_dirname() { | 
					
						
							|  |  |  |   # dirname is not builtin, and universally available, this is a built-in | 
					
						
							|  |  |  |   # replacement using parameter expansion | 
					
						
							| 
									
										
										
										
											2024-12-03 21:52:58 +00:00
										 |  |  |   local path="$1" | 
					
						
							|  |  |  |   while [ "${path: -1}" = "/" ]; do | 
					
						
							|  |  |  |     path="${path%/}" | 
					
						
							|  |  |  |   done | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   local dir_name="${path%/*}" | 
					
						
							|  |  |  |   while [ "${dir_name: -1}" = "/" ]; do | 
					
						
							|  |  |  |     dir_name="${dir_name%/}" | 
					
						
							|  |  |  |   done | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if [ "$path" = "$dir_name" ]; then | 
					
						
							|  |  |  |     dir_name="." | 
					
						
							|  |  |  |   elif [ -z "$dir_name" ]; then | 
					
						
							|  |  |  |     dir_name="/" | 
					
						
							| 
									
										
										
										
											2019-11-17 18:58:49 +00:00
										 |  |  |   fi | 
					
						
							| 
									
										
										
										
											2024-12-03 21:52:58 +00:00
										 |  |  |   echo "$dir_name" | 
					
						
							| 
									
										
										
										
											2019-11-17 18:58:49 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function relative_path() { | 
					
						
							|  |  |  |   # Output a path to $2/full, relative to $1/base | 
					
						
							|  |  |  |   # | 
					
						
							| 
									
										
										
										
											2024-12-03 21:52:58 +00:00
										 |  |  |   # This function created with ideas from | 
					
						
							| 
									
										
										
										
											2019-11-17 18:58:49 +00:00
										 |  |  |   # https://stackoverflow.com/questions/2564634 | 
					
						
							| 
									
										
										
										
											2024-12-03 21:52:58 +00:00
										 |  |  |   local base="$1" | 
					
						
							|  |  |  |   if [ "${base:0:1}" != "/" ]; then | 
					
						
							|  |  |  |     base="$PWD/$base" | 
					
						
							| 
									
										
										
										
											2019-11-17 18:58:49 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 21:52:58 +00:00
										 |  |  |   local full="$2" | 
					
						
							|  |  |  |   if [ "${full:0:1}" != "/" ]; then | 
					
						
							|  |  |  |     full="$PWD/$full" | 
					
						
							| 
									
										
										
										
											2019-11-17 18:58:49 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 21:52:58 +00:00
										 |  |  |   local common_part="$base" | 
					
						
							|  |  |  |   local result="" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   while [ "$common_part" != "$full" ]; do | 
					
						
							|  |  |  |     if [ "$common_part" = "/" ]; then | 
					
						
							|  |  |  |       # No common part found. Append / if result is set to make the final | 
					
						
							|  |  |  |       # result correct. | 
					
						
							|  |  |  |       result="${result:+$result/}" | 
					
						
							|  |  |  |       break | 
					
						
							|  |  |  |     elif [ "${full#"$common_part"/}" != "$full" ]; then | 
					
						
							|  |  |  |       common_part="$common_part/" | 
					
						
							|  |  |  |       result="${result:+$result/}" | 
					
						
							|  |  |  |       break | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  |     # Move to parent directory and update result | 
					
						
							|  |  |  |     common_part=$(builtin_dirname "$common_part") | 
					
						
							|  |  |  |     result="..${result:+/$result}" | 
					
						
							|  |  |  |   done | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   echo "$result${full#"$common_part"}" | 
					
						
							| 
									
										
										
										
											2019-11-17 18:58:49 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  | # ****** Auto Functions ****** | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | function auto_alt() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # process alternates if there are possible changes | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if [ "$CHANGES_POSSIBLE" = "1" ]; then | 
					
						
							| 
									
										
										
										
											2015-07-14 21:39:52 +00:00
										 |  |  |     auto_alt=$(config --bool yadm.auto-alt) | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     if [ "$auto_alt" != "false" ]; then | 
					
						
							| 
									
										
										
										
											2017-01-21 17:41:14 +00:00
										 |  |  |       [ -d "$YADM_REPO" ] && alt | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |     fi | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function auto_perms() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # process permissions if there are possible changes | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if [ "$CHANGES_POSSIBLE" = "1" ]; then | 
					
						
							| 
									
										
										
										
											2015-07-14 21:39:52 +00:00
										 |  |  |     auto_perms=$(config --bool yadm.auto-perms) | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     if [ "$auto_perms" != "false" ]; then | 
					
						
							| 
									
										
										
										
											2017-01-21 17:41:14 +00:00
										 |  |  |       [ -d "$YADM_REPO" ] && perms | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  |     fi | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-25 07:07:07 +00:00
										 |  |  | function auto_bootstrap() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   bootstrap_available || return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   [ "$DO_BOOTSTRAP" -eq 0 ] && return | 
					
						
							|  |  |  |   [ "$DO_BOOTSTRAP" -eq 3 ] && return | 
					
						
							|  |  |  |   [ "$DO_BOOTSTRAP" -eq 2 ] && bootstrap | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if [ "$DO_BOOTSTRAP" -eq 1 ]; then | 
					
						
							| 
									
										
										
										
											2017-01-25 07:07:07 +00:00
										 |  |  |     echo "Found $YADM_BOOTSTRAP" | 
					
						
							|  |  |  |     echo "It appears that a bootstrap program exists." | 
					
						
							|  |  |  |     echo "Would you like to execute it now? (y/n)" | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |     read -r answer </dev/tty | 
					
						
							|  |  |  |     if [[ $answer =~ ^[yY]$ ]]; then | 
					
						
							| 
									
										
										
										
											2017-01-25 07:07:07 +00:00
										 |  |  |       bootstrap | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-28 15:09:19 +00:00
										 |  |  | # ****** Helper Functions ****** | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function join_string { | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   local IFS="$1" | 
					
						
							|  |  |  |   printf "%s" "${*:2}" | 
					
						
							| 
									
										
										
										
											2019-12-28 15:09:19 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-10 21:38:42 +00:00
										 |  |  | function in_list { | 
					
						
							|  |  |  |   local element="$1" | 
					
						
							|  |  |  |   shift | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for e in "$@"; do | 
					
						
							|  |  |  |     [[ "$e" = "$element" ]] && return 0 | 
					
						
							|  |  |  |   done | 
					
						
							|  |  |  |   return 1 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-11 19:13:24 +00:00
										 |  |  | function get_mode { | 
					
						
							|  |  |  |   local filename="$1" | 
					
						
							|  |  |  |   local mode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # most *nixes | 
					
						
							|  |  |  |   mode=$(stat -c '%a' "$filename" 2>/dev/null) | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if [ -z "$mode" ]; then | 
					
						
							| 
									
										
										
										
											2020-07-11 19:13:24 +00:00
										 |  |  |     # BSD-style | 
					
						
							| 
									
										
										
										
											2020-08-20 18:08:19 +00:00
										 |  |  |     mode=$(stat -f '%p' "$filename" 2>/dev/null) | 
					
						
							|  |  |  |     mode=${mode: -4} | 
					
						
							| 
									
										
										
										
											2020-07-11 19:13:24 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # only accept results if they are octal | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if [[ ! $mode =~ ^[0-7]+$ ]]; then | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |     return 1 | 
					
						
							| 
									
										
										
										
											2020-07-11 19:13:24 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   echo "$mode" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function copy_perms { | 
					
						
							|  |  |  |   local source="$1" | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  |   local target="$2" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   local mode | 
					
						
							|  |  |  |   if ! mode=$(get_mode "$source") || ! chmod "$mode" "$target"; then | 
					
						
							|  |  |  |     debug "Unable to copy perms '$mode' from '$source' to '$target'" | 
					
						
							| 
									
										
										
										
											2023-10-11 21:10:21 +00:00
										 |  |  |   fi | 
					
						
							| 
									
										
										
										
											2024-12-15 14:10:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-11 19:13:24 +00:00
										 |  |  |   return 0 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 04:46:48 +00:00
										 |  |  | function mk_tmp_dir { | 
					
						
							|  |  |  |   local tempdir="$YADM_DATA/tmp.$$.$RANDOM" | 
					
						
							|  |  |  |   assert_parent "$tempdir/" | 
					
						
							|  |  |  |   echo "$tempdir" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  | # ****** Prerequisites Functions ****** | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | function require_archive() { | 
					
						
							|  |  |  |   [ -f "$YADM_ARCHIVE" ] || error_out "$YADM_ARCHIVE does not exist. did you forget to create it?" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | function require_encrypt() { | 
					
						
							|  |  |  |   [ -f "$YADM_ENCRYPT" ] || error_out "$YADM_ENCRYPT does not exist. did you forget to create it?" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | function require_git() { | 
					
						
							| 
									
										
										
										
											2017-01-08 01:44:43 +00:00
										 |  |  |   local alt_git | 
					
						
							|  |  |  |   alt_git="$(config yadm.git-program)" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-21 21:51:48 +00:00
										 |  |  |   local more_info="" | 
					
						
							| 
									
										
										
										
											2017-01-08 01:44:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if [ "$alt_git" != "" ]; then | 
					
						
							| 
									
										
										
										
											2017-01-08 01:44:43 +00:00
										 |  |  |     GIT_PROGRAM="$alt_git" | 
					
						
							|  |  |  |     more_info="\nThis command has been set via the yadm.git-program configuration." | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   command -v "$GIT_PROGRAM" &>/dev/null || | 
					
						
							| 
									
										
										
										
											2017-01-08 01:44:43 +00:00
										 |  |  |     error_out "This functionality requires Git to be installed, but the command '$GIT_PROGRAM' cannot be located.$more_info" | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | } | 
					
						
							|  |  |  | function require_gpg() { | 
					
						
							| 
									
										
										
										
											2016-08-13 22:17:16 +00:00
										 |  |  |   local alt_gpg | 
					
						
							|  |  |  |   alt_gpg="$(config yadm.gpg-program)" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-21 21:51:48 +00:00
										 |  |  |   local more_info="" | 
					
						
							| 
									
										
										
										
											2016-08-13 22:17:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if [ "$alt_gpg" != "" ]; then | 
					
						
							| 
									
										
										
										
											2016-08-13 22:17:16 +00:00
										 |  |  |     GPG_PROGRAM="$alt_gpg" | 
					
						
							|  |  |  |     more_info="\nThis command has been set via the yadm.gpg-program configuration." | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   command -v "$GPG_PROGRAM" &>/dev/null || | 
					
						
							| 
									
										
										
										
											2016-08-13 22:17:16 +00:00
										 |  |  |     error_out "This functionality requires GPG to be installed, but the command '$GPG_PROGRAM' cannot be located.$more_info" | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  | function require_openssl() { | 
					
						
							|  |  |  |   local alt_openssl | 
					
						
							|  |  |  |   alt_openssl="$(config yadm.openssl-program)" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-21 21:51:48 +00:00
										 |  |  |   local more_info="" | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if [ "$alt_openssl" != "" ]; then | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  |     OPENSSL_PROGRAM="$alt_openssl" | 
					
						
							|  |  |  |     more_info="\nThis command has been set via the yadm.openssl-program configuration." | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   command -v "$OPENSSL_PROGRAM" &>/dev/null || | 
					
						
							| 
									
										
										
										
											2019-01-09 11:05:06 +00:00
										 |  |  |     error_out "This functionality requires OpenSSL to be installed, but the command '$OPENSSL_PROGRAM' cannot be located.$more_info" | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | function require_repo() { | 
					
						
							|  |  |  |   [ -d "$YADM_REPO" ] || error_out "Git repo does not exist. did you forget to run 'init' or 'clone'?" | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-03-30 21:30:22 +00:00
										 |  |  | function require_shell() { | 
					
						
							|  |  |  |   [ -x "$SHELL" ] || error_out "\$SHELL does not refer to an executable." | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-12-16 00:42:21 +00:00
										 |  |  | function require_git_crypt() { | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   command -v "$GIT_CRYPT_PROGRAM" &>/dev/null || | 
					
						
							| 
									
										
										
										
											2019-12-16 00:42:21 +00:00
										 |  |  |     error_out "This functionality requires git-crypt to be installed, but the command '$GIT_CRYPT_PROGRAM' cannot be located." | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-21 14:05:56 +00:00
										 |  |  | function require_transcrypt() { | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   command -v "$TRANSCRYPT_PROGRAM" &>/dev/null || | 
					
						
							| 
									
										
										
										
											2020-02-21 14:05:56 +00:00
										 |  |  |     error_out "This functionality requires transcrypt to be installed, but the command '$TRANSCRYPT_PROGRAM' cannot be located." | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-01-23 23:23:06 +00:00
										 |  |  | function bootstrap_available() { | 
					
						
							|  |  |  |   [ -f "$YADM_BOOTSTRAP" ] && [ -x "$YADM_BOOTSTRAP" ] && return | 
					
						
							|  |  |  |   return 1 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  | function awk_available() { | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   command -v "${AWK_PROGRAM[0]}" &>/dev/null && return | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  |   return 1 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | function j2cli_available() { | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   command -v "$J2CLI_PROGRAM" &>/dev/null && return | 
					
						
							| 
									
										
										
										
											2019-10-01 13:12:18 +00:00
										 |  |  |   return 1 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-03-31 04:51:23 +00:00
										 |  |  | function envtpl_available() { | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   command -v "$ENVTPL_PROGRAM" &>/dev/null && return | 
					
						
							| 
									
										
										
										
											2017-03-31 04:51:23 +00:00
										 |  |  |   return 1 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-05-20 02:27:14 +00:00
										 |  |  | function esh_available() { | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   command -v "$ESH_PROGRAM" &>/dev/null && return | 
					
						
							| 
									
										
										
										
											2020-05-20 02:27:14 +00:00
										 |  |  |   return 1 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-04-10 13:43:11 +00:00
										 |  |  | function readlink_available() { | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   command -v "readlink" &>/dev/null && return | 
					
						
							| 
									
										
										
										
											2019-04-10 13:43:11 +00:00
										 |  |  |   return 1 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-14 12:48:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-28 15:09:19 +00:00
										 |  |  | # ****** Directory translations ****** | 
					
						
							| 
									
										
										
										
											2016-10-09 17:24:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-09 01:04:40 +00:00
										 |  |  | function unix_path() { | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # for paths used by bash/yadm | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if [ "$USE_CYGPATH" = "1" ]; then | 
					
						
							| 
									
										
										
										
											2016-10-09 17:24:31 +00:00
										 |  |  |     cygpath -u "$1" | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     echo "$1" | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-01-09 01:04:40 +00:00
										 |  |  | function mixed_path() { | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  |   # for paths used by Git | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  |   if [ "$USE_CYGPATH" = "1" ]; then | 
					
						
							| 
									
										
										
										
											2016-10-09 17:24:31 +00:00
										 |  |  |     cygpath -m "$1" | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     echo "$1" | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  | # ****** echo replacements ****** | 
					
						
							| 
									
										
										
										
											2019-12-28 15:09:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-15 23:35:41 +00:00
										 |  |  | function echo() { | 
					
						
							|  |  |  |   IFS=' ' | 
					
						
							|  |  |  |   printf '%s\n' "$*" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | function echo_n() { | 
					
						
							|  |  |  |   IFS=' ' | 
					
						
							|  |  |  |   printf '%s' "$*" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | function echo_e() { | 
					
						
							|  |  |  |   IFS=' ' | 
					
						
							|  |  |  |   printf '%b\n' "$*" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 22:22:11 +00:00
										 |  |  | # ****** Main processing (when not unit testing) ****** | 
					
						
							| 
									
										
										
										
											2016-03-24 00:16:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 19:05:16 +00:00
										 |  |  | if [ "$YADM_TEST" != 1 ]; then | 
					
						
							| 
									
										
										
										
											2016-03-24 00:16:21 +00:00
										 |  |  |   process_global_args "$@" | 
					
						
							| 
									
										
										
										
											2017-04-09 12:56:27 +00:00
										 |  |  |   set_operating_system | 
					
						
							| 
									
										
										
										
											2019-12-05 04:18:22 +00:00
										 |  |  |   set_awk | 
					
						
							| 
									
										
										
										
											2020-11-15 20:23:49 +00:00
										 |  |  |   set_yadm_dirs | 
					
						
							| 
									
										
										
										
											2016-03-24 00:16:21 +00:00
										 |  |  |   configure_paths | 
					
						
							|  |  |  |   main "${MAIN_ARGS[@]}" | 
					
						
							|  |  |  | fi |