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>

Junio C Hamano committed Aug 15, 2014 at 14:28 UTC 39895c74d809962bf76a6d720618df30f4bac8b1
1 file changed +29 -21
builtin/receive-pack.c
+29 -21
@@ -831,16 +831,40 @@ static void execute_commands(struct command *commands,
831 "the reported refs above");
832 }
833
834 +static struct command **queue_command(struct command **tail,
835 + const char *line,
836 + int linelen)
837 +{
838 + unsigned char old_sha1[20], new_sha1[20];
839 + struct command *cmd;
840 + const char *refname;
841 + int reflen;
842 +
843 + if (linelen < 83 ||
844 + line[40] != ' ' ||
845 + line[81] != ' ' ||
846 + get_sha1_hex(line, old_sha1) ||
847 + get_sha1_hex(line + 41, new_sha1))
848 + die("protocol error: expected old/new/ref, got '%s'", line);
849 +
850 + refname = line + 82;
851 + reflen = linelen - 82;
852 + cmd = xcalloc(1, sizeof(struct command) + reflen + 1);
853 + hashcpy(cmd->old_sha1, old_sha1);
854 + hashcpy(cmd->new_sha1, new_sha1);
855 + memcpy(cmd->ref_name, refname, reflen);
856 + cmd->ref_name[reflen] = '\0';
857 + *tail = cmd;
858 + return &cmd->next;
859 +}
860 +
861 static struct command *read_head_info(struct sha1_array *shallow)
862 {
863 struct command *commands = NULL;
864 struct command **p = &commands;
865 for (;;) {
866 char *line;
840 - unsigned char old_sha1[20], new_sha1[20];
841 - struct command *cmd;
842 - char *refname;
843 - int len, reflen, linelen;
867 + int len, linelen;
868
869 line = packet_read_line(0, &len);
870 if (!line)
@@ -866,23 +890,7 @@ static struct command *read_head_info(struct sha1_array *shallow)
890 quiet = 1;
891 }
892
869 - if (linelen < 83 ||
870 - line[40] != ' ' ||
871 - line[81] != ' ' ||
872 - get_sha1_hex(line, old_sha1) ||
873 - get_sha1_hex(line + 41, new_sha1))
874 - die("protocol error: expected old/new/ref, got '%s'",
875 - line);
876 -
877 - refname = line + 82;
878 - reflen = linelen - 82;
879 - cmd = xcalloc(1, sizeof(struct command) + reflen + 1);
880 - hashcpy(cmd->old_sha1, old_sha1);
881 - hashcpy(cmd->new_sha1, new_sha1);
882 - memcpy(cmd->ref_name, refname, reflen);
883 - cmd->ref_name[reflen] = '\0';
884 - *p = cmd;
885 - p = &cmd->next;
893 + p = queue_command(p, line, linelen);
894 }
895 return commands;
896 }