Gitignore file if a file with a different extension exists

In a project where, for example, a parser is used, some source code is a product of the program. For example, yacc generates - based on the .yy file - a source code file that the parser implements.

Since small changes in the .yy file can lead to large changes in the corresponding source code file after compilation, and since the resulting source code (almost) never changes after it is created. Such source code files are ideal candidates for the .gitignore list.

Now you can, of course, manually write such files to the list:

 lexer1.cpp parser1.cpp lexer2.cpp parser2.cpp 

But most likely, when you create a new parser, he / she will forget to add the file to the list.

Is there a way to specify a template that acts as follows:

If the file foo.ext1 exists, ignore foo.ext2 .

Where, in this way, ext1 and ext2 provided. In the case of the lexer / parser, this means using this template for .xx / .cpp and .yy / .cpp .

Perhaps this is possible not only with .gitignore , but maybe there are already some interceptors for this?

+8
git wildcard gitignore githooks
source share
2 answers

I see three ways to do this - to directly answer the question, no, ignore the processing, consider only the specified path, and not something else about the environment.

  • Put your generated source in the generated folder that you ignore.

    I, I like it best, I know I'm outnumbered, but I don't like building detritus in my source directories.

  • For your purposes, the makefile also updates .gitignore , with

     grep -qs ^target$ .gitignore || echo target >>.gitignore 

    where target is the generated source file as part of the recipe.

  • put some marker in the generated file names, scanner.generated.c or something like that.

For some reason, I really don't like to intercept the capture of an unwanted source, git to remove things from the commit is all by itself is simply alarming.

+8
source share

My suggestion is to create a script to update .gitignore. I do not know, but you can look for a solution that runs this script automatically before committing.

Edit: I wentogled and found this: https://github.com/observing/pre-commit - Some tests are probably needed.

+1
source share

All Articles