Skip to content

Commit a3ab5e9

Browse files
committed
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 1146f24 commit a3ab5e9

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

object-file.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,17 @@ static int check_and_freshen_nonlocal(const struct object_id *oid, int freshen)
114114
return 0;
115115
}
116116

117-
static int check_and_freshen(const struct object_id *oid, int freshen)
117+
static int check_and_freshen(const struct object_id *oid, int freshen,
118+
int skip_virtualized_objects)
118119
{
119120
int ret;
120121
int tried_hook = 0;
121122

122123
retry:
123124
ret = check_and_freshen_local(oid, freshen) ||
124125
check_and_freshen_nonlocal(oid, freshen);
125-
if (!ret && core_virtualize_objects && !tried_hook) {
126+
if (!ret && core_virtualize_objects && !skip_virtualized_objects &&
127+
!tried_hook) {
126128
tried_hook = 1;
127129
if (!read_object_process(oid))
128130
goto retry;
@@ -138,7 +140,7 @@ int has_loose_object_nonlocal(const struct object_id *oid)
138140

139141
int has_loose_object(const struct object_id *oid)
140142
{
141-
return check_and_freshen(oid, 0);
143+
return check_and_freshen(oid, 0, 0);
142144
}
143145

144146
int format_object_header(char *str, size_t size, enum object_type type,
@@ -931,9 +933,10 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
931933
FOF_SKIP_COLLISION_CHECK);
932934
}
933935

934-
static int freshen_loose_object(const struct object_id *oid)
936+
static int freshen_loose_object(const struct object_id *oid,
937+
int skip_virtualized_objects)
935938
{
936-
return check_and_freshen(oid, 1);
939+
return check_and_freshen(oid, 1, skip_virtualized_objects);
937940
}
938941

939942
static int freshen_packed_object(const struct object_id *oid)
@@ -1029,7 +1032,7 @@ int stream_loose_object(struct input_stream *in_stream, size_t len,
10291032
die(_("deflateEnd on stream object failed (%d)"), ret);
10301033
close_loose_object(fd, tmp_file.buf);
10311034

1032-
if (freshen_packed_object(oid) || freshen_loose_object(oid)) {
1035+
if (freshen_packed_object(oid) || freshen_loose_object(oid, 1)) {
10331036
unlink_or_warn(tmp_file.buf);
10341037
goto cleanup;
10351038
}
@@ -1092,7 +1095,7 @@ int write_object_file_flags(const void *buf, size_t len,
10921095
* it out into .git/objects/??/?{38} file.
10931096
*/
10941097
write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen);
1095-
if (freshen_packed_object(oid) || freshen_loose_object(oid))
1098+
if (freshen_packed_object(oid) || freshen_loose_object(oid, 1))
10961099
return 0;
10971100
if (write_loose_object(oid, hdr, hdrlen, buf, len, 0, flags))
10981101
return -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)