Fix bug with alternate linked directories

Previously the tracked files were sorted, and then the files and their
parent directories were considered for possible alternates. Depending on
the length of directories and names of files, inconsistencies would
occur because the directory separator (/) would be part of the sorting.

To fix this, a unique list of tracked files and their parent directories
are sorted into a single list which is processed.
This commit is contained in:
Tim Byrne 2019-04-05 07:46:56 -05:00
parent fb1181c8a9
commit 0c6be5e398
No known key found for this signature in database
GPG Key ID: 14DB4FC2465A4B12
1 changed files with 17 additions and 19 deletions

36
yadm
View File

@ -181,29 +181,27 @@ function alt() {
for match in $match1 $match2; do
last_linked=''
local IFS=$'\n'
for tracked_file in $("$GIT_PROGRAM" ls-files | sort) "${ENCRYPT_INCLUDE_FILES[@]}"; do
tracked_file="$YADM_WORK/$tracked_file"
# process both the path, and it's parent directory
for alt_path in "$tracked_file" "${tracked_file%/*}"; do
if [ -e "$alt_path" ] ; then
if [[ $alt_path =~ $match ]] ; then
if [ "$alt_path" != "$last_linked" ] ; then
new_link="${BASH_REMATCH[1]}"
debug "Linking $alt_path to $new_link"
[ -n "$loud" ] && echo "Linking $alt_path to $new_link"
if [ "$do_copy" -eq 1 ]; then
if [ -L "$new_link" ]; then
rm -f "$new_link"
fi
cp -f "$alt_path" "$new_link"
else
ln -nfs "$alt_path" "$new_link"
# 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 $("$GIT_PROGRAM" ls-files); do printf "%s\n" "$tracked" "${tracked%/*}"; done | sort -u) "${ENCRYPT_INCLUDE_FILES[@]}"; do
alt_path="$YADM_WORK/$alt_path"
if [ -e "$alt_path" ] ; then
if [[ $alt_path =~ $match ]] ; then
if [ "$alt_path" != "$last_linked" ] ; then
new_link="${BASH_REMATCH[1]}"
debug "Linking $alt_path to $new_link"
[ -n "$loud" ] && echo "Linking $alt_path to $new_link"
if [ "$do_copy" -eq 1 ]; then
if [ -L "$new_link" ]; then
rm -f "$new_link"
fi
last_linked="$alt_path"
cp -f "$alt_path" "$new_link"
else
ln -nfs "$alt_path" "$new_link"
fi
last_linked="$alt_path"
fi
fi
done
fi
done
done