object-file: move `force_object_loose()`

In the preceding commits we have refactored `force_object_loose()` to not call internal functions anymore for writing the object. Instead, it now only uses generic functions that are accessible to all callers. Consequently, we can now easily move the function to its only caller, which is git-pack-objects(1). Do so. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jul 17, 2026 at 11:32 UTC a6c1e16b480a797a61a47616772e7254f7759053
3 files changed +46 -48
builtin/pack-objects.c
+46
@@ -32,6 +32,7 @@
32 #include "list.h"
33 #include "packfile.h"
34 #include "object-file.h"
35 +#include "object-file-convert.h"
36 #include "odb.h"
37 #include "odb/streaming.h"
38 #include "replace-object.h"
@@ -4622,6 +4623,51 @@ static int loosened_object_can_be_discarded(const struct object_id *oid,
4623 return 1;
4624 }
4625
4626 +static int force_object_loose(struct odb_source *source,
4627 + const struct object_id *oid,
4628 + const time_t *mtime)
4629 +{
4630 + struct odb_source_files *files = odb_source_files_downcast(source);
4631 + const struct git_hash_algo *compat = source->odb->repo->compat_hash_algo;
4632 + struct object_info oi = OBJECT_INFO_INIT;
4633 + struct object_id compat_oid, *compat_oid_p = NULL;
4634 + enum object_type type;
4635 + void *buf = NULL;
4636 + size_t len;
4637 + int ret;
4638 +
4639 + for (struct odb_source *s = source->odb->sources; s; s = s->next) {
4640 + struct odb_source_files *files = odb_source_files_downcast(s);
4641 + if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0))
4642 + return 0;
4643 + }
4644 +
4645 + oi.typep = &type;
4646 + oi.sizep = &len;
4647 + oi.contentp = &buf;
4648 + if (odb_read_object_info_extended(source->odb, oid, &oi, 0)) {
4649 + ret = error(_("cannot read object for %s"), oid_to_hex(oid));
4650 + goto out;
4651 + }
4652 +
4653 + if (compat) {
4654 + if (repo_oid_to_algop(source->odb->repo, oid, compat, &compat_oid)) {
4655 + ret = error(_("cannot map object %s to %s"),
4656 + oid_to_hex(oid), compat->name);
4657 + goto out;
4658 + }
4659 +
4660 + compat_oid_p = &compat_oid;
4661 + }
4662 +
4663 + ret = odb_source_write_object(&files->loose->base, buf, len, type, oid,
4664 + compat_oid_p, mtime, 0);
4665 +
4666 +out:
4667 + free(buf);
4668 + return ret;
4669 +}
4670 +
4671 static void loosen_unused_packed_objects(void)
4672 {
4673 struct packed_git *p;
object-file.c
-44
@@ -893,50 +893,6 @@ cleanup:
893 return err;
894 }
895
896 -int force_object_loose(struct odb_source *source,
897 - const struct object_id *oid, const time_t *mtime)
898 -{
899 - struct odb_source_files *files = odb_source_files_downcast(source);
900 - const struct git_hash_algo *compat = source->odb->repo->compat_hash_algo;
901 - struct object_info oi = OBJECT_INFO_INIT;
902 - struct object_id compat_oid, *compat_oid_p = NULL;
903 - enum object_type type;
904 - void *buf = NULL;
905 - size_t len;
906 - int ret;
907 -
908 - for (struct odb_source *s = source->odb->sources; s; s = s->next) {
909 - struct odb_source_files *files = odb_source_files_downcast(s);
910 - if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0))
911 - return 0;
912 - }
913 -
914 - oi.typep = &type;
915 - oi.sizep = &len;
916 - oi.contentp = &buf;
917 - if (odb_read_object_info_extended(source->odb, oid, &oi, 0)) {
918 - ret = error(_("cannot read object for %s"), oid_to_hex(oid));
919 - goto out;
920 - }
921 -
922 - if (compat) {
923 - if (repo_oid_to_algop(source->odb->repo, oid, compat, &compat_oid)) {
924 - ret = error(_("cannot map object %s to %s"),
925 - oid_to_hex(oid), compat->name);
926 - goto out;
927 - }
928 -
929 - compat_oid_p = &compat_oid;
930 - }
931 -
932 - ret = odb_source_write_object(&files->loose->base, buf, len, type, oid,
933 - compat_oid_p, mtime, 0);
934 -
935 -out:
936 - free(buf);
937 - return ret;
938 -}
939 -
896 /*
897 * We can't use the normal fsck_error_function() for index_mem(),
898 * because we don't yet have a valid oid for it to report. Instead,
object-file.h
-4
@@ -98,10 +98,6 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
98 int format_object_header(char *str, size_t size, enum object_type type,
99 size_t objsize);
100
101 -int force_object_loose(struct odb_source *source,
102 - const struct object_id *oid,
103 - const time_t *mtime);
104 -
101 /**
102 * With in-core object data in "buf", rehash it to make sure the
103 * object name actually matches "oid" to detect object corruption.