Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

git - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...