user3458442
user3458442

Reputation: 9

What is the algorithm git uses to find a commit by a partial sha-1 (at least first 4 characters)?

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?

Upvotes: 0

Views: 225

Answers (1)

VonC
VonC

Reputation: 1323223

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 to get_short_sha1().

So you can see the algorithm in sha1_name.c#get_short_sha1() function, which will looks in:

Upvotes: 3

Related Questions