如果存在具有不同扩展名的文件,请为文件创建一个文件 [英] Gitignore a file if file with different extension exists

查看:108
本文介绍了如果存在具有不同扩展名的文件,请为文件创建一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个涉及解析器的项目中,一些源代码是程序的产物。例如 yacc 根据 .yy 文件生成 - 一个实现解析器的源代码文件。



由于对 .yy 文件的小修改会导致编译后对应的源代码文件发生大的变化,源代码在生成后几乎不会改变。这样的源代码文件是 .gitignore 列表的完美候选。



现在人们可以手动编写这样的文件到列表:

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

但是,当您构建新的解析器时,他/她可能会忘记添加文件到列表中。



有没有一种方法可以指定一种模式,其行为如下所示:


给定一个文件 foo.ext1 exists,忽略 foo.ext2


凡提供 ext1 ext2 。在词法分析器/解析器的情况下,这意味着应用这种模式用于 .xx / .cpp .yy / .cpp



这可能是不可能的由 .gitignore 直接,但也许有一些钩子已经实现了这个?

解决方案

我可以看到三种方式来做到这一点 - 直接回答问题,不,忽略处理仅查询有问题的路径名,而不是其他任何关于环境的。





将您生成的源文件放入生成的 p>我,我最喜欢这个,我知道我很少,但我不喜欢我的源代码目录中的构建碎片。 你的makefile目标也会更新 .gitignore ,并带有
$ b

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

其中目标是生成的源文件,作为配方的一部分。

  • 在生成的文件名本身中放置一些标记, scanner.generated .c 或其他东西。


  • 由于某种原因,我真的不喜欢pre-commit hook清理不需要的来源,git从一个提交中删除事情本身就是令人不安的。


    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?

    解决方案

    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.

    这篇关于如果存在具有不同扩展名的文件,请为文件创建一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆