What is the algorithm git uses to find a commit by a partial sha-1 (at least first 4 characters). Are there any implementations of such algorithm out there?
Asked
Active
Viewed 209 times
0
-
Have you checked the source code? – Tim Mar 25 '14 at 06:46
-
1This question is off-topic because Stack Overflow is for questions about writing code. – Adi Inbar Mar 25 '14 at 07:08
-
1To me this question makes sense and looks a valid Stack Overflow question. – Learner Mar 25 '14 at 08:23
1 Answers
3
One very simple way (but ineffective) to find the full SHA1 given a partial "01234
" one (a "short SHA1")is:
git rev-list --all --objects | grep ^01234
The actual way is:
git rev-parse --verify 01234
It is illustrated in commit 6269b6b
Teach
get_describe_name()
to pass the disambiguation hint down the callchain toget_short_sha1()
.
So you can see the algorithm in sha1_name.c#get_short_sha1() function, which will looks in:
- objects:
find_short_object_filename(len, hex_pfx, &ds);
- and in pack files:
find_short_packed_object(len, bin_pfx, &ds);
(See "Git Internals - Packfiles")