Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ** and * in glob matching (.gitignore)

Tags:

I have the following directory structure and files.

pw-spec/ |-- event_spec.coffee |-- event_spec.js |-- integration |   `-- service |       |-- auth_spec.coffee |       |-- auth_spec.js |       |-- chat_spec.coffee |       |-- chat_spec.js |       |-- transport_spec.coffee |       `-- transport_spec.js |-- message_spec.coffee |-- message_spec.js |-- pw_spec.coffee |-- pw_spec.js |-- run.coffee |-- run.html |-- run.js |-- service |   |-- auth_spec.coffee |   |-- auth_spec.js |   |-- chat_spec.coffee |   |-- chat_spec.js |   |-- stream_spec.coffee |   `-- stream_spec.js |-- spec.coffee |-- spec.js `-- stub     |-- stream.coffee     |-- stream.js     |-- transport.coffee     `-- transport.js  4 directories, 27 files 

I would like to ignore all *.js files anywhere within pw-spec directory.

However, adding the following patterns to .gitignore doesn't cut it:

pw-spec/*.js pw-spec/**/*.js 

The problem is that the second one only matches js files that are exactly 1 level deep within the tree, while I want to match all js files under pw-spec.

Doing

ls pw-spec/**/*.js 

produces [1]:

pw-spec/service/auth_spec.js pw-spec/service/chat_spec.js pw-spec/service/stream_spec.js pw-spec/stub/stream.js pw-spec/stub/transport.js 

As you can see

pw-spec/integration/service/auth_spec.js pw-spec/integration/service/chat_spec.js pw-spec/integration/service/transport_spec.js 

are missing from [1].

like image 607
Emil Ivanov Avatar asked Jan 12 '12 09:01

Emil Ivanov


1 Answers

There are two approaches for this type of situation, depending on your needs.

One solution is to put

# generated files *.js 

in pw-spec/.gitignore.

The second solution is to put:

/pw-spec/*.js /pw-spec/*/*.js /pw-spec/*/*/*.js 

and so forth in the main .gitignore file.This approach is brittle if more sub-directories are added.

I generally prefer to put the .gitignore file at the same level as the Makefile which generates the files that I am ignoring.

like image 145
Ben Martin Avatar answered Sep 20 '22 17:09

Ben Martin