Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c7a469

Browse files
committed
Merge pull request libgit2#3262 from libgit2/vmg/prefix-len
Desenfuckenate prefix lookups
2 parents 1488807 + 13e5e34 commit 3c7a469

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

src/object.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ int git_object_lookup_prefix(
129129
if (error < 0)
130130
return error;
131131

132-
if (len > GIT_OID_RAWSZ)
133-
len = GIT_OID_RAWSZ;
132+
if (len > GIT_OID_HEXSZ)
133+
len = GIT_OID_HEXSZ;
134134

135-
if (len == GIT_OID_RAWSZ) {
135+
if (len == GIT_OID_HEXSZ) {
136136
git_cached_obj *cached = NULL;
137137

138138
/* We want to match the full id : we can first look up in the cache,
@@ -172,9 +172,9 @@ int git_object_lookup_prefix(
172172
memcpy(short_oid.id, id->id, (len + 1) / 2);
173173
if (len % 2)
174174
short_oid.id[len / 2] &= 0xF0;
175-
memset(short_oid.id + (len + 1) / 2, 0, (GIT_OID_RAWSZ - len) / 2);
175+
memset(short_oid.id + (len + 1) / 2, 0, (GIT_OID_HEXSZ - len) / 2);
176176

177-
/* If len < GIT_OID_RAWSZ (a strict short oid was given), we have
177+
/* If len < GIT_OID_HEXSZ (a strict short oid was given), we have
178178
* 2 options :
179179
* - We always search in the cache first. If we find that short oid is
180180
* ambiguous, we can stop. But in all the other cases, we must then

tests/diff/blob.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ void test_diff_blob__initialize(void)
3333

3434
/* tests/resources/attr/root_test4.txt */
3535
cl_git_pass(git_oid_fromstrn(&oid, "a0f7217a", 8));
36-
cl_git_pass(git_blob_lookup_prefix(&d, g_repo, &oid, 4));
36+
cl_git_pass(git_blob_lookup_prefix(&d, g_repo, &oid, 8));
3737

3838
/* alien.png */
3939
cl_git_pass(git_oid_fromstrn(&oid, "edf3dcee", 8));
40-
cl_git_pass(git_blob_lookup_prefix(&alien, g_repo, &oid, 4));
40+
cl_git_pass(git_blob_lookup_prefix(&alien, g_repo, &oid, 8));
4141
}
4242

4343
void test_diff_blob__cleanup(void)
@@ -80,7 +80,7 @@ void test_diff_blob__can_compare_text_blobs(void)
8080

8181
/* tests/resources/attr/root_test3 */
8282
cl_git_pass(git_oid_fromstrn(&c_oid, "c96bbb2c2557a832", 16));
83-
cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 8));
83+
cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 16));
8484

8585
/* Doing the equivalent of a `git diff -U1` on these files */
8686

@@ -163,15 +163,15 @@ void test_diff_blob__can_compare_text_blobs_with_patch(void)
163163

164164
/* tests/resources/attr/root_test1 */
165165
cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8));
166-
cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4));
166+
cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 8));
167167

168168
/* tests/resources/attr/root_test2 */
169169
cl_git_pass(git_oid_fromstrn(&b_oid, "4d713dc4", 8));
170-
cl_git_pass(git_blob_lookup_prefix(&b, g_repo, &b_oid, 4));
170+
cl_git_pass(git_blob_lookup_prefix(&b, g_repo, &b_oid, 8));
171171

172172
/* tests/resources/attr/root_test3 */
173173
cl_git_pass(git_oid_fromstrn(&c_oid, "c96bbb2c2557a832", 16));
174-
cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 8));
174+
cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 16));
175175

176176
/* Doing the equivalent of a `git diff -U1` on these files */
177177

@@ -437,7 +437,7 @@ void test_diff_blob__can_compare_two_binary_blobs(void)
437437

438438
/* heart.png */
439439
cl_git_pass(git_oid_fromstrn(&h_oid, "de863bff", 8));
440-
cl_git_pass(git_blob_lookup_prefix(&heart, g_repo, &h_oid, 4));
440+
cl_git_pass(git_blob_lookup_prefix(&heart, g_repo, &h_oid, 8));
441441

442442
cl_git_pass(git_diff_blobs(
443443
alien, NULL, heart, NULL, &opts,
@@ -505,7 +505,7 @@ void test_diff_blob__comparing_two_text_blobs_honors_interhunkcontext(void)
505505

506506
/* tests/resources/attr/root_test1 from commit f5b0af1 */
507507
cl_git_pass(git_oid_fromstrn(&old_d_oid, "fe773770", 8));
508-
cl_git_pass(git_blob_lookup_prefix(&old_d, g_repo, &old_d_oid, 4));
508+
cl_git_pass(git_blob_lookup_prefix(&old_d, g_repo, &old_d_oid, 8));
509509

510510
/* Test with default inter-hunk-context (not set) => default is 0 */
511511
cl_git_pass(git_diff_blobs(
@@ -598,7 +598,7 @@ void test_diff_blob__can_compare_blob_to_buffer(void)
598598

599599
/* tests/resources/attr/root_test1 */
600600
cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8));
601-
cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4));
601+
cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 8));
602602

603603
/* diff from blob a to content of b */
604604
quick_diff_blob_to_str(a, NULL, b_content, 0, NULL);
@@ -640,7 +640,7 @@ void test_diff_blob__can_compare_blob_to_buffer_with_patch(void)
640640

641641
/* tests/resources/attr/root_test1 */
642642
cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8));
643-
cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4));
643+
cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 8));
644644

645645
/* diff from blob a to content of b */
646646
cl_git_pass(git_patch_from_blob_and_buffer(
@@ -719,10 +719,10 @@ void test_diff_blob__binary_data_comparisons(void)
719719
opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
720720

721721
cl_git_pass(git_oid_fromstrn(&oid, "45141a79", 8));
722-
cl_git_pass(git_blob_lookup_prefix(&nonbin, g_repo, &oid, 4));
722+
cl_git_pass(git_blob_lookup_prefix(&nonbin, g_repo, &oid, 8));
723723

724724
cl_git_pass(git_oid_fromstrn(&oid, "b435cd56", 8));
725-
cl_git_pass(git_blob_lookup_prefix(&bin, g_repo, &oid, 4));
725+
cl_git_pass(git_blob_lookup_prefix(&bin, g_repo, &oid, 8));
726726

727727
/* non-binary to reference content */
728728

@@ -825,11 +825,11 @@ void test_diff_blob__using_path_and_attributes(void)
825825
opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
826826

827827
cl_git_pass(git_oid_fromstrn(&oid, "45141a79", 8));
828-
cl_git_pass(git_blob_lookup_prefix(&nonbin, g_repo, &oid, 4));
828+
cl_git_pass(git_blob_lookup_prefix(&nonbin, g_repo, &oid, 8));
829829
/* 20b: "Hello from the root\n" */
830830

831831
cl_git_pass(git_oid_fromstrn(&oid, "b435cd56", 8));
832-
cl_git_pass(git_blob_lookup_prefix(&bin, g_repo, &oid, 4));
832+
cl_git_pass(git_blob_lookup_prefix(&bin, g_repo, &oid, 8));
833833
/* 33b: "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\n0123456789\n" */
834834

835835
/* non-binary to reference content */

tests/refs/revparse.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,3 +812,18 @@ void test_refs_revparse__ext_returns_NULL_reference_when_expression_points_at_a_
812812
"0266163a49e280c4f5ed1e08facd36a2bd716bcf",
813813
NULL);
814814
}
815+
816+
void test_refs_revparse__uneven_sizes(void)
817+
{
818+
test_object("a65fedf39aefe402d3bb6e24df4d4f5fe454775",
819+
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
820+
821+
test_object("a65fedf39aefe402d3bb6e24df4d4f5fe45477",
822+
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
823+
824+
test_object("a65fedf39aefe402d3bb6e24df4d4f5fe4547",
825+
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
826+
827+
test_object("a65fedf39aefe402d3bb6e24df4d",
828+
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
829+
}

0 commit comments

Comments
 (0)