Skip to content

Commit 7033d54

Browse files
jiangxingitster
authored andcommitted
pkt-line: do not chomp newlines for sideband messages
When calling "packet_read_with_status()" to parse pkt-line encoded packets, we can turn on the flag "PACKET_READ_CHOMP_NEWLINE" to chomp newline character for each packet for better line matching. But when receiving data and progress information using sideband, we should turn off the flag "PACKET_READ_CHOMP_NEWLINE" to prevent mangling newline characters from data and progress information. When both the server and the client support "sideband-all" capability, we have a dilemma that newline characters in negotiation packets should be removed, but the newline characters in the progress information should be left intact. Add new flag "PACKET_READ_USE_SIDEBAND" for "packet_read_with_status()" to prevent mangling newline characters in sideband messages. Helped-by: Jonathan Tan <[email protected]> Helped-by: Oswald Buddenhagen <[email protected]> Signed-off-by: Jiang Xin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 64220dc commit 7033d54

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

pkt-line.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,32 @@ enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
462462
}
463463

464464
if ((options & PACKET_READ_CHOMP_NEWLINE) &&
465-
len && buffer[len-1] == '\n')
466-
len--;
465+
len && buffer[len-1] == '\n') {
466+
if (options & PACKET_READ_USE_SIDEBAND) {
467+
int band = *buffer & 0xff;
468+
switch (band) {
469+
case 1:
470+
/* Chomp newline for payload */
471+
len--;
472+
break;
473+
case 2:
474+
case 3:
475+
/*
476+
* Do not chomp newline for progress and error
477+
* message.
478+
*/
479+
break;
480+
default:
481+
/*
482+
* Bad sideband, let's leave it to
483+
* demultiplex_sideband() to catch this error.
484+
*/
485+
break;
486+
}
487+
} else {
488+
len--;
489+
}
490+
}
467491

468492
buffer[len] = 0;
469493
if (options & PACKET_READ_REDACT_URI_PATH &&
@@ -602,6 +626,9 @@ enum packet_read_status packet_reader_read(struct packet_reader *reader)
602626
return reader->status;
603627
}
604628

629+
if (reader->use_sideband)
630+
reader->options |= PACKET_READ_USE_SIDEBAND;
631+
605632
/*
606633
* Consume all progress packets until a primary payload packet is
607634
* received

pkt-line.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ void packet_fflush(FILE *f);
8585
#define PACKET_READ_DIE_ON_ERR_PACKET (1u<<2)
8686
#define PACKET_READ_GENTLE_ON_READ_ERROR (1u<<3)
8787
#define PACKET_READ_REDACT_URI_PATH (1u<<4)
88+
#define PACKET_READ_USE_SIDEBAND (1u<<5)
8889
int packet_read(int fd, char *buffer, unsigned size, int options);
8990

9091
/*

t/t0070-fundamental.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ test_expect_success 'unpack-sideband: packet_reader_read() consumes sideband, no
9797
test_cmp expect-err err
9898
'
9999

100-
test_expect_failure 'unpack-sideband: packet_reader_read() consumes sideband, chomp payload' '
100+
test_expect_success 'unpack-sideband: packet_reader_read() consumes sideband, chomp payload' '
101101
test_when_finished "rm -f expect-out expect-err" &&
102102
test-tool pkt-line send-split-sideband >split-sideband &&
103103
test-tool pkt-line unpack-sideband \

0 commit comments

Comments
 (0)