Skip to content

Commit

Permalink
update rclone ignore script
Browse files Browse the repository at this point in the history
  • Loading branch information
contrun committed Oct 21, 2024
1 parent 951de73 commit 2d001d3
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions dot_bin/executable_convert-gitignore-to-rcloneignore.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

set -euo pipefail

# Mostly copied from
# https://github.com/rclone/rclone/issues/671#issuecomment-1153700933

if ! cd "$@"; then
>&2 echo "Folder $* not found"
exit 1
Expand All @@ -24,51 +27,51 @@ while IFS= read -r -d $'\0' path <&3; do
[[ $ignore =~ ^# ]] && continue

# Trim spaces using parameter expansion
ignore=${ignore##+([[:space:]])}
# Starting and trailling whitespace can be escaped with \, but we ignore that anyway
ignore="${ignore##+([[:space:]])}"

# Ignore empty lines
[[ -z "$ignore" ]] && continue

# An optional prefix "!" which negates the pattern.
if [[ $ignore == !* ]]; then
# Use parameter expansion instead of sed
ignore=${ignore#!}
ignore="${ignore#!}"
include=true
else
include=false
fi

pattern="$ignore"

# A separator before the end of the pattern makes it absolute
if [[ $ignore =~ / && $ignore != */ ]]; then
# Use parameter expansion instead of sed
# A pattern with a slash in the middle or beginning means this is a absolute path,
# while a trailing slash means this is a directory.
if [[ "${ignore%/}" =~ / ]]; then
pattern="${root%/}/${pattern#/}"
else
# Mimic relative search by making an absolute WRT to current directory,
# preceded by a recursive glob `**`
pattern="${root%/}/**/$pattern"
fi

# A separator at the end only matches directories
if [[ $ignore =~ /$ ]]; then
# rclone only matches files, so we need to add `**` to match a directory
pattern="$pattern**"
fi

if [[ $include = true ]]; then
pattern="+ $pattern"
else
pattern="- $pattern"

fi

# If the pattern doesn't end with `/`, include the file variant:
[[ $ignore =~ [^/]$ ]] && echo "$pattern"

# In any case, include the directory variant:
# Use parameter expansion instead of sed
echo "${pattern%/}/**"
# A separator at the end only matches directories
if [[ $ignore =~ /$ ]]; then
# rclone only matches files, so we need to add `**` to match a directory
echo "$pattern**"
else
# The pattern doesn't end with `/`, it may be a file or a directory
# The output below is for the case this is a file
echo "$pattern"
# The output below is for the case this is a directory
echo "$pattern/**"
fi
done <"$path"
done 3< <(find ./ -type f -name '.gitignore' -print0) | tee "$tmprcloneignore"

Expand Down

0 comments on commit 2d001d3

Please sign in to comment.