Merge branch 'dl/complete-cherry-pick-revert-skip'
[gitweb.git] / pkt-line.c
index 321ff632a59cc5de01194773385d7c66c685aeac..a0e87b1e81408e17b4bb8e99bc162b6e7bce4012 100644 (file)
@@ -88,13 +88,15 @@ static void packet_trace(const char *buf, unsigned int len, int write)
 void packet_flush(int fd)
 {
        packet_trace("0000", 4, 1);
-       write_or_die(fd, "0000", 4);
+       if (write_in_full(fd, "0000", 4) < 0)
+               die_errno(_("unable to write flush packet"));
 }
 
 void packet_delim(int fd)
 {
        packet_trace("0001", 4, 1);
-       write_or_die(fd, "0001", 4);
+       if (write_in_full(fd, "0001", 4) < 0)
+               die_errno(_("unable to write delim packet"));
 }
 
 int packet_flush_gently(int fd)
@@ -117,7 +119,7 @@ void packet_buf_delim(struct strbuf *buf)
        strbuf_add(buf, "0001", 4);
 }
 
-static void set_packet_header(char *buf, const int size)
+void set_packet_header(char *buf, int size)
 {
        static char hexchar[] = "0123456789abcdef";
 
@@ -348,16 +350,17 @@ enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
                return PACKET_READ_EOF;
        }
 
-       if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
-           starts_with(buffer, "ERR "))
-               die(_("remote error: %s"), buffer + 4);
-
        if ((options & PACKET_READ_CHOMP_NEWLINE) &&
            len && buffer[len-1] == '\n')
                len--;
 
        buffer[len] = 0;
        packet_trace(buffer, len, 0);
+
+       if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
+           starts_with(buffer, "ERR "))
+               die(_("remote error: %s"), buffer + 4);
+
        *pktlen = len;
        return PACKET_READ_NORMAL;
 }
@@ -449,7 +452,7 @@ int recv_sideband(const char *me, int in_stream, int out)
        while (1) {
                len = packet_read(in_stream, NULL, NULL, buf, LARGE_PACKET_MAX,
                                  0);
-               if (!demultiplex_sideband(me, buf, len, &scratch,
+               if (!demultiplex_sideband(me, buf, len, 0, &scratch,
                                          &sideband_type))
                        continue;
                switch (sideband_type) {
@@ -475,25 +478,43 @@ void packet_reader_init(struct packet_reader *reader, int fd,
        reader->buffer = packet_buffer;
        reader->buffer_size = sizeof(packet_buffer);
        reader->options = options;
+       reader->me = "git";
 }
 
 enum packet_read_status packet_reader_read(struct packet_reader *reader)
 {
+       struct strbuf scratch = STRBUF_INIT;
+
        if (reader->line_peeked) {
                reader->line_peeked = 0;
                return reader->status;
        }
 
-       reader->status = packet_read_with_status(reader->fd,
-                                                &reader->src_buffer,
-                                                &reader->src_len,
-                                                reader->buffer,
-                                                reader->buffer_size,
-                                                &reader->pktlen,
-                                                reader->options);
+       /*
+        * Consume all progress packets until a primary payload packet is
+        * received
+        */
+       while (1) {
+               enum sideband_type sideband_type;
+               reader->status = packet_read_with_status(reader->fd,
+                                                        &reader->src_buffer,
+                                                        &reader->src_len,
+                                                        reader->buffer,
+                                                        reader->buffer_size,
+                                                        &reader->pktlen,
+                                                        reader->options);
+               if (!reader->use_sideband)
+                       break;
+               if (demultiplex_sideband(reader->me, reader->buffer,
+                                        reader->pktlen, 1, &scratch,
+                                        &sideband_type))
+                       break;
+       }
 
        if (reader->status == PACKET_READ_NORMAL)
-               reader->line = reader->buffer;
+               /* Skip the sideband designator if sideband is used */
+               reader->line = reader->use_sideband ?
+                       reader->buffer + 1 : reader->buffer;
        else
                reader->line = NULL;
 
@@ -515,6 +536,7 @@ enum packet_read_status packet_reader_peek(struct packet_reader *reader)
 void packet_writer_init(struct packet_writer *writer, int dest_fd)
 {
        writer->dest_fd = dest_fd;
+       writer->use_sideband = 0;
 }
 
 void packet_writer_write(struct packet_writer *writer, const char *fmt, ...)
@@ -522,7 +544,8 @@ void packet_writer_write(struct packet_writer *writer, const char *fmt, ...)
        va_list args;
 
        va_start(args, fmt);
-       packet_write_fmt_1(writer->dest_fd, 0, "", fmt, args);
+       packet_write_fmt_1(writer->dest_fd, 0,
+                          writer->use_sideband ? "\001" : "", fmt, args);
        va_end(args);
 }
 
@@ -531,7 +554,8 @@ void packet_writer_error(struct packet_writer *writer, const char *fmt, ...)
        va_list args;
 
        va_start(args, fmt);
-       packet_write_fmt_1(writer->dest_fd, 0, "ERR ", fmt, args);
+       packet_write_fmt_1(writer->dest_fd, 0,
+                          writer->use_sideband ? "\003" : "ERR ", fmt, args);
        va_end(args);
 }