Skip to content

Instantly share code, notes, and snippets.

@jyn514
Created February 8, 2023 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jyn514/1238f0cc10ef8e2e2b5762a3ebd79707 to your computer and use it in GitHub Desktop.
Save jyn514/1238f0cc10ef8e2e2b5762a3ebd79707 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -x
error() {
echo "$@" >&2
}
usage() {
error "usage: $0 <src> <dest>"
exit 1
}
if ! [ "$#" = 2 ]; then
usage
fi
src_dir=$1
dst_dir=$2
# $1 must be the absolute path of the source file.
dest_path() {
# https://stackoverflow.com/a/16623897/7669110
relative_src=${1#"$src_dir"}
echo "$dst_dir/$relative_src"
}
IFS=':' read -ra MODIFIED <<< "$WATCHEXEC_WRITTEN_PATH:$WATCHEXEC_CREATED_PATH"
for f in ${MODIFIED[@]}; do
abs_path=$WATCHEXEC_COMMON_PATH/$f
cp "$abs_path" "$(dest_path "$abs_path")"
done
IFS=':' read -ra REMOVED <<< "$WATCHEXEC_REMOVED_PATH"
for f in ${REMOVED[@]}; do
rm -f "$(dest_path "$WATCHEXEC_COMMON_PATH/$f")"
done
IFS=':' read -ra RENAMED <<< "$WATCHEXEC_RENAMED_PATH"
abs_path=$WATCHEXEC_COMMON_PATH/$f
dst_path=$(dest_path "$abs_path")
# If a file is renamed, that will show up in "metadata changed". Avoid creating a file that's been deleted in the source dir.
if [ -e "$abs_path" ]; then
cp "$abs_path" "$dst_path"
else
rm -f "$dst_path"
fi
done
IFS=':' read -ra CHANGED <<< "$WATCHEXEC_META_CHANGED_PATH"
for f in ${CHANGED[@]}; do
abs_path=$WATCHEXEC_COMMON_PATH/$f
# If a file is renamed, that will show up in "metadata changed". Avoid creating a file that's been deleted in the source dir.
if [ -e "$abs_path" ]; then
touch "$(dest_path "$WATCHEXEC_COMMON_PATH/$f")"
fi
done
@jyn514
Copy link
Author

jyn514 commented Feb 8, 2023

you don't want this. use rsync instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment