receive-pack: move message generation to separate function

Post the reference transaction, both `report()` and `report_v2()`
generate the message to be sent to the client. In v2, we also add
reports for each reference if available. Since they share common code,
move them to a common function. This will also help the following
commit, where we will need to regenerate the message during hook
failure.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
seen
Karthik Nayak 2026-08-26 12:19:38 +02:00 committed by Junio C Hamano
parent 4202813b30
commit 063bb0a987
1 changed files with 49 additions and 45 deletions

View File

@ -2530,22 +2530,58 @@ static void update_shallow_info(struct command *commands,
free(ref_status);
}

static void report(struct command *commands, const char *unpack_status)
/*
* Generate the response to be sent to the client invoking 'git-receive-pack(1)'.
* For v2 protocol, set `add_reports` to true, which will also add additional
* report per reference update.
*/
static void generate_response(struct strbuf *buf, struct command *commands,
const char *unpack_status, bool add_reports)
{
struct command *cmd;

packet_buf_write(buf, "unpack %s\n",
unpack_status ? unpack_status : "ok");

for (cmd = commands; cmd; cmd = cmd->next) {
struct ref_push_report *report;
int count = 0;

if (cmd->error_string)
packet_buf_write(buf, "ng %s %s\n",
cmd->ref_name, cmd->error_string);
else
packet_buf_write(buf, "ok %s\n", cmd->ref_name);

if (!add_reports || cmd->error_string)
continue;

for (report = cmd->report; report; report = report->next) {
if (count++ > 0)
packet_buf_write(buf, "ok %s\n",
cmd->ref_name);
if (report->ref_name)
packet_buf_write(buf, "option refname %s\n",
report->ref_name);
if (report->old_oid)
packet_buf_write(buf, "option old-oid %s\n",
oid_to_hex(report->old_oid));
if (report->new_oid)
packet_buf_write(buf, "option new-oid %s\n",
oid_to_hex(report->new_oid));
if (report->forced_update)
packet_buf_write(buf, "option forced-update\n");
}
}

packet_buf_flush(buf);
}

static void report(struct command *commands, const char *unpack_status)
{
struct strbuf buf = STRBUF_INIT;

packet_buf_write(&buf, "unpack %s\n",
unpack_status ? unpack_status : "ok");
for (cmd = commands; cmd; cmd = cmd->next) {
if (!cmd->error_string)
packet_buf_write(&buf, "ok %s\n",
cmd->ref_name);
else
packet_buf_write(&buf, "ng %s %s\n",
cmd->ref_name, cmd->error_string);
}
packet_buf_flush(&buf);
generate_response(&buf, commands, unpack_status, false);

if (use_sideband)
send_sideband(1, 1, buf.buf, buf.len, use_sideband);
@ -2556,41 +2592,9 @@ static void report(struct command *commands, const char *unpack_status)

static void report_v2(struct command *commands, const char *unpack_status)
{
struct command *cmd;
struct strbuf buf = STRBUF_INIT;
struct ref_push_report *report;

packet_buf_write(&buf, "unpack %s\n",
unpack_status ? unpack_status : "ok");
for (cmd = commands; cmd; cmd = cmd->next) {
int count = 0;

if (cmd->error_string) {
packet_buf_write(&buf, "ng %s %s\n",
cmd->ref_name,
cmd->error_string);
continue;
}
packet_buf_write(&buf, "ok %s\n",
cmd->ref_name);
for (report = cmd->report; report; report = report->next) {
if (count++ > 0)
packet_buf_write(&buf, "ok %s\n",
cmd->ref_name);
if (report->ref_name)
packet_buf_write(&buf, "option refname %s\n",
report->ref_name);
if (report->old_oid)
packet_buf_write(&buf, "option old-oid %s\n",
oid_to_hex(report->old_oid));
if (report->new_oid)
packet_buf_write(&buf, "option new-oid %s\n",
oid_to_hex(report->new_oid));
if (report->forced_update)
packet_buf_write(&buf, "option forced-update\n");
}
}
packet_buf_flush(&buf);
generate_response(&buf, commands, unpack_status, true);

if (use_sideband)
send_sideband(1, 1, buf.buf, buf.len, use_sideband);