Last active
November 27, 2022 15:48
-
-
Save isaaclw/48dea18d16884b3714df765913e73f84 to your computer and use it in GitHub Desktop.
Clean up syncthing conflicts #bash #fs #vim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
compare_files() { | |
file="$1" | |
original="$2" | |
case "$(file "$original")" in | |
*JPEG*|*PNG*) | |
identify "$file" | |
identify "$original" | |
if sudo -Hu "$USER" xdpyinfo -display "$DISPLAY" > /dev/null 2>&1; then | |
gthumb "$file" "$original" | |
fi | |
;; | |
*MP4*) | |
diff <(echo "$file"; ffprobe -hide_banner "$file" 2>&1) <(echo "$original"; ffprobe -hide_banner "$original" 2>&1) | |
if sudo -Hu "$USER" xdpyinfo -display "$DISPLAY" > /dev/null 2>&1; then | |
gthumb "$file" "$original" | |
fi | |
;; | |
*text*|*json*) | |
vimdiff "$file" "$original" | |
;; | |
esac | |
} | |
manage_file() { | |
file="$1" | |
original="$(echo "$file" | perl -ne 's/\.sync-conflict[^\.]*(?=\.|$)//; print;')" | |
# do an initial check to see if the files are the same. | |
if cmp "$file" "$original"; then | |
echo "Identical:" | |
echo " - $original" | |
echo " - $file" | |
[ ! -e "$original" ] && echo "original missing: $original" && return 1 | |
rm -vf "$file" | |
return 0 | |
fi | |
# do some manual diffs | |
compare_files "$file" "$original" | |
if [ ! -e "$file" ]; then | |
exit 0 # nothing to do, the conflict was removed | |
elif [ ! -e "$original" ]; then | |
mv "$file" "$original" # original was removed, move the file to the original place | |
exit 0 | |
fi | |
if ! cmp "$file" "$original"; then | |
echo "Manual Merge" | |
ls -al "$file" "$original" | |
echo -n "Prefer Conflict [1], Original [0], or skip [s]? " | |
read -N 1 REPLY | |
echo | |
if [ "$REPLY" == "0" ]; then | |
[ ! -e "$original" ] && echo "original missing: $original" && return 1 | |
rm -vf "$file" | |
elif [ "$REPLY" == "1" ]; then | |
mv "$file" "$original" | |
elif [ "$REPLY" == "s" ]; then | |
return 0 | |
else | |
echo "Bad option, doing nothing" | |
return 1 | |
fi | |
else | |
echo "Identical:" | |
echo " - $original" | |
echo " - $file" | |
[ ! -e "$original" ] && echo "original missing: $original" && return 1 | |
rm -vf "$file" | |
fi | |
echo | |
} | |
export -f manage_file | |
export -f compare_files | |
[ -n "$1" ] && path="$1" || path='.' | |
find "$path" -name "*.sync-conflict*" -exec bash -c 'manage_file "$0"' {} \; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment