Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wildcard support to .bsync-ignore #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,35 @@ A sample run with a conflict: file deleted one side and updated the other side.
.bsync-ignore files
-------------------

You can add directories/files paths in a `.bsync-ignore` file located at the root of a sync directory.
Every path in it will be ignored when syncing with other dirs. You can also see that as a mask for the synchronization.
You can add directories/file paths in a `.bsync-ignore` file located at the root of a sync directory.
It uses Unix-style pattern matching:

| Pattern | Meaning |
|---------|----------------------------------|
| * | matches everything |
| ? | matches any single character |
| [seq] | matches any character in seq |
| [!seq] | matches any character not in seq |

For example:
```
# Ignore a directory and all its contents
path/to/ignoredir/

# Ignore a specific file
path/to/ignorefile

# Ignore a file with a specific name in any direcotory
*.DS_Store

# Ignore certain files in any directory under a specific path
path/*/.DS_Store

# Ignore a file in any directory EXCEPT the root
*/.DS_Store
```

You can also see that as a mask for the synchronization.

Say, if I have a `dir1/.bsync-ignore` file with content:

Expand All @@ -128,7 +155,7 @@ Say, if I have a `dir1/.bsync-ignore` file with content:

`dir1/path/to/ignoredir` (+content) and `dir1/path/to/ignorefile` will be ignored in the next bsync runs.

The ignore file has to be very simple. No comments, just path prefixes.
The ignore file has to be very simple. Lines that start with `#` are comments and are ignored.

### See also

Expand Down
10 changes: 5 additions & 5 deletions bsync
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os, sys, shutil, subprocess, collections, time, datetime, shlex, getopt, stat
import os, sys, shutil, subprocess, collections, time, datetime, shlex, getopt, stat, fnmatch

# from python3.3 tree: Lib/shlex.py (shlex.quote not in python3.2)
import re
Expand Down Expand Up @@ -301,19 +301,19 @@ def get_ignores(ignorefile, ssh,dirname):

ignores = set()
for l in lines:
if l != "":
if not l.endswith("/"): l+="/"
ignores.add(l)
if l != "" and not l.strip().startswith("#"):
ignores.add(str.encode(l))
return ignores


# returns True if the path has to be ignored
# ignore root path and .bsync files
def ignorepath(path, ignoreset):
if path == b"" or path.startswith(b".bsync-"):
return True
else:
for ignore in ignoreset:
if (path+b"/").startswith(ignore.encode()):
if fnmatch.fnmatchcase(path, ignore):
return True
return False

Expand Down
26 changes: 26 additions & 0 deletions tests/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,31 @@ test_exotic_filename_ssh() {
ssh $sshargs $SSHLOGIN "find $SSHDIR | grep exotic:"
}

test_ignore_files() {
echo "**test_ignore_files**"
mkdir $DIR1/ignore
touch $DIR1/ignore/afile
touch $DIR1/ignore/exactfile
touch $DIR1/ignore/otherfile
mkdir $DIR1/ignore2
touch $DIR1/ignore2/otherfile
echo '# Ignore files named otherfile no matter what directory they are in' > $DIR1/.bsync-ignore
echo '**/otherfile' >> $DIR1/.bsync-ignore
echo '# Ignore exact file match (old style matches)' >> $DIR1/.bsync-ignore
echo 'ignore/exactfile' >> $DIR1/.bsync-ignore
yes | $BSYNC $DIR1 $DIR2
[ ! -f $DIR2/ignore/otherfile ]
[ ! -f $DIR2/ignore/exactfile ]
[ ! -f $DIR2/ignore2/otherfile ]
[ -f $DIR2/ignore/afile ]
rm -f $DIR1/.bsync-ignore
rm -rf $DIR1/ignore/
rm -rf $DIR1/ignore2/
rm -rf $DIR2/ignore/
rm -rf $DIR2/ignore2/
}


########

test_no_args
Expand Down Expand Up @@ -176,6 +201,7 @@ test_non_interactive
test_non_interactive_exit

test_symlinks
test_ignore_files

test_ssh_fail_noremotedir

Expand Down