Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
git-mirror
/
git
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
0
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security
Insights
Files
a941d5e
Documentation
block-sha1
builtin
compat
contrib
git-gui
git_remote_helpers
gitk-git
gitweb
perl
ppc
t
templates
xdiff
.gitattributes
.gitignore
.mailmap
COPYING
GIT-VERSION-GEN
INSTALL
Makefile
README
RelNotes
abspath.c
advice.c
advice.h
alias.c
alloc.c
archive-tar.c
archive-zip.c
archive.c
archive.h
attr.c
attr.h
base85.c
bisect.c
bisect.h
blob.c
blob.h
branch.c
branch.h
builtin.h
bundle.c
bundle.h
cache-tree.c
cache-tree.h
cache.h
check-builtins.sh
check-racy.c
check_bindir
color.c
color.h
combine-diff.c
command-list.txt
commit.c
commit.h
config.c
config.mak.in
configure.ac
connect.c
convert.c
copy.c
csum-file.c
csum-file.h
ctype.c
daemon.c
date.c
decorate.c
decorate.h
delta.h
diff-delta.c
diff-lib.c
diff-no-index.c
diff.c
diff.h
diffcore-break.c
diffcore-delta.c
diffcore-order.c
diffcore-pickaxe.c
diffcore-rename.c
diffcore.h
dir.c
dir.h
editor.c
entry.c
environment.c
exec_cmd.c
exec_cmd.h
fast-import.c
fetch-pack.h
fixup-builtins
fsck.c
fsck.h
generate-cmdlist.sh
git-add--interactive.perl
git-am.sh
git-archimport.perl
git-bisect.sh
git-compat-util.h
git-cvsexportcommit.perl
git-cvsimport.perl
git-cvsserver.perl
git-difftool--helper.sh
git-difftool.perl
git-filter-branch.sh
git-instaweb.sh
git-lost-found.sh
git-merge-octopus.sh
git-merge-one-file.sh
git-merge-resolve.sh
git-mergetool--lib.sh
git-mergetool.sh
git-parse-remote.sh
git-pull.sh
git-quiltimport.sh
git-rebase--interactive.sh
git-rebase.sh
git-relink.perl
git-repack.sh
git-request-pull.sh
git-send-email.perl
git-sh-setup.sh
git-stash.sh
git-submodule.sh
git-svn.perl
git-web--browse.sh
git.c
git.spec.in
graph.c
graph.h
grep.c
grep.h
hash.c
hash.h
help.c
help.h
hex.c
http-backend.c
http-fetch.c
http-push.c
http-walker.c
http.c
http.h
ident.c
imap-send.c
levenshtein.c
levenshtein.h
list-objects.c
list-objects.h
ll-merge.c
ll-merge.h
lockfile.c
log-tree.c
log-tree.h
mailmap.c
mailmap.h
match-trees.c
merge-file.c
merge-recursive.c
merge-recursive.h
name-hash.c
notes-cache.c
notes-cache.h
notes.c
notes.h
object.c
object.h
pack-check.c
pack-refs.c
pack-refs.h
pack-revindex.c
pack-revindex.h
pack-write.c
pack.h
pager.c
parse-options.c
parse-options.h
patch-delta.c
patch-ids.c
patch-ids.h
path.c
pkt-line.c
pkt-line.h
preload-index.c
pretty.c
progress.c
progress.h
quote.c
quote.h
reachable.c
reachable.h
read-cache.c
reflog-walk.c
reflog-walk.h
refs.c
refs.h
remote-curl.c
remote.c
remote.h
replace_object.c
rerere.c
rerere.h
resolve-undo.c
resolve-undo.h
revision.c
revision.h
run-command.c
run-command.h
send-pack.h
server-info.c
setup.c
sha1-lookup.c
sha1-lookup.h
sha1_file.c
sha1_name.c
shallow.c
shell.c
shortlog.h
show-index.c
sideband.c
sideband.h
sigchain.c
sigchain.h
strbuf.c
strbuf.h
string-list.c
string-list.h
submodule.c
submodule.h
symlinks.c
tag.c
tag.h
tar.h
test-chmtime.c
test-ctype.c
test-date.c
test-delta.c
test-dump-cache-tree.c
test-genrandom.c
test-index-version.c
test-match-trees.c
test-parse-options.c
test-path-utils.c
test-run-command.c
test-sha1.c
test-sha1.sh
test-sigchain.c
thread-utils.c
thread-utils.h
trace.c
transport-helper.c
transport.c
transport.h
tree-diff.c
tree-walk.c
tree-walk.h
tree.c
tree.h
unimplemented.sh
unpack-trees.c
unpack-trees.h
upload-pack.c
usage.c
userdiff.c
userdiff.h
utf8.c
utf8.h
walker.c
walker.h
wrap-for-bin.sh
wrapper.c
write_or_die.c
ws.c
wt-status.c
wt-status.h
xdiff-interface.c
xdiff-interface.h
Breadcrumbs
git
/
sha1-lookup.c
Blame
Blame
Latest commit
History
History
272 lines (256 loc) · 7.84 KB
Breadcrumbs
git
/
sha1-lookup.c
Top
File metadata and controls
Code
Blame
272 lines (256 loc) · 7.84 KB
Raw
#include "cache.h" #include "sha1-lookup.h" static uint32_t take2(const unsigned char *sha1) { return ((sha1[0] << 8) | sha1[1]); } /* * Conventional binary search loop looks like this: * * do { * int mi = (lo + hi) / 2; * int cmp = "entry pointed at by mi" minus "target"; * if (!cmp) * return (mi is the wanted one) * if (cmp > 0) * hi = mi; "mi is larger than target" * else * lo = mi+1; "mi is smaller than target" * } while (lo < hi); * * The invariants are: * * - When entering the loop, lo points at a slot that is never * above the target (it could be at the target), hi points at a * slot that is guaranteed to be above the target (it can never * be at the target). * * - We find a point 'mi' between lo and hi (mi could be the same * as lo, but never can be the same as hi), and check if it hits * the target. There are three cases: * * - if it is a hit, we are happy. * * - if it is strictly higher than the target, we update hi with * it. * * - if it is strictly lower than the target, we update lo to be * one slot after it, because we allow lo to be at the target. * * When choosing 'mi', we do not have to take the "middle" but * anywhere in between lo and hi, as long as lo <= mi < hi is * satisfied. When we somehow know that the distance between the * target and lo is much shorter than the target and hi, we could * pick mi that is much closer to lo than the midway. */ /* * The table should contain "nr" elements. * The sha1 of element i (between 0 and nr - 1) should be returned * by "fn(i, table)". */ int sha1_pos(const unsigned char *sha1, void *table, size_t nr, sha1_access_fn fn) { size_t hi = nr; size_t lo = 0; size_t mi = 0; if (!nr) return -1; if (nr != 1) { size_t lov, hiv, miv, ofs; for (ofs = 0; ofs < 18; ofs += 2) { lov = take2(fn(0, table) + ofs); hiv = take2(fn(nr - 1, table) + ofs); miv = take2(sha1 + ofs); if (miv < lov) return -1; if (hiv < miv) return -1 - nr; if (lov != hiv) { /* * At this point miv could be equal * to hiv (but sha1 could still be higher); * the invariant of (mi < hi) should be * kept. */ mi = (nr - 1) * (miv - lov) / (hiv - lov); if (lo <= mi && mi < hi) break; die("BUG: assertion failed in binary search"); } } if (18 <= ofs) die("cannot happen -- lo and hi are identical"); } do { int cmp; cmp = hashcmp(fn(mi, table), sha1); if (!cmp) return mi; if (cmp > 0) hi = mi; else lo = mi + 1; mi = (hi + lo) / 2; } while (lo < hi); return -lo-1; } /* * Conventional binary search loop looks like this: * * unsigned lo, hi; * do { * unsigned mi = (lo + hi) / 2; * int cmp = "entry pointed at by mi" minus "target"; * if (!cmp) * return (mi is the wanted one) * if (cmp > 0) * hi = mi; "mi is larger than target" * else * lo = mi+1; "mi is smaller than target" * } while (lo < hi); * * The invariants are: * * - When entering the loop, lo points at a slot that is never * above the target (it could be at the target), hi points at a * slot that is guaranteed to be above the target (it can never * be at the target). * * - We find a point 'mi' between lo and hi (mi could be the same * as lo, but never can be as same as hi), and check if it hits * the target. There are three cases: * * - if it is a hit, we are happy. * * - if it is strictly higher than the target, we set it to hi, * and repeat the search. * * - if it is strictly lower than the target, we update lo to * one slot after it, because we allow lo to be at the target. * * If the loop exits, there is no matching entry. * * When choosing 'mi', we do not have to take the "middle" but * anywhere in between lo and hi, as long as lo <= mi < hi is * satisfied. When we somehow know that the distance between the * target and lo is much shorter than the target and hi, we could * pick mi that is much closer to lo than the midway. * * Now, we can take advantage of the fact that SHA-1 is a good hash * function, and as long as there are enough entries in the table, we * can expect uniform distribution. An entry that begins with for * example "deadbeef..." is much likely to appear much later than in * the midway of the table. It can reasonably be expected to be near * 87% (222/256) from the top of the table. * * However, we do not want to pick "mi" too precisely. If the entry at * the 87% in the above example turns out to be higher than the target * we are looking for, we would end up narrowing the search space down * only by 13%, instead of 50% we would get if we did a simple binary * search. So we would want to hedge our bets by being less aggressive. * * The table at "table" holds at least "nr" entries of "elem_size" * bytes each. Each entry has the SHA-1 key at "key_offset". The * table is sorted by the SHA-1 key of the entries. The caller wants * to find the entry with "key", and knows that the entry at "lo" is * not higher than the entry it is looking for, and that the entry at * "hi" is higher than the entry it is looking for. */ int sha1_entry_pos(const void *table, size_t elem_size, size_t key_offset, unsigned lo, unsigned hi, unsigned nr, const unsigned char *key) { const unsigned char *base = table; const unsigned char *hi_key, *lo_key; unsigned ofs_0; static int debug_lookup = -1; if (debug_lookup < 0) debug_lookup = !!getenv("GIT_DEBUG_LOOKUP"); if (!nr || lo >= hi) return -1; if (nr == hi) hi_key = NULL; else hi_key = base + elem_size * hi + key_offset; lo_key = base + elem_size * lo + key_offset; ofs_0 = 0; do { int cmp; unsigned ofs, mi, range; unsigned lov, hiv, kyv; const unsigned char *mi_key; range = hi - lo; if (hi_key) { for (ofs = ofs_0; ofs < 20; ofs++) if (lo_key[ofs] != hi_key[ofs]) break; ofs_0 = ofs; /* * byte 0 thru (ofs-1) are the same between * lo and hi; ofs is the first byte that is * different. */ hiv = hi_key[ofs_0]; if (ofs_0 < 19) hiv = (hiv << 8) | hi_key[ofs_0+1]; } else { hiv = 256; if (ofs_0 < 19) hiv <<= 8; } lov = lo_key[ofs_0]; kyv = key[ofs_0]; if (ofs_0 < 19) { lov = (lov << 8) | lo_key[ofs_0+1]; kyv = (kyv << 8) | key[ofs_0+1]; } assert(lov < hiv); if (kyv < lov) return -1 - lo; if (hiv < kyv) return -1 - hi; /* * Even if we know the target is much closer to 'hi' * than 'lo', if we pick too precisely and overshoot * (e.g. when we know 'mi' is closer to 'hi' than to * 'lo', pick 'mi' that is higher than the target), we * end up narrowing the search space by a smaller * amount (i.e. the distance between 'mi' and 'hi') * than what we would have (i.e. about half of 'lo' * and 'hi'). Hedge our bets to pick 'mi' less * aggressively, i.e. make 'mi' a bit closer to the * middle than we would otherwise pick. */ kyv = (kyv * 6 + lov + hiv) / 8; if (lov < hiv - 1) { if (kyv == lov) kyv++; else if (kyv == hiv) kyv--; } mi = (range - 1) * (kyv - lov) / (hiv - lov) + lo; if (debug_lookup) { printf("lo %u hi %u rg %u mi %u ", lo, hi, range, mi); printf("ofs %u lov %x, hiv %x, kyv %x\n", ofs_0, lov, hiv, kyv); } if (!(lo <= mi && mi < hi)) die("assertion failure lo %u mi %u hi %u %s", lo, mi, hi, sha1_to_hex(key)); mi_key = base + elem_size * mi + key_offset; cmp = memcmp(mi_key + ofs_0, key + ofs_0, 20 - ofs_0); if (!cmp) return mi; if (cmp > 0) { hi = mi; hi_key = mi_key; } else { lo = mi + 1; lo_key = mi_key + elem_size; } } while (lo < hi); return -lo-1; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
You can’t perform that action at this time.