Skip to content

Commit 02a1026

Browse files
dschojeffhostetler
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 870e719 commit 02a1026

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
@@ -1048,15 +1048,17 @@ static int check_and_freshen_nonlocal(const struct object_id *oid, int freshen)
10481048
return 0;
10491049
}
10501050

1051-
static int check_and_freshen(const struct object_id *oid, int freshen)
1051+
static int check_and_freshen(const struct object_id *oid, int freshen,
1052+
int skip_virtualized_objects)
10521053
{
10531054
int ret;
10541055
int tried_hook = 0;
10551056

10561057
retry:
10571058
ret = check_and_freshen_local(oid, freshen) ||
10581059
check_and_freshen_nonlocal(oid, freshen);
1059-
if (!ret && core_virtualize_objects && !tried_hook) {
1060+
if (!ret && core_virtualize_objects && !skip_virtualized_objects &&
1061+
!tried_hook) {
10601062
tried_hook = 1;
10611063
if (!read_object_process(oid))
10621064
goto retry;
@@ -1072,7 +1074,7 @@ int has_loose_object_nonlocal(const struct object_id *oid)
10721074

10731075
static int has_loose_object(const struct object_id *oid)
10741076
{
1075-
return check_and_freshen(oid, 0);
1077+
return check_and_freshen(oid, 0, 0);
10761078
}
10771079

10781080
static void mmap_limit_check(size_t length)
@@ -2050,9 +2052,10 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
20502052
return finalize_object_file(tmp_file.buf, filename.buf);
20512053
}
20522054

2053-
static int freshen_loose_object(const struct object_id *oid)
2055+
static int freshen_loose_object(const struct object_id *oid,
2056+
int skip_virtualized_objects)
20542057
{
2055-
return check_and_freshen(oid, 1);
2058+
return check_and_freshen(oid, 1, skip_virtualized_objects);
20562059
}
20572060

20582061
static int freshen_packed_object(const struct object_id *oid)
@@ -2079,7 +2082,7 @@ int write_object_file(const void *buf, unsigned long len, const char *type,
20792082
*/
20802083
write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr,
20812084
&hdrlen);
2082-
if (freshen_packed_object(oid) || freshen_loose_object(oid))
2085+
if (freshen_packed_object(oid) || freshen_loose_object(oid, 1))
20832086
return 0;
20842087
return write_loose_object(oid, hdr, hdrlen, buf, len, 0);
20852088
}
@@ -2099,7 +2102,7 @@ int hash_object_file_literally(const void *buf, unsigned long len,
20992102

21002103
if (!(flags & HASH_WRITE_OBJECT))
21012104
goto cleanup;
2102-
if (freshen_packed_object(oid) || freshen_loose_object(oid))
2105+
if (freshen_packed_object(oid) || freshen_loose_object(oid, 1))
21032106
goto cleanup;
21042107
status = write_loose_object(oid, header, hdrlen, buf, len, 0);
21052108

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
@@ -23,5 +23,12 @@ test_expect_success 'invalid blobs generate errors' '
2323
test_must_fail git cat-file blob "invalid")
2424
'
2525

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

2734
test_done

0 commit comments

Comments
 (0)