Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-clone

If I retrieve a shallow clone, e.g.

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

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

like image 877
knocte Avatar asked May 12 '16 09:05

knocte


People also ask

What is shallow clone depth?

A shallow clone is a repository created by limiting the depth of the history that is cloned from an original repository. The depth of the cloned repository, which is selected when the cloning operation is performed, is defined as the number of total commits that the linear history of the repository will contain.

What is -- depth in git clone?

"Clone depth" is a feature of git to reduce server load: Instead of cloning the complete repository (as usually done with git), using clone depth just clones the last clone-depth-number revisions of your repository. In literature this is also called "shallow clone"

What is fetch depth in git?

--depth=<depth> Limit fetching to the specified number of commits from the tip of each remote branch history. If fetching to a shallow repository created by git clone with --depth=<depth> option (see git-clone[1]), deepen or shorten the history to the specified number of commits.


1 Answers

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.

like image 131
torek Avatar answered Nov 02 '22 22:11

torek