2016-06-18 15:33:49 +00:00
#!/bin/sh
2015-07-14 12:48:47 +00:00
# yadm - Yet Another Dotfiles Manager
2020-02-06 13:53:18 +00:00
# Copyright (C) 2015-2020 Tim Byrne
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
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
2020-02-06 13:53:18 +00:00
VERSION=2.4.0
2015-07-14 12:48:47 +00:00
YADM_WORK="$HOME"
2019-08-06 13:19:45 +00:00
YADM_DIR=
YADM_LEGACY_DIR="${HOME}/.yadm"
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_REPO="repo.git"
YADM_CONFIG="config"
YADM_ENCRYPT="encrypt"
YADM_ARCHIVE="files.gpg"
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
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"
2019-10-01 13:12:18 +00:00
J2CLI_PROGRAM="j2"
2017-03-31 04:51:23 +00:00
ENVTPL_PROGRAM="envtpl"
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"
2019-12-03 14:30:51 +00:00
LEGACY_WARNING_ISSUED=0
INVALID_ALT=()
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// /\\ }"
_fc+=( "$param" )
done
FULL_COMMAND="${_fc[*]}"
2017-07-05 12:58:40 +00:00
2019-03-24 22:22:11 +00:00
# create the YADM_DIR if it doesn't exist yet
2016-04-05 13:52:21 +00:00
[ -d "$YADM_DIR" ] || mkdir -p "$YADM_DIR"
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
2019-12-16 00:42:21 +00:00
internal_commands="^(alt|bootstrap|clean|clone|config|decrypt|encrypt|enter|git-crypt|help|init|introspect|list|perms|upgrade|version)$"
2015-07-14 12:48:47 +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
elif [[ "$1" =~ $internal_commands ]] ; then
2019-03-24 22:22:11 +00:00
# for internal commands, process all of the arguments
2019-12-16 00:42:21 +00:00
YADM_COMMAND="${1/-/_}"
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
if [[ "$YADM_COMMAND" =~ ^(enter|git_crypt)$ ]] ; then
YADM_ARGS=("$@")
else
while [[ $# -gt 0 ]] ; do
key="$1"
case $key in
-a) # used by list()
LIST_ALL="YES"
;;
-d) # used by all commands
DEBUG="YES"
;;
-f) # used by init() and clone()
FORCE="YES"
;;
-l) # used by decrypt()
DO_LIST="YES"
[ "$YADM_COMMAND" = "config" ] && YADM_ARGS+=("$1")
;;
-w) # used by init() and clone()
if [[ ! "$2" =~ ^/ ]] ; then
error_out "You must specify a fully qualified work tree"
fi
YADM_WORK="$2"
shift
;;
*) # any unhandled arguments
YADM_ARGS+=("$1")
;;
esac
shift
done
fi
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() {
2019-11-25 13:44:44 +00:00
src="$1"
tgt="${src%%##*}"
conditions="${src#*##}"
2019-10-10 13:23:36 +00:00
2019-11-25 13:44:44 +00:00
if [ "${tgt#$YADM_ALT/}" != "${tgt}" ]; then
tgt="${YADM_WORK}/${tgt#$YADM_ALT/}"
2019-10-10 13:23:36 +00:00
fi
2019-10-01 13:12:18 +00:00
score=0
IFS=',' read -ra fields <<< "$conditions"
for field in "${fields[@]}"; do
label=${field%%.*}
value=${field#*.}
2019-10-12 14:14:45 +00:00
[ "$field" = "$label" ] && value="" # when .value is omitted
2019-10-01 13:12:18 +00:00
score=$((score + 1000))
# default condition
if [[ "$label" =~ ^(default)$ ]]; then
score=$((score + 0))
# variable conditions
elif [[ "$label" =~ ^(o|os)$ ]]; then
if [ "$value" = "$local_system" ]; then
score=$((score + 1))
else
score=0
return
fi
2019-10-06 16:04:21 +00:00
elif [[ "$label" =~ ^(d|distro)$ ]]; then
if [ "$value" = "$local_distro" ]; then
score=$((score + 2))
else
score=0
return
fi
2019-10-01 13:12:18 +00:00
elif [[ "$label" =~ ^(c|class)$ ]]; then
if [ "$value" = "$local_class" ]; then
2019-10-06 16:04:21 +00:00
score=$((score + 4))
2019-10-01 13:12:18 +00:00
else
score=0
return
fi
elif [[ "$label" =~ ^(h|hostname)$ ]]; then
if [ "$value" = "$local_host" ]; then
2019-10-06 16:04:21 +00:00
score=$((score + 8))
2019-10-01 13:12:18 +00:00
else
score=0
return
fi
elif [[ "$label" =~ ^(u|user)$ ]]; then
if [ "$value" = "$local_user" ]; then
2019-10-06 16:04:21 +00:00
score=$((score + 16))
2019-10-01 13:12:18 +00:00
else
score=0
return
fi
# templates
elif [[ "$label" =~ ^(t|template|yadm)$ ]]; then
score=0
cmd=$(choose_template_cmd "$value")
if [ -n "$cmd" ]; then
2019-11-25 13:44:44 +00:00
record_template "$tgt" "$cmd" "$src"
2019-10-01 13:12:18 +00:00
else
2019-11-25 13:44:44 +00:00
debug "No supported template processor for template $src"
[ -n "$loud" ] && echo "No supported template processor for template $src"
2019-10-01 13:12:18 +00:00
fi
return 0
# unsupported values
else
2019-12-03 14:30:51 +00:00
INVALID_ALT+=("$src")
2019-10-01 13:12:18 +00:00
score=0
return
fi
done
2019-11-25 13:44:44 +00:00
record_score "$score" "$tgt" "$src"
2019-10-01 13:12:18 +00:00
}
function record_score() {
score="$1"
2019-11-25 13:44:44 +00:00
tgt="$2"
src="$3"
2019-10-01 13:12:18 +00:00
# record nothing if the score is zero
[ "$score" -eq 0 ] && return
2019-11-25 13:44:44 +00:00
# search for the index of this target, to see if we already are tracking it
2019-10-01 13:12:18 +00:00
index=-1
2019-11-25 13:44:44 +00:00
for search_index in "${!alt_targets[@]}"; do
if [ "${alt_targets[$search_index]}" = "$tgt" ]; then
2019-10-01 13:12:18 +00:00
index="$search_index"
break
fi
done
# if we don't find an existing index, create one by appending to the array
if [ "$index" -eq -1 ]; then
2019-11-25 13:44:44 +00:00
alt_targets+=("$tgt")
2019-10-01 13:12:18 +00:00
# set index to the last index (newly created one)
2019-11-25 13:44:44 +00:00
for index in "${!alt_targets[@]}"; do :; done
2019-10-01 13:12:18 +00:00
# and set its initial score to zero
alt_scores[$index]=0
fi
# record nothing if a template command is registered for this file
[ "${alt_template_cmds[$index]+isset}" ] && return
2019-11-25 13:44:44 +00:00
# record higher scoring sources
2019-10-01 13:12:18 +00:00
if [ "$score" -gt "${alt_scores[$index]}" ]; then
alt_scores[$index]="$score"
2019-11-25 13:44:44 +00:00
alt_sources[$index]="$src"
2019-10-01 13:12:18 +00:00
fi
}
function record_template() {
2019-11-25 13:44:44 +00:00
tgt="$1"
2019-10-01 13:12:18 +00:00
cmd="$2"
2019-11-25 13:44:44 +00:00
src="$3"
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
2019-10-01 13:12:18 +00:00
index=-1
2019-11-25 13:44:44 +00:00
for search_index in "${!alt_targets[@]}"; do
if [ "${alt_targets[$search_index]}" = "$tgt" ]; then
2019-10-01 13:12:18 +00:00
index="$search_index"
break
fi
done
# if we don't find an existing index, create one by appending to the array
if [ "$index" -eq -1 ]; then
2019-11-25 13:44:44 +00:00
alt_targets+=("$tgt")
2019-10-01 13:12:18 +00:00
# set index to the last index (newly created one)
2019-11-25 13:44:44 +00:00
for index in "${!alt_targets[@]}"; do :; done
2019-10-01 13:12:18 +00:00
fi
# record the template command, last one wins
alt_template_cmds[$index]="$cmd"
2019-11-25 13:44:44 +00:00
alt_sources[$index]="$src"
2019-10-01 13:12:18 +00:00
}
function choose_template_cmd() {
kind="$1"
2019-10-30 22:29:17 +00:00
if [ "$kind" = "default" ] || [ "$kind" = "" ] && awk_available; then
echo "template_default"
2019-10-01 13:12:18 +00:00
elif [ "$kind" = "j2cli" ] || [ "$kind" = "j2" ] && j2cli_available; then
echo "template_j2cli"
elif [ "$kind" = "envtpl" ] || [ "$kind" = "j2" ] && envtpl_available; then
echo "template_envtpl"
else
return # this "kind" of template is not supported
fi
}
# ****** Template Processors ******
2019-10-30 22:29:17 +00:00
function template_default() {
2019-10-01 13:12:18 +00:00
input="$1"
output="$2"
2020-01-16 22:27:59 +00:00
temp_file="${output}.$$.$RANDOM"
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:]]
2019-10-01 13:12:18 +00:00
awk_pgm=$(cat << "EOF"
2019-10-30 22:29:17 +00:00
# built-in default template processor
2019-10-01 13:12:18 +00:00
BEGIN {
2019-10-01 13:17:28 +00:00
blank = "[ ]"
c["class"] = class
c["os"] = os
c["hostname"] = host
c["user"] = user
c["distro"] = distro
2019-10-10 13:09:31 +00:00
c["source"] = source
2019-10-05 16:01:13 +00:00
vld = conditions()
ifs = "^{%" blank "*if"
els = "^{%" blank "*else" blank "*%}$"
2019-10-01 13:17:28 +00:00
end = "^{%" blank "*endif" blank "*%}$"
2019-10-05 16:01:13 +00:00
skp = "^{%" blank "*(if|else|endif)"
prt = 1
2019-10-01 13:12:18 +00:00
}
{ replace_vars() } # variable replacements
2019-10-05 16:01:13 +00:00
$0 ~ vld, $0 ~ end {
if ($0 ~ vld || $0 ~ end) prt=1;
if ($0 ~ els) prt=0;
if ($0 ~ skp) next;
}
($0 ~ ifs && $0 !~ vld), $0 ~ end {
if ($0 ~ ifs && $0 !~ vld) prt=0;
if ($0 ~ els || $0 ~ end) prt=1;
if ($0 ~ skp) next;
}
{ if (prt) print }
2019-10-01 13:12:18 +00:00
function replace_vars() {
for (label in c) {
2019-10-01 13:17:28 +00:00
gsub(("{{" blank "*yadm\\." label blank "*}}"), c[label])
2019-10-01 13:12:18 +00:00
}
}
function conditions() {
2019-10-01 13:17:28 +00:00
pattern = "^{%" blank "*if" blank "*("
2019-10-01 13:12:18 +00:00
for (label in c) {
value = c[label]
gsub(/[\\.^$(){}\[\]|*+?]/, "\\\\&", value)
2019-10-01 13:17:28 +00:00
pattern = sprintf("%syadm\\.%s" blank "*==" blank "*\"%s\"|", pattern, label, value)
2019-10-01 13:12:18 +00:00
}
sub(/\|$/,")",pattern)
return pattern
}
EOF
)
2019-12-05 04:18:22 +00:00
"${AWK_PROGRAM[0]}" \
2019-10-01 13:12:18 +00:00
-v class="$local_class" \
-v os="$local_system" \
-v host="$local_host" \
-v user="$local_user" \
-v distro="$local_distro" \
2019-10-10 13:09:31 +00:00
-v source="$input" \
2019-10-01 13:12:18 +00:00
"$awk_pgm" \
2020-01-16 22:27:59 +00:00
"$input" > "$temp_file"
[ -f "$temp_file" ] && mv -f "$temp_file" "$output"
2019-10-01 13:12:18 +00:00
}
function template_j2cli() {
input="$1"
output="$2"
2020-01-16 22:27:59 +00:00
temp_file="${output}.$$.$RANDOM"
2019-10-01 13:12:18 +00:00
YADM_CLASS="$local_class" \
YADM_OS="$local_system" \
YADM_HOSTNAME="$local_host" \
YADM_USER="$local_user" \
YADM_DISTRO="$local_distro" \
2019-10-10 13:09:31 +00:00
YADM_SOURCE="$input" \
2020-01-16 22:27:59 +00:00
"$J2CLI_PROGRAM" "$input" -o "$temp_file"
[ -f "$temp_file" ] && mv -f "$temp_file" "$output"
2019-10-01 13:12:18 +00:00
}
function template_envtpl() {
input="$1"
output="$2"
2020-01-16 22:27:59 +00:00
temp_file="${output}.$$.$RANDOM"
2019-10-01 13:12:18 +00:00
YADM_CLASS="$local_class" \
YADM_OS="$local_system" \
YADM_HOSTNAME="$local_host" \
YADM_USER="$local_user" \
YADM_DISTRO="$local_distro" \
2019-10-10 13:09:31 +00:00
YADM_SOURCE="$input" \
2020-01-16 22:27:59 +00:00
"$ENVTPL_PROGRAM" --keep-template "$input" -o "$temp_file"
[ -f "$temp_file" ] && mv -f "$temp_file" "$output"
2019-10-01 13:12:18 +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
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
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
# deprecated yadm.cygwin-copy option (to be removed)
[ "$(config --bool yadm.cygwin-copy)" == "true" ] && do_copy=1
2019-08-18 17:51:53 +00:00
cd_work "Alternates" || return
# determine all tracked files
local tracked_files
tracked_files=()
local IFS=$'\n'
for tracked_file in $("$GIT_PROGRAM" ls-files | LC_ALL=C sort); do
tracked_files+=("$tracked_file")
done
# generate data for removing stale links
local possible_alts
possible_alts=()
local IFS=$'\n'
for possible_alt in "${tracked_files[@]}" "${ENCRYPT_INCLUDE_FILES[@]}"; do
if [[ $possible_alt =~ .\#\#. ]]; then
2019-11-17 19:07:29 +00:00
base_alt="${possible_alt%%##*}"
yadm_alt="${YADM_WORK}/${base_alt}"
if [ "${yadm_alt#$YADM_ALT/}" != "${yadm_alt}" ]; then
base_alt="${yadm_alt#$YADM_ALT/}"
fi
possible_alts+=("$YADM_WORK/${base_alt}")
2019-08-18 17:51:53 +00:00
fi
done
local alt_linked
alt_linked=()
2019-08-17 20:20:56 +00:00
if [ "$YADM_COMPATIBILITY" = "1" ]; then
2019-08-18 17:51:53 +00:00
alt_past_linking
2017-03-25 18:26:10 +00:00
else
2019-08-18 17:51:53 +00:00
alt_future_linking
2017-01-20 06:14:10 +00:00
fi
2019-08-18 17:51:53 +00:00
2019-09-30 13:44:41 +00:00
remove_stale_links
2019-12-03 14:30:51 +00:00
report_invalid_alts
}
function report_invalid_alts() {
[ "$YADM_COMPATIBILITY" = "1" ] && return
[ "$LEGACY_WARNING_ISSUED" = "1" ] && return
[ "${#INVALID_ALT[@]}" = "0" ] && return
local path_list
for invalid in "${INVALID_ALT[@]}"; do
path_list="$path_list * $invalid"$'\n'
done
cat <<EOF
**WARNING**
Invalid alternates have been detected.
Beginning with version 2.0.0, yadm uses a new naming convention for alternate
files. Read more about this change here:
https://yadm.io/docs/upgrade_from_1
Or to learn more about alternates in general, read:
https://yadm.io/docs/alternates
To rename the invalid alternates run:
yadm mv <old name> <new name>
Invalid alternates detected:
${path_list}
***********
EOF
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
for stale_candidate in "${possible_alts[@]}"; do
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() {
2017-03-25 18:26:10 +00:00
local_class="$(config local.class)"
2017-01-20 06:14:10 +00:00
2017-03-25 18:26:10 +00:00
local_system="$(config local.os)"
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)"
2017-03-25 18:26:10 +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)"
if [ -z "$local_user" ] ; then
local_user=$(id -u -n)
2017-01-19 01:51:28 +00:00
fi
2019-10-01 13:12:18 +00:00
local_distro="$(query_distro)"
2015-07-14 12:48:47 +00:00
2019-08-16 13:19:11 +00:00
}
2015-07-14 12:48:47 +00:00
2019-08-18 17:51:53 +00:00
function alt_future_linking() {
2019-10-01 13:12:18 +00:00
local alt_scores
local alt_targets
2019-11-25 13:44:44 +00:00
local alt_sources
2019-10-01 13:12:18 +00:00
local alt_template_cmds
alt_scores=()
alt_targets=()
2019-11-25 13:44:44 +00:00
alt_sources=()
2019-10-01 13:12:18 +00:00
alt_template_cmds=()
for alt_path in $(for tracked in "${tracked_files[@]}"; do printf "%s\n" "$tracked" "${tracked%/*}"; done | LC_ALL=C sort -u) "${ENCRYPT_INCLUDE_FILES[@]}"; do
alt_path="$YADM_WORK/$alt_path"
if [[ "$alt_path" =~ .\#\#. ]]; then
if [ -e "$alt_path" ] ; then
score_file "$alt_path"
fi
fi
done
2015-07-14 12:48:47 +00:00
2019-11-25 13:44:44 +00:00
for index in "${!alt_targets[@]}"; do
tgt="${alt_targets[$index]}"
src="${alt_sources[$index]}"
2019-10-01 13:12:18 +00:00
template_cmd="${alt_template_cmds[$index]}"
if [ -n "$template_cmd" ]; then
# a template is defined, process the template
2019-11-25 13:44:44 +00:00
debug "Creating $tgt from template $src"
[ -n "$loud" ] && echo "Creating $tgt from template $src"
2019-10-12 23:22:02 +00:00
# ensure the destination path exists
2019-11-25 13:44:44 +00:00
assert_parent "$tgt"
2019-10-11 12:20:03 +00:00
# remove any existing symlink before processing template
2019-11-25 13:44:44 +00:00
[ -L "$tgt" ] && rm -f "$tgt"
"$template_cmd" "$src" "$tgt"
elif [ -n "$src" ]; then
# a link source is defined, create symlink
debug "Linking $src to $tgt"
[ -n "$loud" ] && echo "Linking $src to $tgt"
2019-10-12 23:22:02 +00:00
# ensure the destination path exists
2019-11-25 13:44:44 +00:00
assert_parent "$tgt"
2019-10-01 13:12:18 +00:00
if [ "$do_copy" -eq 1 ]; then
2019-10-11 12:20:03 +00:00
# remove any existing symlink before copying
2019-11-25 13:44:44 +00:00
[ -L "$tgt" ] && rm -f "$tgt"
cp -f "$src" "$tgt"
2019-10-01 13:12:18 +00:00
else
2019-11-25 13:44:44 +00:00
ln_relative "$src" "$tgt"
2019-10-01 13:12:18 +00:00
fi
2017-05-29 05:17:55 +00:00
fi
2019-10-01 13:12:18 +00:00
done
2019-08-18 17:51:53 +00:00
2019-08-16 13:19:11 +00:00
}
2019-08-18 17:51:53 +00:00
function alt_past_linking() {
2019-08-16 13:19:11 +00:00
if [ -z "$local_class" ] ; then
match_class="%"
else
match_class="$local_class"
2017-05-29 05:17:55 +00:00
fi
2019-08-16 13:19:11 +00:00
match_class="(%|$match_class)"
match_system="(%|$local_system)"
match_host="(%|$local_host)"
2017-03-25 18:26:10 +00:00
match_user="(%|$local_user)"
2017-05-29 05:17:55 +00:00
2019-03-24 22:22:11 +00:00
# regex for matching "<file>##CLASS.SYSTEM.HOSTNAME.USER"
2017-03-25 17:55:46 +00:00
match1="^(.+)##(()|$match_system|$match_system\.$match_host|$match_system\.$match_host\.$match_user)$"
match2="^(.+)##($match_class|$match_class\.$match_system|$match_class\.$match_system\.$match_host|$match_class\.$match_system\.$match_host\.$match_user)$"
2015-07-14 12:48:47 +00:00
2019-03-24 22:22:11 +00:00
# loop over all "tracked" files
# for every file which matches the above regex, create a symlink
2017-03-25 17:55:46 +00:00
for match in $match1 $match2; do
last_linked=''
local IFS=$'\n'
2019-04-05 12:46:56 +00:00
# the alt_paths looped over here are a unique sorted list of both files and their immediate parent directory
2019-04-09 12:53:27 +00:00
for alt_path in $(for tracked in "${tracked_files[@]}"; do printf "%s\n" "$tracked" "${tracked%/*}"; done | LC_ALL=C sort -u) "${ENCRYPT_INCLUDE_FILES[@]}"; do
2019-04-05 12:46:56 +00:00
alt_path="$YADM_WORK/$alt_path"
if [ -e "$alt_path" ] ; then
if [[ $alt_path =~ $match ]] ; then
if [ "$alt_path" != "$last_linked" ] ; then
new_link="${BASH_REMATCH[1]}"
debug "Linking $alt_path to $new_link"
[ -n "$loud" ] && echo "Linking $alt_path to $new_link"
if [ "$do_copy" -eq 1 ]; then
if [ -L "$new_link" ]; then
rm -f "$new_link"
2017-05-29 05:17:55 +00:00
fi
2019-04-05 12:46:56 +00:00
cp -f "$alt_path" "$new_link"
else
2019-11-25 13:44:44 +00:00
ln_relative "$alt_path" "$new_link"
2017-03-25 17:55:46 +00:00
fi
2019-04-05 12:46:56 +00:00
last_linked="$alt_path"
2016-08-05 21:09:26 +00:00
fi
fi
2019-04-05 12:46:56 +00:00
fi
2016-08-05 21:09:26 +00:00
done
2015-07-14 12:48:47 +00:00
done
2019-03-24 22:22:11 +00:00
# loop over all "tracked" files
# for every file which is a *##yadm.j2 create a real file
2017-03-31 21:42:51 +00:00
local match="^(.+)##yadm\\.j2$"
2019-04-09 12:53:27 +00:00
for tracked_file in "${tracked_files[@]}" "${ENCRYPT_INCLUDE_FILES[@]}"; do
2017-03-25 18:26:10 +00:00
tracked_file="$YADM_WORK/$tracked_file"
if [ -e "$tracked_file" ] ; then
if [[ $tracked_file =~ $match ]] ; then
2017-03-31 04:51:23 +00:00
real_file="${BASH_REMATCH[1]}"
if envtpl_available; then
2017-03-25 18:26:10 +00:00
debug "Creating $real_file from template $tracked_file"
[ -n "$loud" ] && echo "Creating $real_file from template $tracked_file"
2020-01-16 22:27:59 +00:00
temp_file="${real_file}.$$.$RANDOM"
2019-08-09 12:48:10 +00:00
YADM_CLASS="$local_class" \
YADM_OS="$local_system" \
2017-03-31 04:51:23 +00:00
YADM_HOSTNAME="$local_host" \
2019-08-09 12:48:10 +00:00
YADM_USER="$local_user" \
2019-10-01 13:12:18 +00:00
YADM_DISTRO="$local_distro" \
2020-01-16 22:27:59 +00:00
"$ENVTPL_PROGRAM" --keep-template "$tracked_file" -o "$temp_file"
[ -f "$temp_file" ] && mv -f "$temp_file" "$real_file"
2017-03-31 04:51:23 +00:00
else
debug "envtpl not available, not creating $real_file from template $tracked_file"
[ -n "$loud" ] && echo "envtpl not available, not creating $real_file from template $tracked_file"
2017-03-25 18:26:10 +00:00
fi
fi
fi
done
2015-07-14 12:48:47 +00:00
}
2019-11-25 13:44:44 +00:00
function ln_relative() {
local full_source full_target target_dir
full_source="$1"
full_target="$2"
target_dir="${full_target%/*}"
rel_source=$(relative_path "$target_dir" "$full_source")
ln -nfs "$rel_source" "$full_target"
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
2019-10-07 13:36:32 +00:00
local branch
branch="master"
2017-01-25 07:07:07 +00:00
clone_args=()
while [[ $# -gt 0 ]] ; do
key="$1"
case $key in
2019-10-07 13:36:32 +00:00
-b)
if ! is_valid_branch_name "$2"; then
error_out "You must provide a branch name when using '-b'"
fi
branch="$2"
shift
;;
2019-03-24 22:22:11 +00:00
--bootstrap) # force bootstrap, without prompt
2017-01-25 07:07:07 +00:00
DO_BOOTSTRAP=2
;;
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
;;
2019-03-24 22:22:11 +00:00
*) # main arguments are kept intact
2017-01-25 07:07:07 +00:00
clone_args+=("$1")
;;
esac
shift
done
2017-07-17 13:52:06 +00:00
[ -n "$DEBUG" ] && display_private_perms "initial"
2019-03-24 22:22:11 +00:00
# clone will begin with a bare repo
2017-01-08 01:44:43 +00:00
local empty=
init $empty
2015-07-14 12:48:47 +00:00
2019-10-07 13:36:32 +00:00
# add the specified remote, and configure the repo to track origin/$branch
2015-07-14 12:48:47 +00:00
debug "Adding remote to new repo"
2017-01-25 07:07:07 +00:00
"$GIT_PROGRAM" remote add origin "${clone_args[@]}"
2019-10-07 13:36:32 +00:00
debug "Configuring new repo to track origin/${branch}"
"$GIT_PROGRAM" config "branch.${branch}.remote" origin
"$GIT_PROGRAM" config "branch.${branch}.merge" "refs/heads/${branch}"
2015-07-14 12:48:47 +00:00
2019-03-24 22:22:11 +00:00
# fetch / merge (and possibly fallback to reset)
2015-07-14 12:48:47 +00:00
debug "Doing an initial fetch of the origin"
2017-01-08 01:44:43 +00:00
"$GIT_PROGRAM" fetch origin || {
2016-03-30 08:31:21 +00:00
debug "Removing repo after failed clone"
rm -rf "$YADM_REPO"
2017-01-25 07:07:07 +00:00
error_out "Unable to fetch origin ${clone_args[0]}"
2016-03-30 08:31:21 +00:00
}
2019-10-07 13:36:32 +00:00
debug "Verifying '${branch}' is a valid branch to merge"
[ -f "${YADM_REPO}/refs/remotes/origin/${branch}" ] || {
debug "Removing repo after failed clone"
rm -rf "$YADM_REPO"
error_out "Clone failed, 'origin/${branch}' does not exist in ${clone_args[0]}"
}
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
2019-12-12 14:09:00 +00:00
found_log=$("$GIT_PROGRAM" log -n 1 "origin/${branch}" -- "$private_dir" 2>/dev/null)
if [ -n "$found_log" ]; then
debug "Private directory $private_dir is tracked by repo"
assert_private_dirs "$private_dir"
fi
done
fi
2017-07-17 13:52:06 +00:00
[ -n "$DEBUG" ] && display_private_perms "pre-merge"
2019-10-07 13:36:32 +00:00
debug "Doing an initial merge of origin/${branch}"
"$GIT_PROGRAM" merge "origin/${branch}" || {
2017-01-20 23:11:23 +00:00
debug "Merge failed, doing a reset and stashing conflicts."
2019-10-07 13:36:32 +00:00
"$GIT_PROGRAM" reset "origin/${branch}"
2017-01-20 23:11:23 +00:00
if cd "$YADM_WORK"; then # necessary because of a bug in Git
"$GIT_PROGRAM" -c user.name='yadm clone' -c user.email='yadm' stash save Conflicts preserved from yadm clone command 2>&1
cat <<EOF
**NOTE**
2019-10-07 13:36:32 +00:00
Merging origin/${branch} failed.
2017-01-20 23:11:23 +00:00
2019-10-07 13:36:32 +00:00
As a result, yadm did 'reset origin/${branch}', and then
2017-01-20 23:11:23 +00:00
stashed the conflicting data.
This likely happened because you had files in \$HOME
2019-10-07 13:36:32 +00:00
which conflicted with files tracked by origin/${branch}.
2017-01-20 23:11:23 +00:00
2017-01-27 14:22:31 +00:00
You can review the stashed conflicts with the
command 'yadm stash show -p' from within your
\$HOME directory. If you want to restore the
stashed data, you can run 'yadm stash apply' or
'yadm stash pop' and then handle the conflicts
in another way.
2017-01-20 23:11:23 +00:00
EOF
else
2019-03-24 22:22:11 +00:00
# skip auto_bootstrap if conflicts could not be stashed
2017-01-25 07:07:07 +00:00
DO_BOOTSTRAP=0
2017-01-20 23:11:23 +00:00
cat <<EOF
2015-07-14 12:48:47 +00:00
**NOTE**
2019-10-07 13:36:32 +00:00
Merging origin/${branch} failed.
yadm did 'reset origin/${branch}' instead.
2015-07-14 12:48:47 +00:00
2017-01-20 23:11:23 +00:00
yadm did not stash these conflicts beacuse it was unable
to change to the $YADM_WORK directory.
2015-07-14 12:48:47 +00:00
Please review and resolve any differences appropriately
If you know what you're doing, and want to overwrite the
2019-10-07 13:36:32 +00:00
tracked files, consider 'yadm reset --hard origin/${branch}'
2015-07-14 12:48:47 +00:00
EOF
2017-01-20 23:11:23 +00:00
fi
2015-07-14 12:48:47 +00:00
}
2017-07-17 13:52:06 +00:00
[ -n "$DEBUG" ] && display_private_perms "post-merge"
2015-07-14 12:48:47 +00:00
CHANGES_POSSIBLE=1
}
function config() {
2017-01-26 14:29:51 +00:00
use_repo_config=0
2017-03-31 12:55:43 +00:00
local_options="^local\.(class|os|hostname|user)$"
2017-01-26 14:29:51 +00:00
for option in "$@"; do
[[ "$option" =~ $local_options ]] && use_repo_config=1
done
2015-07-14 21:39:52 +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
2017-01-25 23:28:55 +00:00
cat << EOF
Please read the CONFIGURATION section in the man
page for more details about configurations, and
how to adjust them.
EOF
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
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
}
2019-01-09 11:55:25 +00:00
function _get_openssl_ciphername() {
OPENSSL_CIPHERNAME="$(config yadm.openssl-ciphername)"
if [ -z "$OPENSSL_CIPHERNAME" ]; then
OPENSSL_CIPHERNAME="aes-256-cbc"
fi
echo "$OPENSSL_CIPHERNAME"
}
2019-01-09 11:05:06 +00:00
function _decrypt_from() {
local output_archive
output_archive="$1"
local yadm_crypher
yadm_crypher="$(config yadm.cypher)"
if [ -z "$yadm_crypher" ]; then
yadm_crypher="gpg"
fi
case "$yadm_crypher" in
gpg)
require_gpg
$GPG_PROGRAM -d "$output_archive"
;;
openssl)
require_openssl
2019-01-09 11:55:25 +00:00
OPENSSL_CIPHERNAME="$(_get_openssl_ciphername)"
$OPENSSL_PROGRAM enc -d -$OPENSSL_CIPHERNAME -salt -in "$output_archive"
2019-01-09 11:05:06 +00:00
;;
*)
error_out "Unknown cypher '$yadm_crypher'"
;;
esac
}
function _encrypt_to() {
local output_archive
output_archive="$1"
local yadm_crypher
yadm_crypher="$(config yadm.cypher)"
if [ -z "$yadm_crypher" ]; then
yadm_crypher="gpg"
fi
case "$yadm_crypher" in
gpg)
require_gpg
#; Build gpg options for gpg
GPG_KEY="$(config yadm.gpg-recipient)"
if [ "$GPG_KEY" = "ASK" ]; then
GPG_OPTS=("--no-default-recipient" "-e")
elif [ "$GPG_KEY" != "" ]; then
GPG_OPTS=("-e" "-r $GPG_KEY")
else
GPG_OPTS=("-c")
fi
$GPG_PROGRAM --yes "${GPG_OPTS[@]}" --output "$output_archive"
;;
openssl)
require_openssl
2019-01-09 11:55:25 +00:00
OPENSSL_CIPHERNAME="$(_get_openssl_ciphername)"
$OPENSSL_PROGRAM enc -e -$OPENSSL_CIPHERNAME -salt -out "$output_archive"
2019-01-09 11:05:06 +00:00
;;
*)
error_out "Unknown cypher '$yadm_crypher'"
;;
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
2015-07-14 12:48:47 +00:00
2015-07-17 21:21:47 +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
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="^\?\?"
if [[ $archive_status =~ $archive_regex ]] ; then
echo "It appears that $YADM_ARCHIVE is not tracked by yadm's repository."
echo "Would you like to add it now? (y/n)"
2017-01-27 22:58:20 +00:00
read -r answer < /dev/tty
2015-07-17 02:33:25 +00:00
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} $*"
}
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
shell_opts=""
shell_path=""
if [[ "$SHELL" =~ bash$ ]]; then
shell_opts="--norc"
shell_path="\w"
elif [[ "$SHELL" =~ [cz]sh$ ]]; then
shell_opts="-f"
shell_path="%~"
fi
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 > "
2019-12-08 20:39:03 +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
2016-02-13 23:30:33 +00:00
if [ "$1" = "gitconfig" ] ; then
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)
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() {
cat << 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
2015-07-14 21:39:52 +00:00
Files:
2019-08-06 13:19:45 +00:00
\$HOME/.config/yadm/config - yadm's configuration file
\$HOME/.config/yadm/repo.git - yadm's Git repository
\$HOME/.config/yadm/encrypt - List of globs used for encrypt/decrypt
\$HOME/.config/yadm/files.gpg - Encrypted data stored here
2015-07-14 12:48:47 +00:00
Use "man yadm" for complete documentation.
EOF
2017-07-03 21:25:23 +00:00
exit_with_hook 1
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"
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
commands|configs|repo|switches)
"introspect_$1"
;;
esac
}
function introspect_commands() {
cat <<-EOF
alt
bootstrap
clean
clone
config
decrypt
encrypt
enter
gitconfig
2019-12-16 00:42:21 +00:00
git-crypt
2017-04-06 12:30:28 +00:00
help
init
introspect
list
perms
2019-10-22 22:47:41 +00:00
upgrade
2017-04-06 12:30:28 +00:00
version
EOF
}
function introspect_configs() {
2019-11-03 20:13:46 +00:00
cat <<-EOF
2017-04-06 12:30:28 +00:00
local.class
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
<<<<<<< HEAD
=======
2017-05-29 05:17:55 +00:00
yadm.cygwin-copy
2019-01-09 11:05:06 +00:00
yadm.cypher
2020-02-17 10:53:29 +00:00
>>>>>>> local
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
2019-01-09 11:05:06 +00:00
yadm.openssl-program
2017-04-06 12:30:28 +00:00
yadm.ssh-perms
EOF
}
function introspect_repo() {
echo "$YADM_REPO"
}
function introspect_switches() {
cat <<-EOF
--yadm-archive
--yadm-bootstrap
--yadm-config
--yadm-dir
--yadm-encrypt
--yadm-repo
-Y
EOF
}
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
2015-07-14 12:48:47 +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)
if [[ $(config --bool yadm.ssh-perms) != "false" ]] ; then
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)"
2019-12-13 13:34:06 +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)
2019-12-05 04:36:58 +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() {
local actions_performed
actions_performed=0
local repo_updates
repo_updates=0
[ "$YADM_COMPATIBILITY" = "1" ] && \
error_out "Unable to upgrade. YADM_COMPATIBILITY is set to '1'."
[ "$YADM_DIR" = "$YADM_LEGACY_DIR" ] && \
error_out "Unable to upgrade. yadm dir has been resolved as '$YADM_LEGACY_DIR'."
# handle legacy repo
if [ -d "$YADM_LEGACY_DIR/repo.git" ]; then
# 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
echo "Moving $YADM_LEGACY_DIR/repo.git to $YADM_REPO"
assert_parent "$YADM_REPO"
mv "$YADM_LEGACY_DIR/repo.git" "$YADM_REPO"
fi
fi
# handle other legacy paths
2019-11-05 22:36:05 +00:00
GIT_DIR="$YADM_REPO"
export GIT_DIR
2019-10-22 22:47:41 +00:00
for legacy_path in \
"$YADM_LEGACY_DIR/config" \
"$YADM_LEGACY_DIR/encrypt" \
"$YADM_LEGACY_DIR/files.gpg" \
"$YADM_LEGACY_DIR/bootstrap" \
"$YADM_LEGACY_DIR"/hooks/{pre,post}_* \
; \
do
if [ -e "$legacy_path" ]; then
new_filename=${legacy_path#$YADM_LEGACY_DIR/}
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
2019-12-05 04:36:58 +00:00
if "$GIT_PROGRAM" ls-files --error-unmatch "$legacy_path" &> /dev/null; then
2019-11-05 22:36:05 +00:00
"$GIT_PROGRAM" mv "$legacy_path" "$new_filename" && repo_updates=1
2019-10-22 22:47:41 +00:00
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
if [ "$actions_performed" -ne 0 ]; then
cd_work "Upgrade submodules"
2019-12-05 04:36:58 +00:00
if "$GIT_PROGRAM" ls-files --error-unmatch .gitmodules &> /dev/null; then
2019-11-05 22:36:05 +00:00
"$GIT_PROGRAM" submodule deinit -f .
"$GIT_PROGRAM" submodule update --init --recursive
fi
fi
2019-10-22 22:47:41 +00:00
[ "$actions_performed" -eq 0 ] && \
echo "No legacy paths found. Upgrade is not necessary"
[ "$repo_updates" -eq 1 ] && \
echo "Some files tracked by yadm have been renamed. This changes should probably be commited now."
exit 0
2015-07-14 12:48:47 +00:00
}
function version() {
echo "yadm $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
2019-10-15 12:17:38 +00:00
function exclude_encrypted() {
auto_exclude=$(config --bool yadm.auto-exclude)
[ "$auto_exclude" == "false" ] && return 0
exclude_path="${YADM_REPO}/info/exclude"
newline=$'\n'
exclude_flag="# yadm-auto-excludes"
exclude_header="${exclude_flag}${newline}"
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}"
# do nothing if there is no YADM_ENCRYPT
[ -e "$YADM_ENCRYPT" ] || return 0
# read encrypt
encrypt_data=""
while IFS='' read -r line || [ -n "$line" ]; do
encrypt_data="${encrypt_data}${line}${newline}"
done < "$YADM_ENCRYPT"
# read info/exclude
unmanaged=""
managed=""
if [ -e "$exclude_path" ]; then
flag_seen=0
while IFS='' read -r line || [ -n "$line" ]; do
[ "$line" = "$exclude_flag" ] && flag_seen=1
if [ "$flag_seen" -eq 0 ]; then
unmanaged="${unmanaged}${line}${newline}"
else
managed="${managed}${line}${newline}"
fi
done < "$exclude_path"
fi
if [ "${exclude_header}${encrypt_data}" != "$managed" ]; then
debug "Updating ${exclude_path}"
2019-10-22 22:47:41 +00:00
assert_parent "$exclude_path"
2019-10-15 12:17:38 +00:00
printf "%s" "${unmanaged}${exclude_header}${encrypt_data}" > "$exclude_path"
fi
return 0
}
2019-10-07 13:36:32 +00:00
function is_valid_branch_name() {
# Git branches do not allow:
# * path component that begins with "."
# * double dot
# * "~", "^", ":", "\", space
# * end with a "/"
# * end with ".lock"
[[ "$1" =~ (\/\.|\.\.|[~^:\\ ]|\/$|\.lock$) ]] && return 1
return 0
}
2015-07-14 12:48:47 +00:00
2017-07-03 21:21:27 +00:00
function query_distro() {
distro=""
2019-12-05 04:36:58 +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=}"
2019-12-07 04:38:37 +00:00
distro="${distro//\"}"
2019-11-30 16:52:18 +00:00
break
fi
done < "$OS_RELEASE"
2017-07-03 21:21:27 +00:00
fi
echo "$distro"
}
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=()
2016-04-05 13:52:21 +00:00
while [[ $# -gt 0 ]] ; do
2016-03-23 07:41:12 +00:00
key="$1"
case $key in
2019-03-24 22:22:11 +00:00
-Y|--yadm-dir) # override the standard YADM_DIR
2016-03-23 07:41:12 +00:00
if [[ ! "$2" =~ ^/ ]] ; then
error_out "You must specify a fully qualified yadm directory"
fi
YADM_DIR="$2"
shift
;;
2019-03-24 22:22:11 +00:00
--yadm-repo) # override the standard YADM_REPO
2016-04-08 22:21:38 +00:00
if [[ ! "$2" =~ ^/ ]] ; then
error_out "You must specify a fully qualified repo path"
fi
YADM_OVERRIDE_REPO="$2"
shift
;;
2019-03-24 22:22:11 +00:00
--yadm-config) # override the standard YADM_CONFIG
2016-04-08 22:21:38 +00:00
if [[ ! "$2" =~ ^/ ]] ; then
error_out "You must specify a fully qualified config path"
fi
YADM_OVERRIDE_CONFIG="$2"
shift
;;
2019-03-24 22:22:11 +00:00
--yadm-encrypt) # override the standard YADM_ENCRYPT
2016-04-08 22:21:38 +00:00
if [[ ! "$2" =~ ^/ ]] ; then
error_out "You must specify a fully qualified encrypt path"
fi
YADM_OVERRIDE_ENCRYPT="$2"
shift
;;
2019-03-24 22:22:11 +00:00
--yadm-archive) # override the standard YADM_ARCHIVE
2016-04-08 22:21:38 +00:00
if [[ ! "$2" =~ ^/ ]] ; then
error_out "You must specify a fully qualified archive path"
fi
YADM_OVERRIDE_ARCHIVE="$2"
shift
;;
2019-03-24 22:22:11 +00:00
--yadm-bootstrap) # override the standard YADM_BOOTSTRAP
2017-01-23 23:23:06 +00:00
if [[ ! "$2" =~ ^/ ]] ; then
error_out "You must specify a fully qualified bootstrap path"
fi
YADM_OVERRIDE_BOOTSTRAP="$2"
shift
;;
2019-03-24 22:22:11 +00:00
*) # main arguments are kept intact
2016-03-23 07:41:12 +00:00
MAIN_ARGS+=("$1")
;;
esac
shift
done
}
2019-08-06 13:19:45 +00:00
function set_yadm_dir() {
# only resolve YADM_DIR if it hasn't been provided already
[ -n "$YADM_DIR" ] && return
# compatibility with major version 1 ignores XDG_CONFIG_HOME
if [ "$YADM_COMPATIBILITY" = "1" ]; then
YADM_DIR="$YADM_LEGACY_DIR"
return
fi
local base_yadm_dir
base_yadm_dir="$XDG_CONFIG_HOME"
if [[ ! "$base_yadm_dir" =~ ^/ ]] ; then
base_yadm_dir="${HOME}/.config"
fi
YADM_DIR="${base_yadm_dir}/yadm"
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
2019-08-06 13:19:45 +00:00
# no warnings if YADM_DIR is resolved as the leacy path
[ "$YADM_DIR" = "$YADM_LEGACY_DIR" ] && return
# no warnings if the legacy directory doesn't exist
[ ! -d "$YADM_LEGACY_DIR" ] && return
# test for legacy paths
local legacy_found
legacy_found=()
# this is ordered by importance
2019-10-22 22:47:41 +00:00
for legacy_path in \
"$YADM_LEGACY_DIR/$YADM_REPO" \
"$YADM_LEGACY_DIR/$YADM_CONFIG" \
"$YADM_LEGACY_DIR/$YADM_ENCRYPT" \
"$YADM_LEGACY_DIR/$YADM_ARCHIVE" \
"$YADM_LEGACY_DIR/$YADM_BOOTSTRAP" \
"$YADM_LEGACY_DIR/$YADM_HOOKS"/{pre,post}_* \
; \
2019-08-06 13:19:45 +00:00
do
[ -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
cat <<EOF
**WARNING**
Legacy configuration paths have been detected.
Beginning with version 2.0.0, yadm uses the XDG Base Directory Specification
to find its configurations. Read more about this change here:
2019-11-04 23:31:55 +00:00
https://yadm.io/docs/upgrade_from_1
2019-08-06 13:19:45 +00:00
In your environment, the configuration directory has been resolved to:
$YADM_DIR
To remove this warning do one of the following:
2019-10-22 22:47:41 +00:00
* Run "yadm upgrade" to move the yadm data to the new directory. (RECOMMENDED)
* Manually move yadm configurations to the directory listed above.
2019-08-06 13:19:45 +00:00
* Specify your preferred yadm directory with -Y each execution.
* Define an environment variable "YADM_COMPATIBILITY=1" to run in version 1
compatibility mode. (DEPRECATED)
Legacy paths detected:
${path_list}
***********
EOF
2019-12-03 14:30:51 +00:00
LEGACY_WARNING_ISSUED=1
2019-08-06 13:19:45 +00:00
}
2016-03-23 07:41:12 +00:00
function configure_paths() {
2019-03-24 22:22:11 +00:00
# change all paths to be relative to YADM_DIR
2016-03-23 07:41:12 +00:00
YADM_REPO="$YADM_DIR/$YADM_REPO"
YADM_CONFIG="$YADM_DIR/$YADM_CONFIG"
YADM_ENCRYPT="$YADM_DIR/$YADM_ENCRYPT"
YADM_ARCHIVE="$YADM_DIR/$YADM_ARCHIVE"
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
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
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() {
local proc_version
proc_version=$(cat "$PROC_VERSION" 2>/dev/null)
2020-02-03 14:30:50 +00:00
if [[ "$proc_version" =~ [Mm]icrosoft ]]; then
2017-04-09 12:56:27 +00:00
OPERATING_SYSTEM="WSL"
else
OPERATING_SYSTEM=$(uname -s)
fi
case "$OPERATING_SYSTEM" in
2018-03-04 04:02:45 +00:00
CYGWIN*|MINGW*|MSYS*)
2019-07-29 13:00:09 +00:00
git_version="$("$GIT_PROGRAM" --version 2>/dev/null)"
2017-04-09 12:56:27 +00:00
if [[ "$git_version" =~ windows ]] ; then
2017-10-09 13:21:32 +00:00
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
;;
*)
;;
esac
}
2019-12-05 04:18:22 +00:00
function set_awk() {
local pgm
for pgm in "${AWK_PROGRAM[@]}"; do
2019-12-05 04:36:58 +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() {
2017-09-15 23:35:41 +00:00
echo_e "ERROR: $*"
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
2017-07-03 21:25:23 +00:00
if [ -x "$hook_command" ] ; then
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
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
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-05 12:58:40 +00:00
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
2019-12-12 14:00:10 +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%/*}
[ -e "$basedir" ] || mkdir -p "$basedir"
}
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=()
ENCRYPT_EXCLUDE_FILES=()
2017-09-18 12:51:08 +00:00
cd_work "Parsing encrypt" || return
2017-09-18 12:39:10 +00:00
2019-11-26 22:24:51 +00:00
# setting globstar to allow ** in encrypt patterns
# (only supported on Bash >= 4)
local unset_globstar
2019-12-05 04:36:58 +00:00
if ! shopt globstar &> /dev/null; then
2019-11-26 22:24:51 +00:00
unset_globstar=1
fi
2019-12-05 04:36:58 +00:00
shopt -s globstar &> /dev/null
2019-11-26 22:24:51 +00:00
2017-09-18 12:39:10 +00:00
exclude_pattern="^!(.+)"
if [ -f "$YADM_ENCRYPT" ] ; then
2019-03-24 22:22:11 +00:00
# parse both included/excluded
2017-09-18 12:39:10 +00:00
while IFS='' read -r line || [ -n "$line" ]; do
if [[ ! $line =~ ^# && ! $line =~ ^[[:space:]]*$ ]] ; then
local IFS=$'\n'
for pattern in $line; do
if [[ "$pattern" =~ $exclude_pattern ]]; then
for ex_file in ${BASH_REMATCH[1]}; do
if [ -e "$ex_file" ]; then
ENCRYPT_EXCLUDE_FILES+=("$ex_file")
fi
done
else
for in_file in $pattern; do
if [ -e "$in_file" ]; then
ENCRYPT_INCLUDE_FILES+=("$in_file")
fi
done
fi
done
fi
done < "$YADM_ENCRYPT"
2019-03-24 22:22:11 +00:00
# remove excludes from the includes
2017-09-18 12:39:10 +00:00
#(SC2068 is disabled because in this case, we desire globbing)
FINAL_INCLUDE=()
#shellcheck disable=SC2068
for included in "${ENCRYPT_INCLUDE_FILES[@]}"; do
skip=
#shellcheck disable=SC2068
for ex_file in ${ENCRYPT_EXCLUDE_FILES[@]}; do
[ "$included" == "$ex_file" ] && { skip=1; break; }
done
[ -n "$skip" ] || FINAL_INCLUDE+=("$included")
done
2019-03-24 22:05:11 +00:00
2019-03-24 22:22:11 +00:00
# sort the encrypted files
2019-03-24 22:05:11 +00:00
#shellcheck disable=SC2207
2019-04-05 13:02:42 +00:00
IFS=$'\n' ENCRYPT_INCLUDE_FILES=($(LC_ALL=C sort <<<"${FINAL_INCLUDE[*]}"))
2019-03-24 22:05:11 +00:00
unset IFS
2017-09-18 12:39:10 +00:00
fi
2019-11-26 22:24:51 +00:00
if [ "$unset_globstar" = "1" ]; then
2019-12-05 04:36:58 +00:00
shopt -u globstar &> /dev/null
2017-09-18 12:39:10 +00:00
fi
}
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
path="$1"
dname="${path%/*}"
if ! [[ "$path" =~ / ]]; then
echo "."
elif [ "$dname" = "" ]; then
echo "/"
else
echo "$dname"
fi
}
function relative_path() {
# Output a path to $2/full, relative to $1/base
#
# This fucntion created with ideas from
# https://stackoverflow.com/questions/2564634
base="$1"
full="$2"
common_part="$base"
result=""
count=0
while [ "${full#$common_part}" == "${full}" ]; do
[ "$count" = "500" ] && return # this is a failsafe
# no match, means that candidate common part is not correct
# go up one level (reduce common part)
common_part="$(builtin_dirname "$common_part")"
# and record that we went back, with correct / handling
if [[ -z $result ]]; then
result=".."
else
result="../$result"
fi
count=$((count+1))
done
if [[ $common_part == "/" ]]; then
# special case for root (no common path)
result="$result/"
fi
# since we now have identified the common part,
# compute the non-common part
forward_part="${full#$common_part}"
# and now stick all parts together
if [[ -n $result ]] && [[ -n $forward_part ]]; then
result="$result$forward_part"
elif [[ -n $forward_part ]]; then
# extra slash removal
result="${forward_part:1}"
fi
echo "$result"
}
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
2015-07-17 21:21:47 +00:00
if [ "$CHANGES_POSSIBLE" = "1" ] ; then
2015-07-14 21:39:52 +00:00
auto_alt=$(config --bool yadm.auto-alt)
2015-07-14 12:48:47 +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
2015-07-17 21:21:47 +00:00
if [ "$CHANGES_POSSIBLE" = "1" ] ; then
2015-07-14 21:39:52 +00:00
auto_perms=$(config --bool yadm.auto-perms)
2015-07-14 12:48:47 +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
if [ "$DO_BOOTSTRAP" -eq 1 ] ; then
echo "Found $YADM_BOOTSTRAP"
echo "It appears that a bootstrap program exists."
echo "Would you like to execute it now? (y/n)"
2017-01-27 22:58:20 +00:00
read -r answer < /dev/tty
2017-01-25 07:07:07 +00:00
if [[ $answer =~ ^[yY]$ ]] ; then
bootstrap
fi
fi
}
2019-12-28 15:09:19 +00:00
# ****** Helper Functions ******
function join_string {
local IFS="$1"
2019-12-29 23:11:36 +00:00
printf "%s" "${*:2}"
2019-12-28 15:09:19 +00:00
}
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)"
local more_info
more_info=""
if [ "$alt_git" != "" ] ; then
GIT_PROGRAM="$alt_git"
more_info="\nThis command has been set via the yadm.git-program configuration."
fi
2019-12-05 04:36:58 +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)"
local more_info
more_info=""
if [ "$alt_gpg" != "" ] ; then
GPG_PROGRAM="$alt_gpg"
more_info="\nThis command has been set via the yadm.gpg-program configuration."
fi
2019-12-05 04:36:58 +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)"
local more_info
more_info=""
if [ "$alt_openssl" != "" ] ; then
OPENSSL_PROGRAM="$alt_openssl"
more_info="\nThis command has been set via the yadm.openssl-program configuration."
fi
command -v "$OPENSSL_PROGRAM" >/dev/null 2>&1 || \
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() {
command -v "$GIT_CRYPT_PROGRAM" &> /dev/null ||
error_out "This functionality requires git-crypt to be installed, but the command '$GIT_CRYPT_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() {
2019-12-05 04:36:58 +00:00
command -v "${AWK_PROGRAM[0]}" &> /dev/null && return
2019-10-01 13:12:18 +00:00
return 1
}
function j2cli_available() {
2019-12-05 04:36:58 +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() {
2019-12-05 04:36:58 +00:00
command -v "$ENVTPL_PROGRAM" &> /dev/null && return
2017-03-31 04:51:23 +00:00
return 1
}
2019-04-10 13:43:11 +00:00
function readlink_available() {
2019-12-05 04:36:58 +00:00
command -v "readlink" &> /dev/null && return
2017-03-31 04:51:23 +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
2017-01-09 01:04:40 +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
2017-01-09 01:04:40 +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
if [ "$YADM_TEST" != 1 ] ; then
process_global_args "$@"
2017-04-09 12:56:27 +00:00
set_operating_system
2019-12-05 04:18:22 +00:00
set_awk
2019-08-06 13:19:45 +00:00
set_yadm_dir
2016-03-24 00:16:21 +00:00
configure_paths
main "${MAIN_ARGS[@]}"
fi