Skip to content

Commit 790086d

Browse files
dschovdye
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 <johasc@microsoft.com>
1 parent 260d6a0 commit 790086d

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
@@ -1122,15 +1122,17 @@ static int check_and_freshen_nonlocal(const struct object_id *oid, int freshen)
11221122
return 0;
11231123
}
11241124

1125-
static int check_and_freshen(const struct object_id *oid, int freshen)
1125+
static int check_and_freshen(const struct object_id *oid, int freshen,
1126+
int skip_virtualized_objects)
11261127
{
11271128
int ret;
11281129
int tried_hook = 0;
11291130

11301131
retry:
11311132
ret = check_and_freshen_local(oid, freshen) ||
11321133
check_and_freshen_nonlocal(oid, freshen);
1133-
if (!ret && core_virtualize_objects && !tried_hook) {
1134+
if (!ret && core_virtualize_objects && !skip_virtualized_objects &&
1135+
!tried_hook) {
11341136
tried_hook = 1;
11351137
if (!read_object_process(oid))
11361138
goto retry;
@@ -1146,7 +1148,7 @@ int has_loose_object_nonlocal(const struct object_id *oid)
11461148

11471149
int has_loose_object(const struct object_id *oid)
11481150
{
1149-
return check_and_freshen(oid, 0);
1151+
return check_and_freshen(oid, 0, 0);
11501152
}
11511153

11521154
static void mmap_limit_check(size_t length)
@@ -2218,9 +2220,10 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
22182220
return finalize_object_file(tmp_file.buf, filename.buf);
22192221
}
22202222

2221-
static int freshen_loose_object(const struct object_id *oid)
2223+
static int freshen_loose_object(const struct object_id *oid,
2224+
int skip_virtualized_objects)
22222225
{
2223-
return check_and_freshen(oid, 1);
2226+
return check_and_freshen(oid, 1, skip_virtualized_objects);
22242227
}
22252228

22262229
static int freshen_packed_object(const struct object_id *oid)
@@ -2314,7 +2317,7 @@ int stream_loose_object(struct input_stream *in_stream, size_t len,
23142317
die(_("deflateEnd on stream object failed (%d)"), ret);
23152318
close_loose_object(fd, tmp_file.buf);
23162319

2317-
if (freshen_packed_object(oid) || freshen_loose_object(oid)) {
2320+
if (freshen_packed_object(oid) || freshen_loose_object(oid, 1)) {
23182321
unlink_or_warn(tmp_file.buf);
23192322
goto cleanup;
23202323
}
@@ -2354,7 +2357,7 @@ int write_object_file_flags(const void *buf, size_t len,
23542357
*/
23552358
write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr,
23562359
&hdrlen);
2357-
if (freshen_packed_object(oid) || freshen_loose_object(oid))
2360+
if (freshen_packed_object(oid) || freshen_loose_object(oid, 1))
23582361
return 0;
23592362
return write_loose_object(oid, hdr, hdrlen, buf, len, 0, flags);
23602363
}
@@ -2375,7 +2378,7 @@ int write_object_file_literally(const void *buf, size_t len,
23752378

23762379
if (!(flags & HASH_WRITE_OBJECT))
23772380
goto cleanup;
2378-
if (freshen_packed_object(oid) || freshen_loose_object(oid))
2381+
if (freshen_packed_object(oid) || freshen_loose_object(oid, 1))
23792382
goto cleanup;
23802383
status = write_loose_object(oid, header, hdrlen, buf, len, 0, 0);
23812384

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/t0411-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)