Skip to content

Commit e7e952f

Browse files
pks-tgitster
authored andcommitted
object-file: get rid of the_repository when writing objects
The logic that writes loose objects still relies on `the_repository` to decide where exactly the object shall be written to. Refactor it so that the logic instead operates on a `struct odb_source` so that we can get rid of this global dependency. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ab1c6e1 commit e7e952f

File tree

4 files changed

+58
-51
lines changed

4 files changed

+58
-51
lines changed

builtin/unpack-objects.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,8 @@ static void stream_blob(unsigned long size, unsigned nr)
403403
data.zstream = &zstream;
404404
git_inflate_init(&zstream);
405405

406-
if (stream_loose_object(&in_stream, size, &info->oid))
406+
if (stream_loose_object(the_repository->objects->sources,
407+
&in_stream, size, &info->oid))
407408
die(_("failed to write object in stream"));
408409

409410
if (data.status != Z_STREAM_END)

object-file.c

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -667,9 +667,10 @@ void hash_object_file(const struct git_hash_algo *algo, const void *buf,
667667
}
668668

669669
/* Finalize a file on disk, and close it. */
670-
static void close_loose_object(int fd, const char *filename)
670+
static void close_loose_object(struct odb_source *source,
671+
int fd, const char *filename)
671672
{
672-
if (the_repository->objects->sources->will_destroy)
673+
if (source->will_destroy)
673674
goto out;
674675

675676
if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT))
@@ -701,7 +702,8 @@ static inline int directory_size(const char *filename)
701702
* We want to avoid cross-directory filename renames, because those
702703
* can have problems on various filesystems (FAT, NFS, Coda).
703704
*/
704-
static int create_tmpfile(struct strbuf *tmp, const char *filename)
705+
static int create_tmpfile(struct repository *repo,
706+
struct strbuf *tmp, const char *filename)
705707
{
706708
int fd, dirlen = directory_size(filename);
707709

@@ -720,7 +722,7 @@ static int create_tmpfile(struct strbuf *tmp, const char *filename)
720722
strbuf_add(tmp, filename, dirlen - 1);
721723
if (mkdir(tmp->buf, 0777) && errno != EEXIST)
722724
return -1;
723-
if (adjust_shared_perm(the_repository, tmp->buf))
725+
if (adjust_shared_perm(repo, tmp->buf))
724726
return -1;
725727

726728
/* Try again */
@@ -741,26 +743,26 @@ static int create_tmpfile(struct strbuf *tmp, const char *filename)
741743
* Returns a "fd", which should later be provided to
742744
* end_loose_object_common().
743745
*/
744-
static int start_loose_object_common(struct strbuf *tmp_file,
746+
static int start_loose_object_common(struct odb_source *source,
747+
struct strbuf *tmp_file,
745748
const char *filename, unsigned flags,
746749
git_zstream *stream,
747750
unsigned char *buf, size_t buflen,
748751
struct git_hash_ctx *c, struct git_hash_ctx *compat_c,
749752
char *hdr, int hdrlen)
750753
{
751-
struct repository *repo = the_repository;
752-
const struct git_hash_algo *algo = repo->hash_algo;
753-
const struct git_hash_algo *compat = repo->compat_hash_algo;
754+
const struct git_hash_algo *algo = source->odb->repo->hash_algo;
755+
const struct git_hash_algo *compat = source->odb->repo->compat_hash_algo;
754756
int fd;
755757

756-
fd = create_tmpfile(tmp_file, filename);
758+
fd = create_tmpfile(source->odb->repo, tmp_file, filename);
757759
if (fd < 0) {
758760
if (flags & WRITE_OBJECT_SILENT)
759761
return -1;
760762
else if (errno == EACCES)
761763
return error(_("insufficient permission for adding "
762764
"an object to repository database %s"),
763-
repo_get_object_directory(the_repository));
765+
source->path);
764766
else
765767
return error_errno(
766768
_("unable to create temporary file"));
@@ -790,14 +792,14 @@ static int start_loose_object_common(struct strbuf *tmp_file,
790792
* Common steps for the inner git_deflate() loop for writing loose
791793
* objects. Returns what git_deflate() returns.
792794
*/
793-
static int write_loose_object_common(struct git_hash_ctx *c, struct git_hash_ctx *compat_c,
795+
static int write_loose_object_common(struct odb_source *source,
796+
struct git_hash_ctx *c, struct git_hash_ctx *compat_c,
794797
git_zstream *stream, const int flush,
795798
unsigned char *in0, const int fd,
796799
unsigned char *compressed,
797800
const size_t compressed_len)
798801
{
799-
struct repository *repo = the_repository;
800-
const struct git_hash_algo *compat = repo->compat_hash_algo;
802+
const struct git_hash_algo *compat = source->odb->repo->compat_hash_algo;
801803
int ret;
802804

803805
ret = git_deflate(stream, flush ? Z_FINISH : 0);
@@ -818,12 +820,12 @@ static int write_loose_object_common(struct git_hash_ctx *c, struct git_hash_ctx
818820
* - End the compression of zlib stream.
819821
* - Get the calculated oid to "oid".
820822
*/
821-
static int end_loose_object_common(struct git_hash_ctx *c, struct git_hash_ctx *compat_c,
823+
static int end_loose_object_common(struct odb_source *source,
824+
struct git_hash_ctx *c, struct git_hash_ctx *compat_c,
822825
git_zstream *stream, struct object_id *oid,
823826
struct object_id *compat_oid)
824827
{
825-
struct repository *repo = the_repository;
826-
const struct git_hash_algo *compat = repo->compat_hash_algo;
828+
const struct git_hash_algo *compat = source->odb->repo->compat_hash_algo;
827829
int ret;
828830

829831
ret = git_deflate_end_gently(stream);
@@ -836,7 +838,8 @@ static int end_loose_object_common(struct git_hash_ctx *c, struct git_hash_ctx *
836838
return Z_OK;
837839
}
838840

839-
static int write_loose_object(const struct object_id *oid, char *hdr,
841+
static int write_loose_object(struct odb_source *source,
842+
const struct object_id *oid, char *hdr,
840843
int hdrlen, const void *buf, unsigned long len,
841844
time_t mtime, unsigned flags)
842845
{
@@ -851,9 +854,9 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
851854
if (batch_fsync_enabled(FSYNC_COMPONENT_LOOSE_OBJECT))
852855
prepare_loose_object_bulk_checkin();
853856

854-
odb_loose_path(the_repository->objects->sources, &filename, oid);
857+
odb_loose_path(source, &filename, oid);
855858

856-
fd = start_loose_object_common(&tmp_file, filename.buf, flags,
859+
fd = start_loose_object_common(source, &tmp_file, filename.buf, flags,
857860
&stream, compressed, sizeof(compressed),
858861
&c, NULL, hdr, hdrlen);
859862
if (fd < 0)
@@ -865,22 +868,22 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
865868
do {
866869
unsigned char *in0 = stream.next_in;
867870

868-
ret = write_loose_object_common(&c, NULL, &stream, 1, in0, fd,
871+
ret = write_loose_object_common(source, &c, NULL, &stream, 1, in0, fd,
869872
compressed, sizeof(compressed));
870873
} while (ret == Z_OK);
871874

872875
if (ret != Z_STREAM_END)
873876
die(_("unable to deflate new object %s (%d)"), oid_to_hex(oid),
874877
ret);
875-
ret = end_loose_object_common(&c, NULL, &stream, &parano_oid, NULL);
878+
ret = end_loose_object_common(source, &c, NULL, &stream, &parano_oid, NULL);
876879
if (ret != Z_OK)
877880
die(_("deflateEnd on object %s failed (%d)"), oid_to_hex(oid),
878881
ret);
879882
if (!oideq(oid, &parano_oid))
880883
die(_("confused by unstable object source data for %s"),
881884
oid_to_hex(oid));
882885

883-
close_loose_object(fd, tmp_file.buf);
886+
close_loose_object(source, fd, tmp_file.buf);
884887

885888
if (mtime) {
886889
struct utimbuf utb;
@@ -891,7 +894,7 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
891894
warning_errno(_("failed utime() on %s"), tmp_file.buf);
892895
}
893896

894-
return finalize_object_file_flags(the_repository, tmp_file.buf, filename.buf,
897+
return finalize_object_file_flags(source->odb->repo, tmp_file.buf, filename.buf,
895898
FOF_SKIP_COLLISION_CHECK);
896899
}
897900

@@ -921,10 +924,11 @@ static int freshen_packed_object(struct object_database *odb,
921924
return 1;
922925
}
923926

924-
int stream_loose_object(struct input_stream *in_stream, size_t len,
927+
int stream_loose_object(struct odb_source *source,
928+
struct input_stream *in_stream, size_t len,
925929
struct object_id *oid)
926930
{
927-
const struct git_hash_algo *compat = the_repository->compat_hash_algo;
931+
const struct git_hash_algo *compat = source->odb->repo->compat_hash_algo;
928932
struct object_id compat_oid;
929933
int fd, ret, err = 0, flush = 0;
930934
unsigned char compressed[4096];
@@ -940,7 +944,7 @@ int stream_loose_object(struct input_stream *in_stream, size_t len,
940944
prepare_loose_object_bulk_checkin();
941945

942946
/* Since oid is not determined, save tmp file to odb path. */
943-
strbuf_addf(&filename, "%s/", repo_get_object_directory(the_repository));
947+
strbuf_addf(&filename, "%s/", source->path);
944948
hdrlen = format_object_header(hdr, sizeof(hdr), OBJ_BLOB, len);
945949

946950
/*
@@ -951,7 +955,7 @@ int stream_loose_object(struct input_stream *in_stream, size_t len,
951955
* - Setup zlib stream for compression.
952956
* - Start to feed header to zlib stream.
953957
*/
954-
fd = start_loose_object_common(&tmp_file, filename.buf, 0,
958+
fd = start_loose_object_common(source, &tmp_file, filename.buf, 0,
955959
&stream, compressed, sizeof(compressed),
956960
&c, &compat_c, hdr, hdrlen);
957961
if (fd < 0) {
@@ -971,7 +975,7 @@ int stream_loose_object(struct input_stream *in_stream, size_t len,
971975
if (in_stream->is_finished)
972976
flush = 1;
973977
}
974-
ret = write_loose_object_common(&c, &compat_c, &stream, flush, in0, fd,
978+
ret = write_loose_object_common(source, &c, &compat_c, &stream, flush, in0, fd,
975979
compressed, sizeof(compressed));
976980
/*
977981
* Unlike write_loose_object(), we do not have the entire
@@ -994,26 +998,26 @@ int stream_loose_object(struct input_stream *in_stream, size_t len,
994998
*/
995999
if (ret != Z_STREAM_END)
9961000
die(_("unable to stream deflate new object (%d)"), ret);
997-
ret = end_loose_object_common(&c, &compat_c, &stream, oid, &compat_oid);
1001+
ret = end_loose_object_common(source, &c, &compat_c, &stream, oid, &compat_oid);
9981002
if (ret != Z_OK)
9991003
die(_("deflateEnd on stream object failed (%d)"), ret);
1000-
close_loose_object(fd, tmp_file.buf);
1004+
close_loose_object(source, fd, tmp_file.buf);
10011005

1002-
if (freshen_packed_object(the_repository->objects, oid) ||
1003-
freshen_loose_object(the_repository->objects, oid)) {
1006+
if (freshen_packed_object(source->odb, oid) ||
1007+
freshen_loose_object(source->odb, oid)) {
10041008
unlink_or_warn(tmp_file.buf);
10051009
goto cleanup;
10061010
}
10071011

1008-
odb_loose_path(the_repository->objects->sources, &filename, oid);
1012+
odb_loose_path(source, &filename, oid);
10091013

10101014
/* We finally know the object path, and create the missing dir. */
10111015
dirlen = directory_size(filename.buf);
10121016
if (dirlen) {
10131017
struct strbuf dir = STRBUF_INIT;
10141018
strbuf_add(&dir, filename.buf, dirlen);
10151019

1016-
if (safe_create_dir_in_gitdir(the_repository, dir.buf) &&
1020+
if (safe_create_dir_in_gitdir(source->odb->repo, dir.buf) &&
10171021
errno != EEXIST) {
10181022
err = error_errno(_("unable to create directory %s"), dir.buf);
10191023
strbuf_release(&dir);
@@ -1022,23 +1026,23 @@ int stream_loose_object(struct input_stream *in_stream, size_t len,
10221026
strbuf_release(&dir);
10231027
}
10241028

1025-
err = finalize_object_file_flags(the_repository, tmp_file.buf, filename.buf,
1029+
err = finalize_object_file_flags(source->odb->repo, tmp_file.buf, filename.buf,
10261030
FOF_SKIP_COLLISION_CHECK);
10271031
if (!err && compat)
1028-
err = repo_add_loose_object_map(the_repository->objects->sources, oid, &compat_oid);
1032+
err = repo_add_loose_object_map(source, oid, &compat_oid);
10291033
cleanup:
10301034
strbuf_release(&tmp_file);
10311035
strbuf_release(&filename);
10321036
return err;
10331037
}
10341038

1035-
int write_object_file(const void *buf, unsigned long len,
1039+
int write_object_file(struct odb_source *source,
1040+
const void *buf, unsigned long len,
10361041
enum object_type type, struct object_id *oid,
10371042
struct object_id *compat_oid_in, unsigned flags)
10381043
{
1039-
struct repository *repo = the_repository;
1040-
const struct git_hash_algo *algo = repo->hash_algo;
1041-
const struct git_hash_algo *compat = repo->compat_hash_algo;
1044+
const struct git_hash_algo *algo = source->odb->repo->hash_algo;
1045+
const struct git_hash_algo *compat = source->odb->repo->compat_hash_algo;
10421046
struct object_id compat_oid;
10431047
char hdr[MAX_HEADER_LEN];
10441048
int hdrlen = sizeof(hdr);
@@ -1051,7 +1055,7 @@ int write_object_file(const void *buf, unsigned long len,
10511055
hash_object_file(compat, buf, len, type, &compat_oid);
10521056
else {
10531057
struct strbuf converted = STRBUF_INIT;
1054-
convert_object_file(the_repository, &converted, algo, compat,
1058+
convert_object_file(source->odb->repo, &converted, algo, compat,
10551059
buf, len, type, 0);
10561060
hash_object_file(compat, converted.buf, converted.len,
10571061
type, &compat_oid);
@@ -1063,13 +1067,13 @@ int write_object_file(const void *buf, unsigned long len,
10631067
* it out into .git/objects/??/?{38} file.
10641068
*/
10651069
write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen);
1066-
if (freshen_packed_object(repo->objects, oid) ||
1067-
freshen_loose_object(repo->objects, oid))
1070+
if (freshen_packed_object(source->odb, oid) ||
1071+
freshen_loose_object(source->odb, oid))
10681072
return 0;
1069-
if (write_loose_object(oid, hdr, hdrlen, buf, len, 0, flags))
1073+
if (write_loose_object(source, oid, hdr, hdrlen, buf, len, 0, flags))
10701074
return -1;
10711075
if (compat)
1072-
return repo_add_loose_object_map(repo->objects->sources, oid, &compat_oid);
1076+
return repo_add_loose_object_map(source, oid, &compat_oid);
10731077
return 0;
10741078
}
10751079

@@ -1101,7 +1105,7 @@ int force_object_loose(const struct object_id *oid, time_t mtime)
11011105
oid_to_hex(oid), compat->name);
11021106
}
11031107
hdrlen = format_object_header(hdr, sizeof(hdr), type, len);
1104-
ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime, 0);
1108+
ret = write_loose_object(repo->objects->sources, oid, hdr, hdrlen, buf, len, mtime, 0);
11051109
if (!ret && compat)
11061110
ret = repo_add_loose_object_map(the_repository->objects->sources, oid, &compat_oid);
11071111
free(buf);

object-file.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
157157
struct object_info;
158158
int parse_loose_header(const char *hdr, struct object_info *oi);
159159

160-
int write_object_file(const void *buf, unsigned long len,
160+
int write_object_file(struct odb_source *source,
161+
const void *buf, unsigned long len,
161162
enum object_type type, struct object_id *oid,
162163
struct object_id *compat_oid_in, unsigned flags);
163164

@@ -167,7 +168,8 @@ struct input_stream {
167168
int is_finished;
168169
};
169170

170-
int stream_loose_object(struct input_stream *in_stream, size_t len,
171+
int stream_loose_object(struct odb_source *source,
172+
struct input_stream *in_stream, size_t len,
171173
struct object_id *oid);
172174

173175
int force_object_loose(const struct object_id *oid, time_t mtime);

odb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,14 +980,14 @@ void odb_assert_oid_type(struct object_database *odb,
980980
type_name(expect));
981981
}
982982

983-
int odb_write_object_ext(struct object_database *odb UNUSED,
983+
int odb_write_object_ext(struct object_database *odb,
984984
const void *buf, unsigned long len,
985985
enum object_type type,
986986
struct object_id *oid,
987987
struct object_id *compat_oid,
988988
unsigned flags)
989989
{
990-
return write_object_file(buf, len, type, oid, compat_oid, flags);
990+
return write_object_file(odb->sources, buf, len, type, oid, compat_oid, flags);
991991
}
992992

993993
struct object_database *odb_new(struct repository *repo)

0 commit comments

Comments
 (0)