Combine local and assignments where appropriate

This commit is contained in:
Tim Byrne 2020-12-21 15:51:48 -06:00
parent 6b068a141d
commit e3d2ea2863
No known key found for this signature in database
GPG Key ID: 14DB4FC2465A4B12
1 changed files with 21 additions and 36 deletions

57
yadm
View File

@ -515,16 +515,14 @@ function alt() {
cd_work "Alternates" || return cd_work "Alternates" || return
# determine all tracked files # determine all tracked files
local tracked_files local tracked_files=()
tracked_files=()
local IFS=$'\n' local IFS=$'\n'
for tracked_file in $("$GIT_PROGRAM" ls-files | LC_ALL=C sort); do for tracked_file in $("$GIT_PROGRAM" ls-files | LC_ALL=C sort); do
tracked_files+=("$tracked_file") tracked_files+=("$tracked_file")
done done
# generate data for removing stale links # generate data for removing stale links
local possible_alts local possible_alts=()
possible_alts=()
local IFS=$'\n' local IFS=$'\n'
for possible_alt in "${tracked_files[@]}" "${ENCRYPT_INCLUDE_FILES[@]}"; do for possible_alt in "${tracked_files[@]}" "${ENCRYPT_INCLUDE_FILES[@]}"; do
if [[ $possible_alt =~ .\#\#. ]]; then if [[ $possible_alt =~ .\#\#. ]]; then
@ -536,8 +534,7 @@ function alt() {
possible_alts+=("$YADM_BASE/${base_alt}") possible_alts+=("$YADM_BASE/${base_alt}")
fi fi
done done
local alt_linked local alt_linked=()
alt_linked=()
alt_linking alt_linking
remove_stale_links remove_stale_links
@ -589,14 +586,10 @@ function set_local_alt_values() {
function alt_linking() { function alt_linking() {
local alt_scores local alt_scores=()
local alt_targets local alt_targets=()
local alt_sources local alt_sources=()
local alt_template_cmds local alt_template_cmds=()
alt_scores=()
alt_targets=()
alt_sources=()
alt_template_cmds=()
for alt_path in $(for tracked in "${tracked_files[@]}"; do printf "%s\n" "$tracked" "${tracked%/*}"; done | LC_ALL=C sort -u) "${ENCRYPT_INCLUDE_FILES[@]}"; do for alt_path in $(for tracked in "${tracked_files[@]}"; do printf "%s\n" "$tracked" "${tracked%/*}"; done | LC_ALL=C sort -u) "${ENCRYPT_INCLUDE_FILES[@]}"; do
alt_path="$YADM_BASE/$alt_path" alt_path="$YADM_BASE/$alt_path"
@ -657,7 +650,7 @@ function alt_past_linking() {
# loop over all "tracked" files # loop over all "tracked" files
# for every file which matches the above regex, create a symlink # for every file which matches the above regex, create a symlink
for match in $match1 $match2; do for match in $match1 $match2; do
last_linked='' local last_linked=''
local IFS=$'\n' local IFS=$'\n'
# the alt_paths looped over here are a unique sorted list of both files and their immediate parent directory # the alt_paths looped over here are a unique sorted list of both files and their immediate parent directory
for alt_path in $(for tracked in "${tracked_files[@]}"; do printf "%s\n" "$tracked" "${tracked%/*}"; done | LC_ALL=C sort -u) "${ENCRYPT_INCLUDE_FILES[@]}"; do 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
@ -714,12 +707,13 @@ function alt_past_linking() {
function ln_relative() { function ln_relative() {
local full_source full_target target_dir local full_source full_target target_dir
full_source="$1" local full_source="$1"
full_target="$2" local full_target="$2"
target_dir="${full_target%/*}" local target_dir="${full_target%/*}"
if [ "$target_dir" == "" ]; then if [ "$target_dir" == "" ]; then
target_dir="/" target_dir="/"
fi fi
local rel_source
rel_source=$(relative_path "$target_dir" "$full_source") rel_source=$(relative_path "$target_dir" "$full_source")
ln -nfs "$rel_source" "$full_target" ln -nfs "$rel_source" "$full_target"
alt_linked+=("$rel_source") alt_linked+=("$rel_source")
@ -1328,12 +1322,9 @@ function perms() {
function upgrade() { function upgrade() {
local actions_performed local actions_performed=0
actions_performed=0 local repo_moved=0
local repo_moved local repo_updates=0
repo_moved=0
local repo_updates
repo_updates=0
[[ -n "${YADM_OVERRIDE_REPO}${YADM_OVERRIDE_ARCHIVE}" || "$YADM_DATA" = "$YADM_DIR" ]] && \ [[ -n "${YADM_OVERRIDE_REPO}${YADM_OVERRIDE_ARCHIVE}" || "$YADM_DATA" = "$YADM_DIR" ]] && \
error_out "Unable to upgrade. Paths have been overridden with command line options" error_out "Unable to upgrade. Paths have been overridden with command line options"
@ -1539,8 +1530,7 @@ function set_yadm_dirs() {
# only resolve YADM_DATA if it hasn't been provided already # only resolve YADM_DATA if it hasn't been provided already
if [ -z "$YADM_DATA" ]; then if [ -z "$YADM_DATA" ]; then
local base_yadm_data local base_yadm_data="$XDG_DATA_HOME"
base_yadm_data="$XDG_DATA_HOME"
if [[ ! "$base_yadm_data" =~ ^/ ]] ; then if [[ ! "$base_yadm_data" =~ ^/ ]] ; then
base_yadm_data="${HOME}/.local/share" base_yadm_data="${HOME}/.local/share"
fi fi
@ -1549,8 +1539,7 @@ function set_yadm_dirs() {
# only resolve YADM_DIR if it hasn't been provided already # only resolve YADM_DIR if it hasn't been provided already
if [ -z "$YADM_DIR" ]; then if [ -z "$YADM_DIR" ]; then
local base_yadm_dir local base_yadm_dir="$XDG_CONFIG_HOME"
base_yadm_dir="$XDG_CONFIG_HOME"
if [[ ! "$base_yadm_dir" =~ ^/ ]] ; then if [[ ! "$base_yadm_dir" =~ ^/ ]] ; then
base_yadm_dir="${HOME}/.config" base_yadm_dir="${HOME}/.config"
fi fi
@ -1570,8 +1559,7 @@ function issue_legacy_path_warning() {
[[ -n "${YADM_OVERRIDE_REPO}${YADM_OVERRIDE_ARCHIVE}" || "$YADM_DATA" = "$YADM_DIR" ]] && return [[ -n "${YADM_OVERRIDE_REPO}${YADM_OVERRIDE_ARCHIVE}" || "$YADM_DATA" = "$YADM_DIR" ]] && return
# test for legacy paths # test for legacy paths
local legacy_found local legacy_found=()
legacy_found=()
# this is ordered by importance # this is ordered by importance
for legacy_path in \ for legacy_path in \
"$YADM_DIR/$YADM_REPO" \ "$YADM_DIR/$YADM_REPO" \
@ -2055,8 +2043,7 @@ function require_git() {
local alt_git local alt_git
alt_git="$(config yadm.git-program)" alt_git="$(config yadm.git-program)"
local more_info local more_info=""
more_info=""
if [ "$alt_git" != "" ] ; then if [ "$alt_git" != "" ] ; then
GIT_PROGRAM="$alt_git" GIT_PROGRAM="$alt_git"
@ -2069,8 +2056,7 @@ function require_gpg() {
local alt_gpg local alt_gpg
alt_gpg="$(config yadm.gpg-program)" alt_gpg="$(config yadm.gpg-program)"
local more_info local more_info=""
more_info=""
if [ "$alt_gpg" != "" ] ; then if [ "$alt_gpg" != "" ] ; then
GPG_PROGRAM="$alt_gpg" GPG_PROGRAM="$alt_gpg"
@ -2083,8 +2069,7 @@ function require_openssl() {
local alt_openssl local alt_openssl
alt_openssl="$(config yadm.openssl-program)" alt_openssl="$(config yadm.openssl-program)"
local more_info local more_info=""
more_info=""
if [ "$alt_openssl" != "" ] ; then if [ "$alt_openssl" != "" ] ; then
OPENSSL_PROGRAM="$alt_openssl" OPENSSL_PROGRAM="$alt_openssl"