找到空的,不可忽略的目录

时间:2015-02-18 06:57:58

标签: git

我想将.gitkeep文件添加到我的仓库中的所有空目录,但来忽略目录。我已经可以找到空目录:

$ find . -type d -empty

但我怎么知道哪些被忽略了?可以直接忽略它们,或者忽略目录的子项......有没有办法直接从git获取此信息?类似的东西:

$ find . -type d -empty | git classify --stdin

ignored     : xxx
non-ignored : yyy

会很棒。

1 个答案:

答案 0 :(得分:2)

您可以使用git check-ignore执行此任务。


假设您有一个具有以下结构的存储库:

foo/a.tmp
foo/b
bar/test/
baz/

这是.gitignore

foo/*.tmp
bar/

如果您现在将find . -type d -empty的输出传递给git check-ignore,您会收到以下输出:

$ find . -type d -empty | git check-ignore --stdin
./bar/test

如您所见,git check-ignore会返回您.gitignore匹配的文件夹。要获得更详细的输出,您可以使用-n--non-matching)选项,该选项需要与-v--verbose)合并。

$ find . -type d -empty | git check-ignore --stdin -nv
::  ./.git/branches
::  ./.git/objects/info
::  ./.git/objects/pack
::  ./.git/refs/tags
.gitignore:1:bar/   ./bar/test
::  ./baz

要从搜索中排除.git文件夹,您可以向finddocumentation)提供更多参数。

$ find . -type d -empty -not -path "./.git/*" | git check-ignore --stdin -nv
.gitignore:1:bar/   ./bar/test
::  ./baz

从此处开始,您只需grep .gitignore::不匹配的文件夹,即可删除前导$ find . -type d -empty -not -path "./.git/*" | git check-ignore --stdin -nv | grep '::' | sed -E 's/::[[:space:]]*//' ./baz

{{1}}
相关问题