Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitignore a file if file with different extension exists

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

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

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

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

But it is likely when one builds a new parser, he/she will forget to add the file to the list.

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

Given a file foo.ext1 exists, ignore foo.ext2.

Where one thus provides ext1 and ext2. In the case of the lexer/parser, this would mean applying this pattern for .xx/.cpp and .yy/.cpp.

This is probably not possible by .gitignore directly, but perhaps there are some hooks already implemented for this?

like image 596
Willem Van Onsem Avatar asked Feb 04 '15 15:02

Willem Van Onsem


People also ask

How do I ignore a specific file in git?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

How do I use wildcards in Gitignore?

. gitignore is a plain text file in which each line contains a pattern for files or directories to ignore. It uses globbing patterns to match filenames with wildcard characters. If you have files or directories containing a wildcard pattern, you can use a single backslash ( \ ) to escape the character.

What is the extension of a Gitignore file?

A file with the GITIGNORE file extension is a Git Ignore file used with the version/source control system called Git.


1 Answers

I can see three ways to do this -- to answer the question directly, no, ignore processing consults only the pathname in question, not anything else about the environment.

  1. Put your generated source in a generated folder you ignore.

    Me, I like this one best, I know I'm in a minority but I don't like build detritus in my source directories.

  2. Have your makefile targets also update the .gitignore, with

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

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

  3. put some marker in the generated filenames themselves, scanner.generated.c or something.

For some reason I really dislike the pre-commit hook cleaning out unwanted source, git deleting things from a commit all by itself is just disturbing.

like image 93
jthill Avatar answered Oct 08 '22 17:10

jthill