pocesar
pocesar

Reputation: 7050

How can I list the git subtrees on the root?

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.

Upvotes: 56

Views: 33933

Answers (4)

Bobax
Bobax

Reputation: 55

Have a look at subrepo. It solves drawbacks when using "native" git subtree commands. these drawback are due to the fact that git does not store the original repo url of the subtree.

Upvotes: 0

Paul Mundt
Paul Mundt

Reputation: 509

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"

Upvotes: 4

miraculixx
miraculixx

Reputation: 10349

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

Upvotes: 38

Sagi
Sagi

Reputation: 1159

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

Upvotes: 75

Related Questions