Skip to content

Commit c443758

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 78eeb63 commit c443758

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

object-file.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,16 @@ int check_and_freshen_file(const char *fn, int freshen)
9292

9393
static int check_and_freshen_source(struct odb_source *source,
9494
const struct object_id *oid,
95-
int freshen)
95+
int freshen, int skip_virtualized_objects)
9696
{
9797
static struct strbuf path = STRBUF_INIT;
9898
int ret, tried_hook = 0;
9999

100100
odb_loose_path(source, &path, oid);
101101
retry:
102102
ret = check_and_freshen_file(path.buf, freshen);
103-
if (!ret && gvfs_virtualize_objects(source->odb->repo) && !tried_hook) {
103+
if (!ret && gvfs_virtualize_objects(source->odb->repo) &&
104+
!skip_virtualized_objects && !tried_hook) {
104105
tried_hook = 1;
105106
if (!read_object_process(source->odb->repo, oid))
106107
goto retry;
@@ -111,7 +112,7 @@ static int check_and_freshen_source(struct odb_source *source,
111112
int has_loose_object(struct odb_source *source,
112113
const struct object_id *oid)
113114
{
114-
return check_and_freshen_source(source, oid, 0);
115+
return check_and_freshen_source(source, oid, 0, 0);
115116
}
116117

117118
int format_object_header(char *str, size_t size, enum object_type type,
@@ -909,11 +910,12 @@ static int write_loose_object(struct odb_source *source,
909910
}
910911

911912
static int freshen_loose_object(struct object_database *odb,
912-
const struct object_id *oid)
913+
const struct object_id *oid,
914+
int skip_virtualized_objects)
913915
{
914916
odb_prepare_alternates(odb);
915917
for (struct odb_source *source = odb->sources; source; source = source->next)
916-
if (check_and_freshen_source(source, oid, 1))
918+
if (check_and_freshen_source(source, oid, 1, skip_virtualized_objects))
917919
return 1;
918920
return 0;
919921
}
@@ -1014,7 +1016,7 @@ int stream_loose_object(struct odb_source *source,
10141016
close_loose_object(source, fd, tmp_file.buf);
10151017

10161018
if (freshen_packed_object(source->odb, oid) ||
1017-
freshen_loose_object(source->odb, oid)) {
1019+
freshen_loose_object(source->odb, oid, 1)) {
10181020
unlink_or_warn(tmp_file.buf);
10191021
goto cleanup;
10201022
}
@@ -1078,7 +1080,7 @@ int write_object_file(struct odb_source *source,
10781080
*/
10791081
write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen);
10801082
if (freshen_packed_object(source->odb, oid) ||
1081-
freshen_loose_object(source->odb, oid))
1083+
freshen_loose_object(source->odb, oid, 1))
10821084
return 0;
10831085
if (write_loose_object(source, oid, hdr, hdrlen, buf, len, 0, flags))
10841086
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)