Get git sha depth on a remote branch
Asked Answered
G

2

9

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.

Goshawk answered 8/10, 2016 at 17:56 Comment(1)
do we have any change in this on the git side - to have this done more efficently?Goshawk
M
6

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
Multure answered 9/10, 2016 at 9:20 Comment(1)
The command git rev-list HEAD ^42c6ee9 --count was failing with the error: fatal: option '--count' must come before non-option arguments What worked for me was git rev-list 42c6ee9..HEAD --countFatshan
T
1

You can create a preliminary shallow clone with git clone --depth 1 and then incrementally increase the history depth with git fetch --depth N until the desired commit arrives.

From the documentation of git-fetch:

--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), deepen or shorten the history to the specified number of commits. Tags for the deepened commits are not fetched.

Torras answered 8/10, 2016 at 19:14 Comment(1)
Thanks @Leon, but this sounds as something that might result of taking longer and more traffic than cloning the whole repo at onceGoshawk

© 2022 - 2024 — McMap. All rights reserved.