Skip to content

Commit

Permalink
Wild stab at fixing overlapping renames ...
Browse files Browse the repository at this point in the history
Taking a list of files in an otherwise empty directory like:
    01.txt
    02.txt
    03.txt

And making it into:
    02.txt
    03.txt
    04.txt

Will naively create these operations:
    mv 01.txt 02.txt
    mv 02.txt 03.txt
    mv 03.txt 04.txt

And since they are performed in order, the final directory state will
contain a single file, 04.txt, which will be the 01.txt from the
original list.  This is not the most likely intention.  Performing the
operations in the reverse order would result in the most likely, desired
outcome.  However, more complext situations could arise where files are
renamed in an arbitrary order that would not be handled in a naive pass
either forward or backward.  The solution here is a loop that tries
until there are 2 passes across the list of operations in which no files
are renamed.

Ideally this would be tested for ahead of time instead of leaving the
directory in an intermediate state.  But that would exceed "wild stab"
level of implementation.  :)
  • Loading branch information
opello committed Oct 29, 2022
1 parent 3bb51a4 commit 4a94aba
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions vimv
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,36 @@ if (( ${#src[@]} != ${#dest[@]} )); then
exit 1
fi

declare -i count=0
declare -i desired_renames=0
for ((i=0;i<${#src[@]};++i)); do
if [ "${src[i]}" != "${dest[i]}" ]; then
mkdir -p "$(dirname "${dest[i]}")"
if git ls-files --error-unmatch "${src[i]}" > /dev/null 2>&1; then
git mv "${src[i]}" "${dest[i]}"
else
mv "${src[i]}" "${dest[i]}"
((++desired_renames))
fi
done

declare -i fruitless_attempts=0
declare -i old_count=0
declare -i count=0
while [ "$fruitless_attempts" -lt 2 ] && [ "$count" != "$desired_renames" ]; do
for ((i=0;i<${#src[@]};++i)); do
if [ "${src[i]}" != "${dest[i]}" ]; then
if [ ! -e "${dest[i]}" ]; then
mkdir -p "$(dirname "${dest[i]}")"
if git ls-files --error-unmatch "${src[i]}" > /dev/null 2>&1; then
git mv "${src[i]}" "${dest[i]}"
else
mv -n "${src[i]}" "${dest[i]}"
fi
((++count))
else
continue
fi
fi
((++count))
done
if [ "$count" != "$old_count" ]; then
((old_count=count))
else
((++fruitless_attempts))
fi
done

Expand Down

0 comments on commit 4a94aba

Please sign in to comment.