如何在根目录上列出 git 子树?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16641057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:32:57  来源:igfitidea点击:

How can I list the git subtrees on the root?

gitgit-subtree

提问by pocesar

For example, you can do a git remote --verboseand git will show all the remotes you have on your project, git branchwill show all the branches and signal the current branch, but how to list all subtrees, without any destructive command? git subtreewill give the usage examples, but won't list anything. subtree only have add,pull,push,split,merge.

例如,您可以执行 agit remote --verbose并且 git 将显示您项目中的所有遥控器,git branch将显示所有分支并发出当前分支的信号,但是如何在没有任何破坏性命令的情况下列出所有子树?git subtree将给出使用示例,但不会列出任何内容。子树只有add, pull, push, split, merge

回答by Sagi Iltus

There isn't any explicit way to do that (at least, for now), the only available commands are listed here (as you noted yourself, but here's a reference for future seekers): https://github.com/git/git/blob/master/contrib/subtree/git-subtree.txt

没有任何明确的方法可以做到这一点(至少目前是这样),此处列出了唯一可用的命令(正如您自己指出的那样,但这是未来寻求者的参考):https: //github.com/git/ git/blob/master/contrib/subtree/git-subtree.txt

I went through the code (basically all this mechanism is a big shell script file), all of the tracking is done through commit messages, so all the functions use git logmechanism with lots of grep-ing to locate it's own data.

我浏览了代码(基本上所有这些机制都是一个大的 shell 脚本文件),所有的跟踪都是通过提交消息完成的,所以所有的函数都使用git log带有很多grep-ing 的机制来定位它自己的数据。

Since subtree must have a folder with the same name in the root folder of the repository, you can run this to get the info you want (in Bash shell):

由于子树必须在存储库的根文件夹中具有相同名称的文件夹,因此您可以运行它以获取所需的信息(在 Bash shell 中):

git log | grep git-subtree-dir | tr -d ' ' | cut -d ":" -f2 | sort | uniq

Now, this doesn't check whether the folder exist or not (you may delete it and the subtree mechanism won't know), so here's how you can list only the existing subtrees, this will work in any folder in the repository:

现在,这不会检查文件夹是否存在(您可以删除它并且子树机制不会知道),因此您可以通过以下方式仅列出现有子树,这将适用于存储库中的任何文件夹:

 git log | grep git-subtree-dir | tr -d ' ' | cut -d ":" -f2 | sort | uniq | xargs -I {} bash -c 'if [ -d $(git rev-parse --show-toplevel)/{} ] ; then echo {}; fi'

If you're really up to it, propose it to Git guys to include in next versions:)

如果您真的能胜任,请将其建议给 Git 人员以包含在下一个版本中:)

回答by miraculixx

Following up on Sagi Illtus' answer, add the following alias to your ~/.gitconfig

跟进Sagi Illtus 的回答,将以下别名添加到您的~/.gitconfig

[alias]
    ls-subtrees = !"git log | grep git-subtree-dir | awk '{ print  }'"

Then you can git ls-subtreesfrom the root of your repository to show all subtree paths:

然后您可以git ls-subtrees从存储库的根目录显示所有子树路径:

$> cd /path/to/repository
$> git ls-subtrees
some/subtree/dir

回答by Paul Mundt

The problem with grepping the log is this tells you nothing about whether the subtree still exists or not. I've worked around this by simply testing the existence of the directory:

grep 日志的问题在于,这不会告诉您子树是否仍然存在。我通过简单地测试目录的存在来解决这个问题:

[alias]
        ls-subtrees = !"for i in $(git log | grep git-subtree-dir | sed -e 's/^.*: //g' | uniq); do test -d $i && echo $i; done"