receive-pack: factor out queueing of command

Make a helper function to accept a line of a protocol message and
queue an update command out of the code from read_head_info().

Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Junio C Hamano 2014-08-15 14:28:28 -07:00
Родитель c09b71ccc4
Коммит 39895c74d8
1 изменённых файлов: 29 добавлений и 21 удалений

Просмотреть файл

@ -831,16 +831,40 @@ static void execute_commands(struct command *commands,
"the reported refs above"); "the reported refs above");
} }
static struct command **queue_command(struct command **tail,
const char *line,
int linelen)
{
unsigned char old_sha1[20], new_sha1[20];
struct command *cmd;
const char *refname;
int reflen;
if (linelen < 83 ||
line[40] != ' ' ||
line[81] != ' ' ||
get_sha1_hex(line, old_sha1) ||
get_sha1_hex(line + 41, new_sha1))
die("protocol error: expected old/new/ref, got '%s'", line);
refname = line + 82;
reflen = linelen - 82;
cmd = xcalloc(1, sizeof(struct command) + reflen + 1);
hashcpy(cmd->old_sha1, old_sha1);
hashcpy(cmd->new_sha1, new_sha1);
memcpy(cmd->ref_name, refname, reflen);
cmd->ref_name[reflen] = '\0';
*tail = cmd;
return &cmd->next;
}
static struct command *read_head_info(struct sha1_array *shallow) static struct command *read_head_info(struct sha1_array *shallow)
{ {
struct command *commands = NULL; struct command *commands = NULL;
struct command **p = &commands; struct command **p = &commands;
for (;;) { for (;;) {
char *line; char *line;
unsigned char old_sha1[20], new_sha1[20]; int len, linelen;
struct command *cmd;
char *refname;
int len, reflen, linelen;
line = packet_read_line(0, &len); line = packet_read_line(0, &len);
if (!line) if (!line)
@ -866,23 +890,7 @@ static struct command *read_head_info(struct sha1_array *shallow)
quiet = 1; quiet = 1;
} }
if (linelen < 83 || p = queue_command(p, line, linelen);
line[40] != ' ' ||
line[81] != ' ' ||
get_sha1_hex(line, old_sha1) ||
get_sha1_hex(line + 41, new_sha1))
die("protocol error: expected old/new/ref, got '%s'",
line);
refname = line + 82;
reflen = linelen - 82;
cmd = xcalloc(1, sizeof(struct command) + reflen + 1);
hashcpy(cmd->old_sha1, old_sha1);
hashcpy(cmd->new_sha1, new_sha1);
memcpy(cmd->ref_name, refname, reflen);
cmd->ref_name[reflen] = '\0';
*p = cmd;
p = &cmd->next;
} }
return commands; return commands;
} }