Skip to content

Commit 141f8c8

Browse files
peffgitster
authored andcommitted
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 63a6745 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f710fd7 commit 141f8c8

File tree

2 files changed

+6
-80
lines changed

2 files changed

+6
-80
lines changed

object-file.c

Lines changed: 5 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,6 @@ int has_loose_object(const struct object_id *oid)
130130
return check_and_freshen(oid, 0);
131131
}
132132

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-
139133
int format_object_header(char *str, size_t size, enum object_type type,
140134
size_t objsize)
141135
{
@@ -144,7 +138,7 @@ int format_object_header(char *str, size_t size, enum object_type type,
144138
if (!name)
145139
BUG("could not get a type name for 'enum object_type' value %d", type);
146140

147-
return format_object_header_literally(str, size, name, objsize);
141+
return xsnprintf(str, size, "%s %"PRIuMAX, name, (uintmax_t)objsize) + 1;
148142
}
149143

150144
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,
558552
hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen);
559553
}
560554

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-
572555
#define CHECK_COLLISION_DEST_VANISHED -2
573556

574557
static int check_collision(const char *source, const char *dest)
@@ -698,21 +681,14 @@ int finalize_object_file_flags(const char *tmpfile, const char *filename,
698681
return 0;
699682
}
700683

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-
711684
void hash_object_file(const struct git_hash_algo *algo, const void *buf,
712685
unsigned long len, enum object_type type,
713686
struct object_id *oid)
714687
{
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);
716692
}
717693

718694
/* Finalize a file on disk, and close it. */
@@ -1114,53 +1090,6 @@ int write_object_file_flags(const void *buf, unsigned long len,
11141090
return 0;
11151091
}
11161092

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-
11641093
int force_object_loose(const struct object_id *oid, time_t mtime)
11651094
{
11661095
struct repository *repo = the_repository;

object-file.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ int parse_loose_header(const char *hdr, struct object_info *oi);
159159

160160
enum {
161161
/*
162-
* By default, `write_object_file_literally()` does not actually write
162+
* By default, `write_object_file()` does not actually write
163163
* anything into the object store, but only computes the object ID.
164164
* This flag changes that so that the object will be written as a loose
165165
* object and persisted.
@@ -187,9 +187,6 @@ struct input_stream {
187187
int is_finished;
188188
};
189189

190-
int write_object_file_literally(const void *buf, unsigned long len,
191-
const char *type, struct object_id *oid,
192-
unsigned flags);
193190
int stream_loose_object(struct input_stream *in_stream, size_t len,
194191
struct object_id *oid);
195192

0 commit comments

Comments
 (0)