object-file.c: factor out deflate part of write_loose_object()

Split out the part of write_loose_object() that deals with calling git_deflate() into a utility function, a subsequent commit will introduce another function that'll make use of it. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jun 11, 2022 at 10:44 UTC 21e7d8814063d8c5ada74464e30bf0c5188f1f80
1 file changed +25 -6
object-file.c
+25 -6
@@ -2000,6 +2000,28 @@ static int start_loose_object_common(struct strbuf *tmp_file,
2000 return fd;
2001 }
2002
2003 +/**
2004 + * Common steps for the inner git_deflate() loop for writing loose
2005 + * objects. Returns what git_deflate() returns.
2006 + */
2007 +static int write_loose_object_common(git_hash_ctx *c,
2008 + git_zstream *stream, const int flush,
2009 + unsigned char *in0, const int fd,
2010 + unsigned char *compressed,
2011 + const size_t compressed_len)
2012 +{
2013 + int ret;
2014 +
2015 + ret = git_deflate(stream, flush ? Z_FINISH : 0);
2016 + the_hash_algo->update_fn(c, in0, stream->next_in - in0);
2017 + if (write_buffer(fd, compressed, stream->next_out - compressed) < 0)
2018 + die(_("unable to write loose object file"));
2019 + stream->next_out = compressed;
2020 + stream->avail_out = compressed_len;
2021 +
2022 + return ret;
2023 +}
2024 +
2025 /**
2026 * Common steps for loose object writers to end writing loose objects:
2027 *
@@ -2047,12 +2069,9 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
2069 stream.avail_in = len;
2070 do {
2071 unsigned char *in0 = stream.next_in;
2050 - ret = git_deflate(&stream, Z_FINISH);
2051 - the_hash_algo->update_fn(&c, in0, stream.next_in - in0);
2052 - if (write_buffer(fd, compressed, stream.next_out - compressed) < 0)
2053 - die(_("unable to write loose object file"));
2054 - stream.next_out = compressed;
2055 - stream.avail_out = sizeof(compressed);
2072 +
2073 + ret = write_loose_object_common(&c, &stream, 1, in0, fd,
2074 + compressed, sizeof(compressed));
2075 } while (ret == Z_OK);
2076
2077 if (ret != Z_STREAM_END)