让树隐藏 gitignored 文件

Bra*_*ani 23 git tree

有没有办法tree不显示被忽略的文件.gitignore

小智 10

树支持-I标志。

-I pattern
   Do not list those files that match the wild-card pattern.
Run Code Online (Sandbox Code Playgroud)

树支持单一模式,该模式将排除与其匹配的所有文件/目录。

Git 的忽略文件有点复杂:

排除可以来自多个文件、 $HOME/.config/git/ignoregit config --get core.excludesfile.gitignore(每个目录)、 等的输出~/.gitignore(请参阅 参考资料man gitignore)。

另一个问题是tree支持的模式与 git 所做的不同(如@Brad Urani 所述)。

但是我们可以近距离接触...

tree -I "$(grep -hvE '^$|^#' {~/,,$(git rev-parse --show-toplevel)/}.gitignore|sed 's:/$::'|tr \\n '\|')"
Run Code Online (Sandbox Code Playgroud)

或者作为一个函数:

function gtree {
    git_ignore_files=("$(git config --get core.excludesfile)" .gitignore ~/.gitignore)
    ignore_pattern="$(grep -hvE '^$|^#' "${git_ignore_files[@]}" 2>/dev/null|sed 's:/$::'|tr '\n' '\|')"
    if git status &> /dev/null && [[ -n "${ignore_pattern}" ]]; then
      tree -I "${ignore_pattern}" "${@}"
    else 
      tree "${@}"
    fi
}
Run Code Online (Sandbox Code Playgroud)


Zep*_*yro 9

如果您正在使用,另一种方法是可能的,tree 1.8.0因为它支持该--fromfile标志:

   --fromfile  Reads a directory listing from a file rather than the file-system.  Paths provided on the command
   line are files to read from rather than directories to search.  The dot (.)  directory  indicates  that  tree
   should read paths from standard input.
Run Code Online (Sandbox Code Playgroud)

我们可以git ls-tree用来获取项目中所有非 git-ignored 文件,并将输出通过管道传输到 .gitignore 文件tree

假设我们有一个 git 存储库,其中ignored文件被忽略.gitignore

git_repo
??? .gitignore
??? bar
?   ??? b.txt
?   ??? ignored
??? foo
?   ??? a.txt
?   ??? ignored
??? ignored
Run Code Online (Sandbox Code Playgroud)

以下命令:

   --fromfile  Reads a directory listing from a file rather than the file-system.  Paths provided on the command
   line are files to read from rather than directories to search.  The dot (.)  directory  indicates  that  tree
   should read paths from standard input.
Run Code Online (Sandbox Code Playgroud)

给出:

.
??? .gitignore
??? bar
?   ??? b.txt
??? foo
    ??? a.txt

2 directories, 3 files
Run Code Online (Sandbox Code Playgroud)

或者,如果您需要特定路径:

git_repo
??? .gitignore
??? bar
?   ??? b.txt
?   ??? ignored
??? foo
?   ??? a.txt
?   ??? ignored
??? ignored
Run Code Online (Sandbox Code Playgroud)

给出:

.
??? a.txt

0 directories, 1 file
Run Code Online (Sandbox Code Playgroud)

注意事项

  • 请注意,尚未提交的已删除或重命名文件等更改可能会导致git ls-tree不同步。


小智 8

这可能会有所帮助:git以几乎兼容的方式为tree过滤器列出被忽略的文件:

function tree-git-ignore {
    # tree respecting gitignore

    local ignored=$(git ls-files -ci --others --directory --exclude-standard)
    local ignored_filter=$(echo "$ignored" \
                    | egrep -v "^#.*$|^[[:space:]]*$" \
                    | sed 's~^/~~' \
                    | sed 's~/$~~' \
                    | tr "\\n" "|")
    tree --prune -I ".git|${ignored_filter: : -1}" "$@"
}
Run Code Online (Sandbox Code Playgroud)


小智 6

--gitignore自 2021 年 12 月发布的 2.0.0 版本起,tree 有一个选项。

https://gitlab.com/OldManProgrammer/unix-tree/-/blob/master/CHANGES


小智 5

.gitignore默认情况下,Ripgrep尊重(以及其他一些“忽略”文件),并且可以用作 git 命令的替代方法。不同之处在于,即使尚未提交版本控制,也会列出所有文件:

rg --files | tree --fromfile
Run Code Online (Sandbox Code Playgroud)

正如另一个答案中所指出的,需要 tree > 1.8。