Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get git sha depth on a remote branch

Tags:

git

I would like to use git clone --depth [N] , but to use such N that guarantees that a particular sha is obtained.

How I can determine the depth of a sha of in a remote repo. Note cloning it locally to do that is catch 22. I would like to do that to avoid cloning it all.

like image 509
gsf Avatar asked Oct 08 '16 17:10

gsf


1 Answers

Option 1:

If you have the ability to get to a full clone of the repository, you can find the depth using git rev-list HEAD ^42c6ee9 --count.

This will find the depth of any particular commit. There is no remote version so this only works if you can maintain a full copy and then ssh into it to figure out the depth.

This allows you to only have to clone once but then you'll be able to answer the question for all the following times you'd like to do a shallow copy.

Option 2:

Using git clone --depth 1 then iterating on git fetch --depth=i+1 is actually a good idea worth testing. (Also proposed by @leon above).

Depending on the characteristics of your repository, this will make sense.

E.g. Django repository has 23330 commits (at the time of testing)

./full.sh - pulling the full local repository

git clone https://github.com/django/django

./oracle.sh - if you magically knew the right answer. lower bound on time.

git clone --depth 10 https://github.com/django/django.git

./search.sh - iterating

git clone --depth 1 https://github.com/django/django.git
cd django

i=1
until git show 5d35181 > /dev/null
do
    i=$((i+1))
    git fetch --depth=$i
done

The search, while there is overhead, may still come out faster then a full clone.

./full.sh  21.34s
./oracle.sh 1.12s
./search.sh 3.05s
like image 107
Liyan Chang Avatar answered Sep 28 '22 07:09

Liyan Chang