Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement .gitignore behavior in a shell script?

I'm writing a shell script that syncs files and I want to give users the ability to exclude certain files from syncing by creating a .syncignore file similar to Git's .gitignore file. According to the gitignore documentation, and my own experiments, these exclusion rules are more complicated than a simple glob match. Some examples:

  1. If you have foo in your .gitignore file, it will exclude foo appearing anywhere in the path (e.g. ./foo, ./bar/foo, and ./bar/foo/baz would be excluded) but not partial matches of foo (e.g. ./foobar, ./bar/foobar/baz would NOT be excluded).
  2. If you include a slash, then the rule is applied relative to the current directory. For example, if you have /foo in your .gitignore file, it will exclude ./foo but not ./bar/foo.
  3. You can include wildcards. For example, foo* will exclude ./foo, ./foobar, and ./bar/foobar/baz.

Is there any easy way to replicate the exclusion rules for .gitignore in a shell script on OS X?

like image 233
Yevgeniy Brikman Avatar asked Mar 20 '26 13:03

Yevgeniy Brikman


1 Answers

  1. Use rsync to synchronize the files. Use its existing include/exclude pattern support. Put the rules in .rsync-filter and pass the -F flag to make it read the patterns from that file.

    rsync man page

  2. Just use git. Make sure you have git 2.3.0 or later on both sides, and use push-to-deploy.

like image 148
rob mayoff Avatar answered Mar 22 '26 05:03

rob mayoff