object-file: drop support for writing objects with unknown types

Since "hash-object --literally" no longer supports objects with unknown types, there are now no callers of write_object_file_literally() and its helpers. Let's drop them to simplify the code. In particular, this gets rid of some ugly copy-and-paste code from write_object_file_literally(), which is a parallel implementation of write_object_file(). When the split was originally made, the two weren't that long, but commits like 63a6745a07 (object-file: update the loose object map when writing loose objects, 2023-10-01) ended up having to duplicate some tricky code. This patch drops all of that duplication and should make things less error-prone going forward. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed May 16, 2025 at 00:50 UTC 141f8c8c0535004fa5432d9a6d57bf08129a7dd8
2 files changed +6 -80
object-file.c
+5 -76
@@ -130,12 +130,6 @@ int has_loose_object(const struct object_id *oid)
130 return check_and_freshen(oid, 0);
131 }
132
133 -static int format_object_header_literally(char *str, size_t size,
134 - const char *type, size_t objsize)
135 -{
136 - return xsnprintf(str, size, "%s %"PRIuMAX, type, (uintmax_t)objsize) + 1;
137 -}
138 -
133 int format_object_header(char *str, size_t size, enum object_type type,
134 size_t objsize)
135 {
@@ -144,7 +138,7 @@ int format_object_header(char *str, size_t size, enum object_type type,
138 if (!name)
139 BUG("could not get a type name for 'enum object_type' value %d", type);
140
147 - return format_object_header_literally(str, size, name, objsize);
141 + return xsnprintf(str, size, "%s %"PRIuMAX, name, (uintmax_t)objsize) + 1;
142 }
143
144 int check_object_signature(struct repository *r, const struct object_id *oid,
@@ -558,17 +552,6 @@ static void write_object_file_prepare(const struct git_hash_algo *algo,
552 hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen);
553 }
554
561 -static void write_object_file_prepare_literally(const struct git_hash_algo *algo,
562 - const void *buf, unsigned long len,
563 - const char *type, struct object_id *oid,
564 - char *hdr, int *hdrlen)
565 -{
566 - struct git_hash_ctx c;
567 -
568 - *hdrlen = format_object_header_literally(hdr, *hdrlen, type, len);
569 - hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen);
570 -}
571 -
555 #define CHECK_COLLISION_DEST_VANISHED -2
556
557 static int check_collision(const char *source, const char *dest)
@@ -698,21 +681,14 @@ out:
681 return 0;
682 }
683
701 -static void hash_object_file_literally(const struct git_hash_algo *algo,
702 - const void *buf, unsigned long len,
703 - const char *type, struct object_id *oid)
704 -{
705 - char hdr[MAX_HEADER_LEN];
706 - int hdrlen = sizeof(hdr);
707 -
708 - write_object_file_prepare_literally(algo, buf, len, type, oid, hdr, &hdrlen);
709 -}
710 -
684 void hash_object_file(const struct git_hash_algo *algo, const void *buf,
685 unsigned long len, enum object_type type,
686 struct object_id *oid)
687 {
715 - hash_object_file_literally(algo, buf, len, type_name(type), oid);
688 + char hdr[MAX_HEADER_LEN];
689 + int hdrlen = sizeof(hdr);
690 +
691 + write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen);
692 }
693
694 /* Finalize a file on disk, and close it. */
@@ -1114,53 +1090,6 @@ int write_object_file_flags(const void *buf, unsigned long len,
1090 return 0;
1091 }
1092
1117 -int write_object_file_literally(const void *buf, unsigned long len,
1118 - const char *type, struct object_id *oid,
1119 - unsigned flags)
1120 -{
1121 - char *header;
1122 - struct repository *repo = the_repository;
1123 - const struct git_hash_algo *algo = repo->hash_algo;
1124 - const struct git_hash_algo *compat = repo->compat_hash_algo;
1125 - struct object_id compat_oid;
1126 - int hdrlen, status = 0;
1127 - int compat_type = -1;
1128 -
1129 - if (compat) {
1130 - compat_type = type_from_string_gently(type, -1, 1);
1131 - if (compat_type == OBJ_BLOB)
1132 - hash_object_file(compat, buf, len, compat_type,
1133 - &compat_oid);
1134 - else if (compat_type != -1) {
1135 - struct strbuf converted = STRBUF_INIT;
1136 - convert_object_file(the_repository,
1137 - &converted, algo, compat,
1138 - buf, len, compat_type, 0);
1139 - hash_object_file(compat, converted.buf, converted.len,
1140 - compat_type, &compat_oid);
1141 - strbuf_release(&converted);
1142 - }
1143 - }
1144 -
1145 - /* type string, SP, %lu of the length plus NUL must fit this */
1146 - hdrlen = strlen(type) + MAX_HEADER_LEN;
1147 - header = xmalloc(hdrlen);
1148 - write_object_file_prepare_literally(the_hash_algo, buf, len, type,
1149 - oid, header, &hdrlen);
1150 -
1151 - if (!(flags & WRITE_OBJECT_FILE_PERSIST))
1152 - goto cleanup;
1153 - if (freshen_packed_object(oid) || freshen_loose_object(oid))
1154 - goto cleanup;
1155 - status = write_loose_object(oid, header, hdrlen, buf, len, 0, 0);
1156 - if (compat_type != -1)
1157 - return repo_add_loose_object_map(repo, oid, &compat_oid);
1158 -
1159 -cleanup:
1160 - free(header);
1161 - return status;
1162 -}
1163 -
1093 int force_object_loose(const struct object_id *oid, time_t mtime)
1094 {
1095 struct repository *repo = the_repository;
object-file.h
+1 -4
@@ -159,7 +159,7 @@ int parse_loose_header(const char *hdr, struct object_info *oi);
159
160 enum {
161 /*
162 - * By default, `write_object_file_literally()` does not actually write
162 + * By default, `write_object_file()` does not actually write
163 * anything into the object store, but only computes the object ID.
164 * This flag changes that so that the object will be written as a loose
165 * object and persisted.
@@ -187,9 +187,6 @@ struct input_stream {
187 int is_finished;
188 };
189
190 -int write_object_file_literally(const void *buf, unsigned long len,
191 - const char *type, struct object_id *oid,
192 - unsigned flags);
190 int stream_loose_object(struct input_stream *in_stream, size_t len,
191 struct object_id *oid);
192