How can I list the git subtrees on the root?

孤街浪徒 提交于 2019-11-27 10:18:11

问题


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


回答1:


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

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 log mechanism with lots of grep-ing to locate it's own data.

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):

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:)




回答2:


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

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

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

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



回答3:


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:

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


来源:https://stackoverflow.com/questions/16641057/how-can-i-list-the-git-subtrees-on-the-root

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!