动态 .gitignore 配置一遍又一遍地遵循相同的模式

发布于 2025-01-10 17:01:05 字数 1808 浏览 0 评论 0原文

git 在这里。

我的项目结构将如下所示:

app/
    README.md
    .gitignore
    src/
        <all source code goes here>
    docs/
        <docs go here>
    misc/
        examples/
            <examples go here>
        fizzbuzz/
            properties/
            configs/

misc/fizzbuzz/properties/ 目录非常特殊,我希望能够为以下内容设置一些特殊的 .gitignore 配置:它。在这个目录中,我将有许多子目录,如下所示:

app/
    README.md
    .gitignore
    src/
        <all source code goes here>
    docs/
        <docs go here>
    misc/
        examples/
            <examples go here>
        fizzbuzz/
            properties/
                abc/
                def/
                ghi/
                ...etc.
            configs/

每个子目录(properties/)都将遵循相同的模式:

abc/
    abc.properties
    abc.txt
    many many other files...
def/
    def.properties
    def.txt
    many many other files...
ghi/
    ghi.properties
    ghi.txt
    many many other files...

因此,在每个子目录中都会有:

  1. 一个属性文件,其名称与其所属子目录的名称完全相同(properties/ghi/ 下的 ghi.properties 等); 一个 txt 文件,
  2. 其名称也与其所属子目录的名称完全相同(properties/ghi/ 下的 ghi.txt 等) ;还有
  3. 很多很多其他文件。有些是 ghi.* (“ghi”文件,只是不是属性或 txt 类型)。有些是 *.properties*.txt (其他属性文件或 txt 文件,但不是 ghi.propertiesghi。 txt 等)。有些会在各处被命名(foobar.mp4flam.csv 等)。但这些文件的共同点是它们的格式不是“.properties”或“.txt””。

我正在尝试配置.gitignore以忽略这些子目录中的所有内容,除了与其所在目录名称匹配的属性和txt文件之外。

任何人都可以帮忙吗把我推向正确的方向?

git here.

My project structure will look something like this:

app/
    README.md
    .gitignore
    src/
        <all source code goes here>
    docs/
        <docs go here>
    misc/
        examples/
            <examples go here>
        fizzbuzz/
            properties/
            configs/

The misc/fizzbuzz/properties/ directory is very special and I am hoping to be able to set up some special .gitignore configurations for it. In this directory I will have many child directories, like so:

app/
    README.md
    .gitignore
    src/
        <all source code goes here>
    docs/
        <docs go here>
    misc/
        examples/
            <examples go here>
        fizzbuzz/
            properties/
                abc/
                def/
                ghi/
                ...etc.
            configs/

Each of these child directories (of properties/) will follow the same pattern:

abc/
    abc.properties
    abc.txt
    many many other files...
def/
    def.properties
    def.txt
    many many other files...
ghi/
    ghi.properties
    ghi.txt
    many many other files...

So in each child directory there will be:

  1. A properties file whose name is the exact same as the name of the child directory it is a member of (ghi.properties under properties/ghi/, etc.); and
  2. A txt file whose name is also the exact same as the name of the child directory it is a member of (ghi.txt under properties/ghi/, etc.); and
  3. Many, many other files. Some will be ghi.* (a "ghi" file, just not of type properties or txt). Some will be *.properties or *.txt (other properties files or txt files, just not ghi.properties or ghi.txt, etc.). Some will be named all over the place (foobar.mp4, flam.csv, etc.). But the thing these files have in common is that they are not of the form "<parent-dir-name>.properties" or "<parent-dir-name>.txt".

I am trying to configure .gitignore to ignore everything in these child directories except for the properties and txt file that matches the name of the directory they live inside of.

Can anyone help nudge me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别靠近我心 2025-01-17 17:01:05

手动编写一个简单的 .gitignore 文件似乎很简单:

abc
!abc/abc.properties
!abc/abc.txt
def
!def/def.properties
!def/def.txt
# ...etc.

生成此类 .gitignore 文件的简短 Bash shell 脚本可能如下所示,但带有 basename 的更具可读性的版本可能会更清晰:

cd app/...../properties
for i in */; do printf "%s\n" "${i%/}" '!'"$i${i%/}."{properties,txt}; done > .gitignore

这样的脚本可以添加到 git-commit 挂钩或生成文件夹的自动化中。

Composing a simple .gitignore file by hand seems simple:

abc
!abc/abc.properties
!abc/abc.txt
def
!def/def.properties
!def/def.txt
# ...etc.

A short Bash shell script to generate such .gitignore file could look like this, but a more readable version with basename would maybe be clearer:

cd app/...../properties
for i in */; do printf "%s\n" "${i%/}" '!'"$i${i%/}."{properties,txt}; done > .gitignore

Such a script could be added to git-commit hooks or to the automation that generates the folders.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文