简体   繁体   中英

how to know the depth of a git's shallow clone?

If I retrieve a shallow clone, eg

git clone --depth 10 http://foo.bar/baz.git

Does git have any command to retrieve the depth of this clone? (eg a command that simply prints 10 ).

Short answer: no. The number is not stored anywhere (unless you store it yourself—and it might be nice if Git saved it in .git/config somewhere).

A repository is shallow (by Git's internal definition) if and only if the file .git/shallow exists. The contents of this file are a bit sneaky: most of Git treats it in exactly the same way as .git/grafts . That is, each line in the shallow file contains a commit hash ID (and nothing else, vs grafts , where each line is followed by all the grafted parent IDs: since the line is empty, there are no parent IDs and the commit becomes a root commit). (Git can flag it internally as "not really a root", indicating that it has parents that simply have never been obtained. To be completely reliable, this would also require ensuring that no actual root commit IDs are ever stored in the file, and I am not sure if that is the case.)

You could compute a max depth of all references: starting with each reference, count graph depth from that tipmost commit down to a (possibly grafted) root. This would not, however, necessarily be the number passed to --depth (here or, later, with a --depth given to git fetch ).

Suppose, for instance, that we clone a repository that has only two commits, while also using --depth 10 . The deepest chain is either one or two commits long, since there are only two commits: one (real) root for certain, and one commit that might have the other as its parent, or might be another (real) root commit. If—this is the case I do not know the answer to—the .git/shallow file never contains real roots, it would be empty at this point and we would know that however long the longest chain is, the --depth argument must have been greater than that, but we would not know the actual number.

Suppose, on the other hand, we clone a 10-or-more-commit repository, with our --depth 10 , and get a chain of 10 commits terminated by a fake-root-graft. Then we add two new commits to this 10-long chain, so that we have a 12-commit chain. The --depth is still 10, but counting chains, we find 12.

So, this shows two ways that a computed count could be too small or too large. In many circumstances, though, the computed count would work well. To get computed counts, use git for-each-ref to find each reference, and git rev-list --count --first-parent on each found reference. Whatever maximum you get is likely the depth number, or something close enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM