如何在此递归查找和替换中引用当前路径? [英] How to refer to the current path in this recursive find and replace?

查看:72
本文介绍了如何在此递归查找和替换中引用当前路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明 :(脱离主题警告)这不是关于输出存储库中实际检测到的被忽略文件列表的不是.这是关于忽略的 paths 的信息,即使实际上没有文件与这些路径之一匹配.

Disclaimer: (off-topic warning) This is not about outputting the list of ignored files actually detected in the repo. This is about ignored paths, even when no file is in fact matching one of these paths.

上下文:我正在尝试编写git别名,以递归方式平化"所有.gitignore模式,并输出从顶层看到的路径列表.

Context: I'm attempting to write a git alias to "flatten" all .gitignore patterns recursively and output a list of paths as they're seen from the top level.

我的意思是一个例子:

├─ .git
├─ .gitignore
└─ dir1
    ├─ .gitignore
    ├─ file1.txt
    └─ file2.txt

这些内容包含在.gitignore个文件中:

With these contents in .gitignore files:

# (currently pointing at top-level directory)
$ cat .gitignore
some_path

$ cat dir1/.gitignore
yet_another_path
*.txt

我尝试使用别名来输出类似

I try to have an alias to output something along the lines of

$ git flattened-ignore-list
some_path
dir1/yet_another_path
dir1/*.txt


到目前为止,我有什么?

我知道我可以使用以下命令在存储库中搜索所有.gitignore文件

I know I can search for all .gitignore files in the repo with

find . -name ".gitignore"

在这种情况下将输出

.gitignore
dir1/.gitignore

因此,我尝试将其与cat结合使用以获取其内容(两项工作中的任何一项)

So I've tried to combine this with cat to get their contents (either of these work)

find . -name ".gitignore" | xargs cat
# or
cat $(find . -name ".gitignore")

结果如下:

some_path
yet_another_path
*.txt

这在技术上是可以预期的,但不幸的是对于我要实现的目标没有帮助.所以(最后!)得出我的实际问题:

which is technically expected but unfortunately unhelpful for what I am trying to achieve. So to (at last!) arrive at my actual question:

对于find的每个结果,我如何参考当前路径?(以便最终将其添加到行中)

How can I, for each result of find, refer to the current path? (in order to eventually prepend it to the line)

为怀疑有XY问题的人提供的提示:可能是 ,我的方法在这里可能只是天真,但不确定,我不确定.例如,我没有考虑过嵌套的.gitignore文件可以引用更高级别的特殊情况,也没有考虑使用**的特殊语法.我现在只停留在非常简单的结构上,因此,如果您发现一个缺陷和/或可以提出一种完全不同的方法来实现相同的目标,我当然也很高兴听到它.

Note for people suspecting an XY problem : It might be the case, my approach might just be naive here, but maybe not, I'm unsure. For example I didn't consider complex cases where nested .gitignore files could refer to upper-levels, or special syntax with **. I've stuck to very simple structures for now, so in case you see a flaw and/or can suggest a totally different way to achieve the same goal, I'll of course be happy to hear about it also.

推荐答案

您可以生成完整的忽略模式列表,其目录前缀如下:

You can produce a complete list of ignore patterns, with directory prefix like this:

#!usr/bin/env sh

find \
  . \
  -type f \
  -name '.gitignore' \
  -printf '%h\n' \
  | while IFS= read -r dir_name; do
    printf \
      "${dir_name}/%s\\n" \
      $(
        sed \
          --silent \
          '/^[^#[:space:]]/p' \
          "$dir_name/.gitignore"
      )
  done

上面的代码将只列出在目录中的.gitignore文件中找到的所有模式,并将目录添加为每个模式的前缀.

The above code will just list all patterns found in .gitignore files across directories, and add the directory as prefix of each pattern.

它不反映git文档中此处描述的gitignore语法和行为: https://git-scm.com/docs/gitignore

It does not reflect gitignore syntax and behavior that is described here in git documentation: https://git-scm.com/docs/gitignore

这篇关于如何在此递归查找和替换中引用当前路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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