Johann Bauer
Johann Bauer

Reputation: 2606

phar exclude directories when creating tar archive

I want to create a tar archive in a PHP script using the built-in PharData class.

I want the tar archive to represent a directory so I thought of using PharData::buildFromDirectory() to do that. Unfortunately the directory also is a git repository and has a .git folder in it which is much bigger than the directory itself.

So I need to remove that .git directory (ideally also the .idea directory...) from the archive because it would bloat it unnecessarily.

What I tried so far:

So, what is the best way to do what I want?

Upvotes: 4

Views: 1847

Answers (2)

Frug
Frug

Reputation: 448

I've seen the following used to exclude git directories using a negative lookahead:

$phar->buildFromDirectory(__DIR__, '/^((?!\.git).)*$/');

Upvotes: 3

cweiske
cweiske

Reputation: 31078

The problem with your regex is that the full path of the file gets matched, not only the directory (basename). Thus you cannot filter out the .git directory itself.

Using a character class negation ([^.][^g][^i][^t]) does also not help because there are parts of the path that do match this regex, so the path matches anyway.

This means you can use positive matches only.


You could use Phar::buildFromIterator and then use RecursiveFilterIterator to filter out the files. There you can define your own matching method that filters correctly.

Upvotes: 2

Related Questions