Skip to content

Commit 28d0788

Browse files
dschomjcheetham
authored andcommitted
sha1_file: when writing objects, skip the read_object_hook
If we are going to write an object there is no use in calling the read object hook to get an object from a potentially remote source. We would rather just write out the object and avoid the potential round trip for an object that doesn't exist. This change adds a flag to the check_and_freshen() and freshen_loose_object() functions' signatures so that the hook is bypassed when the functions are called before writing loose objects. The check for a local object is still performed so we don't overwrite something that has already been written to one of the objects directories. Based on a patch by Kevin Willford. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 4e30600 commit 28d0788

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

object-file.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,15 +1187,17 @@ static int check_and_freshen_nonlocal(const struct object_id *oid, int freshen)
11871187
return 0;
11881188
}
11891189

1190-
static int check_and_freshen(const struct object_id *oid, int freshen)
1190+
static int check_and_freshen(const struct object_id *oid, int freshen,
1191+
int skip_virtualized_objects)
11911192
{
11921193
int ret;
11931194
int tried_hook = 0;
11941195

11951196
retry:
11961197
ret = check_and_freshen_local(oid, freshen) ||
11971198
check_and_freshen_nonlocal(oid, freshen);
1198-
if (!ret && core_virtualize_objects && !tried_hook) {
1199+
if (!ret && core_virtualize_objects && !skip_virtualized_objects &&
1200+
!tried_hook) {
11991201
tried_hook = 1;
12001202
if (!read_object_process(oid))
12011203
goto retry;
@@ -1211,7 +1213,7 @@ int has_loose_object_nonlocal(const struct object_id *oid)
12111213

12121214
int has_loose_object(const struct object_id *oid)
12131215
{
1214-
return check_and_freshen(oid, 0);
1216+
return check_and_freshen(oid, 0, 0);
12151217
}
12161218

12171219
static void mmap_limit_check(size_t length)
@@ -2465,9 +2467,10 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
24652467
FOF_SKIP_COLLISION_CHECK);
24662468
}
24672469

2468-
static int freshen_loose_object(const struct object_id *oid)
2470+
static int freshen_loose_object(const struct object_id *oid,
2471+
int skip_virtualized_objects)
24692472
{
2470-
return check_and_freshen(oid, 1);
2473+
return check_and_freshen(oid, 1, skip_virtualized_objects);
24712474
}
24722475

24732476
static int freshen_packed_object(const struct object_id *oid)
@@ -2563,7 +2566,7 @@ int stream_loose_object(struct input_stream *in_stream, size_t len,
25632566
die(_("deflateEnd on stream object failed (%d)"), ret);
25642567
close_loose_object(fd, tmp_file.buf);
25652568

2566-
if (freshen_packed_object(oid) || freshen_loose_object(oid)) {
2569+
if (freshen_packed_object(oid) || freshen_loose_object(oid, 1)) {
25672570
unlink_or_warn(tmp_file.buf);
25682571
goto cleanup;
25692572
}
@@ -2625,7 +2628,7 @@ int write_object_file_flags(const void *buf, size_t len,
26252628
* it out into .git/objects/??/?{38} file.
26262629
*/
26272630
write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen);
2628-
if (freshen_packed_object(oid) || freshen_loose_object(oid))
2631+
if (freshen_packed_object(oid) || freshen_loose_object(oid, 1))
26292632
return 0;
26302633
if (write_loose_object(oid, hdr, hdrlen, buf, len, 0, flags))
26312634
return -1;
@@ -2669,7 +2672,7 @@ int write_object_file_literally(const void *buf, size_t len,
26692672

26702673
if (!(flags & HASH_WRITE_OBJECT))
26712674
goto cleanup;
2672-
if (freshen_packed_object(oid) || freshen_loose_object(oid))
2675+
if (freshen_packed_object(oid) || freshen_loose_object(oid, 1))
26732676
goto cleanup;
26742677
status = write_loose_object(oid, header, hdrlen, buf, len, 0, 0);
26752678
if (compat_type != -1)

t/t0410/read-object

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ while (1) {
108108
system ('git --git-dir="' . $DIR . '" cat-file blob ' . $sha1 . ' | git -c core.virtualizeobjects=false hash-object -w --stdin >/dev/null 2>&1');
109109
packet_txt_write(($?) ? "status=error" : "status=success");
110110
packet_flush();
111+
112+
open my $log, '>>.git/read-object-hook.log';
113+
print $log "Read object $sha1, exit code $?\n";
114+
close $log;
111115
} else {
112116
die "bad command '$command'";
113117
}

t/t0499-read-object.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,12 @@ test_expect_success 'invalid blobs generate errors' '
2626
test_must_fail git cat-file blob "invalid")
2727
'
2828

29+
test_expect_success 'read-object-hook is bypassed when writing objects' '
30+
(cd guest-repo &&
31+
echo hello >hello.txt &&
32+
git add hello.txt &&
33+
hash="$(git rev-parse --verify :hello.txt)" &&
34+
! grep "$hash" .git/read-object-hook.log)
35+
'
2936

3037
test_done

0 commit comments

Comments
 (0)