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
988 views
in Technique[技术] by (71.8m points)

git - Does .gitignore have an equivalent for version controlled files?

This question is related to the following questions: https://stackoverflow.com/search?q=[git]+%2Bassume-unchanged, however, none of the provided answers to these questions seem to meet my needs.

I have a header file (key_info.h) that gets generated by a python script. To be clever, I version controlled a placeholder version of the header (key_info.h) with a single line of code:

int i = you_need_to_run_the_key_info_gen_python_script_to_generate_this_file();

Prior to running the python script, compiling this code will yield an error telling the developer exactly what to do; run the script.

Naturally, the developer will proceed by running the python script to generate the latest version of key_info.h and the code will successfully compile. The developer will then go on to make a series of changes and then 'git commit' their code.

The problem we run into is the fact that key_info.h has now been modified. As such, git will pick it up as a modification and will allow it to be committed. However, the generated version of this header should never be version controlled. We need the placeholder version to remain unchanged to continue to provide the compile error hint to developers.

My first instinct was to use .gitignore to prevent git from picking up key_info.h in a commit. .gitignore is then easily shared between developers since .gitignore itself is version controlled. Unfortunately, .gitignore only works for files that aren't version controlled.

My next thought was to use 'git update-index --assume-unchanged /path/to/file'. This works great locally, but I have no way of enforcing this rule upon other developers. It's only a matter of time before a dev accidentally commits the generated header.

So my question is this: is there a way to version control a file, while silently enforcing a rule that the file is to be ignored when it comes time to commit changes? What I really want is .gitignore for version controlled files.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

but I have no way of enforcing this rule upon other developers

You do, almost: you could make your key-info-gen script run git update-index --assume-unchanged /some/path.

But I don't think this is a good idea.

I would probably opt for not overwriting the header file at all. Let your Python script generate the correct header file and write it into a different directory, and pass -I compiler options for both of those directories, in the right order. If the script has not been run, the version-controlled header will be found. If the script has been run, the user-generated header will be found. And you can then put the path of the user-generated header in .gitignore.


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

1.4m articles

1.4m replys

5 comments

57.0k users

...