Skip to content

Instantly share code, notes, and snippets.

@dargmuesli
Last active November 18, 2022 12:49
Show Gist options
  • Save dargmuesli/400c1488cd4b14db6a52314d29a2f1d5 to your computer and use it in GitHub Desktop.
Save dargmuesli/400c1488cd4b14db6a52314d29a2f1d5 to your computer and use it in GitHub Desktop.
Removes Syncthing sync conflicts that are exactly the same as their base files.
#!/bin/bash
set -o errexit -o pipefail -o noclobber -o nounset
LIGHT_BLUE='\e[94m'
LIGHT_GREEN='\e[92m'
LIGHT_RED='\e[91m'
LIGHT_YELLOW='\e[93m'
NC='\e[0m'
dry_run=false
path="."
function usage {
echo -e "usage: ${0##*/} ${LIGHT_YELLOW}<options>${NC}"
echo -e ""
echo -e "${LIGHT_YELLOW}options${NC}"
echo -e " -h, --help Display this help."
echo -e " -d, --dry-run Do not delete duplicates."
echo -e " -p, --path <path> The path to search in."
exit 1
}
# Check if getopt is available
! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]
then
echo -e "${LIGHT_RED}Cannot parse parameters!${NC}"
usage
fi
# Parse command line parameters
OPTIONS=hdp:
LONGOPTS=help,dry-run,path:
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS \
--name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]
then
exit 2
fi
eval set -- "$PARSED"
while true
do
case "$1" in
-h|--help)
usage
;;
-d|--dry-run)
dry_run=true
shift 1
;;
-p|--path)
path="$2"
shift 2
;;
--)
shift
break
;;
*)
echo -e "${LIGHT_RED}Programming error!${NC}"
exit 2
;;
esac
done
if [ ! -d "$path" ]
then
echo -e "${LIGHT_RED}Directory does not exist!${NC}"
usage
fi
echo -e "Searching in ${LIGHT_BLUE}$path${NC}...\n"
find "$path" -name "*.sync-conflict-*" |
while read conflict
do
base=$(echo "$conflict" | sed -e "s/sync-conflict-.*\.//")
echo "$conflict"
if [ ! -f "$base" ]
then
echo -e "Base file does not exist.\n"
continue
fi
base_sha=$(sha256sum "$base" | cut -f1 -d ' ' || true)
conflict_sha=$(sha256sum "$conflict" | cut -f1 -d ' ' || true)
if [[ "$base_sha" != "" && "$base_sha" == "$conflict_sha" ]]
then
echo -e "${LIGHT_GREEN}Match${NC} ${LIGHT_BLUE}$base_sha${NC}"
if [ "$dry_run" == "false" ]
then
rm "$conflict"
echo "Removed."
fi
echo ""
else
echo -e "${LIGHT_RED}Not a match${NC}"
echo -e "Base sha: ${LIGHT_BLUE}$base_sha${NC}"
echo -e "Conflict sha: ${LIGHT_BLUE}$conflict_sha${NC}\n"
fi
done
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment