object-file API: add a format_object_header() function
Add a convenience function to wrap the xsnprintf() command that generates loose object headers. This code was copy/pasted in various parts of the codebase, let's define it in one place and re-use it from there. All except one caller of it had a valid "enum object_type" for us, it's only write_object_file_prepare() which might need to deal with "git hash-object --literally" and a potential garbage type. Let's have the primary API use an "enum object_type", and define a *_literally() function that can take an arbitrary "const char *" for the type. See [1] for the discussion that prompted this patch, i.e. new code in object-file.c that wanted to copy/paste the xsnprintf() invocation. In the case of fast-import.c the callers unfortunately need to cast back & forth between "unsigned char *" and "char *", since format_object_header() ad encode_in_pack_object_header() take different signedness. 1. https://lore.kernel.org/git/211213.86bl1l9bfz.gmgdl@evledraar.gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
63e05f9056
commit
b04cdea46c
@ -220,8 +220,8 @@ static int deflate_to_pack(struct bulk_checkin_state *state,
|
||||
if (seekback == (off_t) -1)
|
||||
return error("cannot find the current offset");
|
||||
|
||||
header_len = xsnprintf((char *)obuf, sizeof(obuf), "%s %" PRIuMAX,
|
||||
type_name(type), (uintmax_t)size) + 1;
|
||||
header_len = format_object_header((char *)obuf, sizeof(obuf),
|
||||
type, size);
|
||||
the_hash_algo->init_fn(&ctx);
|
||||
the_hash_algo->update_fn(&ctx, obuf, header_len);
|
||||
|
||||
|
Reference in New Issue
Block a user