Git filter protocol
A message that is not in the archive (sent off-list or not yet received): 20160727000605.49982-1-larsxschneider%40gmail.com/
v3 cover Git filter protocol
From: Lars Schneider <[email protected]>
Hi,
thanks a lot to Jakub, Peff, Torsten, Eric, and Junio for comments and reviews.
Here is what has changed since v2:
* replace /dev/urandom with test-genrandom (Torsten, Peff)
* improve commit message "Git filter driver command with spaces" (Jakub)
* use proper types for memory and disk (Peff)
* create packet read buffer with overflow check (Peff)
* change capabilities format: "capabilities clean smudge" (Jakub)
* replace "%zu" (Eric)
* remove &= error handling (Eric, Peff)
* initialize *argv[] with { cmd, NULL } (Jakub)
* reorder multi_packet_read() parameters to match read(2) (Eric)
* do not continue if fstat fails (Eric)
* filter: add reject response
* add functions to pkt-line.h/c that: (Jakub, Peff)
- can write a packet without creating a new buffer
- do not die in case of a failure
* add function to pkt-line.h/c that writes a pkt-line flush and does not die on error
* add filter stream capability
* add filter shutdown capability
* docs: fix LARGE_PACKET_MAX documentaion
see http://public-inbox.org/git/20160726134257.GB19277%40sigill.intra.peff.net/
* docs: fix s/seperated/separated/ (Jakub)
* docs: "mis-configured one-shot filters would hang" (Jakub)
* docs: filter protocol filename absolute (Jakub)
* docs: state that Git can use more than one packet (Jabub)
* docs: add "\n" to lines (Jakub)
* docs: filter precedence (Jakub)
Cheers,
Lars
PS: If you prefer checkout the code from a Git repo instead then you can find it here: https://github.com/larsxschneider/git/tree/protocol-filter/v3
Lars Schneider (10):
pkt-line: extract set_packet_header()
pkt-line: add direct_packet_write() and direct_packet_write_data()
pkt-line: add packet_flush_gentle()
pkt-line: call packet_trace() only if a packet is actually send
pack-protocol: fix maximum pkt-line size
run-command: add clean_on_exit_handler
convert: quote filter names in error messages
convert: modernize tests
convert: generate large test files only once
convert: add filter.<driver>.process option
Documentation/gitattributes.txt | 84 ++++- Documentation/technical/protocol-common.txt | 6 +- convert.c | 412 +++++++++++++++++++++- pkt-line.c | 53 ++- pkt-line.h | 6 + run-command.c | 12 +- run-command.h | 1 + t/t0021-conversion.sh | 515 +++++++++++++++++++++++++--- t/t0021/rot13-filter.pl | 177 ++++++++++ 9 files changed, 1193 insertions(+), 73 deletions(-) create mode 100755 t/t0021/rot13-filter.pl
--
2.9.0
PATCH v3 1/10 pkt-line: extract set_packet_header()
From: Lars Schneider <[email protected]>
set_packet_header() converts an integer to a 4 byte hex string. Make this function locally available so that other pkt-line functions can use it.
--- pkt-line.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
pkt-line.c +10 -5
@@ -98,9 +98,17 @@ void packet_buf_flush(struct strbuf *buf) }#define hex (a) (hexchar[(a) & 15]) -static void format_packet (struct strbuf *out,const char *fmt,va_list args) +static void set_packet_header (char *buf,const int size) {static char hexchar[] ="0123456789abcdef" ; + buf[0 ] =hex (size >>12 ); + buf[1 ] =hex (size >>8 ); + buf[2 ] =hex (size >>4 ); + buf[3 ] =hex (size); +} + +static void format_packet (struct strbuf *out,const char *fmt,va_list args) +{size_t orig_len, n; orig_len = out->len;
@@ -111,10 +119,7 @@ static void format_packet(struct strbuf *out, const char *fmt, va_list args)if (n >LARGE_PACKET_MAX )die ("protocol error: impossibly long line" ); - out->buf[orig_len +0 ] =hex (n >>12 ); - out->buf[orig_len +1 ] =hex (n >>8 ); - out->buf[orig_len +2 ] =hex (n >>4 ); - out->buf[orig_len +3 ] =hex (n); +set_packet_header (&out->buf[orig_len], n);packet_trace (out->buf + orig_len +4 , n -4 ,1 ); }
Signature · 1 line
2.9.0
Re: [PATCH v3 01/10] pkt-line: extract set_packet_header()
W dniu 30.07.2016 o 01:37, [email protected] pisze:
Quoted 5 lines
From: Lars Schneider <[email protected]>
set_packet_header()converts an integer to a 4 byte hex string. Make this function locally available so that other pkt-line functions can use it.
This description is not that clear that set_packet_header() is a new function. Perhaps something like the following
Extract the part of format_packet() that converts an integer to a 4 byte hex string into set_packet_header(). Make this new function ...
I also wonder if the part "Make this [new] function locally available..." is needed; we need to justify exports, but I think we don't need to justify limiting it to a module. If you want to justify that it is "static", perhaps it would be better to say why not to export it.
Anyway, I think it is worthy refactoring (and compiler should be able to inline it, so there are no nano-performance considerations).
Good work!
Quoted 11 lines
Signed-off-by Lars Schneider <[email protected]>--- pkt-line.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)pkt-line.c +0
-0
pkt-line.c void packet_buf_flush(struct strbuf *buf) · line 100
Quoted 3 lines
pkt-line.c +0
-0@@ -98,9 +98,17 @@ void packet_buf_flush(struct strbuf *buf) }#define hex (a) (hexchar[(a) & 15])
I guess that this is inherited from the original, but this preprocessor macro is local to the format_header() / set_packet_header() function, and would not work outside it. Therefore I think we should #undef it after set_packet_header(), just in case somebody mistakes it for a generic hex() function. Perhaps even put it inside set_packet_header(), together with #undef.
But I might be mistaken... let's check... no, it isn't used outside it.
pkt-line.c void packet_buf_flush(struct strbuf *buf) · line 110
Quoted 11 lines
pkt-line.c +8
-1-static void format_packet (struct strbuf *out,const char *fmt,va_list args) +static void set_packet_header (char *buf,const int size) {static char hexchar[] ="0123456789abcdef" ; + buf[0 ] =hex (size >>12 ); + buf[1 ] =hex (size >>8 ); + buf[2 ] =hex (size >>4 ); + buf[3 ] =hex (size); +} + +static void format_packet (struct strbuf *out,const char *fmt,va_list args)
It is strange how 'git diff' chosen to represent this patch...
Quoted 17 lines
pkt-line.c +2
-4+{size_t orig_len, n; orig_len = out->len;@@ -111,10 +119,7 @@ static void format_packet(struct strbuf *out, const char *fmt, va_list args)if (n >LARGE_PACKET_MAX )die ("protocol error: impossibly long line" ); - out->buf[orig_len +0 ] =hex (n >>12 ); - out->buf[orig_len +1 ] =hex (n >>8 ); - out->buf[orig_len +2 ] =hex (n >>4 ); - out->buf[orig_len +3 ] =hex (n); +set_packet_header (&out->buf[orig_len], n);packet_trace (out->buf + orig_len +4 , n -4 ,1 ); }
Re: [PATCH v3 01/10] pkt-line: extract set_packet_header()
Quoted 21 lines · Jakub Narębski
W dniu 30.07.2016 o 01:37, [email protected] pisze:
Quoted 5 lines
From: Lars Schneider <[email protected]>
set_packet_header()converts an integer to a 4 byte hex string. Make this function locally available so that other pkt-line functions can use it.This description is not that clear that
set_packet_header()is a new function. Perhaps something like the followingExtract the part of
format_packet()that converts an integer to a 4 byte hex string intoset_packet_header(). Make this new function ...I also wonder if the part "Make this [new] function locally available..." is needed; we need to justify exports, but I think we don't need to justify limiting it to a module. If you want to justify that it is "static", perhaps it would be better to say why not to export it.
Quoted 4 lines
Anyway, I think it is worthy refactoring (and compiler should be able to inline it, so there are no nano-performance considerations).
Good work!
Thank you! I would go with this then:
Extract the part of format_packet() that converts an integer to a 4 byte hex string into set_packet_header().
OK?
Quoted 22 lines
Quoted 11 lines
Signed-off-by Lars Schneider <[email protected]>---
pkt-line.c| 15 ++++++++++-----1 file changed, 10 insertions(+), 5 deletions(-)pkt-line.c +0
-0pkt-line.c void packet_buf_flush(struct strbuf *buf) · line 100
Quoted 3 lines
pkt-line.c +0
-0@@ -98,9 +98,17 @@ void packet_buf_flush(struct strbuf *buf) }#define hex (a) (hexchar[(a) & 15])I guess that this is inherited from the original, but this preprocessor macro is local to the
format_header()/set_packet_header()function, and would not work outside it. Therefore I think we should #undef it afterset_packet_header(), just in case somebody mistakes it for a generichex()function. Perhaps even put it insideset_packet_header(), together with #undef.
Quoted 1 line
But I might be mistaken... let's check... no, it isn't used outside it.
Agreed. Would that be OK?
static void set_packet_header(char *buf, const int size)
{
static char hexchar[] = "0123456789abcdef";
#define hex(a) (hexchar[(a) & 15])
buf[0] = hex(size >> 12);
buf[1] = hex(size >> 8);
buf[2] = hex(size >> 4);
buf[3] = hex(size);
#undef hex
}- Lars
Re: [PATCH v3 01/10] pkt-line: extract set_packet_header()
[This response might have been invalidated by v4]
W dniu 01.08.2016 o 13:33, Lars Schneider pisze:
Quoted 16 lines
Quoted 12 lines · Jakub Narębski
Quoted 2 lines
#define hex(a) (hexchar[(a) & 15])
I guess that this is inherited from the original, but this preprocessor macro is local to the
format_header()/set_packet_header()function, and would not work outside it. Therefore I think we should #undef it afterset_packet_header(), just in case somebody mistakes it for a generichex()function. Perhaps even put it insideset_packet_header(), together with #undef.But I might be mistaken... let's check... no, it isn't used outside it.
Agreed. Would that be OK?
static void
set_packet_header(char *buf, const int size)
Quoted 9 lines
{
static char hexchar[] = "0123456789abcdef";
#define hex(a) (hexchar[(a) & 15])
buf[0] = hex(size >> 12);
buf[1] = hex(size >> 8);
buf[2] = hex(size >> 4);
buf[3] = hex(size);
#undef hex
}That's better, though I wonder if we need to start #defines at begining of line. But I think current proposal is O.K.
Either this (which has unnecessary larger scope)
#define hex(a) (hexchar[(a) & 15])
static void set_packet_header(char *buf, const int size)
{
static char hexchar[] = "0123456789abcdef";
buf[0] = hex(size >> 12);
buf[1] = hex(size >> 8);
buf[2] = hex(size >> 4);
buf[3] = hex(size);
}
#undef hexor this (which looks worse)
static void set_packet_header(char *buf, const int size)
{
static char hexchar[] = "0123456789abcdef";
#define hex(a) (hexchar[(a) & 15])
buf[0] = hex(size >> 12);
buf[1] = hex(size >> 8);
buf[2] = hex(size >> 4);
buf[3] = hex(size);
#undef hex
}Re: [PATCH v3 01/10] pkt-line: extract set_packet_header()
Quoted 55 lines · Jakub Narębski
[This response might have been invalidated by v4]
W dniu 01.08.2016 o 13:33, Lars Schneider pisze:
Quoted 16 lines
Quoted 12 lines · Jakub Narębski
Quoted 2 lines
#define hex(a) (hexchar[(a) & 15])
I guess that this is inherited from the original, but this preprocessor macro is local to the
format_header()/set_packet_header()function, and would not work outside it. Therefore I think we should #undef it afterset_packet_header(), just in case somebody mistakes it for a generichex()function. Perhaps even put it insideset_packet_header(), together with #undef.But I might be mistaken... let's check... no, it isn't used outside it.
Agreed. Would that be OK?
static void
set_packet_header(char *buf, const int size)Quoted 9 lines
{ static char hexchar[] = "0123456789abcdef"; #define hex(a) (hexchar[(a) & 15]) buf[0] = hex(size >> 12); buf[1] = hex(size >> 8); buf[2] = hex(size >> 4); buf[3] = hex(size); #undef hex }That's better, though I wonder if we need to start #defines at begining of line. But I think current proposal is O.K.
Either this (which has unnecessary larger scope)
#define hex(a) (hexchar[(a) & 15])
static voidset_packet_header(char *buf, const int size){(quoted diff) +0
-0static char hexchar[] = "0123456789abcdef"; buf[0] = hex(size >> 12); buf[1] = hex(size >> 8); buf[2] = hex(size >> 4); buf[3] = hex(size); } #undef hexor this (which looks worse)
static void
set_packet_header(char *buf, const int size){
Review
Quoted 8 lines
(quoted diff) +0
-0static char hexchar[] = "0123456789abcdef"; #define hex(a) (hexchar[(a) & 15]) buf[0] = hex(size >> 12); buf[1] = hex(size >> 8); buf[2] = hex(size >> 4); buf[3] = hex(size); #undef hex }
I probably will drop this patch as Junio is not convinced that it is a good idea:
http://public-inbox.org/git/xmqqd1lp8v2o.fsf%40gitster.mtv.corp.google.com/
- Lars
PATCH v3 2/10 pkt-line: add direct_packet_write() and direct_packet_write_data()
In reply to [email protected]From: Lars Schneider <[email protected]>
Sometimes pkt-line data is already available in a buffer and it would be a waste of resources to write the packet using packet_write() which would copy the existing buffer into a strbuf before writing it.
If the caller has control over the buffer creation then the PKTLINE_DATA_START macro can be used to skip the header and write directly into the data section of a pkt-line (PKTLINE_DATA_LEN bytes would be the maximum). direct_packet_write() would take this buffer, adjust the pkt-line header and write it.
If the caller has no control over the buffer creation then direct_packet_write_data() can be used. This function creates a pkt-line header. Afterwards the header and the data buffer are written using two consecutive write calls.
Both functions have a gentle parameter that indicates if Git should die in case of a write error (gentle set to 0) or return with a error (gentle set to 1).
--- pkt-line.c | 30 ++++++++++++++++++++++++++++++ pkt-line.h | 5 +++++ 2 files changed, 35 insertions(+)
pkt-line.c +30 -0
@@ -135,6 +135,36 @@ void packet_write(int fd, const char *fmt, ...)write_or_die (fd, buf.buf, buf.len); } +int direct_packet_write (int fd,char *buf,size_t size,int gentle) +{ +int ret =0 ; +packet_trace (buf +4 , size -4 ,1 ); +set_packet_header (buf, size); +if (gentle) + ret = !write_or_whine_pipe (fd, buf, size,"pkt-line" ); +else +write_or_die (fd, buf, size); +return ret; +} + +int direct_packet_write_data (int fd,const char *buf,size_t size,int gentle) +{ +int ret =0 ; +char hdr[4 ]; +set_packet_header (hdr,sizeof (hdr) + size); +packet_trace (buf, size,1 ); +if (gentle) { + ret = ( + !write_or_whine_pipe (fd, hdr,sizeof (hdr),"pkt-line header" ) || + !write_or_whine_pipe (fd, buf, size,"pkt-line data" ) + ); + }else { +write_or_die (fd, hdr,sizeof (hdr)); +write_or_die (fd, buf, size); + } +return ret; +} +void packet_buf_write (struct strbuf *buf,const char *fmt, ...) {va_list args;
pkt-line.h +5 -0
@@ -23,6 +23,8 @@ void packet_flush(int fd);void packet_write (int fd,const char *fmt, ...) __attribute__((format (printf,2 ,3 )));void packet_buf_flush (struct strbuf *buf);void packet_buf_write (struct strbuf *buf,const char *fmt, ...) __attribute__((format (printf,2 ,3 ))); +int direct_packet_write (int fd,char *buf,size_t size,int gentle); +int direct_packet_write_data (int fd,const char *buf,size_t size,int gentle);/* * Read a packetized line into the buffer, which must be at least size bytes
@@ -77,6 +79,9 @@ char *packet_read_line_buf(char **src_buf, size_t *src_len, int *size);#define DEFAULT_PACKET_MAX 1000#define LARGE_PACKET_MAX 65520 +#define PKTLINE_HEADER_LEN 4 +#define PKTLINE_DATA_START (pkt) ((pkt) + PKTLINE_HEADER_LEN) +#define PKTLINE_DATA_LEN (LARGE_PACKET_MAX - PKTLINE_HEADER_LEN)extern char packet_buffer[LARGE_PACKET_MAX ];#endif
Signature · 1 line
2.9.0
Re: [PATCH v3 02/10] pkt-line: add direct_packet_write() and direct_packet_write_data()
W dniu 30.07.2016 o 01:37, [email protected] pisze:
Quoted 12 lines
From: Lars Schneider <[email protected]>
Sometimes pkt-line data is already available in a buffer and it would be a waste of resources to write the packet using
packet_write()which would copy the existing buffer into a strbuf before writing it.If the caller has control over the buffer creation then the
PKTLINE_DATA_STARTmacro can be used to skip the header and write directly into the data section of a pkt-line (PKTLINE_DATA_LENbytes would be the maximum).direct_packet_write()would take this buffer, adjust the pkt-line header and write it.
Quoted 4 lines
If the caller has no control over the buffer creation then
direct_packet_write_data()can be used. This function creates a pkt-line header. Afterwards the header and the data buffer are written using two consecutive write calls.
I don't quite understand what do you mean by "caller has control over the buffer creation". Do you mean that caller either can write over the buffer, or cannot overwrite the buffer? Or do you mean that caller either can allocate buffer to hold header, or is getting only the data?
Quoted 4 lines
Both functions have a gentle parameter that indicates if Git should die in case of a write error (gentle set to 0) or return with a error (gentle set to 1).
So they are *_maybe_gently(), isn't it ;-)? Are there any existing functions in Git codebase that take 'gently' / 'strict' / 'die_on_error' parameter?
Quoted 14 lines
Signed-off-by Lars Schneider <[email protected]>--- pkt-line.c | 30 ++++++++++++++++++++++++++++++ pkt-line.h | 5 +++++ 2 files changed, 35 insertions(+)pkt-line.c +0
-0@@ -135,6 +135,36 @@ void packet_write(int fd, const char *fmt, ...)write_or_die (fd, buf.buf, buf.len); }
pkt-line.c void packet_write(int fd, const char *fmt, ...) · line 146
Quoted 10 lines
pkt-line.c +9
-0+int direct_packet_write (int fd,char *buf,size_t size,int gentle) +{ +int ret =0 ; +packet_trace (buf +4 , size -4 ,1 ); +set_packet_header (buf, size); +if (gentle) + ret = !write_or_whine_pipe (fd, buf, size,"pkt-line" ); +else +write_or_die (fd, buf, size);
Hmmm... in gently case we get the information in the warning that it is about "pkt-line", which is missing from !gently case. But it is probably not important.
pkt-line.c void packet_write(int fd, const char *fmt, ...) · line 148
Quoted 2 lines
pkt-line.c +2
-0+return ret; +}
Nice clean function, thanks to extracting set_packet_header().
pkt-line.c void packet_write(int fd, const char *fmt, ...) · line 150
Quoted 2 lines
pkt-line.c +2
-0+ +int direct_packet_write_data (int fd,const char *buf,size_t size,int gentle)
I would name the parameter 'data', rather than 'buf'; IMVHO it better describes it.
pkt-line.c void packet_write(int fd, const char *fmt, ...) · line 158
Quoted 8 lines
pkt-line.c +8
-0+{ +int ret =0 ; +char hdr[4 ]; +set_packet_header (hdr,sizeof (hdr) + size); +packet_trace (buf, size,1 ); +if (gentle) { + ret = ( + !write_or_whine_pipe(fd, hdr, sizeof(hdr), "pkt-line header") ||
You can write '4' here, no need for sizeof(hdr)... though compiler would optimize it away.
pkt-line.c void packet_write(int fd, const char *fmt, ...) · line 160
Quoted 2 lines
pkt-line.c +2
-0+ !write_or_whine_pipe (fd, buf, size,"pkt-line data" ) + );
Do we want to try to write "pkt-line data" if "pkt-line header" failed? If not, perhaps De Morgan-ize it
+ ret = !(
+ write_or_whine_pipe(fd, hdr, sizeof(hdr), "pkt-line header") &&
+ write_or_whine_pipe(fd, buf, size, "pkt-line data")
+ );
pkt-line.c void packet_write(int fd, const char *fmt, ...) · line 163
Quoted 3 lines
pkt-line.c +3
-0+ }else { +write_or_die (fd, hdr,sizeof (hdr)); +write_or_die (fd, buf, size);
I guess these two writes (here and in 'gently' case) are unavoidable...
Quoted 21 lines
pkt-line.c +4
-0+ } +return ret; +} +void packet_buf_write (struct strbuf *buf,const char *fmt, ...) {va_list args;pkt-line.h +2
-0@@ -23,6 +23,8 @@ void packet_flush(int fd);void packet_write (int fd,const char *fmt, ...) __attribute__((format (printf,2 ,3 )));void packet_buf_flush (struct strbuf *buf);void packet_buf_write (struct strbuf *buf,const char *fmt, ...) __attribute__((format (printf,2 ,3 ))); +int direct_packet_write (int fd,char *buf,size_t size,int gentle); +int direct_packet_write_data (int fd,const char *buf,size_t size,int gentle);/* * Read a packetized line into the buffer, which must be at least size bytes
pkt-line.h char *packet_read_line_buf(char **src_buf, size_t *src_len, int *size); · line 84
Quoted 6 lines
pkt-line.h +3
-0@@ -77,6 +79,9 @@ char *packet_read_line_buf(char **src_buf, size_t *src_len, int *size);#define DEFAULT_PACKET_MAX 1000#define LARGE_PACKET_MAX 65520 +#define PKTLINE_HEADER_LEN 4 +#define PKTLINE_DATA_START (pkt) ((pkt) + PKTLINE_HEADER_LEN) +#define PKTLINE_DATA_LEN (LARGE_PACKET_MAX - PKTLINE_HEADER_LEN)
Those are not used in direct_packet_write() and direct_packet_write_data(); but they would make them more verbose and less readable.
Quoted 4 lines
pkt-line.h +0
-0extern char packet_buffer[LARGE_PACKET_MAX ];#endif
Re: [PATCH v3 02/10] pkt-line: add direct_packet_write() and direct_packet_write_data()
Quoted 21 lines · Jakub Narębski
W dniu 30.07.2016 o 01:37, [email protected] pisze:
Quoted 12 lines
From: Lars Schneider <[email protected]>
Sometimes pkt-line data is already available in a buffer and it would be a waste of resources to write the packet using
packet_write()which would copy the existing buffer into a strbuf before writing it.If the caller has control over the buffer creation then the
PKTLINE_DATA_STARTmacro can be used to skip the header and write directly into the data section of a pkt-line (PKTLINE_DATA_LENbytes would be the maximum).direct_packet_write()would take this buffer, adjust the pkt-line header and write it.Quoted 4 lines
If the caller has no control over the buffer creation then
direct_packet_write_data()can be used. This function creates a pkt-line header. Afterwards the header and the data buffer are written using two consecutive write calls.
Quoted 5 lines
I don't quite understand what do you mean by "caller has control over the buffer creation". Do you mean that caller either can write over the buffer, or cannot overwrite the buffer? Or do you mean that caller either can allocate buffer to hold header, or is getting only the data?
How about this:
[...]
If the caller creates the buffer then a proper pkt-line buffer with header and data section can be created. The PKTLINE_DATA_START macro can be used to skip the header section and write directly to the data section (PKTLINE_DATA_LEN bytes would be the maximum). direct_packet_write() would take this buffer, fill the pkt-line header section with the appropriate data length value and write the entire buffer.
If the caller does not create the buffer, and consequently cannot leave room for the pkt-line header, then direct_packet_write_data() can be used. This function creates an extra buffer for the pkt-line header and afterwards writes the header buffer and the data buffer with two consecutive write calls.
---
Is that more clear?
Quoted 9 lines
Quoted 5 lines
Both functions have a gentle parameter that indicates if Git should die in case of a write error (gentle set to 0) or return with a error (gentle set to 1).
So they are
*_maybe_gently(), isn't it ;-)? Are there any existing functions in Git codebase that take 'gently' / 'strict' / 'die_on_error' parameter?
Yes, git grep "gentle" reveals:
wrapper.c:static int memory_limit_check(size_t size, int gentle) object.c:int type_from_string_gently(const char *str, ssize_t len, int gentle)
Quoted 34 lines
Quoted 14 lines
Signed-off-by Lars Schneider <[email protected]>---
pkt-line.c| 30 ++++++++++++++++++++++++++++++pkt-line.h| 5 +++++2 files changed, 35 insertions(+)pkt-line.c +0
-0@@ -135,6 +135,36 @@ void packet_write(int fd, const char *fmt, ...)write_or_die (fd, buf.buf, buf.len); }pkt-line.c void packet_write(int fd, const char *fmt, ...) · line 146
Quoted 10 lines
pkt-line.c +9
-0+int direct_packet_write (int fd,char *buf,size_t size,int gentle) +{ +int ret =0 ; +packet_trace (buf +4 , size -4 ,1 ); +set_packet_header (buf, size); +if (gentle) + ret = !write_or_whine_pipe (fd, buf, size,"pkt-line" ); +else +write_or_die (fd, buf, size);Hmmm... in gently case we get the information in the warning that it is about "pkt-line", which is missing from !gently case. But it is probably not important.
pkt-line.c void packet_write(int fd, const char *fmt, ...) · line 148
Quoted 2 lines
pkt-line.c +2
-0+return ret; +}Nice clean function, thanks to extracting
set_packet_header().
Quoted 5 lines
pkt-line.c void packet_write(int fd, const char *fmt, ...) · line 150
Quoted 2 lines
pkt-line.c +2
-0+ +int direct_packet_write_data (int fd,const char *buf,size_t size,int gentle)I would name the parameter 'data', rather than 'buf'; IMVHO it better describes it.
Agreed!
Quoted 12 lines
pkt-line.c void packet_write(int fd, const char *fmt, ...) · line 158
Quoted 9 lines
pkt-line.c +8
-0+{ +int ret =0 ; +char hdr[4 ]; +set_packet_header (hdr,sizeof (hdr) + size); +packet_trace (buf, size,1 ); +if (gentle) { + ret = ( + !write_or_whine_pipe(fd, hdr, sizeof(hdr), "pkt-line header") ||You can write '4' here, no need for sizeof(hdr)... though compiler would optimize it away.
Right, it would be optimized. However, I don't like the 4 there either. OK to use a macro instead? PKTLINE_HEADER_LEN ?
Quoted 10 lines
pkt-line.c void packet_write(int fd, const char *fmt, ...) · line 160
Quoted 2 lines
pkt-line.c +2
-0+ !write_or_whine_pipe (fd, buf, size,"pkt-line data" ) + );Do we want to try to write "pkt-line data" if "pkt-line header" failed? If not, perhaps De Morgan-ize it
+ ret = !(
+write_or_whine_pipe(fd, hdr, sizeof(hdr), "pkt-line header")&&
+write_or_whine_pipe(fd, buf, size, "pkt-line data")
+ );
Original:
ret = ( !write_or_whine_pipe(fd, hdr, sizeof(hdr), "pkt-line header") || !write_or_whine_pipe(fd, data, size, "pkt-line data") );
Well, if the first write call fails (return == 0), then it is negated and evaluates to true. I would think the second call is not evaluated, then?!
CPP reference:
"For the built-in logical OR operator, the result is true if either the first or the second operand (or both) is true. If the firstoperand is true, the second operand is not evaluated."
http://en.cppreference.com/w/cpp/language/operator_logical
Should I make this more explicit with a if clause?
Quoted 5 lines
pkt-line.c void packet_write(int fd, const char *fmt, ...) · line 163
Quoted 3 lines
pkt-line.c +3
-0+ }else { +write_or_die (fd, hdr,sizeof (hdr)); +write_or_die (fd, buf, size);I guess these two writes (here and in 'gently' case) are unavoidable...
I think so, too.
Quoted 23 lines
Quoted 23 lines
pkt-line.c +4
-0+ } +return ret; +} +void packet_buf_write (struct strbuf *buf,const char *fmt, ...) {va_list args;pkt-line.h +2
-0@@ -23,6 +23,8 @@ void packet_flush(int fd);void packet_write (int fd,const char *fmt, ...) __attribute__((format (printf,2 ,3 )));void packet_buf_flush (struct strbuf *buf);void packet_buf_write (struct strbuf *buf,const char *fmt, ...) __attribute__((format (printf,2 ,3 ))); +int direct_packet_write (int fd,char *buf,size_t size,int gentle); +int direct_packet_write_data (int fd,const char *buf,size_t size,int gentle);/* * Read a packetized line into the buffer, which must be at least size bytes @@ -77,6 +79,9 @@ char *packet_read_line_buf(char **src_buf, size_t *src_len, int *size);
Quoted 8 lines
pkt-line.h char *packet_read_line_buf(char **src_buf, size_t *src_len, int *size); · line 84
Quoted 5 lines
pkt-line.h +3
-0#define DEFAULT_PACKET_MAX 1000#define LARGE_PACKET_MAX 65520 +#define PKTLINE_HEADER_LEN 4 +#define PKTLINE_DATA_START (pkt) ((pkt) + PKTLINE_HEADER_LEN) +#define PKTLINE_DATA_LEN (LARGE_PACKET_MAX - PKTLINE_HEADER_LEN)Those are not used in
direct_packet_write()anddirect_packet_write_data(); but they would make them more verbose and less readable.
Good point, I should use them to check for the maximal packet length!
Thanks,
Lars
Quoted 6 lines
Quoted 5 lines
pkt-line.h +0
-0extern char packet_buffer[LARGE_PACKET_MAX ];#endif
Re: [PATCH v3 02/10] pkt-line: add direct_packet_write() and direct_packet_write_data()
[This response might have been invalidated by v4]
W dniu 01.08.2016 o 14:00, Lars Schneider pisze:
Quoted 35 lines
Quoted 18 lines · Jakub Narębski
W dniu 30.07.2016 o 01:37, [email protected] pisze:
Quoted 11 lines
Sometimes pkt-line data is already available in a buffer and it would be a waste of resources to write the packet using
packet_write()which would copy the existing buffer into a strbuf before writing it.If the caller has control over the buffer creation then the
PKTLINE_DATA_STARTmacro can be used to skip the header and write directly into the data section of a pkt-line (PKTLINE_DATA_LENbytes would be the maximum).direct_packet_write()would take this buffer, adjust the pkt-line header and write it.Quoted 4 lines
If the caller has no control over the buffer creation then
direct_packet_write_data()can be used. This function creates a pkt-line header. Afterwards the header and the data buffer are written using two consecutive write calls.Quoted 5 lines
I don't quite understand what do you mean by "caller has control over the buffer creation". Do you mean that caller either can write over the buffer, or cannot overwrite the buffer? Or do you mean that caller either can allocate buffer to hold header, or is getting only the data?
How about this:
[...]
If the caller creates the buffer then a proper pkt-line buffer with header and data section can be created. The
PKTLINE_DATA_STARTmacro can be used to skip the header section and write directly to the data section (PKTLINE_DATA_LENbytes would be the maximum).direct_packet_write()would take this buffer, fill the pkt-line header section with the appropriate data length value and write the entire buffer.
Quoted 7 lines
If the caller does not create the buffer, and consequently cannot leave room for the pkt-line header, then
direct_packet_write_data()can be used. This function creates an extra buffer for the pkt-line header and afterwards writes the header buffer and the data buffer with two consecutive write calls.---
Is that more clear?
Yes, I think it is more clear.
The only thing that could be improved is to perhaps instead of using
"then a proper pkt-line buffer with header and data section can be created"
it might be more clear to write
"then a proper pkt-line buffer with data section and a place for pkt-line header"
Quoted 12 lines
Quoted 11 lines
pkt-line.c void packet_write(int fd, const char *fmt, ...) · line 158
Quoted 8 lines
pkt-line.c +8
-0+{ +int ret =0 ; +char hdr[4 ]; +set_packet_header (hdr,sizeof (hdr) + size); +packet_trace (buf, size,1 ); +if (gentle) { + ret = ( + !write_or_whine_pipe(fd, hdr, sizeof(hdr), "pkt-line header") ||You can write '4' here, no need for sizeof(hdr)... though compiler would optimize it away.
Quoted 2 lines
Right, it would be optimized. However, I don't like the 4 there either. OK to use a macro instead?
PKTLINE_HEADER_LEN?
Did you mean
+ char hdr[PKTLINE_HEADER_LEN];
+ set_packet_header(hdr, sizeof(hdr) + size);Quoted 12 lines
Quoted 10 lines
pkt-line.c void packet_write(int fd, const char *fmt, ...) · line 160
Quoted 2 lines
pkt-line.c +2
-0+ !write_or_whine_pipe (fd, buf, size,"pkt-line data" ) + );Do we want to try to write "pkt-line data" if "pkt-line header" failed? If not, perhaps De Morgan-ize it
+ ret = !(
+write_or_whine_pipe(fd, hdr, sizeof(hdr), "pkt-line header")&&
+write_or_whine_pipe(fd, buf, size, "pkt-line data")
+ );
Quoted 8 lines
Original:
ret = ( !write_or_whine_pipe(fd, hdr, sizeof(hdr), "pkt-line header") || !write_or_whine_pipe(fd, data, size, "pkt-line data") );Well, if the first write call fails (return == 0), then it is negated and evaluates to true. I would think the second call is not evaluated, then?!
This is true both for || and for &&, as in C logical boolean operators short-circuit.
Quoted 1 line
Should I make this more explicit with a if clause?
No need.
Signature · 1 line
Jakub Narębski
Re: [PATCH v3 02/10] pkt-line: add direct_packet_write() and direct_packet_write_data()
Quoted 51 lines · Jakub Narębski
[This response might have been invalidated by v4]
W dniu 01.08.2016 o 14:00, Lars Schneider pisze:
Quoted 35 lines
Quoted 18 lines · Jakub Narębski
W dniu 30.07.2016 o 01:37, [email protected] pisze:
Quoted 11 lines
Sometimes pkt-line data is already available in a buffer and it would be a waste of resources to write the packet using
packet_write()which would copy the existing buffer into a strbuf before writing it.If the caller has control over the buffer creation then the
PKTLINE_DATA_STARTmacro can be used to skip the header and write directly into the data section of a pkt-line (PKTLINE_DATA_LENbytes would be the maximum).direct_packet_write()would take this buffer, adjust the pkt-line header and write it.Quoted 4 lines
If the caller has no control over the buffer creation then
direct_packet_write_data()can be used. This function creates a pkt-line header. Afterwards the header and the data buffer are written using two consecutive write calls.Quoted 5 lines
I don't quite understand what do you mean by "caller has control over the buffer creation". Do you mean that caller either can write over the buffer, or cannot overwrite the buffer? Or do you mean that caller either can allocate buffer to hold header, or is getting only the data?
How about this:
[...]
If the caller creates the buffer then a proper pkt-line buffer with header and data section can be created. The
PKTLINE_DATA_STARTmacro can be used to skip the header section and write directly to the data section (PKTLINE_DATA_LENbytes would be the maximum).direct_packet_write()would take this buffer, fill the pkt-line header section with the appropriate data length value and write the entire buffer.Quoted 7 lines
If the caller does not create the buffer, and consequently cannot leave room for the pkt-line header, then
direct_packet_write_data()can be used. This function creates an extra buffer for the pkt-line header and afterwards writes the header buffer and the data buffer with two consecutive write calls.---
Is that more clear?Yes, I think it is more clear.
Quoted 7 lines
The only thing that could be improved is to perhaps instead of using
"then a proper pkt-line buffer with header and data section can be created"
it might be more clear to write
"then a proper pkt-line buffer with data section and a place for pkt-line header"
OK. I changed it to
"If the caller has control over the buffer creation then a proper pkt-line buffer with header and data section can be allocated. The PKTLINE_DATA_START macro can be used to skip the header and write directly into the data section of a pkt-line (PKTLINE_DATA_LEN bytes would be the maximum)..."
However, I am not yet sure if I can/will keep this patch:
http://public-inbox.org/git/xmqqeg645x6b.fsf%40gitster.mtv.corp.google.com/
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
pkt-line.c void packet_write(int fd, const char *fmt, ...) · line 158
Quoted 9 lines
pkt-line.c +8
-0+{ +int ret =0 ; +char hdr[4 ]; +set_packet_header (hdr,sizeof (hdr) + size); +packet_trace (buf, size,1 ); +if (gentle) { + ret = ( + !write_or_whine_pipe(fd, hdr, sizeof(hdr), "pkt-line header") ||You can write '4' here, no need for sizeof(hdr)... though compiler would optimize it away.
Quoted 7 lines
Quoted 2 lines
Right, it would be optimized. However, I don't like the 4 there either. OK to use a macro instead?
PKTLINE_HEADER_LEN?Did you mean
+ char hdr[PKTLINE_HEADER_LEN]; + set_packet_header(hdr, sizeof(hdr) + size);
yes!
Quoted 18 lines
Quoted 18 lines
Review
Quoted 10 lines
pkt-line.c void packet_write(int fd, const char *fmt, ...) · line 160
Quoted 2 lines
pkt-line.c +2
-0+ !write_or_whine_pipe (fd, buf, size,"pkt-line data" ) + );Do we want to try to write "pkt-line data" if "pkt-line header" failed? If not, perhaps De Morgan-ize it
(quoted diff) +4
-0+ ret = !( + write_or_whine_pipe(fd, hdr, sizeof(hdr), "pkt-line header") && + write_or_whine_pipe(fd, buf, size, "pkt-line data") + );Original:
ret = ( !write_or_whine_pipe(fd, hdr, sizeof(hdr), "pkt-line header") || !write_or_whine_pipe(fd, data, size, "pkt-line data") );
Quoted 5 lines
Quoted 2 lines
Well, if the first write call fails (return == 0), then it is negated and evaluates to true. I would think the second call is not evaluated, then?!
This is true both for || and for &&, as in C logical boolean operators short-circuit.
True. That's why I did not get your "de morganize" it comment... what would de morgan change?
Quoted 4 lines
Quoted 2 lines
Should I make this more explicit with a if clause?
No need.
OK
Thanks,
Lars
PATCH v3 3/10 pkt-line: add packet_flush_gentle()
In reply to [email protected]From: Lars Schneider <[email protected]>
packet_flush() would die in case of a write error even though for some callers an error would be acceptable. Add packet_flush_gentle() which writes a pkt-line flush packet and returns 0 for success and 1 for failure.
--- pkt-line.c | 6 ++++++ pkt-line.h | 1 + 2 files changed, 7 insertions(+)
pkt-line.c +6 -0
@@ -91,6 +91,12 @@ void packet_flush(int fd)write_or_die (fd,"0000" ,4 ); } +int packet_flush_gentle (int fd) +{ +packet_trace ("0000" ,4 ,1 ); +return !write_or_whine_pipe (fd,"0000" ,4 ,"flush packet" ); +} +void packet_buf_flush (struct strbuf *buf) {packet_trace ("0000" ,4 ,1 );
pkt-line.h +1 -0
@@ -23,6 +23,7 @@ void packet_flush(int fd);void packet_write (int fd,const char *fmt, ...) __attribute__((format (printf,2 ,3 )));void packet_buf_flush (struct strbuf *buf);void packet_buf_write (struct strbuf *buf,const char *fmt, ...) __attribute__((format (printf,2 ,3 ))); +int packet_flush_gentle (int fd);int direct_packet_write (int fd,char *buf,size_t size,int gentle);int direct_packet_write_data (int fd,const char *buf,size_t size,int gentle);
Signature · 1 line
2.9.0
Re: [PATCH v3 03/10] pkt-line: add packet_flush_gentle()
W dniu 30.07.2016 o 01:37, [email protected] pisze:
Quoted 5 lines
From: Lars Schneider <[email protected]>
packet_flush()would die in case of a write error even though for some callers an error would be acceptable. Addpacket_flush_gentle()which writes a pkt-line flush packet and returns0for success and1for failure.
I think it should be packet_flush_gently(), as in "to flush gently", but this is only my opinion; I have not checked the naming rules and practices for the rest of Git codebase.
Quoted 3 lines
Signed-off-by Lars Schneider <[email protected]>---
Re: [PATCH v3 03/10] pkt-line: add packet_flush_gentle()
Quoted 6 lines · Jakub Narębski
W dniu 30.07.2016 o 01:37, [email protected] pisze:
Quoted 2 lines
From: Lars Schneider <[email protected]>
Quoted 7 lines
Quoted 3 lines
packet_flush()would die in case of a write error even though for some callers an error would be acceptable. Addpacket_flush_gentle()which writes a pkt-line flush packet and returns0for success and1for failure.I think it should be
packet_flush_gently(), as in "to flush gently", but this is only my opinion; I have not checked the naming rules and practices for the rest of Git codebase.
Agreed. This would match:
object.c:int type_from_string_gently(const char *str, ssize_t len, int gentle)
Thanks,
Lars
Quoted 5 lines
Quoted 4 lines
Signed-off-by Lars Schneider <[email protected]>---
Re: [PATCH v3 03/10] pkt-line: add packet_flush_gentle()
In reply to [email protected]Quoted 8 lines
Am 29.07.2016 um 20:37 schrieb [email protected]:
From: Lars Schneider <[email protected]>
packet_flush()would die in case of a write error even though for some callers an error would be acceptable.
What happens if there is a write error ?
Basically the protocol is out of synch.
Lenght information is mixed up with payload, or the other way around.
It may be, that the consequences of a write error are acceptable, because a filter is allowed to fail.
What is not acceptable is a "broken" protocol.
The consequence schould be to close the fd and tear down all resources. connected to it.
In our case to terminate the external filter daemon in some way, and to never use this instance again.
Quoted 41 lines
Add
packet_flush_gentle()which writes a pkt-line flush packet and returns0for success and1for failure.Signed-off-by Lars Schneider <[email protected]>---
pkt-line.c| 6 ++++++pkt-line.h| 1 +2 files changed, 7 insertions(+)pkt-line.c +6
-0@@ -91,6 +91,12 @@ void packet_flush(int fd)write_or_die (fd,"0000" ,4 ); } +int packet_flush_gentle (int fd) +{ +packet_trace ("0000" ,4 ,1 ); +return !write_or_whine_pipe (fd,"0000" ,4 ,"flush packet" ); +} +void packet_buf_flush (struct strbuf *buf) {packet_trace ("0000" ,4 ,1 );pkt-line.h +1
-0@@ -23,6 +23,7 @@ void packet_flush(int fd);void packet_write (int fd,const char *fmt, ...) __attribute__((format (printf,2 ,3 )));void packet_buf_flush (struct strbuf *buf);void packet_buf_write (struct strbuf *buf,const char *fmt, ...) __attribute__((format (printf,2 ,3 ))); +int packet_flush_gentle (int fd);int direct_packet_write (int fd,char *buf,size_t size,int gentle);int direct_packet_write_data (int fd,const char *buf,size_t size,int gentle);Signature · 1 line
2.9.0
Re: [PATCH v3 03/10] pkt-line: add packet_flush_gentle()
Quoted 11 lines · Torstem Bögershausen
Quoted 9 lines
Am 29.07.2016 um 20:37 schrieb [email protected]:
From: Lars Schneider <[email protected]>
packet_flush()would die in case of a write error even though for some callers an error would be acceptable.
Quoted 11 lines
What happens if there is a write error ?
Basically the protocol is out of synch.
Lenght information is mixed up with payload, or the other way around.
It may be, that the consequences of a write error are acceptable, because a filter is allowed to fail.
What is not acceptable is a "broken" protocol.
The consequence schould be to close the fd and tear down all resources. connected to it.
In our case to terminate the external filter daemon in some way, and to never use this instance again.
Correct! That is exactly what is happening in kill_protocol2_filter() here:
+static int apply_protocol2_filter(const char *path, const char *src, size_t len,
+ int fd, struct strbuf *dst, const char *cmd,
+ const int wanted_capability)
+{
...
+ if (ret) {
+ strbuf_swap(dst, &nbuf);
+ } else {
+ if (!filter_result || strcmp(filter_result, "reject")) {
+ // Something went wrong with the protocol filter. Force shutdown!
+ error("external filter '%s' failed", cmd);
+ kill_protocol2_filter(&cmd_process_map, entry);
+ }
+ }
+ strbuf_release(&nbuf);
+ return ret;
+}
More context:
https://github.com/larsxschneider/git/blob/e128326070847ac596e8bb21adebc8abab2003fc/convert.c#L821
- Lars
Quoted 43 lines
Quoted 43 lines
Add
packet_flush_gentle()which writes a pkt-line flush packet and returns0for success and1for failure.Signed-off-by Lars Schneider <[email protected]>---
pkt-line.c| 6 ++++++pkt-line.h| 1 +2 files changed, 7 insertions(+)pkt-line.c +6
-0@@ -91,6 +91,12 @@ void packet_flush(int fd)write_or_die (fd,"0000" ,4 ); } +int packet_flush_gentle (int fd) +{ +packet_trace ("0000" ,4 ,1 ); +return !write_or_whine_pipe (fd,"0000" ,4 ,"flush packet" ); +} +void packet_buf_flush (struct strbuf *buf) {packet_trace ("0000" ,4 ,1 );pkt-line.h +1
-0@@ -23,6 +23,7 @@ void packet_flush(int fd);void packet_write (int fd,const char *fmt, ...) __attribute__((format (printf,2 ,3 )));void packet_buf_flush (struct strbuf *buf);void packet_buf_write (struct strbuf *buf,const char *fmt, ...) __attribute__((format (printf,2 ,3 ))); +int packet_flush_gentle (int fd);int direct_packet_write (int fd,char *buf,size_t size,int gentle);int direct_packet_write_data (int fd,const char *buf,size_t size,int gentle);Signature · 1 line
2.9.0
Re: [PATCH v3 03/10] pkt-line: add packet_flush_gentle()
Quoted 23 lines · Lars Schneider
Quoted 11 lines · Torstem Bögershausen
Quoted 9 lines
Am 29.07.2016 um 20:37 schrieb [email protected]:
From: Lars Schneider <[email protected]>
packet_flush()would die in case of a write error even though for some callers an error would be acceptable.Quoted 11 lines
What happens if there is a write error ?
Basically the protocol is out of synch.
Lenght information is mixed up with payload, or the other way around.
It may be, that the consequences of a write error are acceptable, because a filter is allowed to fail.
What is not acceptable is a "broken" protocol.
The consequence schould be to close the fd and tear down all resources. connected to it.
In our case to terminate the external filter daemon in some way, and to never use this instance again.
Quoted 2 lines
Correct! That is exactly what is happening in
kill_protocol2_filter()here:
Wait a second.
Is kill the same as shutdown ?
I would expect that
The process terminates itself as soon as it detects EOF. As there is nothing more read.
Then the next question: The combination of kill & protocol in kill_protocol(), what does it mean ?
Is it more like a graceful shutdown_protocol() ?
Re: [PATCH v3 03/10] pkt-line: add packet_flush_gentle()
Quoted 27 lines · Torsten Bögershausen
Quoted 23 lines · Lars Schneider
Quoted 22 lines · Torstem Bögershausen
Quoted 9 lines
Am 29.07.2016 um 20:37 schrieb [email protected]:
From: Lars Schneider <[email protected]>
packet_flush()would die in case of a write error even though for some callers an error would be acceptable.What happens if there is a write error ?
Basically the protocol is out of synch.
Lenght information is mixed up with payload, or the other way around.
It may be, that the consequences of a write error are acceptable, because a filter is allowed to fail.
What is not acceptable is a "broken" protocol.
The consequence schould be to close the fd and tear down all resources. connected to it.
In our case to terminate the external filter daemon in some way, and to never use this instance again.
Quoted 6 lines
Quoted 2 lines
Correct! That is exactly what is happening in
kill_protocol2_filter()here:Wait a second.
Is kill the same as shutdown ?
I would expect that
No, kill is used if the filter behaved strangely or signaled an error. "Shutdown" is a graceful shutdown. However, that might not be an ideal name. See the bottom of my discussion with Peff here:
http://public-inbox.org/git/74C2CEA6-EAAB-406F-8B37-969654955413%40gmail.com/
Quoted 5 lines
The process terminates itself as soon as it detects EOF. As there is nothing more read.
Then the next question: The combination of kill & protocol in
kill_protocol(), what does it mean ?
I renamed that function to "kill_multi_file_filter". Initially I called the multi file filter "protocol" (bad decision I know) and named the functions accordingly.
Quoted 1 line
Is it more like a graceful
shutdown_protocol()?
Yes.
Thanks,
Lars
PATCH v3 4/10 pkt-line: call packet_trace() only if a packet is actually send
In reply to [email protected]From: Lars Schneider <[email protected]>
The packet_trace() call is not ideal in format_packet() as we would print a trace when a packet is formatted and (potentially) when the packet is actually send. This was no problem up until now because format_packet() was only used by one function. Fix it by moving the trace call into the function that actally sends the packet.
--- pkt-line.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
pkt-line.c +1 -1
@@ -126,7 +126,6 @@ static void format_packet(struct strbuf *out, const char *fmt, va_list args)die ("protocol error: impossibly long line" );set_packet_header (&out->buf[orig_len], n); -packet_trace (out->buf + orig_len +4 , n -4 ,1 ); }void packet_write (int fd,const char *fmt, ...)
@@ -138,6 +137,7 @@ void packet_write(int fd, const char *fmt, ...)va_start (args, fmt);format_packet (&buf, fmt, args);va_end (args); +packet_trace (buf.buf +4 , buf.len -4 ,1 );write_or_die (fd, buf.buf, buf.len); }
Signature · 1 line
2.9.0
Re: [PATCH v3 04/10] pkt-line: call packet_trace() only if a packet is actually send
W dniu 30.07.2016 o 01:37, [email protected] pisze:
Quoted 3 lines
From: Lars Schneider <[email protected]>
The
packet_trace()call is not ideal informat_packet()as we would print
Style; I think the following is more readable:
The packet_trace() call in format_packet() is not ideal, as we would...
Quoted 4 lines
a trace when a packet is formatted and (potentially) when the packet is actually send. This was no problem up until now because
format_packet()was only used by one function. Fix it by moving the trace call into the function that actally sends the packet.
s/actally/actually/
I don't buy this explanation. If you want to trace packets, you might do it on input (when formatting packet), or on output (when writing packet). It's when there are more than one formatting function, but one writing function, then placing trace call in write function means less code duplication; and of course the reverse.
Another issue is that something may happen between formatting packet and sending it, and we probably want to packet_trace() when packet is actually send.
Neither of those is visible in commit message.
Quoted 27 lines
Signed-off-by Lars Schneider <[email protected]>--- pkt-line.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)pkt-line.c +1
-1@@ -126,7 +126,6 @@ static void format_packet(struct strbuf *out, const char *fmt, va_list args)die ("protocol error: impossibly long line" );set_packet_header (&out->buf[orig_len], n); -packet_trace (out->buf + orig_len +4 , n -4 ,1 ); }void packet_write (int fd,const char *fmt, ...)@@ -138,6 +137,7 @@ void packet_write(int fd, const char *fmt, ...)va_start (args, fmt);format_packet (&buf, fmt, args);va_end (args); +packet_trace (buf.buf +4 , buf.len -4 ,1 );write_or_die (fd, buf.buf, buf.len); }
Re: [PATCH v3 04/10] pkt-line: call packet_trace() only if a packet is actually send
Quoted 11 lines · Jakub Narębski
W dniu 30.07.2016 o 01:37, [email protected] pisze:
Quoted 3 lines
From: Lars Schneider <[email protected]>
The
packet_trace()call is not ideal informat_packet()as we would printStyle; I think the following is more readable:
The
packet_trace()call informat_packet()is not ideal, as we would...
Agreed!
Quoted 6 lines
Quoted 4 lines
a trace when a packet is formatted and (potentially) when the packet is actually send. This was no problem up until now because
format_packet()was only used by one function. Fix it by moving the trace call into the function that actally sends the packet.s/actally/actually/
Thanks!
Quoted 11 lines
I don't buy this explanation. If you want to trace packets, you might do it on input (when formatting packet), or on output (when writing packet). It's when there are more than one formatting function, but one writing function, then placing trace call in write function means less code duplication; and of course the reverse.
Another issue is that something may happen between formatting packet and sending it, and we probably want to
packet_trace()when packet is actually send.Neither of those is visible in commit message.
The packet_trace() call in format_packet() is not ideal, as we would print a trace when a packet is formatted and (potentially) when the same packet is actually written. This was no problem up until now because packet_write(), the function that uses format_packet() and writes the formatted packet, did not trace the packet.
This developer believes that trace calls should only happen when a packet is actually written as the packet could be modified between formatting and writing. Therefore the trace call was moved from format_packet() to packet_write().
--
Better?
Quoted 29 lines
Quoted 28 lines
Signed-off-by Lars Schneider <[email protected]>---
pkt-line.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)pkt-line.c +1
-1@@ -126,7 +126,6 @@ static void format_packet(struct strbuf *out, const char *fmt, va_list args)die ("protocol error: impossibly long line" );set_packet_header (&out->buf[orig_len], n); -packet_trace (out->buf + orig_len +4 , n -4 ,1 ); }void packet_write (int fd,const char *fmt, ...)@@ -138,6 +137,7 @@ void packet_write(int fd, const char *fmt, ...)va_start (args, fmt);format_packet (&buf, fmt, args);va_end (args); +packet_trace (buf.buf +4 , buf.len -4 ,1 );write_or_die (fd, buf.buf, buf.len); }
Re: [PATCH v3 04/10] pkt-line: call packet_trace() only if a packet is actually send
[This response might have been invalidated by v4]
W dniu 01.08.2016 o 14:18, Lars Schneider pisze:
Quoted 21 lines
Quoted 9 lines · Jakub Narębski
W dniu 30.07.2016 o 01:37, [email protected] pisze:
I don't buy this explanation. If you want to trace packets, you might do it on input (when formatting packet), or on output (when writing packet). It's when there are more than one formatting function, but one writing function, then placing trace call in write function means less code duplication; and of course the reverse.
Quoted 5 lines
Another issue is that something may happen between formatting packet and sending it, and we probably want to
packet_trace()when packet is actually send.Neither of those is visible in commit message.
The
packet_trace()call informat_packet()is not ideal, as we would print a trace when a packet is formatted and (potentially) when the same packet is actually written. This was no problem up until now becausepacket_write(), the function that usesformat_packet()and writes the formatted packet, did not trace the packet.
Quoted 8 lines
This developer believes that trace calls should only happen when a packet is actually written as the packet could be modified between formatting and writing. Therefore the trace call was moved from
format_packet()topacket_write().--
Better?
Yes, that's much better.
P.S. Yes, this is one of those changes where commit message is much longer
than the change itself...
Signature · 1 line
Jakub Narębski
PATCH v3 5/10 pack-protocol: fix maximum pkt-line size
In reply to [email protected]From: Lars Schneider <[email protected]>
According to LARGE_PACKET_MAX in pkt-line.h the maximal lenght of a pkt-line packet is 65520 bytes. The pkt-line header takes 4 bytes and therefore the pkt-line data component must not exceed 65516 bytes.
--- Documentation/technical/protocol-common.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Documentation/technical/protocol-common.txt +3 -3
@@ -67,9 +67,9 @@ with non-binary data the same whether or not they contain the trailing LF (stripping the LF if present, and not complaining when it is missing). -The maximum length of a pkt-line's data component is 65520 bytes. -Implementations MUST NOT send pkt-line whose length exceeds 65524 -(65520 bytes of payload + 4 bytes of length data). +The maximum length of a pkt-line's data component is 65516 bytes. +Implementations MUST NOT send pkt-line whose length exceeds 65520 +(65516 bytes of payload + 4 bytes of length data). Implementations SHOULD NOT send an empty pkt-line ("0004").
Signature · 1 line
2.9.0
Re: [PATCH v3 05/10] pack-protocol: fix maximum pkt-line size
W dniu 30.07.2016 o 01:37, [email protected] pisze:
Quoted 5 lines
From: Lars Schneider <[email protected]>
According to
LARGE_PACKET_MAXinpkt-line.hthe maximal lenght of a pkt-line packet is 65520 bytes. The pkt-line header takes 4 bytes and therefore the pkt-line data component must not exceed 65516 bytes.
s/lenght/length/
Is it maximum length of pkt-line packet, or maximum length of data that can be send in a packet?
With 4 hex digits, maximal length if pkt-line packet (together with length) is ffff_16, that is 2^16-1 = 65535. Where does the number 65520 comes from?
Quoted 24 lines
Signed-off-by Lars Schneider <[email protected]>--- Documentation/technical/protocol-common.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)Documentation/technical/protocol-common.txt +3
-3@@ -67,9 +67,9 @@ with non-binary data the same whether or not they contain the trailing LF (stripping the LF if present, and not complaining when it is missing). -The maximum length of a pkt-line's data component is 65520 bytes. -Implementations MUST NOT send pkt-line whose length exceeds 65524 -(65520 bytes of payload + 4 bytes of length data). +The maximum length of a pkt-line's data component is 65516 bytes. +Implementations MUST NOT send pkt-line whose length exceeds 65520 +(65516 bytes of payload + 4 bytes of length data). Implementations SHOULD NOT send an empty pkt-line ("0004").
Re: [PATCH v3 05/10] pack-protocol: fix maximum pkt-line size
Quoted 11 lines · Jakub Narębski
W dniu 30.07.2016 o 01:37, [email protected] pisze:
Quoted 5 lines
From: Lars Schneider <[email protected]>
According to
LARGE_PACKET_MAXinpkt-line.hthe maximal lenght of a pkt-line packet is 65520 bytes. The pkt-line header takes 4 bytes and therefore the pkt-line data component must not exceed 65516 bytes.s/lenght/length/
Thanks!
Quoted 2 lines
Is it maximum length of pkt-line packet, or maximum length of data that can be send in a packet?
65520 is the maximum length of a pkt-line.
Quoted 3 lines
With 4 hex digits, maximal length if pkt-line packet (together with length) is
ffff_16, that is 2^16-1 = 65535. Where does the number 65520 comes from?
Historic reasons, I guess? However, it won't be changed. See response from Peff here:
http://public-inbox.org/git/20160726134257.GB19277%40sigill.intra.peff.net/
Quoted 26 lines
Quoted 25 lines
Signed-off-by Lars Schneider <[email protected]>---
Documentation/technical/protocol-common.txt| 6 +++---1 file changed, 3 insertions(+), 3 deletions(-)Documentation/technical/protocol-common.txt +0
-0@@ -67,9 +67,9 @@ with non-binary data the same whether or not they contain the trailingLF (stripping the LF if present, and not complaining when it is missing).
Documentation/technical/protocol-common.txt +3
-3-The maximum length of a pkt-line's data component is 65520 bytes. -Implementations MUST NOT send pkt-line whose length exceeds 65524 -(65520 bytes of payload + 4 bytes of length data). +The maximum length of a pkt-line's data component is 65516 bytes. +Implementations MUST NOT send pkt-line whose length exceeds 65520 +(65516 bytes of payload + 4 bytes of length data).Implementations SHOULD NOT send an empty pkt-line ("0004").
PATCH v3 6/10 run-command: add clean_on_exit_handler
In reply to [email protected]From: Lars Schneider <[email protected]>
Some commands might need to perform cleanup tasks on exit. Let's give them an interface for doing this.
--- run-command.c | 12 ++++++++---- run-command.h | 1 + 2 files changed, 9 insertions(+), 4 deletions(-)
run-command.c +8 -4
@@ -21,6 +21,7 @@ void child_process_clear(struct child_process *child)struct child_to_clean {pid_t pid; +void (*clean_on_exit_handler)(pid_t );struct child_to_clean *next; };static struct child_to_clean *children_to_clean;
@@ -30,6 +31,8 @@ static void cleanup_children(int sig, int in_signal) {while (children_to_clean) {struct child_to_clean *p = children_to_clean; +if (p->clean_on_exit_handler) + p->clean_on_exit_handler (p->pid); children_to_clean = p->next;kill (p->pid, sig);if (!in_signal)
@@ -49,10 +52,11 @@ static void cleanup_children_on_exit(void)cleanup_children (SIGTERM ,0 ); } -static void mark_child_for_cleanup (pid_t pid) +static void mark_child_for_cleanup (pid_t pid,void (*clean_on_exit_handler)(pid_t )) {struct child_to_clean *p =xmalloc (sizeof (*p)); p->pid = pid; + p->clean_on_exit_handler = clean_on_exit_handler; p->next = children_to_clean; children_to_clean = p;
@@ -422,7 +426,7 @@ int start_command(struct child_process *cmd)if (cmd->pid <0 )error_errno ("cannot fork() for %s" , cmd->argv[0 ]);else if (cmd->clean_on_exit) -mark_child_for_cleanup (cmd->pid); +mark_child_for_cleanup (cmd->pid, cmd->clean_on_exit_handler);/* * Wait for child's execvp. If the execvp succeeds (or if fork()
@@ -483,7 +487,7 @@ int start_command(struct child_process *cmd)if (cmd->pid <0 && (!cmd->silent_exec_failure || errno !=ENOENT ))error_errno ("cannot spawn %s" , cmd->argv[0 ]);if (cmd->clean_on_exit && cmd->pid >=0 ) -mark_child_for_cleanup (cmd->pid); +mark_child_for_cleanup (cmd->pid, cmd->clean_on_exit_handler);argv_array_clear (&nargv); cmd->argv = sargv;
@@ -752,7 +756,7 @@ int start_async(struct async *async)exit (!!async->proc (proc_in, proc_out, async->data)); } -mark_child_for_cleanup (async->pid); +mark_child_for_cleanup (async->pid,NULL );if (need_in)close (fdin[0 ]);
run-command.h +1 -0
@@ -43,6 +43,7 @@ struct child_process { unsigned stdout_to_stderr:1; unsigned use_shell:1; unsigned clean_on_exit:1; +void (*clean_on_exit_handler)(pid_t ); };#define CHILD_PROCESS_INIT { NULL, ARGV_ARRAY_INIT, ARGV_ARRAY_INIT }
Signature · 1 line
2.9.0
Re: [PATCH v3 06/10] run-command: add clean_on_exit_handler
Am 30.07.2016 um 01:37 schrieb [email protected]:
Quoted 22 lines
Some commands might need to perform cleanup tasks on exit. Let's give them an interface for doing this.
Signed-off-by Lars Schneider <[email protected]>--- run-command.c | 12 ++++++++---- run-command.h | 1 + 2 files changed, 9 insertions(+), 4 deletions(-)run-command.c +1
-0@@ -21,6 +21,7 @@ void child_process_clear(struct child_process *child)struct child_to_clean {pid_t pid; +void (*clean_on_exit_handler)(pid_t );struct child_to_clean *next; };static struct child_to_clean *children_to_clean;
run-command.c static void cleanup_children(int sig, int in_signal) · line 35
Quoted 5 lines
run-command.c +2
-0@@ -30,6 +31,8 @@ static void cleanup_children(int sig, int in_signal) {while (children_to_clean) {struct child_to_clean *p = children_to_clean; +if (p->clean_on_exit_handler) + p->clean_on_exit_handler (p->pid);
This summons demons. cleanup_children() is invoked from a signal handler. In this case, it can call only async-signal-safe functions. It does not look like the handler that you are going to install later will take note of this caveat!
run-command.c static void cleanup_children(int sig, int in_signal) · line 38
Quoted 3 lines
run-command.c +0
-0children_to_clean = p->next;kill (p->pid, sig);if (!in_signal)
The condition that we see here in the context protects free(p) (which is not async-signal-safe). Perhaps the invocation of the new callback should be skipped in the same manner when this is called from a signal handler? 507d7804 (pager: don't use unsafe functions in signal handlers) may be worth a look.
-- Hannes
Re: [PATCH v3 06/10] run-command: add clean_on_exit_handler
Quoted 34 lines · Johannes Sixt
Am 30.07.2016 um 01:37 schrieb [email protected]:
Quoted 22 lines
Some commands might need to perform cleanup tasks on exit. Let's give them an interface for doing this.
Signed-off-by Lars Schneider <[email protected]>---
run-command.c| 12 ++++++++----run-command.h| 1 +2 files changed, 9 insertions(+), 4 deletions(-)run-command.c +1
-0@@ -21,6 +21,7 @@ void child_process_clear(struct child_process *child)struct child_to_clean {pid_t pid; +void (*clean_on_exit_handler)(pid_t );struct child_to_clean *next; };static struct child_to_clean *children_to_clean;run-command.c static void cleanup_children(int sig, int in_signal) · line 35
Quoted 5 lines
run-command.c +2
-0@@ -30,6 +31,8 @@ static void cleanup_children(int sig, int in_signal) {while (children_to_clean) {struct child_to_clean *p = children_to_clean; +if (p->clean_on_exit_handler) + p->clean_on_exit_handler (p->pid);This summons demons.
cleanup_children()is invoked from a signal handler. In this case, it can call only async-signal-safe functions. It does not look like the handler that you are going to install later will take note of this caveat!
Quoted 5 lines
run-command.c static void cleanup_children(int sig, int in_signal) · line 38
Quoted 3 lines
run-command.c +0
-0children_to_clean = p->next;kill (p->pid, sig);if (!in_signal)The condition that we see here in the context protects free(p) (which is not async-signal-safe). Perhaps the invocation of the new callback should be skipped in the same manner when this is called from a signal handler?
507d7804(pager: don't use unsafe functions in signal handlers) may be worth a look.
Thanks a lot of pointing this out to me!
Do I get it right that after the signal "SIGTERM" I can do a cleanup and don't need to worry about any function calls but if I get any other signal then I can only perform async-signal-safe calls?
If this is correct, then the following solution would work great:
if (!in_signal && p->clean_on_exit_handler) p->clean_on_exit_handler(p->pid);
Thanks,
Lars
Re: [PATCH v3 06/10] run-command: add clean_on_exit_handler
Am 01.08.2016 um 13:14 schrieb Lars Schneider:
Quoted 25 lines
Quoted 19 lines · Johannes Sixt
Am 30.07.2016 um 01:37 schrieb [email protected]:
run-command.c static void cleanup_children(int sig, int in_signal) · line 35
Quoted 7 lines
static struct
child_to_clean*children_to_clean;run-command.c +2
-0@@ -30,6 +31,8 @@ static void cleanup_children(int sig, int in_signal) {while (children_to_clean) {struct child_to_clean *p = children_to_clean; +if (p->clean_on_exit_handler) + p->clean_on_exit_handler (p->pid);This summons demons.
cleanup_children()is invoked from a signal handler. In this case, it can call only async-signal-safe functions. It does not look like the handler that you are going to install later will take note of this caveat!run-command.c static void cleanup_children(int sig, int in_signal) · line 38
Quoted 3 lines
run-command.c +0
-0children_to_clean = p->next;kill (p->pid, sig);if (!in_signal)Quoted 5 lines
The condition that we see here in the context protects free(p) (which is not async-signal-safe). Perhaps the invocation of the new callback should be skipped in the same manner when this is called from a signal handler?
507d7804(pager: don't use unsafe functions in signal handlers) may be worth a look.
Quoted 5 lines
Thanks a lot of pointing this out to me!
Do I get it right that after the signal "SIGTERM" I can do a cleanup and don't need to worry about any function calls but if I get any other signal then I can only perform async-signal-safe calls?
No. SIGTERM is not special.
Perhaps you were misled by the SIGTERM mentioned in cleanup_children_on_exit()? This function is invoked on regular exit (not from a signal). SIGTERM is used in this case to terminate children that are still lingering around.
Quoted 4 lines
If this is correct, then the following solution would work great:
if (!in_signal && p->clean_on_exit_handler) p->clean_on_exit_handler(p->pid);
This should work nevertheless because in_signal is set when the function is invoked from a signal handler (of any signal that is caught) via cleanup_children_on_signal().
-- Hannes
Re: [PATCH v3 06/10] run-command: add clean_on_exit_handler
Quoted 31 lines · Johannes Sixt
Am 01.08.2016 um 13:14 schrieb Lars Schneider:
Quoted 27 lines
Quoted 19 lines · Johannes Sixt
Am 30.07.2016 um 01:37 schrieb [email protected]:
run-command.c static void cleanup_children(int sig, int in_signal) · line 35
Quoted 7 lines
static struct
child_to_clean*children_to_clean;run-command.c +2
-0@@ -30,6 +31,8 @@ static void cleanup_children(int sig, int in_signal) {while (children_to_clean) {struct child_to_clean *p = children_to_clean; +if (p->clean_on_exit_handler) + p->clean_on_exit_handler (p->pid);This summons demons.
cleanup_children()is invoked from a signal handler. In this case, it can call only async-signal-safe functions. It does not look like the handler that you are going to install later will take note of this caveat!run-command.c static void cleanup_children(int sig, int in_signal) · line 38
Quoted 3 lines
run-command.c +0
-0children_to_clean = p->next;kill (p->pid, sig);if (!in_signal)Quoted 5 lines
The condition that we see here in the context protects free(p) (which is not async-signal-safe). Perhaps the invocation of the new callback should be skipped in the same manner when this is called from a signal handler?
507d7804(pager: don't use unsafe functions in signal handlers) may be worth a look.Thanks a lot of pointing this out to me!
Quoted 7 lines
Quoted 3 lines
Do I get it right that after the signal "SIGTERM" I can do a cleanup and don't need to worry about any function calls but if I get any other signal then I can only perform async-signal-safe calls?
No. SIGTERM is not special.
Perhaps you were misled by the SIGTERM mentioned in
cleanup_children_on_exit()? This function is invoked on regular exit (not from a signal). SIGTERM is used in this case to terminate children that are still lingering around.
Yes, that was my source of confusion. Thanks for the clarification!
Quoted 7 lines
Quoted 5 lines
If this is correct, then the following solution would work great:
if (!in_signal && p->clean_on_exit_handler) p->clean_on_exit_handler(p->pid);This should work nevertheless because
in_signalis set when the function is invoked from a signal handler (of any signal that is caught) viacleanup_children_on_signal().
Right. Thank you!
- Lars
PATCH v3 7/10 convert: quote filter names in error messages
In reply to [email protected]From: Lars Schneider <[email protected]>
Git filter driver commands with spaces (e.g. filter.sh foo) are hard to read in error messages. Quote them to improve the readability.
--- convert.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
convert.c +6 -6
@@ -397,7 +397,7 @@ static int filter_buffer_or_fd(int in, int out, void *data) child_process.out = out;if (start_command (&child_process)) -return error ("cannot fork to run external filter %s" , params->cmd); +return error ("cannot fork to run external filter '%s'" , params->cmd);sigchain_push (SIGPIPE ,SIG_IGN );
@@ -415,13 +415,13 @@ static int filter_buffer_or_fd(int in, int out, void *data)if (close (child_process.in)) write_err =1 ;if (write_err) -error ("cannot feed the input to external filter %s" , params->cmd); +error ("cannot feed the input to external filter '%s'" , params->cmd);sigchain_pop (SIGPIPE ); status =finish_command (&child_process);if (status) -error ("external filter %s failed %d" , params->cmd, status); +error ("external filter '%s' failed %d" , params->cmd, status);strbuf_release (&cmd);return (write_err || status);
@@ -462,15 +462,15 @@ static int apply_filter(const char *path, const char *src, size_t len, int fd,return 0 ;/* error was already reported */ if (strbuf_read (&nbuf, async.out, len) <0 ) { -error ("read from external filter %s failed" , cmd); +error ("read from external filter '%s' failed" , cmd); ret =0 ; }if (close (async.out)) { -error ("read from external filter %s failed" , cmd); +error ("read from external filter '%s' failed" , cmd); ret =0 ; }if (finish_async (&async)) { -error ("external filter %s failed" , cmd); +error ("external filter '%s' failed" , cmd); ret =0 ; }
Signature · 1 line
2.9.0
PATCH v3 8/10 convert: modernize tests
In reply to [email protected]From: Lars Schneider <[email protected]>
Use test_config to set the config, check that files are empty with test_must_be_empty, compare files with test_cmp, and remove spaces after ">" and "<".
--- t/t0021-conversion.sh | 62 +++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 31 deletions(-)
t/t0021-conversion.sh +31 -31
@@ -13,8 +13,8 @@ EOF chmod +x rot13.sh test_expect_success setup ' - git config filter.rot13.smudge ./rot13.sh && - git config filter.rot13.clean ./rot13.sh && + test_config filter.rot13.smudge ./rot13.sh && + test_config filter.rot13.clean ./rot13.sh && { echo "*.t filter=rot13"
@@ -38,8 +38,8 @@ script='s/^\$Id: \([0-9a-f]*\) \$/\1/p' test_expect_success check ' - cmp test.o test && - cmp test.o test.t && + test_cmp test.o test && + test_cmp test.o test.t && # ident should be stripped in the repository git diff --raw --exit-code :test :test.i &&
@@ -47,10 +47,10 @@ test_expect_success check ' embedded=$(sed -ne "$script" test.i) && test "z$id" = "z$embedded" && - git cat-file blob :test.t > test.r && + git cat-file blob :test.t >test.r && - ./rot13.sh < test.o > test.t && - cmp test.r test.t + ./rot13.sh <test.o >test.t && + test_cmp test.r test.t ' # If an expanded ident ever gets into the repository, we want to make sure that
@@ -130,7 +130,7 @@ test_expect_success 'filter shell-escaped filenames' ' # delete the files and check them out again, using a smudge filter # that will count the args and echo the command-line back to us - git config filter.argc.smudge "sh ./argc.sh %f" && + test_config filter.argc.smudge "sh ./argc.sh %f" && rm "$normal" "$special" && git checkout -- "$normal" "$special" &&
@@ -141,7 +141,7 @@ test_expect_success 'filter shell-escaped filenames' ' test_cmp expect "$special" && # do the same thing, but with more args in the filter expression - git config filter.argc.smudge "sh ./argc.sh %f --my-extra-arg" && + test_config filter.argc.smudge "sh ./argc.sh %f --my-extra-arg" && rm "$normal" "$special" && git checkout -- "$normal" "$special" &&
@@ -154,9 +154,9 @@ test_expect_success 'filter shell-escaped filenames' ' ' test_expect_success 'required filter should filter data' ' - git config filter.required.smudge ./rot13.sh && - git config filter.required.clean ./rot13.sh && - git config filter.required.required true && + test_config filter.required.smudge ./rot13.sh && + test_config filter.required.clean ./rot13.sh && + test_config filter.required.required true && echo "*.r filter=required" >.gitattributes &&
@@ -165,17 +165,17 @@ test_expect_success 'required filter should filter data' ' rm -f test.r && git checkout -- test.r && - cmp test.o test.r && + test_cmp test.o test.r && ./rot13.sh <test.o >expected && git cat-file blob :test.r >actual && - cmp expected actual + test_cmp expected actual ' test_expect_success 'required filter smudge failure' ' - git config filter.failsmudge.smudge false && - git config filter.failsmudge.clean cat && - git config filter.failsmudge.required true && + test_config filter.failsmudge.smudge false && + test_config filter.failsmudge.clean cat && + test_config filter.failsmudge.required true && echo "*.fs filter=failsmudge" >.gitattributes &&
@@ -186,9 +186,9 @@ test_expect_success 'required filter smudge failure' ' ' test_expect_success 'required filter clean failure' ' - git config filter.failclean.smudge cat && - git config filter.failclean.clean false && - git config filter.failclean.required true && + test_config filter.failclean.smudge cat && + test_config filter.failclean.clean false && + test_config filter.failclean.required true && echo "*.fc filter=failclean" >.gitattributes &&
@@ -197,8 +197,8 @@ test_expect_success 'required filter clean failure' ' ' test_expect_success 'filtering large input to small output should use little memory' ' - git config filter.devnull.clean "cat >/dev/null" && - git config filter.devnull.required true && + test_config filter.devnull.clean "cat >/dev/null" && + test_config filter.devnull.required true && for i in $(test_seq 1 30); do printf "%1048576d" 1; done >30MB && echo "30MB filter=devnull" >.gitattributes && GIT_MMAP_LIMIT=1m GIT_ALLOC_LIMIT=1m git add 30MB
@@ -207,7 +207,7 @@ test_expect_success 'filtering large input to small output should use little mem test_expect_success 'filter that does not read is fine' ' test-genrandom foo $((128 * 1024 + 1)) >big && echo "big filter=epipe" >.gitattributes && - git config filter.epipe.clean "echo xyzzy" && + test_config filter.epipe.clean "echo xyzzy" && git add big && git cat-file blob :big >actual && echo xyzzy >expect &&
@@ -215,20 +215,20 @@ test_expect_success 'filter that does not read is fine' ' ' test_expect_success EXPENSIVE 'filter large file' ' - git config filter.largefile.smudge cat && - git config filter.largefile.clean cat && + test_config filter.largefile.smudge cat && + test_config filter.largefile.clean cat && for i in $(test_seq 1 2048); do printf "%1048576d" 1; done >2GB && echo "2GB filter=largefile" >.gitattributes && git add 2GB 2>err && - ! test -s err && + test_must_be_empty err && rm -f 2GB && git checkout -- 2GB 2>err && - ! test -s err + test_must_be_empty err ' test_expect_success "filter: clean empty file" ' - git config filter.in-repo-header.clean "echo cleaned && cat" && - git config filter.in-repo-header.smudge "sed 1d" && + test_config filter.in-repo-header.clean "echo cleaned && cat" && + test_config filter.in-repo-header.smudge "sed 1d" && echo "empty-in-worktree filter=in-repo-header" >>.gitattributes && >empty-in-worktree &&
@@ -240,8 +240,8 @@ test_expect_success "filter: clean empty file" ' ' test_expect_success "filter: smudge empty file" ' - git config filter.empty-in-repo.clean "cat >/dev/null" && - git config filter.empty-in-repo.smudge "echo smudged && cat" && + test_config filter.empty-in-repo.clean "cat >/dev/null" && + test_config filter.empty-in-repo.smudge "echo smudged && cat" && echo "empty-in-repo filter=empty-in-repo" >>.gitattributes && echo dead data walking >empty-in-repo &&
Signature · 1 line
2.9.0
PATCH v3 9/10 convert: generate large test files only once
In reply to [email protected]From: Lars Schneider <[email protected]>
Generate more interesting large test files with pseudo random characters in between and reuse these test files in multiple tests. Run tests formerly marked as EXPENSIVE every time but with a smaller data set.
--- t/t0021-conversion.sh | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-)
t/t0021-conversion.sh +38 -10
@@ -4,6 +4,15 @@ test_description='blob conversion via gitattributes' . ./test-lib.sh +if test_have_prereq EXPENSIVE +then + T0021_LARGE_FILE_SIZE=2048 + T0021_LARGISH_FILE_SIZE=100 +else + T0021_LARGE_FILE_SIZE=30 + T0021_LARGISH_FILE_SIZE=2 +fi + cat <<EOF >rot13.sh #!$SHELL_PATH tr \
@@ -31,7 +40,26 @@ test_expect_success setup ' cat test >test.i && git add test test.t test.i && rm -f test test.t test.i && - git checkout -- test test.t test.i + git checkout -- test test.t test.i && + + mkdir generated-test-data && + for i in $(test_seq 1 $T0021_LARGE_FILE_SIZE) + do + RANDOM_STRING="$(test-genrandom end $i | tr -dc "A-Za-z0-9" )" + ROT_RANDOM_STRING="$(echo $RANDOM_STRING | ./rot13.sh )" + # Generate 1MB of empty data and 100 bytes of random characters + # printf "$(test-genrandom start $i)" + printf "%1048576d" 1 >>generated-test-data/large.file && + printf "$RANDOM_STRING" >>generated-test-data/large.file && + printf "%1048576d" 1 >>generated-test-data/large.file.rot13 && + printf "$ROT_RANDOM_STRING" >>generated-test-data/large.file.rot13 && + + if test $i = $T0021_LARGISH_FILE_SIZE + then + cat generated-test-data/large.file >generated-test-data/largish.file && + cat generated-test-data/large.file.rot13 >generated-test-data/largish.file.rot13 + fi + done ' script='s/^\$Id: \([0-9a-f]*\) \$/\1/p'
@@ -199,9 +227,9 @@ test_expect_success 'required filter clean failure' ' test_expect_success 'filtering large input to small output should use little memory' ' test_config filter.devnull.clean "cat >/dev/null" && test_config filter.devnull.required true && - for i in $(test_seq 1 30); do printf "%1048576d" 1; done >30MB && - echo "30MB filter=devnull" >.gitattributes && - GIT_MMAP_LIMIT=1m GIT_ALLOC_LIMIT=1m git add 30MB + cp generated-test-data/large.file large.file && + echo "large.file filter=devnull" >.gitattributes && + GIT_MMAP_LIMIT=1m GIT_ALLOC_LIMIT=1m git add large.file ' test_expect_success 'filter that does not read is fine' '
@@ -214,15 +242,15 @@ test_expect_success 'filter that does not read is fine' ' test_cmp expect actual ' -test_expect_success EXPENSIVE 'filter large file' ' +test_expect_success 'filter large file' ' test_config filter.largefile.smudge cat && test_config filter.largefile.clean cat && - for i in $(test_seq 1 2048); do printf "%1048576d" 1; done >2GB && - echo "2GB filter=largefile" >.gitattributes && - git add 2GB 2>err && + echo "large.file filter=largefile" >.gitattributes && + cp generated-test-data/large.file large.file && + git add large.file 2>err && test_must_be_empty err && - rm -f 2GB && - git checkout -- 2GB 2>err && + rm -f large.file && + git checkout -- large.file 2>err && test_must_be_empty err '
Signature · 1 line
2.9.0
PATCH v3 10/10 convert: add filter.<driver>.process option
In reply to [email protected]From: Lars Schneider <[email protected]>
Git's clean/smudge mechanism invokes an external filter process for every single blob that is affected by a filter. If Git filters a lot of blobs then the startup time of the external filter processes can become a significant part of the overall Git execution time.
This patch adds the filter.<driver>.process string option which, if used, keeps the external filter process running and processes all blobs with the following packet format (pkt-line) based protocol over standard input and standard output.
Git starts the filter on first usage and expects a welcome message, protocol version number, and filter capabilities separated by spaces:
------------------------
packet: git< git-filter-protocol\n packet: git< version 2\n packet: git< capabilities clean smudge\n
------------------------
Supported filter capabilities are "clean", "smudge", "stream", and "shutdown".
Afterwards Git sends a command (based on the supported capabilities), the filename including its path
relative to the repository root, the content size as ASCII number in bytes, the content split in zero or many pkt-line packets, and a flush packet at the end:
------------------------
packet: git> smudge\n packet: git> filename=path/testfile.dat\n packet: git> size=7\n packet: git> CONTENT packet: git> 0000
------------------------
The filter is expected to respond with the result content size as ASCII number in bytes. If the capability "stream" is defined then the filter must not send the content size. Afterwards the result content in send in zero or many pkt-line packets and a flush packet at the end. Finally a "success" packet is send to indicate that everything went well.
------------------------
packet: git< size=57\n (omitted with capability "stream") packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< success\n
------------------------
In case the filter cannot process the content, it is expected to respond with the result content size 0 (only if "stream" is not defined) and a "reject" packet.
------------------------
packet: git< size=0\n (omitted with capability "stream") packet: git< reject\n
------------------------
After the filter has processed a blob it is expected to wait for the next command. A demo implementation can be found in t/t0021/rot13-filter.pl located in the Git core repository.
If the filter supports the "shutdown" capability then Git will send the "shutdown" command and wait until the filter answers with "done". This gives the filter the opportunity to perform cleanup tasks. Afterwards the filter is expected to exit.
------------------------
packet: git> shutdown\n packet: git< done\n
------------------------
If a filter.<driver>.clean or filter.<driver>.smudge command is configured then these commands always take precedence over a configured filter.<driver>.process command.
Please note that you cannot use an existing filter.<driver>.clean or filter.<driver>.smudge command as filter.<driver>.process command. As soon as Git would detect a file that needs to be processed by this filter, it would stop responding.
--- Documentation/gitattributes.txt | 84 ++++++++- convert.c | 400 +++++++++++++++++++++++++++++++++++++-- t/t0021-conversion.sh | 405 ++++++++++++++++++++++++++++++++++++++++ t/t0021/rot13-filter.pl | 177 ++++++++++++++++++ 4 files changed, 1053 insertions(+), 13 deletions(-) create mode 100755 t/t0021/rot13-filter.pl
Documentation/gitattributes.txt +83 -1
@@ -300,7 +300,11 @@ checkout, when the `smudge` command is specified, the command is fed the blob object from its standard input, and its standard output is used to update the worktree file. Similarly, the `clean` command is used to convert the contents of worktree file -upon checkin. +upon checkin. By default these commands process only a single +blob and terminate. If a long running filter process (see section +below) is used then Git can process all blobs with a single filter +invocation for the entire life of a single Git command (e.g. +`git add .`). One use of the content filtering is to massage the content into a shape that is more convenient for the platform, filesystem, and the user to use.
@@ -375,6 +379,84 @@ substitution. For example: ------------------------ +Long Running Filter Process +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +If the filter command (string value) is defined via +filter.<driver>.process then Git can process all blobs with a +single filter invocation for the entire life of a single Git +command. This is achieved by using the following packet +format (pkt-line, see protocol-common.txt) based protocol over +standard input and standard output. + +Git starts the filter on first usage and expects a welcome +message, protocol version number, and filter capabilities +separated by spaces: +------------------------ +packet: git< git-filter-protocol\n +packet: git< version 2\n +packet: git< capabilities clean smudge\n +------------------------ +Supported filter capabilities are "clean", "smudge", "stream", +and "shutdown". + +Afterwards Git sends a command (based on the supported +capabilities), the filename including its path +relative to the repository root, the content size as ASCII number +in bytes, the content split in zero or many pkt-line packets, +and a flush packet at the end: +------------------------ +packet: git> smudge\n +packet: git> filename=path/testfile.dat\n +packet: git> size=7\n +packet: git> CONTENT +packet: git> 0000 +------------------------ + +The filter is expected to respond with the result content size as +ASCII number in bytes. If the capability "stream" is defined then +the filter must not send the content size. Afterwards the result +content in send in zero or many pkt-line packets and a flush packet +at the end. Finally a "success" packet is send to indicate that +everything went well. +------------------------ +packet: git< size=57\n (omitted with capability "stream") +packet: git< SMUDGED_CONTENT +packet: git< 0000 +packet: git< success\n +------------------------ + +In case the filter cannot process the content, it is expected +to respond with the result content size 0 (only if "stream" is +not defined) and a "reject" packet. +------------------------ +packet: git< size=0\n (omitted with capability "stream") +packet: git< reject\n +------------------------ + +After the filter has processed a blob it is expected to wait for +the next command. A demo implementation can be found in +`t/t0021/rot13-filter.pl` located in the Git core repository. + +If the filter supports the "shutdown" capability then Git will +send the "shutdown" command and wait until the filter answers +with "done". This gives the filter the opportunity to perform +cleanup tasks. Afterwards the filter is expected to exit. +------------------------ +packet: git> shutdown\n +packet: git< done\n +------------------------ + +If a filter.<driver>.clean or filter.<driver>.smudge command +is configured then these commands always take precedence over +a configured filter.<driver>.process command. + +Please note that you cannot use an existing filter.<driver>.clean +or filter.<driver>.smudge command as filter.<driver>.process +command. As soon as Git would detect a file that needs to be +processed by this filter, it would stop responding. + + Interaction between checkin/checkout attributes ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
convert.c +388 -12
483 lines not on this page · raw message
t/t0021-conversion.sh +405 -0
409 lines not on this page · raw message
t/t0021/rot13-filter.pl new file +177 -0
177 lines not on this page · raw message
Signature · 1 line
2.9.0
Re: [PATCH v3 10/10] convert: add filter.<driver>.process option
W dniu 30.07.2016 o 01:38, [email protected] pisze:
Quoted 11 lines
From: Lars Schneider <[email protected]>
Git's clean/smudge mechanism invokes an external filter process for every single blob that is affected by a filter. If Git filters a lot of blobs then the startup time of the external filter processes can become a significant part of the overall Git execution time.
This patch adds the filter.<driver>.process string option which, if used, keeps the external filter process running and processes all blobs with the following packet format (pkt-line) based protocol over standard input and standard output.
I think it would be nice to have here at least summary of the benchmarks you did in https://github.com/github/git-lfs/pull/1382
Quoted 8 lines
Git starts the filter on first usage and expects a welcome message, protocol version number, and filter capabilities separated by spaces:
------------------------packet: git< git-filter-protocol\n packet: git< version 2\n packet: git< capabilities clean smudge\n
Sorry for going back and forth, but now I think that 'capabilities' are not really needed here, though they are in line with "version" in the second packet / line, namely "version 2". If it does not make parsing more difficult...
Quoted 3 lines
------------------------
Supported filter capabilities are "clean", "smudge", "stream", and "shutdown".
I'd rather put "stream" and "shutdown" capabilities into separate patches, for easier review.
Quoted 6 lines
Afterwards Git sends a command (based on the supported capabilities), the filename including its path
relative to the repository root, the content size as ASCII number in bytes, the content split in zero or many pkt-line packets, and a flush packet at the end:
I guess the following is the most basic example, with mode detailed description left for the documentation.
Quoted 4 lines
------------------------
packet: git> smudge\n packet: git> filename=path/testfile.dat\n packet: git> size=7\n
So I see you went with "<variable>=<value>" idea, rather than "<value>" (with <variable> defined by position in a sequence of 'header' packets), or "<variable> <value>..." that introductory header uses.
Quoted 9 lines
packet: git> CONTENT packet: git> 0000------------------------
The filter is expected to respond with the result content size as ASCII number in bytes. If the capability "stream" is defined then the filter must not send the content size. Afterwards the result content in send in zero or many pkt-line packets and a flush packet at the end.
If it does not cost filter anything, it could send size upfront (based on size of original, or based on external data), even if it is prepared for streaming.
In the opposite case, where filter cannot stream because it requires whole contents upfront (e.g. to calculate hash of the contents, or to do operation that needs whole file like sorting or reversing lines), it should always be able to calculate the size... or not. For example 'sort | uniq' filter needs whole input upfront for sort, but it does not know how many lines will be in output without doing the 'uniq' part.
So I think the ability of filter to provide size (or size hint) of its output should be decoupled from streaming support.
Quoted 2 lines
Finally a "success" packet is send to indicate that
everything went well.
That's a nice addition, and probably a necessary one, to the stream protocol. Git must know and consume it - we wouldn't be able to retrofit it later.
Quoted 2 lines
------------------------
packet: git< size=57\n (omitted with capability "stream")
I was thinking about having possible responses to receiving file contents (or starting receiving in the streaming case) to be:
packet: git< ok size=7\n (or "ok 7\n", if size is known)
or
packet: git< ok\n (if filter does not know size upfront)
or
packet: git< fail <msg>\n (or just "fail" + packet with msg)
The last would be when filter knows upfront that it cannot perform the operation. Though sending an empty file with non-"success" final would work as well.
For example LFS filter (that is configured as not required) may refuse to store files which are smaller than some pre-defined constant threshold.
Quoted 5 lines
packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< success\n------------------------
Quoted 7 lines
In case the filter cannot process the content, it is expected to respond with the result content size 0 (only if "stream" is not defined) and a "reject" packet.
------------------------packet: git< size=0\n (omitted with capability "stream") packet: git< reject\n------------------------
This is *wrong* idea! Empty file, with size=0, can be a perfectly legitimate response.
For example rot13 filter should respond to an empty file on input with an empty file on output. LFS-like filters and encryption mechanism should return empty file on fetch / decryption if such empty file was stored / encrypted.
A strange LFS could even use filenames (with files being empty themselves) as a lookup key for artifactory. For example a kind of CDN for common libraries, with version embedded in filename, like 'libs/jquery-1.9.0.min.js', etc.
Quoted 4 lines
After the filter has processed a blob it is expected to wait for the next command. A demo implementation can be found in
t/t0021/rot13-filter.pllocated in the Git core repository.
If filter does not support "shutdown" capability (or if said capability is postponed for later patch), it should behave sanely when Git command reaps it (SIGTERM + wait + SIGKILL?, SIGCHLD?).
Quoted 9 lines
If the filter supports the "shutdown" capability then Git will send the "shutdown" command and wait until the filter answers with "done". This gives the filter the opportunity to perform cleanup tasks. Afterwards the filter is expected to exit.
------------------------packet: git> shutdown\n packet: git< done\n------------------------
I guess there is no timeout mechanism: if filter hangs on shutdown, then git command would also hang waiting for signal to exit.
Quoted 4 lines
If a filter.<driver>.clean or filter.<driver>.smudge command is configured then these commands always take precedence over a configured filter.<driver>.process command.
Note: the value of clean, smudge and process is a command, not just a string.
I wonder if it would be worth it to explain the reasoning behind this solution and show alternate ones.
* Using a separate variable to signal that filters are invoked per-command rather than per-file, and use pkt-line interface, like boolean-valued useProtocol, or protocolVersion set to '2' or 'v2', or persistence set to 'per-command', there is high risk of user's trying to use exiting one-shot per-file filters... and Git hanging.
* Using new variables for each capability, e.g. processSmudge and processClean would lead to explosion of variable names; I think.
* Current solution of using process in addition to clean and smudge clearly says that you need to use different command for per-file (clean and smudge), and per-command filter, while allowing to use them together.
The possible disadvantage is Git command starting process filter, only to see that it doesn't offer required capability, for example offering only "clean" but not "smudge". There is simple workaround - set smudge variable (same as not present capability) to empty string.
Quoted 5 lines
Please note that you cannot use an existing filter.<driver>.clean or filter.<driver>.smudge command as filter.<driver>.process command. As soon as Git would detect a file that needs to be processed by this filter, it would stop responding.
I think this needs to be in the documentation (I have not checked yet if it is), but is not needed in the already long commit message.
Quoted 16 lines
Signed-off-by Lars Schneider <[email protected]>Helped-by Martin-Louis Bright <[email protected]>--- Documentation/gitattributes.txt | 84 ++++++++- convert.c | 400 +++++++++++++++++++++++++++++++++++++-- t/t0021-conversion.sh | 405 ++++++++++++++++++++++++++++++++++++++++ t/t0021/rot13-filter.pl | 177 ++++++++++++++++++ 4 files changed, 1053 insertions(+), 13 deletions(-) create mode 100755 t/t0021/rot13-filter.plDocumentation/gitattributes.txt +0
-0
Documentation/gitattributes.txt checkout, when the `smudge` command is specified, the command is · line 307
Quoted 9 lines
Documentation/gitattributes.txt +5
-1@@ -300,7 +300,11 @@ checkout, when the `smudge` command is specified, the command is fed the blob object from its standard input, and its standard output is used to update the worktree file. Similarly, the `clean` command is used to convert the contents of worktree file -upon checkin. +upon checkin. By default these commands process only a single +blob and terminate. If a long running filter process (see section +below) is used then Git can process all blobs with a single filter +invocation for the entire life of a single Git command (e.g. +`git add .`).
Proposed improvement:
If a long running process filter is used
in place of clean and/or smudge filters, then Git can process all blobs with a single filter command invocation for the entire life of a single Git command, for example git add --all. See section below for the description of the protocol used to communicate with a process filter.
Documentation/gitattributes.txt substitution. For example: · line 385
Quoted 11 lines
Documentation/gitattributes.txt +4
-0One use of the content filtering is to massage the content into a shape that is more convenient for the platform, filesystem, and the user to use.@@ -375,6 +379,84 @@ substitution. For example: ------------------------ +Long Running Filter Process +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +If the filter command (string value) is defined via
This is no mere string value, this is command invocation (with its own rules, e.g. splitting parameters on whitespace, etc.). Though I'm not sure how to say it succintly. Maybe skip "(string value)"? But it is there for a reason...
Documentation/gitattributes.txt substitution. For example: · line 386
Quoted 1 line
Documentation/gitattributes.txt +1
-0+filter.<driver>.process then Git can process all blobs with a
Shouldn't it be filter.<driver>.process?
Documentation/gitattributes.txt substitution. For example: · line 389
Quoted 3 lines
Documentation/gitattributes.txt +3
-0+single filter invocation for the entire life of a single Git +command. This is achieved by using the following packet +format (pkt-line, see protocol-common.txt) based protocol over
Can we linkgit-it (to technical documentation)?
Documentation/gitattributes.txt substitution. For example: · line 392
Quoted 3 lines
Documentation/gitattributes.txt +3
-0+standard input and standard output. + +Git starts the filter on first usage and expects a welcome
Is "usage" here correct? Perhaps it would be more readable to say that Git starts filter when encountering first file that needs cleaning or smudgeing.
Documentation/gitattributes.txt substitution. For example: · line 401
Quoted 9 lines
Documentation/gitattributes.txt +9
-0+message, protocol version number, and filter capabilities +separated by spaces: +------------------------ +packet: git< git-filter-protocol\n +packet: git< version 2\n +packet: git< capabilities clean smudge\n +------------------------ +Supported filter capabilities are "clean", "smudge", "stream", +and "shutdown".
Filter should include at least one of "clean" and "smudge" capabilities (currently), otherwise it wouldn't do anything.
I don't know if it is a good place to say that because of pkt-line recommendations about text-content packets, each of those should terminate in endline, with "\n" included in pkt-line length.
Documentation/gitattributes.txt substitution. For example:
Quoted 3 lines
Documentation/gitattributes.txt +3
-0+ +Afterwards Git sends a command (based on the supported +capabilities),
I think it should be something like the following:
If among filter process capabilities there is capability that corresponds to the operation performed by a Git command (that is, either "clean" or "smudge"), then Git would send, in separate packets, a command (based on supported capabilites),
though it feels too "chatty" (and the sentence gets quite long).
Documentation/gitattributes.txt substitution. For example: · line 406
Quoted 2 lines
Documentation/gitattributes.txt +1
-0the filename including its path +relative to the repository root,
Errr... "the filename including its path"? Wouldn't be it simpler to just say:
the pathname of a file relative to the repository root,
Also, isn't it now "filename=<pathname>\n"?
Documentation/gitattributes.txt substitution. For example: · line 408
Quoted 2 lines
Documentation/gitattributes.txt +1
-0the content size as ASCII number +in bytes,
Could Git not give the size, for example if fstat() fails? Do we reserve space for other information here?
Also, isn't it now "size=<bytes>\n"?
Documentation/gitattributes.txt substitution. For example: · line 409
Quoted 1 line
Documentation/gitattributes.txt +0
-0the content split in zero or many pkt-line packets,
s/zero or many/zero or more/
Documentation/gitattributes.txt substitution. For example: · line 407
Quoted 1 line
Documentation/gitattributes.txt +1
-0+and a flush packet at the end:
I wonder if instead of long sentence, it would be more readable to use enumeration (ordered list) or itemize (unordered list).
Documentation/gitattributes.txt substitution. For example:
Quoted 11 lines
Documentation/gitattributes.txt +11
-0+------------------------ +packet: git> smudge\n +packet: git> filename=path/testfile.dat\n +packet: git> size=7\n +packet: git> CONTENT +packet: git> 0000 +------------------------ + +The filter is expected to respond with the result content size as +ASCII number in bytes. If the capability "stream" is defined then +the filter must not send the content size.
As I wrote earlier, I think sending or not the size of the output should be decoupled from the "stream" capability.
Streaming is IMVHO rather a capability of starting to send parts of response before the whole contents of input arrives. I think per-file filters support that and that's what start_async() there is about.
Documentation/gitattributes.txt substitution. For example: · line 421
Quoted 4 lines
Documentation/gitattributes.txt +3
-0Afterwards the result +content in send in zero or many pkt-line packets and a flush packet +at the end. Finally a "success" packet is send to indicate that +everything went well.
I guess it is "success" packet if everything went well, and place for informing about errors in the future - filter is assumed to die if there are errors in filtering, isn't it?
That is, not "send to indicate", but "send if".
Quoted 4 lines
+------------------------
Documentation/gitattributes.txt +3
-0+packet: git< size=57\n (omitted with capability "stream") +packet: git< SMUDGED_CONTENT +packet: git< 0000
Documentation/gitattributes.txt substitution. For example: · line 435
Quoted 10 lines
Documentation/gitattributes.txt +10
-0+packet: git< success\n +------------------------ + +In case the filter cannot process the content, it is expected +to respond with the result content size 0 (only if "stream" is +not defined) and a "reject" packet. +------------------------ +packet: git< size=0\n (omitted with capability "stream") +packet: git< reject\n +------------------------
I would assume that we have two error conditions.
First situation is when the filter knows upfront (after receiving name and size of file, and after receiving contents for not-streaming filters) that it cannot process the file (like e.g. LFS filter with artifactory replica/shard being a bit behind master, and not including contents of the file being filtered).
My proposal is to reply with "fail" _in place of_ size of reply:
packet: git< fail\n (any case: size known or not, stream or not)
It could be "reject", or "error" instead of "fail".
Another situation is if filter encounters error during output, either with streaming filter (or non-stream, but not storing whole input upfront) realizing in the middle of output that there is something wrong with input (e.g. converting between encoding, and encountering character that cannot be represented in output encoding), or e.g. filter process being killed, or network connection dropping with LFS filter, etc. The filter has send some packets with output already. In this case filter should flush, and send "reject" or "error" packet.
<error condition>
packet: git< "0000" (flush packet)
packet: git< reject\n
Should there be a place for an error message, or would standard error (stderr) be used for this?
Documentation/gitattributes.txt substitution. For example: · line 439
Quoted 4 lines
Documentation/gitattributes.txt +4
-0+ +After the filter has processed a blob it is expected to wait for +the next command. A demo implementation can be found in +`t/t0021/rot13-filter.pl` located in the Git core repository.
It is actually in Git sources. Is it the best way to refer to such files?
Documentation/gitattributes.txt substitution. For example: · line 452
Quoted 13 lines
Documentation/gitattributes.txt +13
-0+ +If the filter supports the "shutdown" capability then Git will +send the "shutdown" command and wait until the filter answers +with "done". This gives the filter the opportunity to perform +cleanup tasks. Afterwards the filter is expected to exit. +------------------------ +packet: git> shutdown\n +packet: git< done\n +------------------------ + +If a filter.<driver>.clean or filter.<driver>.smudge command +is configured then these commands always take precedence over +a configured filter.<driver>.process command.
All right; this is quite clear.
Documentation/gitattributes.txt substitution. For example: · line 457
Quoted 5 lines
Documentation/gitattributes.txt +5
-0+ +Please note that you cannot use an existing filter.<driver>.clean +or filter.<driver>.smudge command as filter.<driver>.process +command. As soon as Git would detect a file that needs to be +processed by this filter, it would stop responding.
This isn't.
P.S. I will comment about the implementation part in the next email.
Signature · 1 line
Jakub Narębski
Re: [PATCH v3 10/10] convert: add filter.<driver>.process option
[Excuse me replying to myself, but there are a few things I forgot,
or realized only later]
W dniu 31.07.2016 o 00:05, Jakub Narębski pisze:
Quoted 8 lines
W dniu 30.07.2016 o 01:38, [email protected] pisze:
Quoted 7 lines
From: Lars Schneider <[email protected]>
Git's clean/smudge mechanism invokes an external filter process for every single blob that is affected by a filter. If Git filters a lot of blobs then the startup time of the external filter processes can become a significant part of the overall Git execution time.
Quoted 7 lines
Quoted 4 lines
This patch adds the filter.<driver>.process string option which, if used, keeps the external filter process running and processes all blobs with the following packet format (pkt-line) based protocol over standard input and standard output.
I think it would be nice to have here at least summary of the benchmarks you did in https://github.com/github/git-lfs/pull/1382
Note that this feature is especially useful if startup time is long, that is if you are using an operating system with costly fork / new process startup time like MS Windows (which you have mentioned), or writing filter in a programming language with large startup time like Java or Python (the latter may have changed since).
https://gnustavo.wordpress.com/2012/06/28/programming-languages-start-up-times/
[...]
Quoted 16 lines
I was thinking about having possible responses to receiving file contents (or starting receiving in the streaming case) to be:
packet: git< ok size=7\n (or "ok 7\n", if size is known)
or
packet: git< ok\n (if filter does not know size upfront)
or
packet: git< fail <msg>\n (or just "fail" + packet with msg)
The last would be when filter knows upfront that it cannot perform the operation. Though sending an empty file with non-"success" final would work as well.
[...]
Quoted 10 lines
Quoted 7 lines
In case the filter cannot process the content, it is expected to respond with the result content size 0 (only if "stream" is not defined) and a "reject" packet.
------------------------packet: git< size=0\n (omitted with capability "stream") packet: git< reject\n------------------------
This is *wrong* idea! Empty file, with size=0, can be a perfectly legitimate response.
Actually, I think I have misunderstood your intent. If you want to have simpler protocol, with only one place to signal errors, that is after sending a response, then proper way of signaling the error condition would be to send empty file and then "reject" instead of "success":
packet: git< size=0\n (omitted with capability "stream") packet: git< 0000 (we need this flush packet) packet: git< reject\n
Otherwise in the case without size upfront (capability "stream") file with contents "reject" would be mistaken for the "reject" packet.
See below for proposal with two places to signal errors: before sending first byte, and after.
NOTE: there is a bit of mixed and possibly confusing notation, that is 0000 is flush packet, not packet with 0000 as content. Perhaps write pkt-line in full?
[...]
Quoted 7 lines
Quoted 7 lines
--- Documentation/gitattributes.txt | 84 ++++++++- convert.c | 400 +++++++++++++++++++++++++++++++++++++-- t/t0021-conversion.sh | 405 ++++++++++++++++++++++++++++++++++++++++ t/t0021/rot13-filter.pl | 177 ++++++++++++++++++ 4 files changed, 1053 insertions(+), 13 deletions(-) create mode 100755 t/t0021/rot13-filter.pl
Wouldn't it be better for easier review to split it into separate patches? Perhaps at least the new test...
[...]
Quoted 30 lines
I would assume that we have two error conditions.
First situation is when the filter knows upfront (after receiving name and size of file, and after receiving contents for not-streaming filters) that it cannot process the file (like e.g. LFS filter with artifactory replica/shard being a bit behind master, and not including contents of the file being filtered).
My proposal is to reply with "fail"
_inplaceof_size of reply:packet: git< fail\n (any case: size known or not, stream or not)
It could be "reject", or "error" instead of "fail".
Another situation is if filter encounters error during output, either with streaming filter (or non-stream, but not storing whole input upfront) realizing in the middle of output that there is something wrong with input (e.g. converting between encoding, and encountering character that cannot be represented in output encoding), or e.g. filter process being killed, or network connection dropping with LFS filter, etc. The filter has send some packets with output already. In this case filter should flush, and send "reject" or "error" packet.
<error condition>
packet: git< "0000" (flush packet)
packet: git< reject\nShould there be a place for an error message, or would standard error (stderr) be used for this?
Re: [PATCH v3 10/10] convert: add filter.<driver>.process option
Quoted 5 lines · Jakub Narębski
[Excuse me replying to myself, but there are a few things I forgot, or realized only later]
No worries :)
Quoted 18 lines
W dniu 31.07.2016 o 00:05, Jakub Narębski pisze:
Quoted 8 lines
W dniu 30.07.2016 o 01:38, [email protected] pisze:
Quoted 7 lines
From: Lars Schneider <[email protected]>
Git's clean/smudge mechanism invokes an external filter process for every single blob that is affected by a filter. If Git filters a lot of blobs then the startup time of the external filter processes can become a significant part of the overall Git execution time.
Quoted 7 lines
Quoted 4 lines
This patch adds the filter.<driver>.process string option which, if used, keeps the external filter process running and processes all blobs with the following packet format (pkt-line) based protocol over standard input and standard output.
I think it would be nice to have here at least summary of the benchmarks you did in https://github.com/github/git-lfs/pull/1382
Quoted 7 lines
Note that this feature is especially useful if startup time is long, that is if you are using an operating system with costly fork / new process startup time like MS Windows (which you have mentioned), or writing filter in a programming language with large startup time like Java or Python (the latter may have changed since).
https://gnustavo.wordpress.com/2012/06/28/programming-languages-start-up-times/
OK, I will add this. Is it OK to add the link to the commit message? (since I don't know how long the link will be available).
Quoted 40 lines
[...]
Quoted 16 lines
I was thinking about having possible responses to receiving file contents (or starting receiving in the streaming case) to be:
packet: git< ok size=7\n (or "ok 7\n", if size is known)
or
packet: git< ok\n (if filter does not know size upfront)
or
packet: git< fail <msg>\n (or just "fail" + packet with msg)
The last would be when filter knows upfront that it cannot perform the operation. Though sending an empty file with non-"success" final would work as well.
[...]
Quoted 10 lines
Quoted 7 lines
In case the filter cannot process the content, it is expected to respond with the result content size 0 (only if "stream" is not defined) and a "reject" packet.
------------------------packet: git< size=0\n (omitted with capability "stream") packet: git< reject\n------------------------
This is *wrong* idea! Empty file, with size=0, can be a perfectly legitimate response.
Actually, I think I have misunderstood your intent. If you want to have simpler protocol, with only one place to signal errors, that is after sending a response, then proper way of signaling the error condition would be to send empty file and then "reject" instead of "success":
packet: git< size=0\n (omitted with capability "stream") packet: git< 0000 (we need this flush packet) packet: git< reject\n
Quoted 5 lines
Otherwise in the case without size upfront (capability "stream") file with contents "reject" would be mistaken for the "reject" packet.
See below for proposal with two places to signal errors: before sending first byte, and after.
Right now the protocol is implemented covering the following cases:
## CASE 1 - no stream success
packet: git< size=57\n packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< success\n
## CASE 2 - no stream success but 0 byte response
packet: git< size=0\n packet: git< success\n
## CASE 3 - no stream filter; filter doesn't want to process the file
packet: git< size=0\n packet: git< reject\n
## CASE 4 - no stream filter; filter error
packet: git< size=57\n packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< error\n
CASE 4 is not explicitly checked. If a final message is neither "success" nor "reject" then it is interpreted as error. If that happens then Git will shutdown and restart the filter process if there is another file to filter.
Alternatively a filter process can shutdown itself, too, to signal an error.
The corresponding stream filter look like this:
## CASE 1 - stream success
packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< success\n
## CASE 2 - stream success but 0 byte response
packet: git< 0000 packet: git< success\n
## CASE 3 - stream filter; filter doesn't want to process the file
packet: git< 0000 packet: git< reject\n
## CASE 4 - stream filter; filter error
packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< error\n
--
I just realized that the size 0 case is a bit inconsistent in the no stream case as it has no flush packet. Maybe I should indeed remove the flush packet in the no stream case completely?!
Do the cases above make sense to you?
Regarding error handling. I would prefer it if the filter prints all errors to STDERR by itself. I think that is the safest option to communicate errors to the users because if the communication got into a bad state then Git might not be able to read the errors properly.
See Peff's response on the topic, too:
http://public-inbox.org/git/20160729165018.GA6553%40sigill.intra.peff.net/
Quoted 3 lines
NOTE: there is a bit of mixed and possibly confusing notation, that is 0000 is flush packet, not packet with 0000 as content. Perhaps write pkt-line in full?
I am not sure I understand what you mean (maybe it's too late for me...). Can you try to rephrase or give an example?
Thank you,
Lars
Quoted 46 lines
[...]
Quoted 7 lines
Quoted 7 lines
---
Documentation/gitattributes.txt | 84 ++++++++- convert.c | 400 +++++++++++++++++++++++++++++++++++++-- t/t0021-conversion.sh | 405 ++++++++++++++++++++++++++++++++++++++++ t/t0021/rot13-filter.pl | 177 ++++++++++++++++++ 4 files changed, 1053 insertions(+), 13 deletions(-)create mode 100755
t/t0021/rot13-filter.plWouldn't it be better for easier review to split it into separate patches? Perhaps at least the new test...
[...]
Quoted 30 lines
I would assume that we have two error conditions.
First situation is when the filter knows upfront (after receiving name and size of file, and after receiving contents for not-streaming filters) that it cannot process the file (like e.g. LFS filter with artifactory replica/shard being a bit behind master, and not including contents of the file being filtered).
My proposal is to reply with "fail"
_inplaceof_size of reply:packet: git< fail\n (any case: size known or not, stream or not)
It could be "reject", or "error" instead of "fail".
Another situation is if filter encounters error during output, either with streaming filter (or non-stream, but not storing whole input upfront) realizing in the middle of output that there is something wrong with input (e.g. converting between encoding, and encountering character that cannot be represented in output encoding), or e.g. filter process being killed, or network connection dropping with LFS filter, etc. The filter has send some packets with output already. In this case filter should flush, and send "reject" or "error" packet.
<error condition>
packet: git< "0000" (flush packet)
packet: git< reject\nShould there be a place for an error message, or would standard error (stderr) be used for this?
Re: [PATCH v3 10/10] convert: add filter.<driver>.process option
W dniu 31.07.2016 o 21:49, Lars Schneider pisze:
Quoted 3 lines · Jakub Narębski
Quoted 2 lines
W dniu 31.07.2016 o 00:05, Jakub Narębski pisze:
Quoted 1 line
W dniu 30.07.2016 o 01:38, [email protected] pisze:
[...]
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
I think it would be nice to have here at least summary of the benchmarks you did in https://github.com/github/git-lfs/pull/1382
This would be nice to have in the commit message: real benchmarks.
Quoted 11 lines
Quoted 8 lines
Note that this feature is especially useful if startup time is long, that is if you are using an operating system with costly fork / new process startup time like MS Windows (which you have mentioned), or writing filter in a programming language with large startup time like Java or Python (the latter may have changed since).
https://gnustavo.wordpress.com/2012/06/28/programming-languages-start-up-times/
OK, I will add this. Is it OK to add the link to the commit message? (since I don't know how long the link will be available).
I don't think it is needed. Perhaps only a sentence or half to notify where you could get most from this feature, but even then it is not necessary.
I'm sorry for the confusion.
Quoted 6 lines
Quoted 2 lines
See below for proposal with two places to signal errors: before sending first byte, and after.
Right now the protocol is implemented covering the following cases:
## CASE 1 - no stream success
It is less "stream", more "size unknown". Real streaming is interleaving reading and writing, which is currently not supported due to lack of start_async() - I think.
Quoted 5 lines
packet: git< size=57\n packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< success\n
Right. What happens if either length(SMUDGED_CONTENT) < size, or length(SMUDGED_CONTENT) > size? It could conceivably happen, e.g. due to an error in size calculation.
NOTE that without using flush packet to signal end of contents, we would be not able to signal a situation when filter encounters an error (per-file, or long temporary) when it have written some content already. For example this may happen for git-LFS filter, if the server hosting artifactory (or even whole network) gets down during cleanup / smudging.
Well, unless we would use other special packets:
- empty packet, that is "0004" pkt-line
- invalid packet, that is "0001", "0002", "0003" pkt-line
to signal premature end of SMUDGED_CONTENT.
Quoted 6 lines
## CASE 2 - no stream success but 0 byte response
packet: git< size=0\n packet: git< success\n
Why there is need to special case 0 byte (empty file) response?
packet: git< size=0\n packet: git< 0000 packet: git< success\n
is perfectly fine.
Quoted 4 lines
## CASE 3 - no stream filter; filter doesn't want to process the file
packet: git< size=0\n packet: git< reject\n
Why not simply
packet: git< reject\n
Or, if we are going success/reject/whatever route
packet: git< size=0\n packet: git< 0000 packet: git< reject\n
Quoted 11 lines
## CASE 4 - no stream filter; filter error
packet: git< size=57\n packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< error\nCASE 4 is not explicitly checked. If a final message is neither "success" nor "reject" then it is interpreted as error. If that happens then Git will shutdown and restart the filter process if there is another file to filter.
This should be documented.
Quoted 31 lines
Alternatively a filter process can shutdown itself, too, to signal an error.
The corresponding stream filter look like this:
## CASE 1 - stream success
packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< success\n## CASE 2 - stream success but 0 byte response
packet: git< 0000 packet: git< success\n## CASE 3 - stream filter; filter doesn't want to process the file
packet: git< 0000 packet: git< reject\n## CASE 4 - stream filter; filter error
packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< error\n
Quoted 6 lines
--
I just realized that the size 0 case is a bit inconsistent in the no stream case as it has no flush packet. Maybe I should indeed remove the flush packet in the no stream case completely?!
That's what I wrote about SPOT (single point of truth), of using either size or flush packet, but not both. But...
As I wrote, you need some mechanism to signal premature end of contents, and start of an error description.
Quoted 2 lines
Do the cases above make sense to you?
Except for the inconsistency of the size 0 case. This what I meant to say.
Quoted 9 lines
Regarding error handling. I would prefer it if the filter prints all errors to STDERR by itself. I think that is the safest option to communicate errors to the users because if the communication got into a bad state then Git might not be able to read the errors properly.
See Peff's response on the topic, too:
http://public-inbox.org/git/20160729165018.GA6553%40sigill.intra.peff.net/
Actually it looks like Peff is slightly against using stderr.
JK> Git-LFS sends to stderr because there's no other option. I wonder if it JK> would be nicer to make it Git's responsibility to talk to the user, JK> because then it could respect things like "--quiet". I guess error JK> messages are generally printed regardless of verbosity, though, so JK> printing them unconditionally is OK.
I think it should be O.K., and it makes writing filter drivers simpler if we don't have multiplex channels.
Quoted 6 lines
Quoted 3 lines
NOTE: there is a bit of mixed and possibly confusing notation, that is 0000 is flush packet, not packet with 0000 as content. Perhaps write pkt-line in full?
I am not sure I understand what you mean (maybe it's too late for me...). Can you try to rephrase or give an example?
Compare
packet: git< 0000
with
packet: git< success\n
The former as pkt-line is
git< 0000
the latter is
git< 000csuccess\n
^^^^
\-- packet header
Signature · 1 line
Jakub Narębski
Re: [PATCH v3 10/10] convert: add filter.<driver>.process option
In reply to Jakub NarębskiQuoted 11 lines · Jakub Narębski
W dniu 30.07.2016 o 01:38, [email protected] pisze:
Quoted 7 lines
From: Lars Schneider <[email protected]>
Git's clean/smudge mechanism invokes an external filter process for every single blob that is affected by a filter. If Git filters a lot of blobs then the startup time of the external filter processes can become a significant part of the overall Git execution time.
Quoted 7 lines
Quoted 4 lines
This patch adds the filter.<driver>.process string option which, if used, keeps the external filter process running and processes all blobs with the following packet format (pkt-line) based protocol over standard input and standard output.
I think it would be nice to have here at least summary of the benchmarks you did in https://github.com/github/git-lfs/pull/1382
OK.
Quoted 8 lines
Quoted 7 lines
Git starts the filter on first usage and expects a welcome message, protocol version number, and filter capabilities separated by spaces:
------------------------packet: git< git-filter-protocol\n packet: git< version 2\n packet: git< capabilities clean smudge\n
Quoted 4 lines
Sorry for going back and forth, but now I think that 'capabilities' are not really needed here, though they are in line with "version" in the second packet / line, namely "version 2". If it does not make parsing more difficult...
I don't understand what you mean with "they are not really needed"? The field is necessary to understand the protocol, no?
In the last roll I added the "key=value" format to the protocol upon yours and Peff's suggestion. Would it be OK to change the startup sequence accordingly?
packet: git< version=2\n packet: git< capabilities=clean smudge\n
Quoted 6 lines
Quoted 3 lines
------------------------
Supported filter capabilities are "clean", "smudge", "stream", and "shutdown".I'd rather put "stream" and "shutdown" capabilities into separate patches, for easier review.
I agree with "shutdown". I think I would like to remove the "stream" option and make it the default for the following reasons:
(1) As you mentioned elsewhere, "stream" is not really streaming at this point because we don't read/write in parallel.
(2) Junio and you pointed out that if we transmit size and flush packet then we have redundancy in the protocol.
(3) With the newly introduced "success"/"reject"/"failure" packet at the end of a filter operation, a filter process has a way to signal Git that something went wrong. Initially I had the idea that a filter process just stops writing and Git would detect the mismatch between expected bytes and received bytes. But the final status packet is a much clearer solution.
(4) Maintaining two slightly different protocols is a waste of resources and only increases the size of this (already large) patch.
My only argument for the size packet was that this allows efficient buffer allocation. However, in non of my benchmarks this was actually a problem. Therefore this is probably a epsilon optimization and should be removed.
OK with everyone?
Quoted 9 lines
Quoted 5 lines
Afterwards Git sends a command (based on the supported capabilities), the filename including its path
relative to the repository root, the content size as ASCII number in bytes, the content split in zero or many pkt-line packets, and a flush packet at the end:I guess the following is the most basic example, with mode detailed description left for the documentation.
Quoted 8 lines
Quoted 4 lines
------------------------
packet: git> smudge\n packet: git> filename=path/testfile.dat\n packet: git> size=7\nSo I see you went with "<variable>=<value>" idea, rather than "<value>" (with <variable> defined by position in a sequence of 'header' packets), or "<variable> <value>..." that introductory header uses.
The implementation still requires the exact sequence of the packets. However, we could make this more flexible in a later patch with the "key=value" formatting.
Quoted 23 lines
Quoted 10 lines
packet: git> CONTENT packet: git> 0000------------------------
The filter is expected to respond with the result content size as ASCII number in bytes. If the capability "stream" is defined then the filter must not send the content size. Afterwards the result content in send in zero or many pkt-line packets and a flush packet at the end.
If it does not cost filter anything, it could send size upfront (based on size of original, or based on external data), even if it is prepared for streaming.
In the opposite case, where filter cannot stream because it requires whole contents upfront (e.g. to calculate hash of the contents, or to do operation that needs whole file like sorting or reversing lines), it should always be able to calculate the size... or not. For example 'sort | uniq' filter needs whole input upfront for sort, but it does not know how many lines will be in output without doing the 'uniq' part.
Quoted 2 lines
So I think the ability of filter to provide size (or size hint) of its output should be decoupled from streaming support.
AS mentioned above, I would like to remove the size packet completely to simplify this patch. If there is really a need for such a packet then we could add it later (given the flexible "key=value" format of the protocol).
Quoted 21 lines
Quoted 2 lines
Finally a "success" packet is send to indicate that
everything went well.That's a nice addition, and probably a necessary one, to the stream protocol. Git must know and consume it - we wouldn't be able to retrofit it later.
Quoted 2 lines
------------------------
packet: git< size=57\n (omitted with capability "stream")I was thinking about having possible responses to receiving file contents (or starting receiving in the streaming case) to be:
packet: git< ok size=7\n (or "ok 7\n", if size is known)
or
packet: git< ok\n (if filter does not know size upfront)
or
Quoted 8 lines
packet: git< fail <msg>\n (or just "fail" + packet with msg)
The last would be when filter knows upfront that it cannot perform the operation. Though sending an empty file with non-"success" final would work as well.
For example LFS filter (that is configured as not required) may refuse to store files which are smaller than some pre-defined constant threshold.
Discussed in http://public-inbox.org/git/7255ef06-a9a0-91b7-b6da-a90322de926b%40gmail.com/
Quoted 14 lines
Quoted 6 lines
packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< success\n------------------------
Quoted 7 lines
In case the filter cannot process the content, it is expected to respond with the result content size 0 (only if "stream" is not defined) and a "reject" packet.
------------------------packet: git< size=0\n (omitted with capability "stream") packet: git< reject\n------------------------
Quoted 2 lines
This is *wrong* idea! Empty file, with size=0, can be a perfectly legitimate response.
Discussed in http://public-inbox.org/git/7255ef06-a9a0-91b7-b6da-a90322de926b%40gmail.com/
Quoted 9 lines
For example rot13 filter should respond to an empty file on input with an empty file on output. LFS-like filters and encryption mechanism should return empty file on fetch / decryption if such empty file was stored / encrypted.
A strange LFS could even use filenames (with files being empty themselves) as a lookup key for artifactory. For example a kind of CDN for common libraries, with version embedded in filename, like 'libs/jquery-1.9.0.min.js', etc.
Right, that would be possible with the current implementation. I will add an empty file to the test case to prove it.
Quoted 7 lines
Quoted 3 lines
After the filter has processed a blob it is expected to wait for the next command. A demo implementation can be found in
t/t0021/rot13-filter.pllocated in the Git core repository.If filter does not support "shutdown" capability (or if said capability is postponed for later patch), it should behave sanely when Git command reaps it (SIGTERM + wait + SIGKILL?, SIGCHLD?).
How would you do this? Don't you think the current solution is good enough for processes that don't need a proper shutdown?
Quoted 10 lines
Quoted 9 lines
If the filter supports the "shutdown" capability then Git will send the "shutdown" command and wait until the filter answers with "done". This gives the filter the opportunity to perform cleanup tasks. Afterwards the filter is expected to exit.
------------------------packet: git> shutdown\n packet: git< done\n------------------------
Quoted 2 lines
I guess there is no timeout mechanism: if filter hangs on shutdown, then git command would also hang waiting for signal to exit.
Correct. Even if we implement a timeout - what time would we wait? I think this is still the best option for now.
Quoted 6 lines
Quoted 3 lines
If a filter.<driver>.clean or filter.<driver>.smudge command is configured then these commands always take precedence over a configured filter.<driver>.process command.
Note: the value of
clean,smudgeandprocessis a command, not just a string.
OK
Quoted 19 lines
I wonder if it would be worth it to explain the reasoning behind this solution and show alternate ones.
* Using a separate variable to signal that filters are invoked per-command rather than per-file, and use pkt-line interface, like boolean-valued
useProtocol, orprotocolVersionset to '2' or 'v2', orpersistenceset to 'per-command', there is high risk of user's trying to use exiting one-shot per-file filters... and Git hanging.* Using new variables for each capability, e.g.
processSmudgeandprocessCleanwould lead to explosion of variable names; I think.* Current solution of using
processin addition tocleanandsmudgeclearly says that you need to use different command for per-file (cleanandsmudge), and per-command filter, while allowing to use them together.
Quoted 5 lines
The possible disadvantage is Git command starting
processfilter, only to see that it doesn't offer required capability, for example offering only "clean" but not "smudge". There is simple workaround - setsmudgevariable (same as not present capability) to empty string.
If you think it is necessary to have this discussion in the commit message, then I will add it.
Quoted 7 lines
Quoted 4 lines
Please note that you cannot use an existing filter.<driver>.clean or filter.<driver>.smudge command as filter.<driver>.process command. As soon as Git would detect a file that needs to be processed by this filter, it would stop responding.
I think this needs to be in the documentation (I have not checked yet if it is), but is not needed in the already long commit message.
OK
Quoted 27 lines
Quoted 18 lines
Signed-off-by Lars Schneider <[email protected]>Helped-by Martin-Louis Bright <[email protected]>---
Documentation/gitattributes.txt | 84 ++++++++- convert.c | 400 +++++++++++++++++++++++++++++++++++++-- t/t0021-conversion.sh | 405 ++++++++++++++++++++++++++++++++++++++++ t/t0021/rot13-filter.pl | 177 ++++++++++++++++++ 4 files changed, 1053 insertions(+), 13 deletions(-)create mode 100755
t/t0021/rot13-filter.plDocumentation/gitattributes.txt +0
-0@@ -300,7 +300,11 @@ checkout, when the `smudge` command is specified, the command isfed the blob object from its standard input, and its standard output is used to update the worktree file. Similarly, the
cleancommand is used to convert the contents of worktree fileDocumentation/gitattributes.txt checkout, when the `smudge` command is specified, the command is · line 307
Quoted 6 lines
Documentation/gitattributes.txt +5
-1-upon checkin. +upon checkin. By default these commands process only a single +blob and terminate. If a long running filter process (see section +below) is used then Git can process all blobs with a single filter +invocation for the entire life of a single Git command (e.g. +`git add .`).Proposed improvement:
Quoted 6 lines
If a long running
processfilter is used
in place ofcleanand/orsmudgefilters, then Git can process all blobs with a single filter command invocation for the entire life of a single Git command, for examplegit add --all. See section below for the description of the protocol used to communicate with aprocessfilter.
Sounds good. I will use this!
Quoted 6 lines
Quoted 6 lines
One use of the content filtering is to massage the content into a shape that is more convenient for the platform, filesystem, and the user to use.
Documentation/gitattributes.txt +0
-1@@ -375,6 +379,84 @@ substitution. For example: ------------------------
Quoted 9 lines
Documentation/gitattributes.txt substitution. For example: · line 385
Quoted 4 lines
Documentation/gitattributes.txt +4
-0+Long Running Filter Process +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +If the filter command (string value) is defined viaThis is no mere string value, this is command invocation (with its own rules, e.g. splitting parameters on whitespace, etc.). Though I'm not sure how to say it succintly. Maybe skip "(string value)"? But it is there for a reason...
How about: "If the filter command as string value is defined via ..."?
Quoted 3 lines
Documentation/gitattributes.txt substitution. For example: · line 386
Quoted 1 line
Documentation/gitattributes.txt +1
-0+filter.<driver>.process then Git can process all blobs with aShouldn't it be
filter.<driver>.process?
OK, I will change it.
Quoted 5 lines
Documentation/gitattributes.txt substitution. For example: · line 389
Quoted 3 lines
Documentation/gitattributes.txt +3
-0+single filter invocation for the entire life of a single Git +command. This is achieved by using the following packet +format (pkt-line, see protocol-common.txt) based protocol overCan we linkgit-it (to technical documentation)?
I don't think that is possible because it was never done. See:
git grep "linkgit:tech"
Quoted 7 lines
Documentation/gitattributes.txt substitution. For example: · line 392
Quoted 3 lines
Documentation/gitattributes.txt +3
-0+standard input and standard output. + +Git starts the filter on first usage and expects a welcomeIs "usage" here correct? Perhaps it would be more readable to say that Git starts filter when encountering first file that needs cleaning or smudgeing.
OK. How about this:
Git starts the filter when it encounters the first file that needs to be cleaned or smudged. After the filter started Git expects a welcome message, protocol version number, and filter capabilities separated by spaces:
Quoted 4 lines
Quoted 4 lines
Documentation/gitattributes.txt +4
-0+message, protocol version number, and filter capabilities +separated by spaces: +------------------------ +packet: git< git-filter-protocol\n
Quoted 8 lines
Documentation/gitattributes.txt substitution. For example: · line 401
Quoted 5 lines
Documentation/gitattributes.txt +5
-0+packet: git< version 2\n +packet: git< capabilities clean smudge\n +------------------------ +Supported filter capabilities are "clean", "smudge", "stream", +and "shutdown".Filter should include at least one of "clean" and "smudge" capabilities (currently), otherwise it wouldn't do anything.
Well, I think that should be clear to the reader, no?
Quoted 17 lines
I don't know if it is a good place to say that because of pkt-line recommendations about text-content packets, each of those should terminate in endline, with "\n" included in pkt-line length.
Documentation/gitattributes.txt substitution. For example:
Quoted 3 lines
Documentation/gitattributes.txt +3
-0+ +Afterwards Git sends a command (based on the supported +capabilities),I think it should be something like the following:
If among filter
processcapabilities there is capability that corresponds to the operation performed by a Git command (that is, either "clean" or "smudge"), then Git would send, in separate packets, a command (based on supported capabilites),though it feels too "chatty" (and the sentence gets quite long).
Quoted 7 lines
Documentation/gitattributes.txt substitution. For example: · line 405
Quoted 2 lines
Documentation/gitattributes.txt +1
-0the filename including its path +relative to the repository root,Errr... "the filename including its path"? Wouldn't be it simpler to just say:
the pathname of a file relative to the repository root,
Agreed
Quoted 1 line
Also, isn't it now "filename=<pathname>\n"?
You mean I should change filename to pathname? Agreed!
Quoted 7 lines
Documentation/gitattributes.txt substitution. For example: · line 407
Quoted 2 lines
Documentation/gitattributes.txt +1
-0the content size as ASCII number +in bytes,Could Git not give the size, for example if
fstat()fails? Do we reserve space for other information here?Also, isn't it now "size=<bytes>\n"?
Size will go away as discussed in the beginning of this email.
Quoted 4 lines
Documentation/gitattributes.txt substitution. For example: · line 408
Quoted 2 lines
Documentation/gitattributes.txt +0
-0the content split in zero or many pkt-line packets,s/zero or many/zero or more/
Agreed!
Quoted 4 lines
Documentation/gitattributes.txt substitution. For example: · line 407
Quoted 1 line
Documentation/gitattributes.txt +1
-0+and a flush packet at the end:I wonder if instead of long sentence, it would be more readable to use enumeration (ordered list) or itemize (unordered list).
Maybe, but I think that is good enough for now.
Quoted 15 lines
Documentation/gitattributes.txt substitution. For example:
Quoted 11 lines
Documentation/gitattributes.txt +11
-0+------------------------ +packet: git> smudge\n +packet: git> filename=path/testfile.dat\n +packet: git> size=7\n +packet: git> CONTENT +packet: git> 0000 +------------------------ + +The filter is expected to respond with the result content size as +ASCII number in bytes. If the capability "stream" is defined then +the filter must not send the content size.As I wrote earlier, I think sending or not the size of the output should be decoupled from the "stream" capability.
Quoted 4 lines
Streaming is IMVHO rather a capability of starting to send parts of response before the whole contents of input arrives. I think per-file filters support that and that's what
start_async()there is about.
Correct. However, size will go away as discussed in the beginning of this email.
Quoted 8 lines
Documentation/gitattributes.txt substitution. For example: · line 421
Quoted 4 lines
Documentation/gitattributes.txt +3
-0Afterwards the result +content in send in zero or many pkt-line packets and a flush packet +at the end. Finally a "success" packet is send to indicate that +everything went well.I guess it is "success" packet if everything went well, and place for informing about errors in the future - filter is assumed to die if there are errors in filtering, isn't it?
Correct.
Quoted 1 line
That is, not "send to indicate", but "send if".
Agreed!
Quoted 40 lines
Quoted 5 lines
+------------------------
Documentation/gitattributes.txt +3
-0+packet: git< size=57\n (omitted with capability "stream") +packet: git< SMUDGED_CONTENT +packet: git< 0000Documentation/gitattributes.txt substitution. For example: · line 435
Quoted 10 lines
Documentation/gitattributes.txt +10
-0+packet: git< success\n +------------------------ + +In case the filter cannot process the content, it is expected +to respond with the result content size 0 (only if "stream" is +not defined) and a "reject" packet. +------------------------ +packet: git< size=0\n (omitted with capability "stream") +packet: git< reject\n +------------------------I would assume that we have two error conditions.
First situation is when the filter knows upfront (after receiving name and size of file, and after receiving contents for not-streaming filters) that it cannot process the file (like e.g. LFS filter with artifactory replica/shard being a bit behind master, and not including contents of the file being filtered).
My proposal is to reply with "fail"
_inplaceof_size of reply:packet: git< fail\n (any case: size known or not, stream or not)
It could be "reject", or "error" instead of "fail".
Another situation is if filter encounters error during output, either with streaming filter (or non-stream, but not storing whole input upfront) realizing in the middle of output that there is something wrong with input (e.g. converting between encoding, and encountering character that cannot be represented in output encoding), or e.g. filter process being killed, or network connection dropping with LFS filter, etc. The filter has send some packets with output already. In this case filter should flush, and send "reject" or "error" packet.
Quoted 6 lines
<error condition>
packet: git< "0000" (flush packet)
packet: git< reject\nShould there be a place for an error message, or would standard error (stderr) be used for this?
Already discussed in http://public-inbox.org/git/6765D972-876A-4F94-A170-468002498296%40gmail.com/
I will add an example for the error case, too.
Quoted 7 lines
Documentation/gitattributes.txt substitution. For example: · line 439
Quoted 4 lines
Documentation/gitattributes.txt +4
-0+ +After the filter has processed a blob it is expected to wait for +the next command. A demo implementation can be found in +`t/t0021/rot13-filter.pl` located in the Git core repository.It is actually in Git sources. Is it the best way to refer to such files?
Well, I could add a github.com link but I don't think everyone would like that. What would you suggest?
Quoted 16 lines
Documentation/gitattributes.txt substitution. For example: · line 452
Quoted 13 lines
Documentation/gitattributes.txt +13
-0+ +If the filter supports the "shutdown" capability then Git will +send the "shutdown" command and wait until the filter answers +with "done". This gives the filter the opportunity to perform +cleanup tasks. Afterwards the filter is expected to exit. +------------------------ +packet: git> shutdown\n +packet: git< done\n +------------------------ + +If a filter.<driver>.clean or filter.<driver>.smudge command +is configured then these commands always take precedence over +a configured filter.<driver>.process command.All right; this is quite clear.
Quoted 7 lines
Documentation/gitattributes.txt substitution. For example: · line 457
Quoted 5 lines
Documentation/gitattributes.txt +5
-0+ +Please note that you cannot use an existing filter.<driver>.clean +or filter.<driver>.smudge command as filter.<driver>.process +command. As soon as Git would detect a file that needs to be +processed by this filter, it would stop responding.This isn't.
Would that be better?
Please note that you cannot use an existing filter.<driver>.clean or filter.<driver>.smudge command as filter.<driver>.process command because the former two use a different inter process communication protocol than the latter one. As soon as Git would detect a file that needs to be processed by such an invalid "process" filter, it would wait for a proper protocol handshake and appear "hanging".
Quoted 2 lines
P.S. I will comment about the implementation part in the next email.
Sure! Thanks again for the extensive review,
Lars
Designing the filter process protocol (was: Re: [PATCH v3 10/10] convert: add filter.<driver>.process option)
[I'm sorry for taking so long in writing this, as I see there is v4 already]
Greetings,
I'll answer to individual emails in more detail later, but I'd like to go back to the drawing board, and attempt to summarize the discussion and the proposal so far.
The ultimate goal is to be able to run filter drivers faster for both clean and smudge operations. This is done by starting filter driver once per git command invocation, instead of once per file being processed. Git needs to pass actual contents of files to filter driver, and get its output.
We want the protocol between Git and filter driver process to be extensible, so that new features can be added without modifying protocol.
1. CONFIGURATION
As I wrote, there are different ways of configuring new-type filter driver:
* Using a separate variable to mark filter as using new protocol (the original approach):
[filter "protocol"]
protocolVersion = v2
clean = rot13-clean-filter.pl
smudge = rot13-smudge-filter.pl
PROS: allows to have separate clean and smudge filters
CONS: does not allow using old-style per-file filter together with new;
easy to make mistake and use old-style filter, leading to hang
* Creating new variables for new filter type, separate for each phase, for example cleanProcess and smudgeProcess (or processClean and processSmudge).
[filter "protocol"]
cleanProcess = rot13-clean-filter.pl smudgeProcess = rot13-smudge-filter.pl
PROS: allows to have separate clean and smudge filters;
makes possible to use per-file and per-command filters together
CONS: proliferation of additional variables, (esp. when extending it);
NOTE: need to decide precedence between clean and cleanProcess, etc.
# Using a single variable for new filter type, and decide on which phase
(which operation) is supported by filter driver during the handshake *(current approach)*
[filter "protocol"]
process = rot13-filtes.pl
PROS: per-file and per-command filters possible with precedence rule;
extensible to other types of drivers: textconv, diff, etc. only one invocation for commands which use both clean and smudge
CONS: need single driver to be responsible for both clean and smudge;
need to run driver to know that it does not support given
operation (workaround exists)
2. HANDSHAKE (INITIALIZATION)
Next, there is deciding on and designing the handshake between Git (between Git command) and the filter driver process. With the filter.<driver>.process solution the driver needs to tell which operations among (for now) "clean" and "smudge" it does support. Plus it provides a way to extend protocol, adding new features, like support for streaming, cleaning from file or smudging to file, providing size upfront, perhaps even progress report.
Current handshake consist of filter driver printing a signature, version number and capabilities, in that order. Git checks that it is well formed and matches expectations, and notes which of "clean" and "smudge" operations are supported by the filter.
There is no interaction from the Git side in the handshake, for example to set options and expectations common to all files being filtered. Take one possible extension of protocol: supporting streaming. The filter driver needs to know whether it needs to read all the input, or whether it can start printing output while input is incoming (e.g. to reduce memory consumption)... though we may simply decide it to be next version of the protocol.
On the other hand if the handshake began with Git sending some initializer info to the filter driver, we probably could detect one-shot filter misconfigured as process-filter.
Note that we need some way of deciding where handshake ends, either by specifying number of entries (currently: three lines / pkt-line packets), or providing some terminator ("smart" transport protocol uses flush packet for this).
Current handshake (in symbolic form):
git< [signature] git-filter-protocol
git< [version] version 2
git< [capabilites] clean smudgeIt is expected that the handshake is limited to this information, and that they are in this order; so naming them doesn't buy us much
git< [capabilites] capabilities clean smudge
or
git< [capabilites] capabilities=clean smudge
or
git< [capabilites] capabilities: clean smudge
If capabilities are to be third item, adding "capabilities", as if Git would look at the name and select what to do based on this name, doesn't buy us anything. Well, beside self-documenting of the protocol. The "smart" protocol do not use "capabilities" as prefix/name either.
We would probably do not want to move from strict-order of information, that is "positional parameters". It would require to implement a parser, both for the Git side and for the filter driver process side.
On the other hand requiring flush packet to end the handshake doesn't bring much overhead (it is 4 bytes, it is not over the network), and improves extendability. Well, so does using names, be it "<var> <value>", "<var>=<value>", "<var>: <value>...", "<var>=[<value>, <value>...]", etc.
Let's take a look how other parts of Git communicate with external process (a "helper").
The git-credential(1) protocol uses <variable>=<value> syntax. But capabilities form a list; "<var>=<val1> <val2>" doesn't look that well. Credential helper only uses scalar (single) values.
The gitremote-helpers(1) protocol is command / response; for example helper responds to "capabilities" command with the list of capabilities. Here commands and parameters are space separated, e.g. "option <name> <value>".
The "smart" transport protocol (send-pack and receive-pack) had to (ab)use a quirk of implementation to extend protocol with capabilities negotiation. Here the capabilities list is sent without any prefix; some capabilities are parametrized, and use <capability>=<value> syntax (for example "symref=HEAD:refs/heads/master"). The handshake is closed with flush packet, but as it consist of variable-length ref advertisement, it needs to have explicit terminator of the each part of the "handshake".
3. SENDING CONTENTS (FILE TO BE FILTERED AND FILTER OUTPUT)
Next thing to design is decision how to send contents to be filtered to the filter driver process, and how to get filtered output from the filter driver process.
One thing I think we can agree on early, is sending data to filter process on its standard input, and receiving filtered result from its standard output.
Because Git is sending (and receiving) multiple files, it needs some way to distinguish where one file ends and the next begins, in both directions, to and from filter. Also, the clean and smudge filters support expansion of the '%f' placeholder, so at least some filter drivers need name of the file being filtered. So the protocol must send it somehow to the filter driver.
There are different approaches possible; here are ones that were used, and ones I thought about.
* Send whole data to filter at once, and receiver all data at once, for example using something akin to the 'tar' archive, or uncompressed 'zip' archive (both are implemented in Git for the git archive command). Or just list of sizes and pathnames, empty entry as terminator, and then contents of all files concatenated.
PROS:
- can use the one-shot infrastructure implemented already CONS:
- complicates Git code and filter driver code unnecessarily
- difficult to implement error handling, esp. soft errors on filter driver side (error for single file, perhaps during output)
- in synchronous version (non-streaming) requires absurd amout of memory / storage for the filter driver process
* Send/receive data file by file, using <size> + <content>, that is, send size (plus other data like the filename), then file contents.
This was the protocol used in the first iteration of series.
PROS:
- simple to implement on Git and on filter driver side NOTE:
- you need to loop over read / user read_in_full anyway CONS:
- no way to signal an error encountered during output, e.g. LFS network/server failure for after some contents were actually sent
- impossible to implement streaming for filters that do not know size of output without examining full input
# Send/receive data file by file, using some kind of chunking,
with a end-of-file marker. The solution used by Git is pkt-line, with flush packet used to signal end of file.
This is protocol used by the current implementation.
PROS:
- no need to know size upfront, so easier streaming support
- you can signal error that happened during output, after some data were sent, as well as error known upfront
- tracing support for free (GIT_TRACE_PACKET) CONS:
- filter driver program slightly more difficult to implement
- some negligible amount of overhead
If we want in the end to implement streaming, then the last solution is the way to go.
4. PER-FILE HANDSHAKE - SENDING FILE TO FILTER
Let's assume that for simplicity we want to implement (for now) only the synchronous (non-streaming) case, where we send whole contents of a file to filter driver process, and *then* read filter driver output. This is enough for git-LFS solutions, which were the reason for this patch series. But we want to keep the protocol flexible enough so that streaming and other features could be added easily.
First, if we choose the solution where one process is responsible for both "clean" and "smudge" operations (and in the future possibly also "cleanFromFile" and "smudgeToFile"), Git needs to tell the driver which operation to perform.
Together with operation Git can send additional information (sub-capabilities)... or we can use a separate line / packet to send it.
If we are using pkt-line, then the convention is that text lines are terminated using LF ("\n") character. This needs to be stated explicitly in the documentation for filter.<driver>.process writers.
git> packet: [operation] clean size=67\n
We could denote that it is operation name, but it is obvious from position in the stream, thus not really needed.
Then we need to provide the filename; some filters supposedly need this ('%f' in per-file clean / smudge). Note that filename can contain internal space characters, and could contain newlines, equal signs; anything that is not NUL ("\0") character.
git> packet: [pathname] subdir/sample-file.r\n
In most cases filename would be text, so perhaps we should use "\n" terminator (which filter driver would have to strip). We could use "filename=" prefix, but it is not necessary. We know where / when to expect the pathname (relative to project root).
If we would want to be able to add variable number of packets to the handshake, then Git should send flush packet to signal the end of the handshake. But IMVHO it is unnecessary complication of the protocol; there is enough flexibility in it. We know that handshake consists of two packets.
The Git would sent contents of the file to be filtered, using as many pack lines as needed (note: large file support needs to be tested, at least as expensive test). Flush packet is used to signal the end of the file.
git> packets: <file contents>
git> flush packet
5. FILTER DRIVER PROCESS RESPONSE
First filter should, in my opinion, reply that it received the request (or the command, in the case of streaming supported). Also, in this response it can provide further information to Git process.
git< packet: [received] ok size=67\n
This response could be used to refuse to filter specific file upfront (for example if the file is not present in the artifactory for git-LFS solutions).
git< packet: [rejected] reject\n
We can even provide the reasoning to Git (maybe in the future extension)... or filter driver can print the explanation to the standard error (but then, no --quiet / --verbose support).
git< packet: [rejected] reject with-message\n
git< packet: [message] File not found on server\n
git< flush packet
Another response, which I think should be standarized, or at least described in the documentation, is filter driver refusing to filter further (e.g. git-LFS and network is down), to be not restarted by Git.
git< packet: [quit] quit msg=Server error\n
or
git< packet: [quit] quit Server error\n
or
git< packet: [quit] quit with-message\n
git< packet: [message] Server error\n
git< flush packet
Maybe this is over-engineering, but I don't think so.
Next comes the output from the filter driver (filtered contents), using possibly multiple pkt-lines, ending with a flush packet:
git< packets: <filtered contents>
git< flush packet
Note that empty file would consist of zero pack lines of contents, and one flush packet.
Finally, to allow handling of [resumable] errors that occurred during sending file contents, especially for the future streaming filters case, we want to confirm that we send whole file successfully.
git< packet: [status] success\n
If there was an error during process, making data receives so far invalid, filter driver should tell about it
git< packet: [status] fail\n
or
git< packet: [status] reject\n
This may happen for example for UCS-2 <-> UTF-8 filter when invalid byte sequence is encountered. This may happen for git-LFS if the server fails during fetch, and spare / slave server doesn't have a file.
We may want to quit filtering at this point, and not to send another file.
git< packet: [status] quit\n
There is place for extra information after the status, and in the future we can allow variable length information too.
Best,
Signature · 1 line
Jakub Narębski
Re: Designing the filter process protocol (was: Re: [PATCH v3 10/10] convert: add filter.<driver>.process option)
Quoted 55 lines · Jakub Narębski
The ultimate goal is to be able to run filter drivers faster for both
cleanandsmudgeoperations. This is done by starting filter driver once per git command invocation, instead of once per file being processed. Git needs to pass actual contents of files to filter driver, and get its output.We want the protocol between Git and filter driver process to be extensible, so that new features can be added without modifying protocol.
1. CONFIGURATION
As I wrote, there are different ways of configuring new-type filter driver:
...
# Using a single variable for new filter type, and decide on which phase
(which operation) is supported by filter driver during the handshake *(current approach)*[filter "protocol"]
process = rot13-filtes.plPROS: per-file and per-command filters possible with precedence rule;
extensible to other types of drivers: textconv, diff, etc. only one invocation for commands which use both clean and smudge
CONS: need single driver to be responsible for both clean and smudge;
need to run driver to know that it does not support given
operation (workaround exists)2. HANDSHAKE (INITIALIZATION)
Next, there is deciding on and designing the handshake between Git (between Git command) and the filter driver process. With the
filter.<driver>.processsolution the driver needs to tell which operations among (for now) "clean" and "smudge" it does support. Plus it provides a way to extend protocol, adding new features, like support for streaming, cleaning from file or smudging to file, providing size upfront, perhaps even progress report.Current handshake consist of filter driver printing a signature, version number and capabilities, in that order. Git checks that it is well formed and matches expectations, and notes which of "clean" and "smudge" operations are supported by the filter.
There is no interaction from the Git side in the handshake, for example to set options and expectations common to all files being filtered. Take one possible extension of protocol: supporting streaming. The filter driver needs to know whether it needs to read all the input, or whether it can start printing output while input is incoming (e.g. to reduce memory consumption)... though we may simply decide it to be next version of the protocol.
Quoted 3 lines
On the other hand if the handshake began with Git sending some initializer info to the filter driver, we probably could detect one-shot filter misconfigured as process-filter.
OK, I'll look into this.
Quoted 6 lines
Note that we need some way of deciding where handshake ends, either by specifying number of entries (currently: three lines / pkt-line packets), or providing some terminator ("smart" transport protocol uses flush packet for this).
...
Would you be OK with Peff's suggestion?
version=2
clean=true
smudge=true
0000
http://public-inbox.org/git/20160803224619.bwtbvmslhuicx2qi%40sigill.intra.peff.net/
Quoted 35 lines
3. SENDING CONTENTS (FILE TO BE FILTERED AND FILTER OUTPUT)
Next thing to design is decision how to send contents to be filtered to the filter driver process, and how to get filtered output from the filter driver process.
...
# Send/receive data file by file, using some kind of chunking,
with a end-of-file marker. The solution used by Git is pkt-line, with flush packet used to signal end of file.This is protocol used by the current implementation.
PROS:
- no need to know size upfront, so easier streaming support
- you can signal error that happened during output, after some data were sent, as well as error known upfront
- tracing support for free (GIT_TRACE_PACKET) CONS:
- filter driver program slightly more difficult to implement
- some negligible amount of overheadIf we want in the end to implement streaming, then the last solution is the way to go.
4. PER-FILE HANDSHAKE - SENDING FILE TO FILTER
Let's assume that for simplicity we want to implement (for now) only the synchronous (non-streaming) case, where we send whole contents of a file to filter driver process, and *then* read filter driver output.
...
Quoted 8 lines
If we are using pkt-line, then the convention is that text lines are terminated using LF ("\n") character. This needs to be stated explicitly in the documentation for filter.<driver>.process writers.
git> packet: [operation] clean size=67\n
We could denote that it is operation name, but it is obvious from position in the stream, thus not really needed.
I would prefer not to mix command and size in one packet as it makes parsing a little more difficult.
Quoted 9 lines
...
The Git would sent contents of the file to be filtered, using as many pack lines as needed (note: large file support needs to be tested, at least as expensive test). Flush packet is used to signal the end of the file.
git> packets: <file contents>
git> flush packet
If expensive tests are enabled the test suite will process data larger then max pkt size.
Quoted 8 lines
5. FILTER DRIVER PROCESS RESPONSE
First filter should, in my opinion, reply that it received the request (or the command, in the case of streaming supported). Also, in this response it can provide further information to Git process.
git< packet: [received] ok size=67\n
I think this would be different for real streaming and the current non-streaming... therefore it would complicate the protocol?! I wonder if it is truly necessary.
Quoted 5 lines
This response could be used to refuse to filter specific file upfront (for example if the file is not present in the artifactory for git-LFS solutions).
git< packet: [rejected] reject\n
Reject is already supported in v4.
Quoted 7 lines
We can even provide the reasoning to Git (maybe in the future extension)... or filter driver can print the explanation to the standard error (but then, no --quiet / --verbose support).
git< packet: [rejected] reject with-message\n
git< packet: [message] File not found on server\n
git< flush packet
I think Git shouldn't care about these details. If the filter needs to tell something then it should use stderr.
Quoted 11 lines
Another response, which I think should be standarized, or at least described in the documentation, is filter driver refusing to filter further (e.g. git-LFS and network is down), to be not restarted by Git.
git< packet: [quit] quit msg=Server error\n
or
git< packet: [quit] quit Server error\n
Quoted 7 lines
or
git< packet: [quit] quit with-message\n
git< packet: [message] Server error\n
git< flush packetMaybe this is over-engineering, but I don't think so.
Interesting idea! I will look into this for v5!
Quoted 21 lines
Next comes the output from the filter driver (filtered contents), using possibly multiple pkt-lines, ending with a flush packet:
git< packets: <filtered contents>
git< flush packetNote that empty file would consist of zero pack lines of contents, and one flush packet.
Finally, to allow handling of [resumable] errors that occurred during sending file contents, especially for the future streaming filters case, we want to confirm that we send whole file successfully.
git< packet: [status] success\n
If there was an error during process, making data receives so far invalid, filter driver should tell about it
git< packet: [status] fail\n
Quoted 8 lines
or
git< packet: [status] reject\n
This may happen for example for UCS-2 <-> UTF-8 filter when invalid byte sequence is encountered. This may happen for git-LFS if the server fails during fetch, and spare / slave server doesn't have a file.
Correct!
Quoted 4 lines
We may want to quit filtering at this point, and not to send another file.
git< packet: [status] quit\n
I don't get this one. Git would restart the filter as soon as it finds another file that needs to be filtered, right?
Thanks a lot for this write up!
- Lars
Re: Designing the filter process protocol (was: Re: [PATCH v3 10/10] convert: add filter.<driver>.process option)
In reply to Jakub NarębskiQuoted 20 lines · Jakub Narębski
...
2. HANDSHAKE (INITIALIZATION)
Next, there is deciding on and designing the handshake between Git (between Git command) and the filter driver process. With the
filter.<driver>.processsolution the driver needs to tell which operations among (for now) "clean" and "smudge" it does support. Plus it provides a way to extend protocol, adding new features, like support for streaming, cleaning from file or smudging to file, providing size upfront, perhaps even progress report.Current handshake consist of filter driver printing a signature, version number and capabilities, in that order. Git checks that it is well formed and matches expectations, and notes which of "clean" and "smudge" operations are supported by the filter.
Quoted 7 lines
There is no interaction from the Git side in the handshake, for example to set options and expectations common to all files being filtered. Take one possible extension of protocol: supporting streaming. The filter driver needs to know whether it needs to read all the input, or whether it can start printing output while input is incoming (e.g. to reduce memory consumption)... though we may simply decide it to be next version of the protocol.
I would like to change the startup sequence to this:
Git starts the filter when it encounters the first file that needs to be cleaned or smudged. After the filter started Git sends a welcome message, a list of supported protocol version numbers, and a flush packet. Git expects to read the welcome message and one protocol version number from the previously sent list. Afterwards Git sends a list of supported capabilities and a flush packet. Git expects to read a list of desired capabilities, which must be a subset of the supported capabilities list, and a flush packet as response:
------------------------
packet: git> git-filter-client packet: git> version=2 packet: git> version=42 packet: git> 0000 packet: git< git-filter-server packet: git< version=2 packet: git> clean=true packet: git> smudge=true packet: git> not-yet-invented=true packet: git> 0000 packet: git< clean=true packet: git< smudge=true packet: git< 0000
------------------------
This would allow us to detect the case if a user configures an existing clean/smudge filter as filter.<driver>.process. Since Git is talking first, it would not "hang" in that case.
Would that be ok with you?
Thanks,
Lars
Re: [PATCH v3 10/10] convert: add filter.<driver>.process option
In reply to Lars Schneider[Note that some of this might have been invalidated by v4]
W dniu 01.08.2016 o 15:32, Lars Schneider pisze:
Quoted 19 lines
Quoted 11 lines · Jakub Narębski
W dniu 30.07.2016 o 01:38, [email protected] pisze:
Quoted 7 lines
Git starts the filter on first usage and expects a welcome message, protocol version number, and filter capabilities separated by spaces:
------------------------packet: git< git-filter-protocol\n packet: git< version 2\n packet: git< capabilities clean smudge\nQuoted 4 lines
Sorry for going back and forth, but now I think that 'capabilities' are not really needed here, though they are in line with "version" in the second packet / line, namely "version 2". If it does not make parsing more difficult...
I don't understand what you mean with "they are not really needed"? The field is necessary to understand the protocol, no?
Quoted 6 lines
In the last roll I added the "key=value" format to the protocol upon yours and Peff's suggestion. Would it be OK to change the startup sequence accordingly?
packet: git< version=2\n packet: git< capabilities=clean smudge\n
With current implementation, Git checks second packet of the handshake for version, and third packet for capabilities. The "capabilities" or "capabilities=" is entirely redundant; it is the position of packet (it is the packet number) that matters. At least for now.
The only thing that "version" in "version 2" and "capabilities" in "capabilities: clean smudge" helps is self-describing of the protocol.
To really make use of them you would have to end handshake with flush packet, and do a parsing: loop over every packet, and match known patterns. Well, perhaps with exception of known header: it doesn't makes sense to have "version N" anywhere else than second packet, and it doesn't makes sense to repeat it.
We also don't want to proliferate packets unnecessarily. Each packet is a bit (a tiny bit) of a performance hit.
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
------------------------
Supported filter capabilities are "clean", "smudge", "stream", and "shutdown".
Quoted 8 lines
Quoted 2 lines
I'd rather put "stream" and "shutdown" capabilities into separate patches, for easier review.
I agree with "shutdown". I think I would like to remove the "stream" option and make it the default for the following reasons:
(1) As you mentioned elsewhere, "stream" is not really streaming at this point because we don't read/write in parallel.
We could, following the example of original per-file filter drivers. It is as simple as starting writer using start_async(), as if we did writing from Git in a child process.
Though that might be left for later (assuming that protocol is flexible enough), as synchronous protocol (write, then read) is a bit simpler to implement.
Quoted 2 lines
(2) Junio and you pointed out that if we transmit size and flush packet then we have redundancy in the protocol.
Providing size upfront can be a hint for filter or Git. For example HTTP provides Content-Length: header, though it is not strictly necessary.
Quoted 5 lines
(3) With the newly introduced "success"/"reject"/"failure" packet at the end of a filter operation, a filter process has a way to signal Git that something went wrong. Initially I had the idea that a filter process just stops writing and Git would detect the mismatch between expected bytes and received bytes. But the final status packet is a much clearer solution.
The solution with stopping writing wouldn't work, I don't think.
Quoted 2 lines
(4) Maintaining two slightly different protocols is a waste of resources and only increases the size of this (already large) patch.
Right, better to design and implement basic protocol, taking care that it is extensible, and only then add to it.
Quoted 5 lines
My only argument for the size packet was that this allows efficient buffer allocation. However, in non of my benchmarks this was actually a problem. Therefore this is probably a epsilon optimization and should be removed.
OK with everyone?
All right.
Quoted 10 lines
Quoted 7 lines
Quoted 3 lines
After the filter has processed a blob it is expected to wait for the next command. A demo implementation can be found in
t/t0021/rot13-filter.pllocated in the Git core repository.If filter does not support "shutdown" capability (or if said capability is postponed for later patch), it should behave sanely when Git command reaps it (SIGTERM + wait + SIGKILL?, SIGCHLD?).
How would you do this? Don't you think the current solution is good enough for processes that don't need a proper shutdown?
Actually... couldn't filter driver register atexit() / signal handler to do a clean exit, if it is needed?
Quoted 19 lines
Quoted 19 lines
I wonder if it would be worth it to explain the reasoning behind this solution and show alternate ones.
* Using a separate variable to signal that filters are invoked per-command rather than per-file, and use pkt-line interface, like boolean-valued
useProtocol, orprotocolVersionset to '2' or 'v2', orpersistenceset to 'per-command', there is high risk of user's trying to use exiting one-shot per-file filters... and Git hanging.* Using new variables for each capability, e.g.
processSmudgeandprocessCleanwould lead to explosion of variable names; I think.* Current solution of using
processin addition tocleanandsmudgeclearly says that you need to use different command for per-file (cleanandsmudge), and per-command filter, while allowing to use them together.
Quoted 8 lines
Quoted 5 lines
The possible disadvantage is Git command starting
processfilter, only to see that it doesn't offer required capability, for example offering only "clean" but not "smudge". There is simple workaround - setsmudgevariable (same as not present capability) to empty string.If you think it is necessary to have this discussion in the commit message, then I will add it.
I think it would be good idea (not necessary, but helpful), though possibly not in such exacting detail. Just why this one, one sentence or more.
Quoted 8 lines
Quoted 5 lines
Documentation/gitattributes.txt substitution. For example: · line 389
Quoted 3 lines
Documentation/gitattributes.txt +3
-0+single filter invocation for the entire life of a single Git +command. This is achieved by using the following packet +format (pkt-line, see protocol-common.txt) based protocol overCan we linkgit-it (to technical documentation)?
I don't think that is possible because it was never done. See:
git grep "linkgit:tech"
A pity. Well, not your problem, anyway.
Quoted 6 lines
Quoted 5 lines
Quoted 1 line
+Git starts the filter on first usage and expects a welcome
Is "usage" here correct? Perhaps it would be more readable to say that Git starts filter when encountering first file that needs cleaning or smudgeing.
Quoted 6 lines
OK. How about this:
Git starts the filter when it encounters the first file that needs to be cleaned or smudged. After the filter started Git expects a welcome message, protocol version number, and filter capabilities separated by spaces:
Better.
Quoted 10 lines
Quoted 7 lines
Documentation/gitattributes.txt substitution. For example: · line 439
Quoted 4 lines
Documentation/gitattributes.txt +4
-0+ +After the filter has processed a blob it is expected to wait for +the next command. A demo implementation can be found in +`t/t0021/rot13-filter.pl` located in the Git core repository.It is actually in Git sources. Is it the best way to refer to such files?
Well, I could add a github.com link but I don't think everyone would like that. What would you suggest?
Sorry, I wasn't clear. What I meant is if "<file> located in the Git core repository" is the best way to refer to such files, and if we could do better.
But I think it is all right as it is.
Later we might want to provide some example filter.<driver>.process filters e.g. in contrib/. But that's for the future.
Quoted 11 lines
Quoted 7 lines
Documentation/gitattributes.txt substitution. For example: · line 457
Quoted 5 lines
Documentation/gitattributes.txt +5
-0+ +Please note that you cannot use an existing filter.<driver>.clean +or filter.<driver>.smudge command as filter.<driver>.process +command. As soon as Git would detect a file that needs to be +processed by this filter, it would stop responding.This isn't.
Would that be better?
Quoted 6 lines
Please note that you cannot use an existing
filter.<driver>.cleanorfilter.<driver>.smudgecommand asfilter.<driver>.processcommand because the former two use a different inter process communication protocol than the latter one. As soon as Git would detect a file that needs to be processed by such an invalid "process" filter, it would wait for a proper protocol handshake and appear "hanging".
This is better.
Signature · 1 line
Jakub Narębski
Re: [PATCH v3 10/10] convert: add filter.<driver>.process option
In reply to [email protected]W dniu 30.07.2016 o 01:38, [email protected] pisze:
[...]
Documentation/gitattributes.txt substitution. For example:
Quoted 3 lines
Documentation/gitattributes.txt +3
-0+Please note that you cannot use an existing filter.<driver>.clean +or filter.<driver>.smudge command as filter.<driver>.process +command.
I think it would be more readable and easier to understand to write:
... you cannot use an existing ... command with
filter.<driver>.process
About the style: wouldn't filter.<driver>.process be better?
Quoted 2 lines
As soon as Git would detect a file that needs to be
+processed by this filter, it would stop responding.
This is quite convoluted, and hard to understand. I would say that because clean and smudge filters are expected to read first, while Git expects process filter to say first, using clean or smudge filter without changes as process filter would lead to git command deadlocking / hanging / stopping responding.
Quoted 18 lines
Documentation/gitattributes.txt +2
-0+ + Interaction between checkin/checkout attributes ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^convert.c +1
-0@@ -3,6 +3,7 @@#include "run-command.h" #include "quote.h" #include "sigchain.h" +#include "pkt-line.h" /* * convert.c - convert a file when checking it out and checking it in.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 485
Quoted 4 lines
convert.c +1
-0@@ -481,11 +482,355 @@ static int apply_filter(const char *path, const char *src, size_t len, int fd,return ret; } +static int multi_packet_read (int fd_in,struct strbuf *sb,size_t expected_bytes,int is_stream)
About name of this function: multi_packet_read is fine, though I wonder if packet_read_in_full with nearly the same parameters as packet_read, or packet_read_till_flush, or read_in_full_packetized would be better.
Also, the problem is that while we know that what packet_read() stores would fit in memory (in size_t), it is not true for reading whole file, which might be very large - for example huge graphical assets like raw images or raw videos, or virtual machine images. Isn't that the goal of git-LFS solutions, which need this feature? Shouldn't we have then both multi_packet_read_to_fd and multi_packet_read_to_buf, or whatever?
Also, if we have fd_in, then perhaps sb_out?
I am also unsure if expected_bytes (or expected_size) should not be just a size hint, leaving handing mismatch between expected size and real size of output to the caller; then the is_stream would be not needed.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 488
Quoted 3 lines
convert.c +3
-0+{ +int bytes_read; +size_t total_bytes_read =0 ;
Why bytes_read is int, while total_bytes_read is size_t? Ah, I see that packet_read() returns an int. It should be ssize_t, just like read(), isn't it? But we know that packet size is limited, and would fit in an int (or would it?).
Also, total_bytes_read could overflow size_t, but then we would have problems storing the result in strbuf.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 490
Quoted 2 lines
convert.c +2
-0+if (expected_bytes ==0 && !is_stream) +return 0 ;
So in all cases *except* size = 0 we expect flush packet after the contents, but size = 0 is a corner case without flush packet?
Quoted 4 lines
convert.c +4
-0+ +if (is_stream) +strbuf_grow (sb,LARGE_PACKET_MAX );// allocate space for at least one packet +else
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 504
Quoted 10 lines
convert.c +10
-0+strbuf_grow (sb,st_add (expected_bytes,1 ));// add one extra byte for the packet flush + + do { + bytes_read = packet_read( + fd_in, NULL, NULL, + sb->buf + total_bytes_read, sb->len - total_bytes_read - 1, + PACKET_READ_GENTLE_ON_EOF + ); + if (bytes_read < 0) + return 1; // unexpected EOF
Don't we usually return negative numbers on error? Ah, I see that the return is a bool, which allows to use boolean expression with 'return'. But I am still unsure if it is good API, this return value.
If we move handling of size mismatch to the caller, then the function can simply return the size of data read (probably off_t or uint64_t). Then the caller can check if it is what it expected, and react accordingly.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 514
Quoted 10 lines
convert.c +10
-0+ +if (is_stream && + bytes_read >0 && + sb->len - total_bytes_read -1 <=0 ) +strbuf_grow (sb,st_add (sb->len,LARGE_PACKET_MAX )); + total_bytes_read += bytes_read; + } +while ( + bytes_read >0 &&// the last packet was no flush + sb->len - total_bytes_read - 1 > 0 // we still have space left in the buffer
Ah, so buffer is resized only in the 'is_stream' case. Perhaps then use an "int options" instead of 'is_stream', and have one of flags tell if we should resize or not, that is if size parameter is hint or a strict limit.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 520
Quoted 6 lines
convert.c +6
-0+ ); +strbuf_setlen (sb, total_bytes_read); +return (is_stream ?0 : expected_bytes != total_bytes_read); +} + +static int multi_packet_write_from_fd (const int fd_in,const int fd_out)
Is it equivalent of copy_fd() function, but where destination uses pkt-line and we need to pack data into pkt-lines?
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 525
Quoted 5 lines
convert.c +5
-0+{ +int did_fail =0 ; +ssize_t bytes_to_write; +while (!did_fail) { + bytes_to_write =xread (fd_in,PKTLINE_DATA_START (packet_buffer),PKTLINE_DATA_LEN );
Using global variable packet_buffer makes this code thread-unsafe, isn't it? But perhaps that is not a problem, because other functions are also using this global variable.
It is more of PKTLINE_DATA_MAXLEN, isn't it?
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 533
Quoted 8 lines
convert.c +8
-0+if (bytes_to_write <0 ) +return 1 ; +if (bytes_to_write ==0 ) +break ; + did_fail |=direct_packet_write (fd_out, packet_buffer,PKTLINE_HEADER_LEN + bytes_to_write,1 ); + } +if (!did_fail) + did_fail =packet_flush_gentle (fd_out);
Shouldn't we try to flush even if there was an error? Or is it that if there is an error writing, then there is some problem such that we know that flush would not work?
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 534
Quoted 1 line
convert.c +1
-0+return did_fail;
Return true on fail? Shouldn't we follow example of copy_fd() from copy.c, and return COPY_READ_ERROR, or COPY_WRITE_ERROR, or PKTLINE_WRITE_ERROR?
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 537
Quoted 3 lines
convert.c +3
-0+} + +static int multi_packet_write_from_buf (const char *src,size_t len,int fd_out)
It is equivalent of write_in_full(), with different order of parameters, but where destination file descriptor expects pkt-line and we need to pack data into pkt-lines?
NOTE: function description comments?
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 541
Quoted 4 lines
convert.c +4
-0+{ +int did_fail =0 ; +size_t bytes_written =0 ; +size_t bytes_to_write;
Note to self: bytes_to_write should fit in size_t, as it is limited to PKTLINE_DATA_LEN. bytes_written should fit in size_t, as it is at most len, which is of type size_t.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 550
Quoted 9 lines
convert.c +9
-0+while (!did_fail) { +if ((len - bytes_written) >PKTLINE_DATA_LEN ) + bytes_to_write =PKTLINE_DATA_LEN ; +else + bytes_to_write = len - bytes_written; +if (bytes_to_write ==0 ) +break ; + did_fail |=direct_packet_write_data (fd_out, src + bytes_written, bytes_to_write,1 ); + bytes_written += bytes_to_write;
Ah, I see now why we need both direct_packet_write() and direct_packet_write_data(). Nice abstraction, makes for clear code.
The last parameter of '1' means 'gently', isn't it?
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 535
Quoted 5 lines
convert.c +5
-0+ } +if (!did_fail) + did_fail =packet_flush_gentle (fd_out); +return did_fail; +}
I think all three/four of those functions should be added in a separate commit, separate patch in patch series. Namely:
- for git -> filter:
* read from fd, write pkt-line to fd (off_t)
* read from str+len, write pkt-line to fd (size_t, ssize_t)
- for filter -> git:
* read pkt-line from fd, write to fd (off_t)
* read pkt-line from fd, write to str+len (size_t, ssize_t)
Perhaps some of those can be in one overloaded function, perhaps it would be easier to keep them separate.
Also, I do wonder how the fetch / push code spools pack file received over pkt-lines to disk. Can we reuse that code? Or maybe that code could use those new functions?
Quoted 4 lines
convert.c +4
-0+ +#define FILTER_CAPABILITIES_STREAM 0x1 +#define FILTER_CAPABILITIES_CLEAN 0x2 +#define FILTER_CAPABILITIES_SMUDGE 0x4
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 569
Quoted 10 lines
convert.c +10
-0+#define FILTER_CAPABILITIES_SHUTDOWN 0x8 +#define FILTER_SUPPORTS_STREAM (type) ((type) & FILTER_CAPABILITIES_STREAM) +#define FILTER_SUPPORTS_CLEAN (type) ((type) & FILTER_CAPABILITIES_CLEAN) +#define FILTER_SUPPORTS_SMUDGE (type) ((type) & FILTER_CAPABILITIES_SMUDGE) +#define FILTER_SUPPORTS_SHUTDOWN (type) ((type) & FILTER_CAPABILITIES_SHUTDOWN) + +struct cmd2process { +struct hashmap_entry ent;/* must be the first member! */ +const char *cmd; +int supported_capabilities;
I wonder if switching from int (perhaps with field width of 1 to denote that it is boolean-like flag) to mask makes it more readable, or less. But I think it is.
Reading Documentation/technical/api-hashmap.txt I found the following recommendation:
struct hashmap_entry::
An opaque structure representing an entry in the hash table, which must be used as first member of user data structures. Ideally it should be followed by an int-sized member to prevent unused memory on 64-bit systems due to alignment.
Therefore it "int supported_capabilities" should precede "const char *cmd", I think. Though it is not strictly necessary; it is not as if this hash table were large (maximum size is limited by the number of filter drivers configured), so we don't waste much space due to internal padding / due to alignment.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 574
Quoted 5 lines
convert.c +5
-0+struct child_process process; +}; + +static int cmd_process_map_initialized =0 ; +static struct hashmap cmd_process_map;
Reading Documentation/technical/api-hashmap.txt I see that:
tablesize is the allocated size of the hash table. A non-0 value indicates that the hashmap is initialized.
So cmd_process_map_initialized is not really needed, is it?
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 581
Quoted 7 lines
convert.c +7
-0+ +static int cmd2process_cmp (const struct cmd2process *e1, +const struct cmd2process *e2, +const void *unused) +{ +return strcmp (e1->cmd, e2->cmd); +}
Well, to be exact (which is decidely not needed!) two commands might be equivalent not being identical as strings (e.g. extra space between parameters). But it is something the user should care about, not Git.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 583
Quoted 2 lines
convert.c +2
-0+ +static struct cmd2process *find_protocol2_filter_entry (struct hashmap *hashmap,const char *cmd)
I'm not sure if *_protocol2_* is needed; those functions are static, local to convert.c.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 585
Quoted 2 lines
convert.c +2
-0+{ +struct cmd2process k;
Does this name of variable 'k' follow established convention? 'key' would be more descriptive, but it's not as if this function was long; so 'k' is all right, I think.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 591
Quoted 6 lines
convert.c +6
-0+hashmap_entry_init (&k,strhash (cmd)); + k.cmd = cmd; +return hashmap_get (hashmap, &k,NULL ); +} + +static void kill_protocol2_filter (struct hashmap *hashmap,struct cmd2process *entry) {
Programming style: the opening brace should be on separate line, that is:
+static void kill_protocol2_filter(struct hashmap *hashmap, struct cmd2process *entry)
+{convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 602
Quoted 11 lines
convert.c +11
-0+if (!entry) +return ; +sigchain_push (SIGPIPE ,SIG_IGN ); +close (entry->process.in); +close (entry->process.out); +sigchain_pop (SIGPIPE ); +finish_command (&entry->process); +child_process_clear (&entry->process); +hashmap_remove (hashmap, entry,NULL ); +free (entry); +}
All those, from #define FILTER_CAPABILITIES_ to here could be put in a separate patch, to reduce size of this one. But I am less sure that it is worth it for this case.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 605
Quoted 3 lines
convert.c +3
-0+ +void shutdown_protocol2_filter (pid_t pid) +{
[...]
In my opinion this should be postponed to a separate commit.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 635
Quoted 3 lines
convert.c +3
-0+} + +static struct cmd2process *start_protocol2_filter (struct hashmap *hashmap,const char *cmd)
This has some parts in common with existing filter_buffer_or_fd(). I wonder if it would be worth to extract those common parts.
But perhaps it would be better to leave such refactoring for later.
Quoted 6 lines
convert.c +6
-0+{ +int did_fail; +struct cmd2process *entry; +struct child_process *process; +const char *argv[] = { cmd,NULL }; +struct string_list capabilities =STRING_LIST_INIT_NODUP ;
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 651
Quoted 10 lines
convert.c +10
-0+char *capabilities_buffer; +int i; + + entry =xmalloc (sizeof (*entry)); +hashmap_entry_init (entry,strhash (cmd)); + entry->cmd = cmd; + entry->supported_capabilities =0 ; + process = &entry->process; + +child_process_init (process);
filter_buffer_or_fd() uses instead
struct child_process child_process = CHILD_PROCESS_INIT;
But I see that you need to access &entry->process anyway, so you need to have it here, and in this case child_process_init() is equivalent.
I wonder if it would be worth it to use strbuf for cmd.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 657
Quoted 6 lines
convert.c +6
-0+ process->argv = argv; + process->use_shell =1 ; + process->in =-1 ; + process->out =-1 ; + process->clean_on_exit =1 ; + process->clean_on_exit_handler = shutdown_protocol2_filter;
These two lines are new, and related to the "shutdown" capability, isn't it?
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 661
Quoted 4 lines
convert.c +4
-0+ +if (start_command (process)) { +error ("cannot fork to run external filter '%s'" , cmd); +kill_protocol2_filter (hashmap, entry);
I guess the alternative solution of adding filter to the hashmap only after starting the process would be racy?
Ah, disregard that. I see that this pattern is a common way to error out in this function (for process-related errors).
Quoted 30 lines
convert.c +30
-0+return NULL ; + } + +sigchain_push (SIGPIPE ,SIG_IGN ); + did_fail =strcmp (packet_read_line (process->out,NULL ),"git-filter-protocol" ); +if (!did_fail) + did_fail |=strcmp (packet_read_line (process->out,NULL ),"version 2" ); +if (!did_fail) + capabilities_buffer =packet_read_line (process->out,NULL ); +else + capabilities_buffer =NULL ; +sigchain_pop (SIGPIPE ); + +if (!did_fail && capabilities_buffer) { +string_list_split_in_place (&capabilities, capabilities_buffer,' ' ,-1 ); +if (capabilities.nr >1 && + !strcmp (capabilities.items[0 ].string,"capabilities" )) { +for (i =1 ; i < capabilities.nr; i++) { +const char *requested = capabilities.items[i].string; +if (!strcmp (requested,"stream" )) { + entry->supported_capabilities |=FILTER_CAPABILITIES_STREAM ; + }else if (!strcmp (requested,"clean" )) { + entry->supported_capabilities |=FILTER_CAPABILITIES_CLEAN ; + }else if (!strcmp (requested,"smudge" )) { + entry->supported_capabilities |=FILTER_CAPABILITIES_SMUDGE ; + }else if (!strcmp (requested,"shutdown" )) { + entry->supported_capabilities |=FILTER_CAPABILITIES_SHUTDOWN ; + }else { + warning( + "external filter '%s' requested unsupported filter capability '%s'",
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 701
Quoted 10 lines
convert.c +10
-0+ cmd, requested + ); + } + } + }else { +error ("filter capabilities not found" ); + did_fail =1 ; + } +string_list_clear (&capabilities,0 ); + }
I wonder if the above conditional wouldn't be better to be put in a separate function, parse_filter_capabilities(capabilities_buffer), returning a mask, or having mask as an out parameter, and returning an error condition.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 704
Quoted 3 lines
convert.c +3
-0+ +if (did_fail) { +error ("initialization for external filter '%s' failed" , cmd);
More detailed information not needed, because one can use GIT_PACKET_TRACE. Would it be worth add this information as a kind of advice, or put it in the documentation of the process option?
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 715
Quoted 11 lines
convert.c +11
-0+kill_protocol2_filter (hashmap, entry); +return NULL ; + } + +hashmap_add (hashmap, entry); +return entry; +} + +static int apply_protocol2_filter (const char *path,const char *src,size_t len, +int fd,struct strbuf *dst,const char *cmd, +const int wanted_capability)
apply_protocol2_filter, or apply_process_filter? Or rather, s/_protocol2_/_process_/g ?
This is equivalent to
static int apply_filter(const char *path, const char *src, size_t len, int fd,
struct strbuf *dst, const char *cmd)
Could we have extended that one instead?
Quoted 8 lines
convert.c +8
-0+{ +int ret =1 ; +struct cmd2process *entry; +struct child_process *process; +struct stat file_stat; +struct strbuf nbuf =STRBUF_INIT ; +size_t expected_bytes =0 ; +char *strtol_end;
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 732
Quoted 10 lines
convert.c +9
-0+char *strbuf; +char *filter_type; +char *filter_result =NULL ; + +if (!cmd || !*cmd) +return 0 ; + +if (!dst) +return 1 ;
This is the same as in apply_filter().
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 740
Quoted 8 lines
convert.c +8
-0+ +if (!cmd_process_map_initialized) { + cmd_process_map_initialized =1 ; +hashmap_init (&cmd_process_map, (hashmap_cmp_fn ) cmd2process_cmp,0 ); + entry =NULL ; + }else { + entry =find_protocol2_filter_entry (&cmd_process_map, cmd); + }
Here we try to find existing process, rather than starting new as in apply_filter()
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 742
Quoted 2 lines
convert.c +2
-0+ +fflush (NULL );
This is the same as in apply_filter(), but I wonder what it is for.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 748
Quoted 6 lines
convert.c +6
-0+ +if (!entry) { + entry =start_protocol2_filter (&cmd_process_map, cmd); +if (!entry) { +return 0 ; + }
Style; we prefer:
+ if (!entry)
+ return 0;
This is very similar to apply_filter(), but the latter uses start_async() from "run-command.h", with filter_buffer_or_fd() as asynchronous process, which gets passed command to run in struct filter_params. In this function start_protocol2_filter() runs start_command(), synchronous API.
Why the difference?
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 760
Quoted 12 lines
convert.c +12
-0+ } + process = &entry->process; + +if (!(wanted_capability & entry->supported_capabilities)) +return 1 ;// it is OK if the wanted capability is not supported + + if FILTER_SUPPORTS_CLEAN(wanted_capability) + filter_type ="clean" ; + else if FILTER_SUPPORTS_SMUDGE(wanted_capability) + filter_type ="smudge" ; + else + die("unexpected filter type");
Style: it should be
+ if (FILTER_SUPPORTS_CLEAN(wanted_capability))
+ filter_type = "clean";
+ else if (FILTER_SUPPORTS_SMUDGE(wanted_capability))
+ filter_type = "smudge";
+ else
+ die("unexpected filter type");even though by accident the macro provides the parentheses to "if".
Can we make an error/die message more detailed? Maybe it is not possible...
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 766
Quoted 6 lines
convert.c +6
-0+ +if (fd >=0 && !src) { +if (fstat (fd, &file_stat) ==-1 ) +return 0 ; + len = file_stat.st_size; + }
All right, when fstat() can fail? Could we then send contents without size upfront, or is it better to require size to make it more consistent for filter drivers scripts?
Could this whole "send single file" be put in a separate function? Or is it not worth it?
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 742
Quoted 2 lines
convert.c +2
-0+ +sigchain_push (SIGPIPE ,SIG_IGN );
Hmmm... ignoring SIGPIPE was good for one-shot filters. Is it still O.K. for per-command persistent ones?
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 777
Quoted 9 lines
convert.c +9
-0+ +packet_buf_write (&nbuf,"%s\n" , filter_type); + ret &= !direct_packet_write (process->in, nbuf.buf, nbuf.len,1 ); + +if (ret) { +strbuf_reset (&nbuf); +packet_buf_write (&nbuf,"filename=%s\n" , path); + ret = !direct_packet_write (process->in, nbuf.buf, nbuf.len,1 ); + }
Perhaps a better solution would be
if (err)
goto fin_error;rather than this.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 783
Quoted 6 lines
convert.c +6
-0+ +if (ret) { +strbuf_reset (&nbuf); +packet_buf_write (&nbuf,"size=%" PRIuMAX"\n" , (uintmax_t )len); + ret = !direct_packet_write (process->in, nbuf.buf, nbuf.len,1 ); + }
Or maybe extract writing the header for a file into a separate function? This one gets a bit long...
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 790
Quoted 7 lines
convert.c +7
-0+ +if (ret) { +if (fd >=0 ) + ret = !multi_packet_write_from_fd (fd, process->in); +else + ret = !multi_packet_write_from_buf (src, len, process->in); + }
This is not streaming. The above sends whole file, or whole string to the filter process, without draining filter output. If the filter were to read some, then write some, it might deadlock on full buffers, isn't it? Or am I mistaken?
Quoted 6 lines
convert.c +6
-0+ +if (ret && !FILTER_SUPPORTS_STREAM (entry->supported_capabilities)) { + strbuf =packet_read_line (process->out,NULL ); +if (strlen (strbuf) >5 && !strncmp ("size=" , strbuf,5 )) { + expected_bytes = (off_t )strtol (strbuf +5 , &strtol_end,10 ); + ret = (strtol_end != strbuf && errno !=ERANGE );
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 806
Quoted 10 lines
convert.c +10
-0+ }else { + ret =0 ; + } + } + +if (ret) { +strbuf_reset (&nbuf); + ret = !multi_packet_read (process->out, &nbuf, expected_bytes, +FILTER_SUPPORTS_STREAM (entry->supported_capabilities)); + }
What happens if the output of filter does not fit in size_t? I see that (I think) this problem is inherited from the original implementation.
Quoted 7 lines
convert.c +7
-0+ +if (ret) { + filter_result =packet_read_line (process->out,NULL ); + ret = !strcmp (filter_result,"success" ); + } + +sigchain_pop (SIGPIPE );
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 823
Quoted 10 lines
convert.c +10
-0+ +if (ret) { +strbuf_swap (dst, &nbuf); + }else { +if (!filter_result ||strcmp (filter_result,"reject" )) { +// Something went wrong with the protocol filter. Force shutdown! +error ("external filter '%s' failed" , cmd); +kill_protocol2_filter (&cmd_process_map, entry); + } + }
So if Git gets finish signal "success" from filter, it accepts the output. If Git gets finish signal "reject" from filter, it restarts filter (and reject the output - user can retry the command himself / herself). If Git gets any other finish signal, for example "error" (but this is not standarized), then it rejects the output, keeping the unfiltered result, but keeps filtering.
I think it is not described in this detail in the documentation of the new protocol.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 826
Quoted 3 lines
convert.c +3
-0+strbuf_release (&nbuf); +return ret; +}
I wonder if this point might be start of the new patch... but then you would have no way to test what you wrote.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 835
Quoted 9 lines
convert.c +2
-0+static struct convert_driver {const char *name;struct convert_driver *next;const char *smudge;const char *clean; +const char *process;int required; } *user_convert, **user_convert_tail;
All right.
convert.c static int read_convert_config(const char *var, const char *value, void *cb) · line 877
Quoted 9 lines
convert.c +4
-0@@ -526,6 +871,10 @@ static int read_convert_config(const char *var, const char *value, void *cb)if (!strcmp ("clean" , key))return git_config_string (&drv->clean, var, value); +if (!strcmp ("process" , key)) { +return git_config_string (&drv->process, var, value); + } +
All right.
Quoted 4 lines
convert.c +0
-0if (!strcmp ("required" , key)) { drv->required =git_config_bool (var, value);return 0 ;
convert.c int would_convert_to_git_filter_fd(const char *path) · line 1180
Quoted 10 lines
convert.c +6
-1@@ -823,7 +1172,12 @@ int would_convert_to_git_filter_fd(const char *path)if (!ca.drv->required)return 0 ; -return apply_filter (path,NULL ,0 ,-1 ,NULL , ca.drv->clean); +if (!ca.drv->clean && ca.drv->process) +return apply_protocol2_filter ( + path,NULL ,0 ,-1 ,NULL , ca.drv->process,FILTER_CAPABILITIES_CLEAN + ); +else +return apply_filter (path,NULL ,0 ,-1 ,NULL , ca.drv->clean);
Could we augment apply_filter() instead, so that the invocation is
return apply_filter(path, NULL, 0, -1, NULL, ca.drv, FILTER_CLEAN);
Though I am not sure if moving this conditional to apply_filter would be a good idea; maybe wrapper around augmented apply_filter_do()?
Quoted 10 lines
convert.c +2
-1}const char *get_convert_attr_ascii (const char *path)@@ -856,17 +1210,24 @@ int convert_to_git(const char *path, const char *src, size_t len, struct strbuf *dst, enum safe_crlf checksafe) { int ret = 0; -const char *filter =NULL ; +const char *clean_filter =NULL ; +const char *process_filter =NULL ;
convert.c int convert_to_git(const char *path, const char *src, size_t len, · line 1223
Quoted 10 lines
convert.c +2
-1int required =0 ;struct conv_attrs ca;convert_attrs (&ca, path);if (ca.drv) { - filter = ca.drv->clean; + clean_filter = ca.drv->clean; + process_filter = ca.drv->process; required = ca.drv->required; }
All right (assuming un-augmented apply_filter()).
convert.c int convert_to_git(const char *path, const char *src, size_t len, · line 1230
Quoted 8 lines
convert.c +6
-1- ret |=apply_filter (path, src, len,-1 , dst, filter); +if (!clean_filter && process_filter) + ret |=apply_protocol2_filter ( + path, src, len,-1 , dst, process_filter,FILTER_CAPABILITIES_CLEAN + ); +else + ret |=apply_filter (path, src, len,-1 , dst, clean_filter);
I wonder if it would be more readable to write it like this (and of course elsewhere too):
+ if (!clean_filter && process_filter) + ret |= apply_protocol2_filter( + path, src, len, -1, dst, process_filter, FILTER_CAPABILITIES_CLEAN + ); + else + ret |= apply_filter( + path, src, len, -1, dst, clean_filter); + );
Though it would screw up "git blame -C -C -w"
convert.c int convert_to_git(const char *path, const char *src, size_t len, · line 1249
Quoted 8 lines
convert.c +1
-0if (!ret && required)die ("%s: clean filter '%s' failed" , path, ca.drv->name);@@ -885,13 +1246,21 @@ int convert_to_git(const char *path, const char *src, size_t len,void convert_to_git_filter_fd (const char *path,int fd,struct strbuf *dst,enum safe_crlf checksafe) { +int ret =0 ;
Right, 'ret' is needed because we now have two possibilities:clean filter and process filter.
Quoted 20 lines
convert.c +9
-2struct conv_attrs ca;convert_attrs (&ca, path);assert (ca.drv); -assert (ca.drv->clean); +assert (ca.drv->clean || ca.drv->process); + +if (!ca.drv->clean && ca.drv->process) + ret =apply_protocol2_filter ( + path,NULL ,0 , fd, dst, ca.drv->process,FILTER_CAPABILITIES_CLEAN + ); +else + ret =apply_filter (path,NULL ,0 , fd, dst, ca.drv->clean); -if (!apply_filter (path,NULL ,0 , fd, dst, ca.drv->clean)) +if (!ret)die ("%s: clean filter '%s' failed" , path, ca.drv->name);crlf_to_git (path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
convert.c static int convert_to_working_tree_internal(const char *path, const char *src, · line 1274
Quoted 6 lines
convert.c +1
-2@@ -902,14 +1271,16 @@ static int convert_to_working_tree_internal(const char *path, const char *src, size_t len, struct strbuf *dst, int normalizing) { - int ret = 0, ret_filter = 0; -const char *filter =NULL ; + int ret = 0, ret_filter;
Why the change:
- int ret = 0, ret_filter = 0; + int ret = 0, ret_filter;
convert.c static int convert_to_working_tree_internal(const char *path, const char *src, · line 1285
Quoted 12 lines
convert.c +4
-1+const char *smudge_filter =NULL ; +const char *process_filter =NULL ;int required =0 ;struct conv_attrs ca;convert_attrs (&ca, path);if (ca.drv) { - filter = ca.drv->smudge; + process_filter = ca.drv->process; + smudge_filter = ca.drv->smudge; required = ca.drv->required; }
All right, the same.
[...]
Quoted 10 lines
t/t0021-conversion.sh +2
-0@@ -296,4 +296,409 @@ test_expect_success 'disable filter with empty override' ' test_must_be_empty err ' +test_expect_success PERL 'required process filter should filter data' ' + test_config_global filter.protocol.process "$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge shutdown" &&
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 310
Quoted 10 lines
t/t0021-conversion.sh +10
-0+ test_config_global filter.protocol.required true && + rm -rf repo && + mkdir repo && + ( + cd repo && + git init && + + echo "*.r filter=protocol" >.gitattributes && + git add . && + git commit . -m "test commit" &&
This is more of "Initial commit", not that it matters
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 313
Quoted 3 lines
t/t0021-conversion.sh +3
-0+ git branch empty && + + cat ../test.o >test.r &&
Err, the above is just copying file, isn't it? Maybe it was copied from other tests, I have not checked.
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 316
Quoted 3 lines
t/t0021-conversion.sh +3
-0+ echo "test22" >test2.r && + mkdir testsubdir && + echo "test333" >testsubdir/test3.r &&
All right, we test text file, we test binary file (I assume), we test file in a subdirectory. What about testing empty file? Or large file which would not fit in the stdin/stdout buffer (as EXPENSIVE test)?
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 319
Quoted 3 lines
t/t0021-conversion.sh +3
-0+ + rm -f rot13-filter.log && + git add . &&
So this runs "clean" filter, storing cleaned contents in the index.
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 324
Quoted 5 lines
t/t0021-conversion.sh +5
-0+ sort rot13-filter.log | uniq -c | sed "s/^[ ]*//" >uniq-rot13-filter.log && + cat >expected_add.log <<-\EOF && + 1 IN: clean test.r 57 [OK] -- OUT: 57 [OK] + 1 IN: clean test2.r 7 [OK] -- OUT: 7 [OK] + 1 IN: clean testsubdir/test3.r 8 [OK] -- OUT: 8 [OK]
And we check the "know size upfront" case (mistakenly called non-"stream").
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 325
Quoted 1 line
t/t0021-conversion.sh +1
-0+ 1 IN: shutdown -- [OK]
And test "shutdown" capability (not as separate test).
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 328
Quoted 3 lines
t/t0021-conversion.sh +3
-0+ 1 start + 1 wrote filter header + EOF
And we are required to keep the expected_add.log file sorted by hand???
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 331
Quoted 3 lines
t/t0021-conversion.sh +3
-0+ test_cmp expected_add.log uniq-rot13-filter.log && + + >rot13-filter.log &&
Truncate log. Still in the same test.
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 332
Quoted 1 line
t/t0021-conversion.sh +1
-0+ git commit . -m "test commit" &&
This is test commit with files undergoing "clean" part of filter.
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 334
Quoted 2 lines
t/t0021-conversion.sh +2
-0+ sort rot13-filter.log | uniq -c | sed "s/^[ ]*//" | + sed "s/^\([0-9]\) IN: clean/x IN: clean/" >uniq-rot13-filter.log &&
There is known performance regression, in that filter is run more than once on given file.
Actually... why it does not use cleaned-up contents from the index?
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 341
Quoted 7 lines
t/t0021-conversion.sh +7
-0+ cat >expected_commit.log <<-\EOF && + x IN: clean test.r 57 [OK] -- OUT: 57 [OK] + x IN: clean test2.r 7 [OK] -- OUT: 7 [OK] + x IN: clean testsubdir/test3.r 8 [OK] -- OUT: 8 [OK] + 1 IN: shutdown -- [OK] + 1 start + 1 wrote filter header
Right, this is the goal of the patch series: for filter to be started only once per git command invocation.
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 344
Quoted 3 lines
t/t0021-conversion.sh +3
-0+ EOF + test_cmp expected_commit.log uniq-rot13-filter.log && +
Still in the same test, even though we would be testing "smudge" capability now.
It's a pity that t/test-lib.sh does not support subtests from the TAP specification (Test Anything Protocol that Git testsuite uses).
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 347
Quoted 3 lines
t/t0021-conversion.sh +3
-0+ >rot13-filter.log && + rm -f test?.r testsubdir/test3.r && + git checkout . &&
All right, we removed some files so that "git checkout ." could restore them to life.
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 348
Quoted 1 line
t/t0021-conversion.sh +1
-0+ cat rot13-filter.log | grep -v "IN: clean" >smudge-rot13-filter.log &&
Useless use of cat
+ grep -v "IN: clean" rot13-filter.log >smudge-rot13-filter.log &&
Also: why 'git checkout <path>' would run "clean" filter? Is it existing strange behaviour?
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 355
Quoted 7 lines
t/t0021-conversion.sh +7
-0+ cat >expected_checkout.log <<-\EOF && + start + wrote filter header + IN: smudge test2.r 7 [OK] -- OUT: 7 [OK] + IN: smudge testsubdir/test3.r 8 [OK] -- OUT: 8 [OK] + IN: shutdown -- [OK] + EOF
This time without 'sort | uniq -c'. Is it really needed for the "good" case, or is it there for two cases to look similar?
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 358
Quoted 3 lines
t/t0021-conversion.sh +3
-0+ test_cmp expected_checkout.log smudge-rot13-filter.log && + + git checkout empty &&
Shouldn't we check that switching to branch 'empty' does not run filters, or is it covered by other tests? Or perhaps this simply does not matter here, is it?
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 361
Quoted 3 lines
t/t0021-conversion.sh +3
-0+ + >rot13-filter.log && + git checkout master &&
Does it test different callpath than 'git checkout .'? Well, the set of files is different...
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 372
Quoted 11 lines
t/t0021-conversion.sh +11
-0+ cat rot13-filter.log | grep -v "IN: clean" >smudge-rot13-filter.log && + cat >expected_checkout_master.log <<-\EOF && + start + wrote filter header + IN: smudge test.r 57 [OK] -- OUT: 57 [OK] + IN: smudge test2.r 7 [OK] -- OUT: 7 [OK] + IN: smudge testsubdir/test3.r 8 [OK] -- OUT: 8 [OK] + IN: shutdown -- [OK] + EOF + test_cmp expected_checkout_master.log smudge-rot13-filter.log && +
And here we start checking that the filter did filter, that is the content in the repository is "clean"ed-up. Still the same test.
Quoted 7 lines
t/t0021-conversion.sh +7
-0+ ./../rot13.sh <test.r >expected && + git cat-file blob :test.r >actual && + test_cmp expected actual && + + ./../rot13.sh <test2.r >expected && + git cat-file blob :test2.r >actual && + test_cmp expected actual &&
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 389
Quoted 10 lines
t/t0021-conversion.sh +10
-0+ + ./../rot13.sh <testsubdir/test3.r >expected && + git cat-file blob :testsubdir/test3.r >actual && + test_cmp expected actual + ) +' + +test_expect_success PERL 'required process filter should filter data stream' ' + test_config_global filter.protocol.process "$TEST_DIRECTORY/t0021/rot13-filter.pl stream clean smudge" && + test_config_global filter.protocol.required true &&
Errr... I don't see how it is different from the previous test. [...]
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 471
Quoted 2 lines
t/t0021-conversion.sh +2
-0+ +test_expect_success PERL 'required process filter should filter smudge data and one-shot filter should clean' '
All right, so this tests the precedence... well, it doesn't.
It tests that process filter with "smudge" capability only works well with one-shot clean filter.
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 473
Quoted 2 lines
t/t0021-conversion.sh +2
-0+ test_config_global filter.protocol.clean ./../rot13.sh && + test_config_global filter.protocol.process "$TEST_DIRECTORY/t0021/rot13-filter.pl smudge" &&
Why the difference in pathnames (the directory part) between those two?
Quoted 14 lines
t/t0021-conversion.sh +14
-0+ test_config_global filter.protocol.required true && + rm -rf repo && + mkdir repo && + ( + cd repo && + git init && + + echo "*.r filter=protocol" >.gitattributes && + git add . && + git commit . -m "test commit" && + git branch empty && + + cat ../test.o >test.r && + echo "test22" >test2.r &&
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 497
Quoted 10 lines
t/t0021-conversion.sh +10
-0+ mkdir testsubdir && + echo "test333" >testsubdir/test3.r && + + rm -f rot13-filter.log && + git add . && + test_must_be_empty rot13-filter.log && + + >rot13-filter.log && + git commit . -m "test commit" && + test_must_be_empty rot13-filter.log &&
All right, these tests that process filter is not ran. But we don't know if it is because it lacks capability, or because it is overriden by one-shot filter (well, that comes later).
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 356
Quoted 12 lines
t/t0021-conversion.sh +12
-0+ + >rot13-filter.log && + rm -f test?.r testsubdir/test3.r && + git checkout . && + cat rot13-filter.log | grep -v "IN: clean" >smudge-rot13-filter.log && + cat >expected_checkout.log <<-\EOF && + start + wrote filter header + IN: smudge test2.r 7 [OK] -- OUT: 7 [OK] + IN: smudge testsubdir/test3.r 8 [OK] -- OUT: 8 [OK] + EOF + test_cmp expected_checkout.log smudge-rot13-filter.log &&
This part is repeated many, many times. Maybe add some helper shell function for this?
[...]
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 383
Quoted 11 lines
t/t0021-conversion.sh +11
-0+ ./../rot13.sh <test.r >expected && + git cat-file blob :test.r >actual && + test_cmp expected actual && + + ./../rot13.sh <test2.r >expected && + git cat-file blob :test2.r >actual && + test_cmp expected actual && + + ./../rot13.sh <testsubdir/test3.r >expected && + git cat-file blob :testsubdir/test3.r >actual && + test_cmp expected actual
Here we test that equivalent one-shot cleanup filter was run. Here also we have repeated contents; maybe some helper function would make it shorter?
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 442
Quoted 2 lines
t/t0021-conversion.sh +2
-0+ ) +'
Here I am stopping examining tests in detail.
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 578
Quoted 2 lines
t/t0021-conversion.sh +2
-0+test_expect_success PERL 'required process filter should clean only' ' +test_expect_success PERL 'required process filter should process files larger LARGE_PACKET_MAX' '
Those two tests do not depend on being required or not; it is only that without required they would fail softly in case of latter test (which we can detect too).
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 622
Quoted 2 lines
t/t0021-conversion.sh +2
-0+test_expect_success PERL 'required process filter should with clean error should fail' ' +test_expect_success PERL 'process filter should restart after unexpected write failure' '
So these two are sort of complimentary. When process is required, then it should fail if it cannot filter some file. If it is not, it should keep processing other files.
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 672
Quoted 1 line
t/t0021-conversion.sh +1
-0+test_expect_success PERL 'process filter should not restart after intentionally rejected file' '
Uh... all right, so "reject" means that filter cannot continue? Strange meaning for 'reject', though ;-)
Quoted 7 lines
t/t0021-conversion.sh +0
-0test_donet/t0021/rot13-filter.pl new file +0
-0
t/t0021/rot13-filter.pl line 7
Quoted 7 lines
t/t0021/rot13-filter.pl new file +7
-0@@ -0,0 +1,177 @@ +#!/usr/bin/perl +# +# Example implementation for the Git filter protocol version 2 +# See Documentation/gitattributes.txt, section "Filter Protocol" +# +# The script takes the list of supported protocol capabilities as +# arguments ("stream", "clean", and "smudge" are supported).
What about "shutdown"?
t/t0021/rot13-filter.pl line 13
Quoted 6 lines
t/t0021/rot13-filter.pl new file +6
-0+# +# This implementation supports three special test cases: +# (1) If data with the filename "clean-write-fail.r" is processed with +# a "clean" operation then the write operation will die. +# (2) If data with the filename "smudge-write-fail.r" is processed with +# a "smudge" operation then the write operation will die.
All right, so it is hard failure with filter script dying.
t/t0021/rot13-filter.pl line 16
Quoted 3 lines
t/t0021/rot13-filter.pl new file +3
-0+# (3) If data with the filename "failure.r" is processed with any +# operation then the filter signals that the operation was not +# successful.
All right, so it is failure detected by filter script and signalled to Git.
t/t0021/rot13-filter.pl line 20
Quoted 4 lines
t/t0021/rot13-filter.pl new file +4
-0+# + +use strict; +use warnings;
So no more "use autodie", because of compatibility with old Perls.
t/t0021/rot13-filter.pl line 23
Quoted 3 lines
t/t0021/rot13-filter.pl new file +3
-0+ +my $MAX_PACKET_CONTENT_SIZE = 65516; +my @capabilities = @ARGV;
No autoflush this time?
Quoted 9 lines
t/t0021/rot13-filter.pl new file +9
-0+ +sub rot13 { + my ($str) = @_; + $str =~ y/A-Za-z/N-ZA-Mn-za-m/; + return $str; +} + +sub packet_read { + my $buffer;
t/t0021/rot13-filter.pl line 42
Quoted 10 lines
t/t0021/rot13-filter.pl new file +10
-0+ my $bytes_read = read STDIN, $buffer, 4; + if ( $bytes_read == 0 ) { + return; + } + elsif ( $bytes_read != 4 ) { + die "invalid packet size '$bytes_read' field"; + } + my $pkt_size = hex($buffer); + if ( $pkt_size == 0 ) { + return ( 1, "" );
Unusual return convention. Though it is a test script, so it doesn't matter much.
t/t0021/rot13-filter.pl line 48
Quoted 6 lines
t/t0021/rot13-filter.pl new file +6
-0+ } + elsif ( $pkt_size > 4 ) { + my $content_size = $pkt_size - 4; + $bytes_read = read STDIN, $buffer, $content_size; + if ( $bytes_read != $content_size ) { + die "invalid packet";
More detailed error message, maybe?
Quoted 41 lines
t/t0021/rot13-filter.pl new file +41
-0+ } + return ( 0, $buffer ); + } + else { + die "invalid packet size"; + } +} + +sub packet_write { + my ($packet) = @_; + print STDOUT sprintf( "%04x", length($packet) + 4 ); + print STDOUT $packet; + STDOUT->flush(); +} + +sub packet_flush { + print STDOUT sprintf( "%04x", 0 ); + STDOUT->flush(); +} + +open my $debug, ">>", "rot13-filter.log"; +print $debug "start\n"; +$debug->flush(); + +packet_write("git-filter-protocol\n"); +packet_write("version 2\n"); +packet_write( "capabilities " . join( ' ', @capabilities ) . "\n" ); +print $debug "wrote filter header\n"; +$debug->flush(); + +while (1) { + my $command = packet_read(); + unless ( defined($command) ) { + exit(); + } + chomp $command; + print $debug "IN: $command"; + $debug->flush(); + + if ( $command eq "shutdown" ) { + print $debug " -- [OK]";
t/t0021/rot13-filter.pl line 99
Quoted 10 lines
t/t0021/rot13-filter.pl new file +10
-0+ $debug->flush(); + packet_write("done\n"); + exit(); + } + + my ($filename) = packet_read() =~ /filename=([^=]+)\n/; + print $debug " $filename"; + $debug->flush(); + my ($filelen) = packet_read() =~ /size=([^=]+)\n/; + chomp $filelen;
I think this chomp is not needed, as "\n" is not included. Though the regexp should probably be anchored.
t/t0021/rot13-filter.pl line 106
Quoted 7 lines
t/t0021/rot13-filter.pl new file +7
-0+ print $debug " $filelen"; + $debug->flush(); + + $filelen =~ /\A\d+\z/ or die "bad filelen: $filelen"; + my $output; + + if ( $filelen > 0 ) {
So here is a special case for $filelen = 0.
Negative $filelen is not allowed, via regexp.
Quoted 9 lines
t/t0021/rot13-filter.pl new file +9
-0+ my $input = ""; + { + binmode(STDIN); + my $buffer; + my $done = 0; + while ( !$done ) { + ( $done, $buffer ) = packet_read(); + $input .= $buffer; + }
t/t0021/rot13-filter.pl line 125
Quoted 10 lines
t/t0021/rot13-filter.pl new file +10
-0+ print $debug " [OK] -- "; + $debug->flush(); + } + + if ( $command eq "clean" and grep( /^clean$/, @capabilities ) ) { + $output = rot13($input); + } + elsif ( $command eq "smudge" and grep( /^smudge$/, @capabilities ) ) { + $output = rot13($input); + }
These two conditionals could be shortened, but then they would be less readable. Or not:
if ( grep { $_ eq $command } @capabilities ) {
$output = rot13($input);
}Quoted 12 lines
t/t0021/rot13-filter.pl new file +12
-0+ else { + die "bad command $command"; + } + } + + my $output_len = length($output); + if ( $filename eq "reject.r" ) { + $output_len = 0; + } + + if ( grep( /^stream$/, @capabilities ) ) { + print $debug "OUT: STREAM ";
t/t0021/rot13-filter.pl line 147
Quoted 10 lines
t/t0021/rot13-filter.pl new file +10
-0+ } + else { + packet_write("size=$output_len\n"); + print $debug "OUT: $output_len "; + } + $debug->flush(); + + if ( $filename eq "reject.r" ) { + packet_write("reject\n"); + print $debug "[REJECT]\n"; # Could also be an error
How if could be an error?
t/t0021/rot13-filter.pl line 154
Quoted 7 lines
t/t0021/rot13-filter.pl new file +7
-0+ $debug->flush(); + } + + if ( $output_len > 0 ) { + if (( $command eq "clean" and $filename eq "clean-write-fail.r" ) + or + ( $command eq "smudge" and $filename eq "smudge-write-fail.r" ))
Perhaps simply:
+ if ( $filename eq "${command}-write-fail.r" ) {
Quoted 24 lines
t/t0021/rot13-filter.pl new file +23
-0+ { + print $debug "[WRITE FAIL]\n"; + $debug->flush(); + die "write error"; + } + else { + while ( length($output) > 0 ) { + my $packet = substr( $output, 0, $MAX_PACKET_CONTENT_SIZE ); + packet_write($packet); + if ( length($output) > $MAX_PACKET_CONTENT_SIZE ) { + $output = substr( $output, $MAX_PACKET_CONTENT_SIZE ); + } + else { + $output = ""; + } + } + packet_flush(); + packet_write("success\n"); + print $debug "[OK]\n"; + $debug->flush(); + } + } +}
Re: [PATCH v3 10/10] convert: add filter.<driver>.process option
Quoted 11 lines · Jakub Narębski
W dniu 30.07.2016 o 01:38, [email protected] pisze:
[...]Documentation/gitattributes.txt substitution. For example:
Quoted 3 lines
Documentation/gitattributes.txt +3
-0+Please note that you cannot use an existing filter.<driver>.clean +or filter.<driver>.smudge command as filter.<driver>.process +command.I think it would be more readable and easier to understand to write:
Quoted 4 lines
... you cannot use an existing ... command with
filter.<driver>.processAbout the style: wouldn't
filter.<driver>.processbe better?
OK, changed it!
Quoted 9 lines
Quoted 2 lines
As soon as Git would detect a file that needs to be
+processed by this filter, it would stop responding.This is quite convoluted, and hard to understand. I would say that because
cleanandsmudgefilters are expected to read first, while Git expectsprocessfilter to say first, usingcleanorsmudgefilter without changes asprocessfilter would lead to git command deadlocking / hanging / stopping responding.
How about this:
Please note that you cannot use an existing filter.<driver>.clean or filter.<driver>.smudge command with filter.<driver>.process because the former two use a different inter process communication protocol than the latter one. As soon as Git would detect a file that needs to be processed by such an invalid "process" filter, it would wait for a proper protocol handshake and appear "hanging".
Quoted 21 lines
Quoted 21 lines
(quoted diff) +2
-0+ +Interaction between checkin/checkout attributes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^convert.c +1
-0@@ -3,6 +3,7 @@#include "run-command.h" #include "quote.h" #include "sigchain.h" +#include "pkt-line.h" /* * convert.c - convert a file when checking it out and checking it in. @@ -481,11 +482,355 @@ static int apply_filter(const char *path, const char *src, size_t len, int fd,return ret; }
Quoted 5 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 485
Quoted 1 line
convert.c +1
-0+static int multi_packet_read (int fd_in,struct strbuf *sb,size_t expected_bytes,int is_stream)About name of this function:
multi_packet_readis fine, though I wonder ifpacket_read_in_fullwith nearly the same parameters aspacket_read, orpacket_read_till_flush, orread_in_full_packetizedwould be better.
I like multi_packet_read and will rename!
Quoted 7 lines
Also, the problem is that while we know that what
packet_read()stores would fit in memory (insize_t), it is not true for reading whole file, which might be very large - for example huge graphical assets like raw images or raw videos, or virtual machine images. Isn't that the goal of git-LFS solutions, which need this feature? Shouldn't we have then bothmulti_packet_read_to_fdandmulti_packet_read_to_buf, or whatever?
Git LFS works well with the current clean/smudge mechanism that uses the same on in memory buffers. I understand your concern but I think this improvement is out of scope for this patch series.
Quoted 1 line
Also, if we have
fd_in, then perhapssb_out?
Agreed!
Quoted 4 lines
I am also unsure if
expected_bytes(orexpected_size) should not be just a size hint, leaving handing mismatch between expected size and real size of output to the caller; then theis_streamwould be not needed.
As mentioned in a previous email... I will drop the "size" support in this patch series as it is not really needed.
Quoted 8 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 488
Quoted 3 lines
convert.c +3
-0+{ +int bytes_read; +size_t total_bytes_read =0 ;Why
bytes_readis int, whiletotal_bytes_readissize_t? Ah, I see thatpacket_read()returns an int. It should bessize_t, just likeread(), isn't it? But we know that packet size is limited, and would fit in an int (or would it?).
Yes, it is limited but I agree on ssize_t!
Quoted 2 lines
Also,
total_bytes_readcould overflowsize_t, but then we would have problems storing the result in strbuf.
Would that check be ok?
if (total_bytes_read > SIZE_MAX - bytes_read)
return 1; // total_bytes_read would overflow and is not representable
Quoted 5 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 490
Quoted 2 lines
convert.c +2
-0+if (expected_bytes ==0 && !is_stream) +return 0 ;So in all cases *except* size = 0 we expect flush packet after the contents, but size = 0 is a corner case without flush packet?
I agree that is inconsistent... I will change it!
Quoted 10 lines
Quoted 10 lines
convert.c +10
-0+ +if (is_stream) +strbuf_grow (sb,LARGE_PACKET_MAX );// allocate space for at least one packet +else +strbuf_grow (sb,st_add (expected_bytes,1 ));// add one extra byte for the packet flush + + do { + bytes_read = packet_read( + fd_in, NULL, NULL, + sb->buf + total_bytes_read, sb->len - total_bytes_read - 1,
Quoted 8 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 504
Quoted 4 lines
convert.c +4
-0+PACKET_READ_GENTLE_ON_EOF + ); +if (bytes_read <0 ) +return 1 ;// unexpected EOF Don't we usually return negative numbers on error? Ah, I see that the return is a bool, which allows to use boolean expression with 'return'. But I am still unsure if it is good API, this return value.
According to Peff zero for success is the usual style:
http://public-inbox.org/git/20160728133523.GB21311%40sigill.intra.peff.net/
Quoted 3 lines
If we move handling of size mismatch to the caller, then the function can simply return the size of data read (probably
off_toruint64_t). Then the caller can check if it is what it expected, and react accordingly.
True, but as discussed previously I will remove the size.
Quoted 7 lines
Quoted 7 lines
convert.c +7
-0+ +if (is_stream && + bytes_read >0 && + sb->len - total_bytes_read -1 <=0 ) +strbuf_grow (sb,st_add (sb->len,LARGE_PACKET_MAX )); + total_bytes_read += bytes_read; + }
Quoted 8 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 514
Quoted 3 lines
convert.c +3
-0+while ( + bytes_read >0 &&// the last packet was no flush + sb->len - total_bytes_read - 1 > 0 // we still have space left in the bufferAh, so buffer is resized only in the '
is_stream' case. Perhaps then use an "int options" instead of 'is_stream', and have one of flags tell if we should resize or not, that is if size parameter is hint or a strict limit.
Obsolete
Quoted 9 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 520
Quoted 6 lines
convert.c +6
-0+ ); +strbuf_setlen (sb, total_bytes_read); +return (is_stream ?0 : expected_bytes != total_bytes_read); +} + +static int multi_packet_write_from_fd (const int fd_in,const int fd_out)Is it equivalent of
copy_fd()function, but where destination uses pkt-line and we need to pack data into pkt-lines?
Correct!
Quoted 9 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 525
Quoted 5 lines
convert.c +5
-0+{ +int did_fail =0 ; +ssize_t bytes_to_write; +while (!did_fail) { + bytes_to_write =xread (fd_in,PKTLINE_DATA_START (packet_buffer),PKTLINE_DATA_LEN );Using global variable
packet_buffermakes this code thread-unsafe, isn't it? But perhaps that is not a problem, because other functions are also using this global variable.
Correct!
Quoted 1 line
It is more of
PKTLINE_DATA_MAXLEN, isn't it?
Agreed, will change!
Quoted 13 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 533
Quoted 9 lines
convert.c +8
-0+if (bytes_to_write <0 ) +return 1 ; +if (bytes_to_write ==0 ) +break ; + did_fail |=direct_packet_write (fd_out, packet_buffer,PKTLINE_HEADER_LEN + bytes_to_write,1 ); + } +if (!did_fail) + did_fail =packet_flush_gentle (fd_out);Shouldn't we try to flush even if there was an error? Or is it that if there is an error writing, then there is some problem such that we know that flush would not work?
Right, that's what I though.
Quoted 5 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 534
Quoted 1 line
convert.c +1
-0+return did_fail;Return true on fail? Shouldn't we follow example of
copy_fd()fromcopy.c, and returnCOPY_READ_ERROR, orCOPY_WRITE_ERROR, orPKTLINE_WRITE_ERROR?
OK. How about this?
static int multi_packet_write_from_fd(const int fd_in, const int fd_out)
{
int did_fail = 0;
ssize_t bytes_to_write;
while (!did_fail) {
bytes_to_write = xread(fd_in, PKTLINE_DATA_START(packet_buffer), PKTLINE_DATA_MAXLEN);
if (bytes_to_write < 0)
return COPY_READ_ERROR;
if (bytes_to_write == 0)
break;
did_fail |= direct_packet_write(fd_out, packet_buffer, PKTLINE_HEADER_LEN + bytes_to_write, 1);
}
if (!did_fail)
did_fail = packet_flush_gently(fd_out);
return (did_fail ? COPY_WRITE_ERROR : 0);
}Quoted 7 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 537
Quoted 3 lines
convert.c +3
-0+} + +static int multi_packet_write_from_buf (const char *src,size_t len,int fd_out)It is equivalent of
write_in_full(), with different order of parameters, but where destination file descriptor expects pkt-line and we need to pack data into pkt-lines?
True. Do you suggest to reorder parameters? I also would like to rename src to src_in, OK?
Quoted 2 lines
NOTE: function description comments?
What do you mean here?
Quoted 23 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 541
Quoted 4 lines
convert.c +4
-0+{ +int did_fail =0 ; +size_t bytes_written =0 ; +size_t bytes_to_write;Note to self:
bytes_to_writeshould fit insize_t, as it is limited toPKTLINE_DATA_LEN.bytes_writtenshould fit insize_t, as it is at most len, which is of typesize_t.convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 550
Quoted 9 lines
convert.c +9
-0+while (!did_fail) { +if ((len - bytes_written) >PKTLINE_DATA_LEN ) + bytes_to_write =PKTLINE_DATA_LEN ; +else + bytes_to_write = len - bytes_written; +if (bytes_to_write ==0 ) +break ; + did_fail |=direct_packet_write_data (fd_out, src + bytes_written, bytes_to_write,1 ); + bytes_written += bytes_to_write;Ah, I see now why we need both
direct_packet_write()anddirect_packet_write_data(). Nice abstraction, makes for clear code.
Quoted 1 line
The last parameter of '1' means 'gently', isn't it?
Correct. Thanks :)
Quoted 8 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 535
Quoted 5 lines
convert.c +5
-0+ } +if (!did_fail) + did_fail =packet_flush_gentle (fd_out); +return did_fail; +}I think all three/four of those functions should be added in a separate commit, separate patch in patch series.
OK
Quoted 7 lines
Namely:
- for git -> filter:
* read from fd, write pkt-line to fd (off_t)
* read from str+len, write pkt-line to fd (size_t,ssize_t)
- for filter -> git:
* read pkt-line from fd, write to fd (off_t)
This one does not exist.
Quoted 4 lines
* read pkt-line from fd, write to str+len (
size_t,ssize_t)Perhaps some of those can be in one overloaded function, perhaps it would be easier to keep them separate.
I would like to keep them separate as it is easier to comprehend.
Quoted 3 lines
Also, I do wonder how the fetch / push code spools pack file received over pkt-lines to disk. Can we reuse that code?
I haven't found any.
Quoted 2 lines
Or maybe that code
could use those new functions?
I think so, but this would be out of scope for this series :)
Quoted 30 lines
Quoted 4 lines
convert.c +4
-0+ +#define FILTER_CAPABILITIES_STREAM 0x1 +#define FILTER_CAPABILITIES_CLEAN 0x2 +#define FILTER_CAPABILITIES_SMUDGE 0x4convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 569
Quoted 10 lines
convert.c +10
-0+#define FILTER_CAPABILITIES_SHUTDOWN 0x8 +#define FILTER_SUPPORTS_STREAM (type) ((type) & FILTER_CAPABILITIES_STREAM) +#define FILTER_SUPPORTS_CLEAN (type) ((type) & FILTER_CAPABILITIES_CLEAN) +#define FILTER_SUPPORTS_SMUDGE (type) ((type) & FILTER_CAPABILITIES_SMUDGE) +#define FILTER_SUPPORTS_SHUTDOWN (type) ((type) & FILTER_CAPABILITIES_SHUTDOWN) + +struct cmd2process { +struct hashmap_entry ent;/* must be the first member! */ +const char *cmd; +int supported_capabilities;I wonder if switching from int (perhaps with field width of 1 to denote that it is boolean-like flag) to mask makes it more readable, or less. But I think it is.
Reading
Documentation/technical/api-hashmap.txtI found the following recommendation:
struct hashmap_entry::An opaque structure representing an entry in the hash table, which must be used as first member of user data structures. Ideally it should be followed by an int-sized member to prevent unused memory on 64-bit systems due to alignment.
Quoted 5 lines
Therefore it "int
supported_capabilities" should precede "const char *cmd", I think. Though it is not strictly necessary; it is not as if this hash table were large (maximum size is limited by the number of filter drivers configured), so we don't waste much space due to internal padding / due to alignment.
Thanks! I will change it to your suggestion anyway!
Quoted 9 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 574
Quoted 6 lines
convert.c +5
-0+struct child_process process; +}; + +static int cmd_process_map_initialized =0 ; +static struct hashmap cmd_process_map;Reading
Documentation/technical/api-hashmap.txtI see that:
Quoted 4 lines
tablesizeis the allocated size of the hash table. A non-0 value indicates that the hashmap is initialized.So
cmd_process_map_initializedis not really needed, is it?
I copied that from config.c:
https://github.com/git/git/blob/f8f7adce9fc50a11a764d57815602dcb818d1816/config.c#L1425-L1428
git grep "tablesize" reveals that the check for tablesize is only used in hashmap.c ... so what approach should we use?
Quoted 12 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 581
Quoted 7 lines
convert.c +7
-0+ +static int cmd2process_cmp (const struct cmd2process *e1, +const struct cmd2process *e2, +const void *unused) +{ +return strcmp (e1->cmd, e2->cmd); +}Well, to be exact (which is decidely not needed!) two commands might be equivalent not being identical as strings (e.g. extra space between parameters). But it is something the user should care about, not Git.
Quoted 5 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 583
Quoted 2 lines
convert.c +2
-0+ +static struct cmd2process *find_protocol2_filter_entry (struct hashmap *hashmap,const char *cmd)I'm not sure if *_protocol2_* is needed; those functions are static, local to
convert.c.
I want to make sure that the reader understands that these functions are related to the filter protocol version 2. Not OK?
Quoted 6 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 585
Quoted 2 lines
convert.c +2
-0+{ +struct cmd2process k;Does this name of variable 'k' follow established convention? 'key' would be more descriptive, but it's not as if this function was long; so 'k' is all right, I think.
I agree on "key".
Quoted 11 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 591
Quoted 7 lines
convert.c +6
-0+hashmap_entry_init (&k,strhash (cmd)); + k.cmd = cmd; +return hashmap_get (hashmap, &k,NULL ); +} + +static void kill_protocol2_filter (struct hashmap *hashmap,struct cmd2process *entry) {Programming style: the opening brace should be on separate line, that is:
Quoted 2 lines
+static void
kill_protocol2_filter(struct hashmap *hashmap, struct cmd2process *entry)+{
Agreed!
Quoted 16 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 602
Quoted 11 lines
convert.c +11
-0+if (!entry) +return ; +sigchain_push (SIGPIPE ,SIG_IGN ); +close (entry->process.in); +close (entry->process.out); +sigchain_pop (SIGPIPE ); +finish_command (&entry->process); +child_process_clear (&entry->process); +hashmap_remove (hashmap, entry,NULL ); +free (entry); +}All those, from #define
FILTER_CAPABILITIES_to here could be put in a separate patch, to reduce size of this one. But I am less sure that it is worth it for this case.
Quoted 6 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 605
Quoted 3 lines
convert.c +3
-0+ +void shutdown_protocol2_filter (pid_t pid) +{[...]
In my opinion this should be postponed to a separate commit.
Agreed!
Quoted 29 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 635
Quoted 4 lines
convert.c +3
-0+} + +static struct cmd2process *start_protocol2_filter (struct hashmap *hashmap,const char *cmd)This has some parts in common with existing
filter_buffer_or_fd(). I wonder if it would be worth to extract those common parts.But perhaps it would be better to leave such refactoring for later.
Quoted 6 lines
convert.c +6
-0+{ +int did_fail; +struct cmd2process *entry; +struct child_process *process; +const char *argv[] = { cmd,NULL }; +struct string_list capabilities =STRING_LIST_INIT_NODUP ;convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 651
Quoted 10 lines
convert.c +10
-0+char *capabilities_buffer; +int i; + + entry =xmalloc (sizeof (*entry)); +hashmap_entry_init (entry,strhash (cmd)); + entry->cmd = cmd; + entry->supported_capabilities =0 ; + process = &entry->process; + +child_process_init (process);
filter_buffer_or_fd()uses instead
Quoted 7 lines
struct
child_processchild_process=CHILD_PROCESS_INIT;But I see that you need to access
&entry->processanyway, so you need to have it here, and in this casechild_process_init()is equivalent.I wonder if it would be worth it to use strbuf for cmd.
What do you mean by "worth it to use strbuf for cmd"? Why would we need a strbuf?
Quoted 8 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 657
Quoted 6 lines
convert.c +6
-0+ process->argv = argv; + process->use_shell =1 ; + process->in =-1 ; + process->out =-1 ; + process->clean_on_exit =1 ; + process->clean_on_exit_handler = shutdown_protocol2_filter;These two lines are new, and related to the "shutdown" capability, isn't it?
Yes.
Quoted 49 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 661
Quoted 5 lines
convert.c +4
-0+ +if (start_command (process)) { +error ("cannot fork to run external filter '%s'" , cmd); +kill_protocol2_filter (hashmap, entry);I guess the alternative solution of adding filter to the hashmap only after starting the process would be racy?
Ah, disregard that. I see that this pattern is a common way to error out in this function (for process-related errors).
Quoted 37 lines
convert.c +37
-0+return NULL ; + } + +sigchain_push (SIGPIPE ,SIG_IGN ); + did_fail =strcmp (packet_read_line (process->out,NULL ),"git-filter-protocol" ); +if (!did_fail) + did_fail |=strcmp (packet_read_line (process->out,NULL ),"version 2" ); +if (!did_fail) + capabilities_buffer =packet_read_line (process->out,NULL ); +else + capabilities_buffer =NULL ; +sigchain_pop (SIGPIPE ); + +if (!did_fail && capabilities_buffer) { +string_list_split_in_place (&capabilities, capabilities_buffer,' ' ,-1 ); +if (capabilities.nr >1 && + !strcmp (capabilities.items[0 ].string,"capabilities" )) { +for (i =1 ; i < capabilities.nr; i++) { +const char *requested = capabilities.items[i].string; +if (!strcmp (requested,"stream" )) { + entry->supported_capabilities |=FILTER_CAPABILITIES_STREAM ; + }else if (!strcmp (requested,"clean" )) { + entry->supported_capabilities |=FILTER_CAPABILITIES_CLEAN ; + }else if (!strcmp (requested,"smudge" )) { + entry->supported_capabilities |=FILTER_CAPABILITIES_SMUDGE ; + }else if (!strcmp (requested,"shutdown" )) { + entry->supported_capabilities |=FILTER_CAPABILITIES_SHUTDOWN ; + }else { +warning ( +"external filter '%s' requested unsupported filter capability '%s'" , + cmd, requested + ); + } + } + }else { +error ("filter capabilities not found" ); + did_fail =1 ;
Quoted 8 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 701
Quoted 3 lines
convert.c +3
-0+ } +string_list_clear (&capabilities,0 ); + }I wonder if the above conditional wouldn't be better to be put in a separate function,
parse_filter_capabilities(capabilities_buffer), returning a mask, or having mask as an out parameter, and returning an error condition.
Agreed.
Quoted 7 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 704
Quoted 3 lines
convert.c +3
-0+ +if (did_fail) { +error ("initialization for external filter '%s' failed" , cmd);More detailed information not needed, because one can use
GIT_PACKET_TRACE. Would it be worth add this information as a kind of advice, or put it in the documentation of theprocessoption?
I will put it into the docs.
Quoted 15 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 715
Quoted 12 lines
convert.c +11
-0+kill_protocol2_filter (hashmap, entry); +return NULL ; + } + +hashmap_add (hashmap, entry); +return entry; +} + +static int apply_protocol2_filter (const char *path,const char *src,size_t len, +int fd,struct strbuf *dst,const char *cmd, +const int wanted_capability)
apply_protocol2_filter, orapply_process_filter? Or rather, s/_protocol2_/_process_/g ?
Mh. I wanted to convey that this functions is protocol V2 related...
Quoted 7 lines
This is equivalent to
static int apply_filter(const char *path, const char *src,
size_tlen, int fd,
struct strbuf *dst, const char *cmd)Could we have extended that one instead?
Initially I had one function but that got kind of long ... I prefer two for now.
Quoted 33 lines
Quoted 8 lines
convert.c +8
-0+{ +int ret =1 ; +struct cmd2process *entry; +struct child_process *process; +struct stat file_stat; +struct strbuf nbuf =STRBUF_INIT ; +size_t expected_bytes =0 ; +char *strtol_end;convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 732
Quoted 10 lines
convert.c +9
-0+char *strbuf; +char *filter_type; +char *filter_result =NULL ; + +if (!cmd || !*cmd) +return 0 ; + +if (!dst) +return 1 ;This is the same as in
apply_filter().convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 740
Quoted 8 lines
convert.c +8
-0+ +if (!cmd_process_map_initialized) { + cmd_process_map_initialized =1 ; +hashmap_init (&cmd_process_map, (hashmap_cmp_fn ) cmd2process_cmp,0 ); + entry =NULL ; + }else { + entry =find_protocol2_filter_entry (&cmd_process_map, cmd); + }Here we try to find existing process, rather than starting new as in
apply_filter()
Quoted 4 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 742
Quoted 2 lines
convert.c +2
-0+ +fflush (NULL );This is the same as in
apply_filter(), but I wonder what it is for.
"If the stream argument is NULL, fflush() flushes all
open output streams."
http://man7.org/linux/man-pages/man3/fflush.3.html
Quoted 10 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 748
Quoted 7 lines
convert.c +6
-0+ +if (!entry) { + entry =start_protocol2_filter (&cmd_process_map, cmd); +if (!entry) { +return 0 ; + }Style; we prefer:
Quoted 2 lines
+ if (!entry)
+ return 0;
Agreed.
Quoted 6 lines
This is very similar to
apply_filter(), but the latter usesstart_async()from "run-command.h", withfilter_buffer_or_fd()as asynchronous process, which gets passed command to run in structfilter_params. In this functionstart_protocol2_filter()runsstart_command(), synchronous API.Why the difference?
The protocol V2 requires a sequential processing of the packets. See discussion with Junio here:
http://public-inbox.org/git/xmqqbn1th5qn.fsf%40gitster.mtv.corp.google.com/
[LONG SNIP]
I will answer the second half in a separate email.
Thanks for the review,
Lars
Re: [PATCH v3 10/10] convert: add filter.<driver>.process option
[Some of those answers might have been invalidated by v4]
W dniu 01.08.2016 o 19:55, Lars Schneider pisze:
Quoted 11 lines
Quoted 9 lines · Jakub Narębski
W dniu 30.07.2016 o 01:38, [email protected] pisze:
[...]Quoted 1 line
+static int
multi_packet_read(int fd_in, struct strbuf *sb, size_t expected_bytes, int is_stream)About name of this function:
multi_packet_readis fine, though I wonder ifpacket_read_in_fullwith nearly the same parameters aspacket_read, orpacket_read_till_flush, orread_in_full_packetizedwould be better.I like
multi_packet_readand will rename!
Errr... what? multi_packet_read() is the current name...
Quoted 11 lines
Quoted 7 lines
Also, the problem is that while we know that what
packet_read()stores would fit in memory (insize_t), it is not true for reading whole file, which might be very large - for example huge graphical assets like raw images or raw videos, or virtual machine images. Isn't that the goal of git-LFS solutions, which need this feature? Shouldn't we have then bothmulti_packet_read_to_fdandmulti_packet_read_to_buf, or whatever?Git LFS works well with the current clean/smudge mechanism that uses the same on in memory buffers. I understand your concern but I think this improvement is out of scope for this patch series.
True.
BTW. this means that it cannot share code with fetch / push codebase, where Git spools from pkt-line to packfile on disk.
Quoted 7 lines
Quoted 2 lines
Also,
total_bytes_readcould overflowsize_t, but then we would have problems storing the result in strbuf.Would that check be ok?
if (
total_bytes_read>SIZE_MAX-bytes_read)
return 1; //total_bytes_readwould overflow and is not representable
Well, if current code doesn't have such check, then I think it would be all right to not have it either.
Note that we do not use C++ comments.
Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
convert.c +10
-0+ +if (is_stream) +strbuf_grow (sb,LARGE_PACKET_MAX );// allocate space for at least one packet +else +strbuf_grow (sb,st_add (expected_bytes,1 ));// add one extra byte for the packet flush + + do { + bytes_read = packet_read( + fd_in, NULL, NULL, + sb->buf + total_bytes_read, sb->len - total_bytes_read - 1,Quoted 8 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 504
Quoted 4 lines
convert.c +4
-0+PACKET_READ_GENTLE_ON_EOF + ); +if (bytes_read <0 ) +return 1 ;// unexpected EOF Don't we usually return negative numbers on error? Ah, I see that the return is a bool, which allows to use boolean expression with 'return'. But I am still unsure if it is good API, this return value.
Quoted 2 lines
According to Peff zero for success is the usual style:
http://public-inbox.org/git/20160728133523.GB21311%40sigill.intra.peff.net/
The usual case is 0 for success, but -1 (and not 1) for error. But I agree with Peff that keeping existing API is better.
Quoted 11 lines
Quoted 9 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 520
Quoted 6 lines
convert.c +6
-0+ ); +strbuf_setlen (sb, total_bytes_read); +return (is_stream ?0 : expected_bytes != total_bytes_read); +} + +static int multi_packet_write_from_fd (const int fd_in,const int fd_out)Is it equivalent of
copy_fd()function, but where destination uses pkt-line and we need to pack data into pkt-lines?Correct!
Yes, and we cannot keep the naming convention. Though maybe mentioning the equivalence in the comment above function would be good idea...
Quoted 9 lines
Quoted 5 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ return did_fail;Return true on fail? Shouldn't we follow example of
copy_fd()fromcopy.c, and returnCOPY_READ_ERROR, orCOPY_WRITE_ERROR, orPKTLINE_WRITE_ERROR?OK. How about this?
static int
multi_packet_write_from_fd(const int fd_in, const int fd_out)
Quoted 15 lines
{
int did_fail = 0;
ssize_t bytes_to_write;
while (!did_fail) {
bytes_to_write = xread(fd_in, PKTLINE_DATA_START(packet_buffer), PKTLINE_DATA_MAXLEN);
if (bytes_to_write < 0)
return COPY_READ_ERROR;
if (bytes_to_write == 0)
break;
did_fail |= direct_packet_write(fd_out, packet_buffer, PKTLINE_HEADER_LEN + bytes_to_write, 1);
}
if (!did_fail)
did_fail = packet_flush_gently(fd_out);
return (did_fail ? COPY_WRITE_ERROR : 0);
}That's better, I think.
Quoted 9 lines
Quoted 7 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 537
Quoted 3 lines
convert.c +3
-0+} + +static int multi_packet_write_from_buf (const char *src,size_t len,int fd_out)It is equivalent of
write_in_full(), with different order of parameters, but where destination file descriptor expects pkt-line and we need to pack data into pkt-lines?True. Do you suggest to reorder parameters? I also would like to rename
srctosrc_in, OK?
Well, no need to reorder parameters. Better keep it the same as for other function. 'src' is input ('source'), 'src_in' is tautologic.
Quoted 3 lines
Quoted 1 line
NOTE: function description comments?
What do you mean here?
Sorry for being so cryptic. What I meant is to think about adding comments describing new functions just above them.
Quoted 9 lines
Quoted 7 lines
Namely:
- for git -> filter:
* read from fd, write pkt-line to fd (off_t)
* read from str+len, write pkt-line to fd (size_t,ssize_t)
- for filter -> git:
* read pkt-line from fd, write to fd (off_t)This one does not exist.
Right, because filter output goes to Git via strbuf.
Quoted 1 line
Quoted 1 line
* read pkt-line from fd, write to str+len (
size_t,ssize_t)
[...]
Quoted 11 lines
Quoted 11 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 574
Quoted 5 lines
convert.c +5
-0+struct child_process process; +}; + +static int cmd_process_map_initialized =0 ; +static struct hashmap cmd_process_map;Reading
Documentation/technical/api-hashmap.txtI see that:
tablesizeis the allocated size of the hash table. A non-0 value indicates that the hashmap is initialized.
Quoted 7 lines
Quoted 1 line
So
cmd_process_map_initializedis not really needed, is it?I copied that from
config.c:
https://github.com/git/git/blob/f8f7adce9fc50a11a764d57815602dcb818d1816/config.c#L1425-L1428
git grep "tablesize"reveals that the check fortablesizeis only used inhashmap.c... so what approach should we use?
Well, git code is not always the best example...
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
convert.c +3
-0+static int apply_protocol2_filter (const char *path,const char *src,size_t len, +int fd,struct strbuf *dst,const char *cmd, +const int wanted_capability)
[...]
Quoted 8 lines
Quoted 6 lines
This is equivalent to
static int apply_filter(const char *path, const char *src,
size_tlen, int fd,
struct strbuf *dst, const char *cmd)Could we have extended that one instead?
Initially I had one function but that got kind of long ... I prefer two for now.
All right, we could always refactor to avoid code duplication later.
Quoted 9 lines
Quoted 4 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 742
Quoted 2 lines
convert.c +2
-0+ +fflush (NULL );This is the same as in
apply_filter(), but I wonder what it is for."If the stream argument is NULL,
fflush()flushes all
open output streams."
What I wanted to ask was not "what it does?",
but "why we need to flush here?".
Quoted 10 lines
Quoted 6 lines
This is very similar to
apply_filter(), but the latter usesstart_async()from "run-command.h", withfilter_buffer_or_fd()as asynchronous process, which gets passed command to run in structfilter_params. In this functionstart_protocol2_filter()runsstart_command(), synchronous API.Why the difference?
The protocol V2 requires a sequential processing of the packets. See discussion with Junio here:
http://public-inbox.org/git/xmqqbn1th5qn.fsf%40gitster.mtv.corp.google.com/
I don't know what you want to refer to. The linked email explains why we fork/start_async() Git process, and the answer was to support streaming.
There isn't anything there about why protocol v2 requires sequential / synchronous processing of file output, that is write file contents in full, then read, instead of having child write, and Git read and ready to read (so filter driver can start writing immediately, and do not need to wait for the other ed to stop writing / finish file).
Best regards,
Signature · 1 line
Jakub Narębski
Re: [PATCH v3 10/10] convert: add filter.<driver>.process option
In reply to Jakub NarębskiQuoted 5 lines · Jakub Narębski
W dniu 30.07.2016 o 01:38, [email protected] pisze:
[LONG SNIP]
First part answered here:
http://public-inbox.org/git/5180D54D-92C4-4875-AEB3-801663D70A8B%40gmail.com/
Quoted 16 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 760
Quoted 13 lines
convert.c +12
-0+ } + process = &entry->process; + +if (!(wanted_capability & entry->supported_capabilities)) +return 1 ;// it is OK if the wanted capability is not supported + + if FILTER_SUPPORTS_CLEAN(wanted_capability) + filter_type ="clean" ; + else if FILTER_SUPPORTS_SMUDGE(wanted_capability) + filter_type ="smudge" ; + else + die("unexpected filter type");Style: it should be
Quoted 8 lines
+ if (
FILTER_SUPPORTS_CLEAN(wanted_capability))
+filter_type= "clean";
+ else if (FILTER_SUPPORTS_SMUDGE(wanted_capability))
+filter_type= "smudge";
+ else
+die("unexpected filter type");even though by accident the macro provides the parentheses to "if".
Agreed.
Quoted 2 lines
Can we make an error/die message more detailed? Maybe it is not possible...
Yeah, I don't see an easy way...
Quoted 11 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 766
Quoted 7 lines
convert.c +6
-0+ +if (fd >=0 && !src) { +if (fstat (fd, &file_stat) ==-1 ) +return 0 ; + len = file_stat.st_size; + }All right, when
fstat()can fail? Could we then send contents without size upfront, or is it better to require size to make it more consistent for filter drivers scripts?
If fstat() fails then there is clearly something wrong and the filter should fail.
Quoted 2 lines
Could this whole "send single file" be put in a separate function? Or is it not worth it?
This function would have almost the same signature as apply_protocol2_filter and therefore I would say it's not worth it since the function is not crazy long.
Quoted 6 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ + sigchain_push(SIGPIPE, SIG_IGN);Hmmm... ignoring SIGPIPE was good for one-shot filters. Is it still O.K. for per-command persistent ones?
Very good question. You are right... we don't want to ignore any errors during the protocol... I will remove it.
Quoted 13 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 777
Quoted 10 lines
convert.c +9
-0+ +packet_buf_write (&nbuf,"%s\n" , filter_type); + ret &= !direct_packet_write (process->in, nbuf.buf, nbuf.len,1 ); + +if (ret) { +strbuf_reset (&nbuf); +packet_buf_write (&nbuf,"filename=%s\n" , path); + ret = !direct_packet_write (process->in, nbuf.buf, nbuf.len,1 ); + }Perhaps a better solution would be
Quoted 4 lines
if (err) goto fin_error;rather than this.
OK, I change it to goto error handling style.
Quoted 10 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 783
Quoted 7 lines
convert.c +6
-0+ +if (ret) { +strbuf_reset (&nbuf); +packet_buf_write (&nbuf,"size=%" PRIuMAX"\n" , (uintmax_t )len); + ret = !direct_packet_write (process->in, nbuf.buf, nbuf.len,1 ); + }Or maybe extract writing the header for a file into a separate function? This one gets a bit long...
Maybe... but I think that would make it harder to understand the protocol. I think I would prefer to have all the communication in one function layer.
Quoted 4 lines
Quoted 4 lines
convert.c +4
-0+ +if (ret) { +if (fd >=0 ) + ret = !multi_packet_write_from_fd (fd, process->in);
Quoted 8 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 790
Quoted 3 lines
convert.c +3
-0+else + ret = !multi_packet_write_from_buf (src, len, process->in); + }This is not streaming. The above sends whole file, or whole string to the filter process, without draining filter output. If the filter were to read some, then write some, it might deadlock on full buffers, isn't it? Or am I mistaken?
Correct.
Quoted 11 lines
Quoted 11 lines
convert.c +11
-0+ +if (ret && !FILTER_SUPPORTS_STREAM (entry->supported_capabilities)) { + strbuf =packet_read_line (process->out,NULL ); +if (strlen (strbuf) >5 && !strncmp ("size=" , strbuf,5 )) { + expected_bytes = (off_t )strtol (strbuf +5 , &strtol_end,10 ); + ret = (strtol_end != strbuf && errno !=ERANGE ); + }else { + ret =0 ; + } + } +
Quoted 8 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 806
Quoted 5 lines
convert.c +5
-0+if (ret) { +strbuf_reset (&nbuf); + ret = !multi_packet_read (process->out, &nbuf, expected_bytes, +FILTER_SUPPORTS_STREAM (entry->supported_capabilities)); + }What happens if the output of filter does not fit in
size_t? I see that (I think) this problem is inherited from the original implementation.
Correct. And therefore I would prefer not to change this in this series.
Quoted 25 lines
Quoted 7 lines
convert.c +7
-0+ +if (ret) { + filter_result =packet_read_line (process->out,NULL ); + ret = !strcmp (filter_result,"success" ); + } + +sigchain_pop (SIGPIPE );convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 823
Quoted 10 lines
convert.c +10
-0+ +if (ret) { +strbuf_swap (dst, &nbuf); + }else { +if (!filter_result ||strcmp (filter_result,"reject" )) { +// Something went wrong with the protocol filter. Force shutdown! +error ("external filter '%s' failed" , cmd); +kill_protocol2_filter (&cmd_process_map, entry); + } + }So if Git gets finish signal "success" from filter, it accepts the output. If Git gets finish signal "reject" from filter, it restarts filter (and reject the output - user can retry the command himself / herself). If Git gets any other finish signal, for example "error" (but this is not standarized), then it rejects the output, keeping the unfiltered result, but keeps filtering.
Quoted 2 lines
I think it is not described in this detail in the documentation of the new protocol.
Agreed, will add!
Quoted 49 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 826
Quoted 4 lines
convert.c +3
-0+strbuf_release (&nbuf); +return ret; +}I wonder if this point might be start of the new patch... but then you would have no way to test what you wrote.
Quoted 9 lines
+
static struct convert_driver { const char *name; struct convert_driver *next; const char *smudge; const char *clean;convert.c +1
-0+const char *process;int required; } *user_convert, **user_convert_tail;All right.
convert.c static int read_convert_config(const char *var, const char *value, void *cb) · line 877
Quoted 9 lines
convert.c +4
-0@@ -526,6 +871,10 @@ static int read_convert_config(const char *var, const char *value, void *cb)if (!strcmp ("clean" , key))return git_config_string (&drv->clean, var, value); +if (!strcmp ("process" , key)) { +return git_config_string (&drv->process, var, value); + } +All right.
Quoted 4 lines
convert.c +0
-0if (!strcmp ("required" , key)) { drv->required =git_config_bool (var, value);return 0 ;convert.c int would_convert_to_git_filter_fd(const char *path) · line 1180
Quoted 10 lines
convert.c +6
-1@@ -823,7 +1172,12 @@ int would_convert_to_git_filter_fd(const char *path)if (!ca.drv->required)return 0 ; -return apply_filter (path,NULL ,0 ,-1 ,NULL , ca.drv->clean); +if (!ca.drv->clean && ca.drv->process) +return apply_protocol2_filter ( + path,NULL ,0 ,-1 ,NULL , ca.drv->process,FILTER_CAPABILITIES_CLEAN + ); +else +return apply_filter (path,NULL ,0 ,-1 ,NULL , ca.drv->clean);Could we augment
apply_filter()instead, so that the invocation is
Quoted 4 lines
return apply_filter(path, NULL, 0, -1, NULL, ca.drv, FILTER_CLEAN);Though I am not sure if moving this conditional to
apply_filterwould be a good idea; maybe wrapper around augmentedapply_filter_do()?
Yes, a wrapper makes it way cleaner!
Quoted 45 lines
Quoted 10 lines
convert.c +2
-1}const char *get_convert_attr_ascii (const char *path)@@ -856,17 +1210,24 @@ int convert_to_git(const char *path, const char *src, size_t len, struct strbuf *dst, enum safe_crlf checksafe) { int ret = 0; -const char *filter =NULL ; +const char *clean_filter =NULL ; +const char *process_filter =NULL ;convert.c int convert_to_git(const char *path, const char *src, size_t len, · line 1223
Quoted 10 lines
convert.c +2
-1int required =0 ;struct conv_attrs ca;convert_attrs (&ca, path);if (ca.drv) { - filter = ca.drv->clean; + clean_filter = ca.drv->clean; + process_filter = ca.drv->process; required = ca.drv->required; }All right (assuming un-augmented
apply_filter()).convert.c int convert_to_git(const char *path, const char *src, size_t len, · line 1230
Quoted 8 lines
convert.c +6
-1- ret |=apply_filter (path, src, len,-1 , dst, filter); +if (!clean_filter && process_filter) + ret |=apply_protocol2_filter ( + path, src, len,-1 , dst, process_filter,FILTER_CAPABILITIES_CLEAN + ); +else + ret |=apply_filter (path, src, len,-1 , dst, clean_filter);I wonder if it would be more readable to write it like this (and of course elsewhere too):
+ if (!clean_filter &&
process_filter)
+ ret |= apply_protocol2_filter(
+ path, src, len, -1, dst,process_filter,FILTER_CAPABILITIES_CLEAN
+ );
+ else
+ ret |= apply_filter(
+ path, src, len, -1, dst,clean_filter);
+ );
Quoted 1 line
Though it would screw up "git blame -C -C -w"
Obsolete with the wrapper mentioned above.
Quoted 41 lines
convert.c int convert_to_git(const char *path, const char *src, size_t len, · line 1249
Quoted 8 lines
convert.c +1
-0if (!ret && required)die ("%s: clean filter '%s' failed" , path, ca.drv->name);@@ -885,13 +1246,21 @@ int convert_to_git(const char *path, const char *src, size_t len,void convert_to_git_filter_fd (const char *path,int fd,struct strbuf *dst,enum safe_crlf checksafe) { +int ret =0 ;Right, 'ret' is needed because we now have two possibilities:
cleanfilter andprocessfilter.Quoted 20 lines
convert.c +9
-2struct conv_attrs ca;convert_attrs (&ca, path);assert (ca.drv); -assert (ca.drv->clean); +assert (ca.drv->clean || ca.drv->process); + +if (!ca.drv->clean && ca.drv->process) + ret =apply_protocol2_filter ( + path,NULL ,0 , fd, dst, ca.drv->process,FILTER_CAPABILITIES_CLEAN + ); +else + ret =apply_filter (path,NULL ,0 , fd, dst, ca.drv->clean); -if (!apply_filter (path,NULL ,0 , fd, dst, ca.drv->clean)) +if (!ret)die ("%s: clean filter '%s' failed" , path, ca.drv->name);crlf_to_git (path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);convert.c static int convert_to_working_tree_internal(const char *path, const char *src, · line 1274
Quoted 6 lines
convert.c +1
-2@@ -902,14 +1271,16 @@ static int convert_to_working_tree_internal(const char *path, const char *src, size_t len, struct strbuf *dst, int normalizing) { - int ret = 0, ret_filter = 0; -const char *filter =NULL ; + int ret = 0, ret_filter;Why the change:
Quoted 2 lines
- int ret = 0,
ret_filter= 0;
+ int ret = 0,ret_filter;
Reverted with the wrapper.
Quoted 39 lines
convert.c static int convert_to_working_tree_internal(const char *path, const char *src, · line 1285
Quoted 12 lines
convert.c +4
-1+const char *smudge_filter =NULL ; +const char *process_filter =NULL ;int required =0 ;struct conv_attrs ca;convert_attrs (&ca, path);if (ca.drv) { - filter = ca.drv->smudge; + process_filter = ca.drv->process; + smudge_filter = ca.drv->smudge; required = ca.drv->required; }All right, the same.
[...]
Quoted 10 lines
t/t0021-conversion.sh +0
-0@@ -296,4 +296,409 @@ test_expect_success 'disable filter with empty override' ' test_must_be_empty err'
t/t0021-conversion.sh +2
-0+test_expect_success PERL 'required process filter should filter data' ' + test_config_global filter.protocol.process "$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge shutdown" &&t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 310
Quoted 10 lines
t/t0021-conversion.sh +10
-0+ test_config_global filter.protocol.required true && + rm -rf repo && + mkdir repo && + ( + cd repo && + git init && + + echo "*.r filter=protocol" >.gitattributes && + git add . && + git commit . -m "test commit" &&This is more of "Initial commit", not that it matters
Quoted 6 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 313
Quoted 3 lines
t/t0021-conversion.sh +3
-0+ git branch empty && + + cat ../test.o >test.r &&Err, the above is just copying file, isn't it? Maybe it was copied from other tests, I have not checked.
It was created in the "setup" test.
Quoted 7 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 316
Quoted 3 lines
t/t0021-conversion.sh +3
-0+ echo "test22" >test2.r && + mkdir testsubdir && + echo "test333" >testsubdir/test3.r &&All right, we test text file, we test binary file (I assume), we test file in a subdirectory. What about testing empty file? Or large file which would not fit in the stdin/stdout buffer (as EXPENSIVE test)?
No binary file. The main reason for this test is to check multiple files. I'll add a empty file. A large file is tested in the next test.
Quoted 6 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 319
Quoted 4 lines
t/t0021-conversion.sh +3
-0+ + rm -f rot13-filter.log && + git add . &&So this runs "clean" filter, storing cleaned contents in the index.
Correct.
Quoted 7 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 324
Quoted 5 lines
t/t0021-conversion.sh +5
-0+ sort rot13-filter.log | uniq -c | sed "s/^[ ]*//" >uniq-rot13-filter.log && + cat >expected_add.log <<-\EOF && + 1 IN: clean test.r 57 [OK] -- OUT: 57 [OK] + 1 IN: clean test2.r 7 [OK] -- OUT: 7 [OK] + 1 IN: clean testsubdir/test3.r 8 [OK] -- OUT: 8 [OK]And we check the "know size upfront" case (mistakenly called non-"stream").
Correct - however, I removed non-stream
Quoted 3 lines
t/t0021-conversion.sh
Quoted 1 line
t/t0021-conversion.sh +1
-0+ 1 IN: shutdown -- [OK]And test "shutdown" capability (not as separate test).
Fixed.
Quoted 5 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 328
Quoted 3 lines
t/t0021-conversion.sh +3
-0+ 1 start + 1 wrote filter header + EOFAnd we are required to keep the expected_add.log file sorted by hand???
Well, the clean invocations (and therefore their order of appearance) are not deterministic. See my discussion with Junio here:
http://public-inbox.org/git/xmqqshv18i8i.fsf%40gitster.mtv.corp.google.com/
Quoted 11 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 331
Quoted 4 lines
t/t0021-conversion.sh +3
-0+ test_cmp expected_add.log uniq-rot13-filter.log && + + >rot13-filter.log &&Truncate log. Still in the same test.
t/t0021-conversion.sh
Quoted 1 line
t/t0021-conversion.sh +1
-0+ git commit . -m "test commit" &&This is test commit with files undergoing "clean" part of filter.
Quoted 7 lines
t/t0021-conversion.sh
Quoted 2 lines
t/t0021-conversion.sh +2
-0+ sort rot13-filter.log | uniq -c | sed "s/^[ ]*//" | + sed "s/^\([0-9]\) IN: clean/x IN: clean/" >uniq-rot13-filter.log &&There is known performance regression, in that filter is run more than once on given file.
Actually... why it does not use cleaned-up contents from the index?
See discussion here: http://public-inbox.org/git/20160722152753.GA6859%40sigill.intra.peff.net/
Quoted 29 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 341
Quoted 7 lines
t/t0021-conversion.sh +7
-0+ cat >expected_commit.log <<-\EOF && + x IN: clean test.r 57 [OK] -- OUT: 57 [OK] + x IN: clean test2.r 7 [OK] -- OUT: 7 [OK] + x IN: clean testsubdir/test3.r 8 [OK] -- OUT: 8 [OK] + 1 IN: shutdown -- [OK] + 1 start + 1 wrote filter headerRight, this is the goal of the patch series: for filter to be started only once per git command invocation.
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 344
Quoted 3 lines
t/t0021-conversion.sh +3
-0+ EOF + test_cmp expected_commit.log uniq-rot13-filter.log && +Still in the same test, even though we would be testing "smudge" capability now.
It's a pity that
t/test-lib.shdoes not support subtests from the TAP specification (Test Anything Protocol that Git testsuite uses).t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 347
Quoted 3 lines
t/t0021-conversion.sh +3
-0+ >rot13-filter.log && + rm -f test?.r testsubdir/test3.r && + git checkout . &&All right, we removed some files so that "git checkout ." could restore them to life.
Quoted 5 lines
t/t0021-conversion.sh
Quoted 1 line
t/t0021-conversion.sh +1
-0+ cat rot13-filter.log | grep -v "IN: clean" >smudge-rot13-filter.log &&Useless use of cat
+ grep -v "IN: clean" rot13-filter.log >smudge-rot13-filter.log &&
Fixed, thanks!
Quoted 2 lines
Also: why 'git checkout <path>' would run "clean" filter? Is it existing strange behaviour?
AFAIK, that's existing behavior.
Quoted 9 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 355
Quoted 7 lines
t/t0021-conversion.sh +7
-0+ cat >expected_checkout.log <<-\EOF && + start + wrote filter header + IN: smudge test2.r 7 [OK] -- OUT: 7 [OK] + IN: smudge testsubdir/test3.r 8 [OK] -- OUT: 8 [OK] + IN: shutdown -- [OK] + EOFThis time without 'sort | uniq -c'.
Yes, because the smudge calls are deterministic!
Quoted 2 lines
Is it really needed for the
"good" case, or is it there for two cases to look similar?
I am not sure what you mean?!
Quoted 7 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 358
Quoted 3 lines
t/t0021-conversion.sh +3
-0+ test_cmp expected_checkout.log smudge-rot13-filter.log && + + git checkout empty &&Shouldn't we check that switching to branch 'empty' does not run filters, or is it covered by other tests? Or perhaps this simply does not matter here, is it?
Easy enough to check. I will add this.
Quoted 36 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 361
Quoted 4 lines
t/t0021-conversion.sh +3
-0+ + >rot13-filter.log && + git checkout master &&Does it test different callpath than 'git checkout .'? Well, the set of files is different...
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 372
Quoted 11 lines
t/t0021-conversion.sh +11
-0+ cat rot13-filter.log | grep -v "IN: clean" >smudge-rot13-filter.log && + cat >expected_checkout_master.log <<-\EOF && + start + wrote filter header + IN: smudge test.r 57 [OK] -- OUT: 57 [OK] + IN: smudge test2.r 7 [OK] -- OUT: 7 [OK] + IN: smudge testsubdir/test3.r 8 [OK] -- OUT: 8 [OK] + IN: shutdown -- [OK] + EOF + test_cmp expected_checkout_master.log smudge-rot13-filter.log && +And here we start checking that the filter did filter, that is the content in the repository is "clean"ed-up. Still the same test.
Quoted 12 lines
t/t0021-conversion.sh +12
-0+ ./../rot13.sh <test.r >expected && + git cat-file blob :test.r >actual && + test_cmp expected actual && + + ./../rot13.sh <test2.r >expected && + git cat-file blob :test2.r >actual && + test_cmp expected actual && + + ./../rot13.sh <testsubdir/test3.r >expected && + git cat-file blob :testsubdir/test3.r >actual && + test_cmp expected actual + )
Quoted 8 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 389
Quoted 5 lines
+'
t/t0021-conversion.sh +4
-0+ +test_expect_success PERL 'required process filter should filter data stream' ' + test_config_global filter.protocol.process "$TEST_DIRECTORY/t0021/rot13-filter.pl stream clean smudge" && + test_config_global filter.protocol.required true &&Errr... I don't see how it is different from the previous test. [...]
stream/non-stream ... but this is obsolete in the next roll. fixed!
Quoted 8 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 471
Quoted 3 lines
t/t0021-conversion.sh +2
-0+ +test_expect_success PERL 'required process filter should filter smudge data and one-shot filter should clean' 'All right, so this tests the precedence... well, it doesn't.
It tests that
processfilter with "smudge" capability only works well with one-shotcleanfilter.
True. Isn't that what the test description indicates?
Quoted 4 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 473
Quoted 2 lines
t/t0021-conversion.sh +2
-0+ test_config_global filter.protocol.clean ./../rot13.sh && + test_config_global filter.protocol.process "$TEST_DIRECTORY/t0021/rot13-filter.pl smudge" &&Why the difference in pathnames (the directory part) between those two?
rot13.sh is generated in the header of the file.
rot13-filter.pl is part of the test suite
Quoted 20 lines
Quoted 20 lines
t/t0021-conversion.sh +20
-0+ test_config_global filter.protocol.required true && + rm -rf repo && + mkdir repo && + ( + cd repo && + git init && + + echo "*.r filter=protocol" >.gitattributes && + git add . && + git commit . -m "test commit" && + git branch empty && + + cat ../test.o >test.r && + echo "test22" >test2.r && + mkdir testsubdir && + echo "test333" >testsubdir/test3.r && + + rm -f rot13-filter.log && + git add . && + test_must_be_empty rot13-filter.log &&
Quoted 8 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 493
Quoted 4 lines
t/t0021-conversion.sh +4
-0+ + >rot13-filter.log && + git commit . -m "test commit" && + test_must_be_empty rot13-filter.log &&All right, these tests that
processfilter is not ran. But we don't know if it is because it lacks capability, or because it is overriden by one-shot filter (well, that comes later).
Only the clean one shot filter is configured. Therefore that shouldn't be a problem, right?
Quoted 7 lines
Quoted 7 lines
t/t0021-conversion.sh +7
-0+ + >rot13-filter.log && + rm -f test?.r testsubdir/test3.r && + git checkout . && + cat rot13-filter.log | grep -v "IN: clean" >smudge-rot13-filter.log && + cat >expected_checkout.log <<-\EOF && + start
Quoted 8 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 356
Quoted 5 lines
t/t0021-conversion.sh +5
-0+ wrote filter header + IN: smudge test2.r 7 [OK] -- OUT: 7 [OK] + IN: smudge testsubdir/test3.r 8 [OK] -- OUT: 8 [OK] + EOF + test_cmp expected_checkout.log smudge-rot13-filter.log &&This part is repeated many, many times. Maybe add some helper shell function for this?
Good idea! Will add!
Quoted 16 lines
[...]
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 383
Quoted 11 lines
t/t0021-conversion.sh +11
-0+ ./../rot13.sh <test.r >expected && + git cat-file blob :test.r >actual && + test_cmp expected actual && + + ./../rot13.sh <test2.r >expected && + git cat-file blob :test2.r >actual && + test_cmp expected actual && + + ./../rot13.sh <testsubdir/test3.r >expected && + git cat-file blob :testsubdir/test3.r >actual && + test_cmp expected actualHere we test that equivalent one-shot cleanup filter was run. Here also we have repeated contents; maybe some helper function would make it shorter?
Agreed!
Quoted 11 lines
t/t0021-conversion.sh
Quoted 2 lines
t/t0021-conversion.sh +2
-0+ ) +'Here I am stopping examining tests in detail.
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 578
Quoted 2 lines
t/t0021-conversion.sh +2
-0+test_expect_success PERL 'required process filter should clean only' ' +test_expect_success PERL 'required process filter should process files larger LARGE_PACKET_MAX' 'Those two tests do not depend on being required or not; it is only that without required they would fail softly in case of latter test (which we can detect too).
True, but since they fail hard it is easier to check.
Quoted 6 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 622
Quoted 2 lines
t/t0021-conversion.sh +2
-0+test_expect_success PERL 'required process filter should with clean error should fail' ' +test_expect_success PERL 'process filter should restart after unexpected write failure' 'So these two are sort of complimentary. When
processis required, then it should fail if it cannot filter some file. If it is not, it should keep processing other files.
True.
Quoted 4 lines
Quoted 1 line
+test_expect_success PERL 'process filter should not restart after intentionally rejected file' '
Uh... all right, so "reject" means that filter cannot continue? Strange meaning for 'reject', though ;-)
No, with reject a filter can say "I don't want to process that file". This is a legitimate response and I don't Git to restart the filter in that case.
Quoted 8 lines
Quoted 8 lines
test_donet/t0021/rot13-filter.pl new file +1
-0@@ -0,0 +1,177 @@ +#!/usr/bin/perl
Quoted 8 lines
t/t0021/rot13-filter.pl line 7
Quoted 6 lines
t/t0021/rot13-filter.pl new file +6
-0+# +# Example implementation for the Git filter protocol version 2 +# See Documentation/gitattributes.txt, section "Filter Protocol" +# +# The script takes the list of supported protocol capabilities as +# arguments ("stream", "clean", and "smudge" are supported).What about "shutdown"?
Will fix.
Quoted 8 lines
t/t0021/rot13-filter.pl line 13
Quoted 6 lines
t/t0021/rot13-filter.pl new file +6
-0+# +# This implementation supports three special test cases: +# (1) If data with the filename "clean-write-fail.r" is processed with +# a "clean" operation then the write operation will die. +# (2) If data with the filename "smudge-write-fail.r" is processed with +# a "smudge" operation then the write operation will die.All right, so it is hard failure with filter script dying.
Correct.
Quoted 14 lines
t/t0021/rot13-filter.pl line 16
Quoted 4 lines
t/t0021/rot13-filter.pl new file +3
-0+# (3) If data with the filename "failure.r" is processed with any +# operation then the filter signals that the operation was not +# successful.All right, so it is failure detected by filter script and signalled to Git.
t/t0021/rot13-filter.pl line 20
Quoted 4 lines
t/t0021/rot13-filter.pl new file +4
-0+# + +use strict; +use warnings;So no more "use autodie", because of compatibility with old Perls.
Quoted 5 lines
t/t0021/rot13-filter.pl line 23
Quoted 3 lines
t/t0021/rot13-filter.pl new file +3
-0+ +my $MAX_PACKET_CONTENT_SIZE = 65516; +my @capabilities = @ARGV;No autoflush this time?
Eric recommended to disable it:
http://public-inbox.org/git/20160723072721.GA20875%40starla/
Quoted 23 lines
Quoted 9 lines
t/t0021/rot13-filter.pl new file +9
-0+ +sub rot13 { + my ($str) = @_; + $str =~ y/A-Za-z/N-ZA-Mn-za-m/; + return $str; +} + +sub packet_read { + my $buffer;t/t0021/rot13-filter.pl line 42
Quoted 10 lines
t/t0021/rot13-filter.pl new file +10
-0+ my $bytes_read = read STDIN, $buffer, 4; + if ( $bytes_read == 0 ) { + return; + } + elsif ( $bytes_read != 4 ) { + die "invalid packet size '$bytes_read' field"; + } + my $pkt_size = hex($buffer); + if ( $pkt_size == 0 ) { + return ( 1, "" );Unusual return convention. Though it is a test script, so it doesn't matter much.
Quoted 8 lines
t/t0021/rot13-filter.pl line 48
Quoted 6 lines
t/t0021/rot13-filter.pl new file +6
-0+ } + elsif ( $pkt_size > 4 ) { + my $content_size = $pkt_size - 4; + $bytes_read = read STDIN, $buffer, $content_size; + if ( $bytes_read != $content_size ) { + die "invalid packet";More detailed error message, maybe?
OK
Quoted 46 lines
Quoted 46 lines
t/t0021/rot13-filter.pl new file +46
-0+ } + return ( 0, $buffer ); + } + else { + die "invalid packet size"; + } +} + +sub packet_write { + my ($packet) = @_; + print STDOUT sprintf( "%04x", length($packet) + 4 ); + print STDOUT $packet; + STDOUT->flush(); +} + +sub packet_flush { + print STDOUT sprintf( "%04x", 0 ); + STDOUT->flush(); +} + +open my $debug, ">>", "rot13-filter.log"; +print $debug "start\n"; +$debug->flush(); + +packet_write("git-filter-protocol\n"); +packet_write("version 2\n"); +packet_write( "capabilities " . join( ' ', @capabilities ) . "\n" ); +print $debug "wrote filter header\n"; +$debug->flush(); + +while (1) { + my $command = packet_read(); + unless ( defined($command) ) { + exit(); + } + chomp $command; + print $debug "IN: $command"; + $debug->flush(); + + if ( $command eq "shutdown" ) { + print $debug " -- [OK]"; + $debug->flush(); + packet_write("done\n"); + exit(); + } +
Quoted 8 lines
t/t0021/rot13-filter.pl line 99
Quoted 5 lines
t/t0021/rot13-filter.pl new file +5
-0+ my ($filename) = packet_read() =~ /filename=([^=]+)\n/; + print $debug " $filename"; + $debug->flush(); + my ($filelen) = packet_read() =~ /size=([^=]+)\n/; + chomp $filelen;I think this chomp is not needed, as "\n" is not included. Though the regexp should probably be anchored.
Agreed.
Quoted 10 lines
t/t0021/rot13-filter.pl line 106
Quoted 7 lines
t/t0021/rot13-filter.pl new file +7
-0+ print $debug " $filelen"; + $debug->flush(); + + $filelen =~ /\A\d+\z/ or die "bad filelen: $filelen"; + my $output; + + if ( $filelen > 0 ) {So here is a special case for $filelen = 0.
Negative $filelen is not allowed, via regexp.
Obsolete in v4.
Quoted 20 lines
Quoted 9 lines
t/t0021/rot13-filter.pl new file +9
-0+ my $input = ""; + { + binmode(STDIN); + my $buffer; + my $done = 0; + while ( !$done ) { + ( $done, $buffer ) = packet_read(); + $input .= $buffer; + }t/t0021/rot13-filter.pl line 125
Quoted 10 lines
t/t0021/rot13-filter.pl new file +10
-0+ print $debug " [OK] -- "; + $debug->flush(); + } + + if ( $command eq "clean" and grep( /^clean$/, @capabilities ) ) { + $output = rot13($input); + } + elsif ( $command eq "smudge" and grep( /^smudge$/, @capabilities ) ) { + $output = rot13($input); + }
Quoted 6 lines
These two conditionals could be shortened, but then they would be less readable. Or not:
if ( grep { $_ eq $command } @capabilities ) { $output = rot13($input); }
I would like to keep it that way for readability since the test script also serves as example implementation.
Quoted 16 lines
Quoted 16 lines
t/t0021/rot13-filter.pl new file +16
-0+ else { + die "bad command $command"; + } + } + + my $output_len = length($output); + if ( $filename eq "reject.r" ) { + $output_len = 0; + } + + if ( grep( /^stream$/, @capabilities ) ) { + print $debug "OUT: STREAM "; + } + else { + packet_write("size=$output_len\n"); + print $debug "OUT: $output_len ";
Quoted 8 lines
t/t0021/rot13-filter.pl line 147
Quoted 6 lines
t/t0021/rot13-filter.pl new file +6
-0+ } + $debug->flush(); + + if ( $filename eq "reject.r" ) { + packet_write("reject\n"); + print $debug "[REJECT]\n"; # Could also be an errorHow if could be an error?
Removed.
Quoted 9 lines
t/t0021/rot13-filter.pl line 154
Quoted 8 lines
t/t0021/rot13-filter.pl new file +7
-0+ $debug->flush(); + } + + if ( $output_len > 0 ) { + if (( $command eq "clean" and $filename eq "clean-write-fail.r" ) + or + ( $command eq "smudge" and $filename eq "smudge-write-fail.r" ))
Quoted 3 lines
Perhaps simply:
+ if ( $filename eq "${command}-write-fail.r" ) {
Nice! Will fix!
Quoted 16 lines
Quoted 16 lines
t/t0021/rot13-filter.pl new file +16
-0+ { + print $debug "[WRITE FAIL]\n"; + $debug->flush(); + die "write error"; + } + else { + while ( length($output) > 0 ) { + my $packet = substr( $output, 0, $MAX_PACKET_CONTENT_SIZE ); + packet_write($packet); + if ( length($output) > $MAX_PACKET_CONTENT_SIZE ) { + $output = substr( $output, $MAX_PACKET_CONTENT_SIZE ); + } + else { + $output = ""; + } + }
Quoted 9 lines
t/t0021/rot13-filter.pl line 177
Quoted 8 lines
t/t0021/rot13-filter.pl new file +7
-0+ packet_flush(); + packet_write("success\n"); + print $debug "[OK]\n"; + $debug->flush(); + } + } +}
Thank you very much (again!) for your extensive review, Lars
Re: [PATCH v3 10/10] convert: add filter.<driver>.process option
[Some of this answer might have been invalidated by v4;
I might be away from computer for a few days, so I won't be reviewing]
W dniu 03.08.2016 o 15:10, Lars Schneider pisze:
Quoted 2 lines · Jakub Narębski
Quoted 1 line
W dniu 30.07.2016 o 01:38, [email protected] pisze:
[...]
Quoted 6 lines
Quoted 2 lines
Could this whole "send single file" be put in a separate function? Or is it not worth it?
This function would have almost the same signature as
apply_protocol2_filterand therefore I would say it's not worth it since the function is not crazy long.
All right. Though I would say that if it makes the function more readable, then it might be worth it.
[...]
Quoted 8 lines
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ + sigchain_push(SIGPIPE, SIG_IGN);Hmmm... ignoring SIGPIPE was good for one-shot filters. Is it still O.K. for per-command persistent ones?
Very good question. You are right... we don't want to ignore any errors during the protocol... I will remove it.
I was actually just wondering.
Actually the default behavior if SIGPIPE is not ignored (or if the SIGPIPE signal is not blocked / masked out) is to *terminate* the writing program, which we do not want.
The correct solution is to check for error during write, and check if errno is set to EPIPE. This means that reader (filter driver process) has closed pipe, usually due to crash, and we need to handle that sanely, either restarting or quitting while providing sane information about error to the user.
Well, we might want to set a signal handler for SIGPIPE, not just simply ignore it (especially for streaming case; stop streaming if filter driver crashed); though signal handlers are quite limited about what might be done in them. But that's for the future.
Read from closed pipe returns EOF; write to closed pipe results in SIGPIPE and returns -1 (setting errno to EPIPE).
Quoted 13 lines
Quoted 13 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 777
Quoted 10 lines
convert.c +9
-0+ +packet_buf_write (&nbuf,"%s\n" , filter_type); + ret &= !direct_packet_write (process->in, nbuf.buf, nbuf.len,1 ); + +if (ret) { +strbuf_reset (&nbuf); +packet_buf_write (&nbuf,"filename=%s\n" , path); + ret = !direct_packet_write (process->in, nbuf.buf, nbuf.len,1 ); + }Perhaps a better solution would be
Quoted 6 lines
Quoted 4 lines
if (err) goto fin_error;rather than this.
OK, I change it to goto error handling style.
Well, at least try it and check if it makes code more readable.
Quoted 11 lines
Quoted 8 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 783
Quoted 5 lines
convert.c +5
-0+if (ret) { +strbuf_reset (&nbuf); +packet_buf_write (&nbuf,"size=%" PRIuMAX"\n" , (uintmax_t )len); + ret = !direct_packet_write (process->in, nbuf.buf, nbuf.len,1 ); + }Or maybe extract writing the header for a file into a separate function? This one gets a bit long...
Maybe... but I think that would make it harder to understand the protocol. I think I would prefer to have all the communication in one function layer.
I don't understand your reasoning here ("make it harder to understand the protocol"). If you choose good names for function writing header, then the main function would be the high-level view of protocol, e.g.
git> <command>
git> <header>
git> <contents>
git> <flush>
git< <command accepted>
git< <contents>
git< <flush>
git< <sent status>
[...]
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
convert.c +5
-0+ +if (ret) { + filter_result =packet_read_line (process->out,NULL ); + ret = !strcmp (filter_result,"success" ); + }
Quoted 8 lines
Quoted 8 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 819
Quoted 8 lines
convert.c +8
-0+ +sigchain_pop (SIGPIPE ); + +if (ret) { +strbuf_swap (dst, &nbuf); + }else { +if (!filter_result ||strcmp (filter_result,"reject" )) { +// Something went wrong with the protocol filter. Force shutdown!
Don't use C++ one-line comments (that's C99-ism).
Quoted 12 lines
Quoted 12 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 823
Quoted 4 lines
convert.c +4
-0+error ("external filter '%s' failed" , cmd); +kill_protocol2_filter (&cmd_process_map, entry); + } + }So if Git gets finish signal "success" from filter, it accepts the output. If Git gets finish signal "reject" from filter, it restarts filter (and reject the output - user can retry the command himself / herself). If Git gets any other finish signal, for example "error" (but this is not standarized), then it rejects the output, keeping the unfiltered result, but keeps filtering.
Quoted 4 lines
Quoted 2 lines
I think it is not described in this detail in the documentation of the new protocol.
Agreed, will add!
That would be nice.
Quoted 10 lines
Quoted 10 lines
convert.c int would_convert_to_git_filter_fd(const char *path) · line 1180
Quoted 7 lines
convert.c +6
-1-return apply_filter (path,NULL ,0 ,-1 ,NULL , ca.drv->clean); +if (!ca.drv->clean && ca.drv->process) +return apply_protocol2_filter ( + path,NULL ,0 ,-1 ,NULL , ca.drv->process,FILTER_CAPABILITIES_CLEAN + ); +else +return apply_filter (path,NULL ,0 ,-1 ,NULL , ca.drv->clean);Could we augment
apply_filter()instead, so that the invocation is
Quoted 6 lines
Quoted 4 lines
return apply_filter(path, NULL, 0, -1, NULL, ca.drv, FILTER_CLEAN);Though I am not sure if moving this conditional to
apply_filterwould be a good idea; maybe wrapper around augmentedapply_filter_do()?Yes, a wrapper makes it way cleaner!
That's good, because we have quite a few of those constructs. And I think the compiler would inline it, so there is no penalty.
Quoted 1 line
Quoted 1 line
Quoted 1 line
t/t0021-conversion.sh +0
-0
[...]
Quoted 8 lines
Quoted 6 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 313
Quoted 3 lines
t/t0021-conversion.sh +3
-0+ git branch empty && + + cat ../test.o >test.r &&Err, the above is just copying file, isn't it? Maybe it was copied from other tests, I have not checked.
It was created in the "setup" test.
What I meant here (among other things) is that you uselessly use 'cat' to copy files:
+ cp ../test.o test.r &&
Quoted 10 lines
Quoted 7 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 316
Quoted 3 lines
t/t0021-conversion.sh +3
-0+ echo "test22" >test2.r && + mkdir testsubdir && + echo "test333" >testsubdir/test3.r &&All right, we test text file, we test binary file (I assume), we test file in a subdirectory. What about testing empty file? Or large file which would not fit in the stdin/stdout buffer (as EXPENSIVE test)?
No binary file. The main reason for this test is to check multiple files. I'll add a empty file. A large file is tested in the next test.
I assume that this large file is binary file; what matters is that it includes NUL character ("\0"), i.e. zero byte, checking that there is no error that would terminate it at NUL.
I'll end here for now.
Signature · 1 line
Jakub Narębski
Re: [PATCH v3 10/10] convert: add filter.<driver>.process option
Quoted 25 lines · Jakub Narębski
...
Quoted 8 lines
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ + sigchain_push(SIGPIPE, SIG_IGN);Hmmm... ignoring SIGPIPE was good for one-shot filters. Is it still O.K. for per-command persistent ones?
Very good question. You are right... we don't want to ignore any errors during the protocol... I will remove it.
I was actually just wondering.
Actually the default behavior if SIGPIPE is not ignored (or if the SIGPIPE signal is not blocked / masked out) is to *terminate* the writing program, which we do not want.
The correct solution is to check for error during write, and check if errno is set to EPIPE. This means that reader (filter driver process) has closed pipe, usually due to crash, and we need to handle that sanely, either restarting or quitting while providing sane information about error to the user.
Quoted 8 lines
Well, we might want to set a signal handler for SIGPIPE, not just simply ignore it (especially for streaming case; stop streaming if filter driver crashed); though signal handlers are quite limited about what might be done in them. But that's for the future.
Read from closed pipe returns EOF; write to closed pipe results in SIGPIPE and returns -1 (setting errno to EPIPE).
OK, I think I understand. I will address that in the next round.
Quoted 16 lines
Quoted 6 lines
Quoted 3 lines
...
Or maybe extract writing the header for a file into a separate function? This one gets a bit long...Maybe... but I think that would make it harder to understand the protocol. I think I would prefer to have all the communication in one function layer.
I don't understand your reasoning here ("make it harder to understand the protocol"). If you choose good names for function writing header, then the main function would be the high-level view of protocol, e.g.
git> <command>
git> <header>
git> <contents>
git> <flush>
Quoted 5 lines
git< <command accepted>
git< <contents>
git< <flush>
git< <sent status>
OK, I will move the header into a separate function.
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Review
Quoted 2 lines
...
(quoted diff) +1
-0+ cat ../test.o >test.r &&Err, the above is just copying file, isn't it? Maybe it was copied from other tests, I have not checked.
Quoted 6 lines
Quoted 1 line
It was created in the "setup" test.
What I meant here (among other things) is that you uselessly use 'cat' to copy files:
+ cp ../test.o test.r &&
Ah right. No idea why I did that. I'll use cp, of course :-)
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
t/t0021-conversion.sh test_expect_success 'disable filter with empty override' ' · line 316
Quoted 3 lines
t/t0021-conversion.sh +3
-0+ echo "test22" >test2.r && + mkdir testsubdir && + echo "test333" >testsubdir/test3.r &&All right, we test text file, we test binary file (I assume), we test file in a subdirectory. What about testing empty file? Or large file which would not fit in the stdin/stdout buffer (as EXPENSIVE test)?
Quoted 6 lines
Quoted 2 lines
No binary file. The main reason for this test is to check multiple files. I'll add a empty file. A large file is tested in the next test.
I assume that this large file is binary file; what matters is that it includes NUL character ("\0"), i.e. zero byte, checking that there is no error that would terminate it at NUL.
Good idea! I will add a small test file with \0 bytes in between to test binaries.
Thanks,
Lars
v4 cover Git filter protocol
In reply to [email protected]From: Lars Schneider <[email protected]>
Hi,
thanks a lot for the very helpful reviews!
Patch 1-10 are preparation. Patch 11 and 12 the real feature.
Diff to v3:
* simplify protocol, remove size information
* run clean_on_exit_handler() only on SIGTERM (Hannes)
* move hex() macro inside set_packet_header(), undef it after use (Jakub)
* rename buf to data in direct_packet_write_data() (Jakub)
* add benchmark summary (Jakub)
* add empty file test case (Jakub)
* rename multi_packet_read() to packet_read_till_flush()
* expect a flush packet even after 0 content
* move packet stream helper functions to pkt-line.c/h (Jakub)
* add GIT_PACKET_TRACE hint to docs
* remove SIGPIPE ignore (Jakub)
* change to goto error handling style (Jakub)
* cleanup test cases with helper functions (Jakub)
* move shutdown implementation to dedicated patch
Thanks,
Lars
Lars Schneider (12):
pkt-line: extract set_packet_header()
pkt-line: add direct_packet_write() and direct_packet_write_data()
pkt-line: add packet_flush_gentle()
pkt-line: call packet_trace() only if a packet is actually send
pkt-line: add functions to read/write flush terminated packet streams
pack-protocol: fix maximum pkt-line size
run-command: add clean_on_exit_handler
convert: quote filter names in error messages
convert: modernize tests
convert: generate large test files only once
convert: add filter.<driver>.process option
convert: add filter.<driver>.process shutdown command option
Documentation/gitattributes.txt | 108 +++++- Documentation/technical/protocol-common.txt | 6 +- convert.c | 324 ++++++++++++++++-- pkt-line.c | 156 ++++++++- pkt-line.h | 13 + run-command.c | 12 +- run-command.h | 1 + t/t0021-conversion.sh | 503 +++++++++++++++++++++++++--- t/t0021/rot13-filter.pl | 155 +++++++++ 9 files changed, 1187 insertions(+), 91 deletions(-) create mode 100755 t/t0021/rot13-filter.pl
--
2.9.0
PATCH v4 1/12 pkt-line: extract set_packet_header()
From: Lars Schneider <[email protected]>
set_packet_header() converts an integer to a 4 byte hex string. Make this function locally available so that other pkt-line functions can use it.
--- pkt-line.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
pkt-line.c +12 -6
@@ -97,10 +97,19 @@ void packet_buf_flush(struct strbuf *buf)strbuf_add (buf,"0000" ,4 ); } -#define hex (a) (hexchar[(a) & 15]) -static void format_packet (struct strbuf *out,const char *fmt,va_list args) +static void set_packet_header (char *buf,const int size) {static char hexchar[] ="0123456789abcdef" ; +#define hex (a) (hexchar[(a) & 15]) + buf[0 ] =hex (size >>12 ); + buf[1 ] =hex (size >>8 ); + buf[2 ] =hex (size >>4 ); + buf[3 ] =hex (size); +#undef hex +} + +static void format_packet (struct strbuf *out,const char *fmt,va_list args) +{size_t orig_len, n; orig_len = out->len;
@@ -111,10 +120,7 @@ static void format_packet(struct strbuf *out, const char *fmt, va_list args)if (n >LARGE_PACKET_MAX )die ("protocol error: impossibly long line" ); - out->buf[orig_len +0 ] =hex (n >>12 ); - out->buf[orig_len +1 ] =hex (n >>8 ); - out->buf[orig_len +2 ] =hex (n >>4 ); - out->buf[orig_len +3 ] =hex (n); +set_packet_header (&out->buf[orig_len], n);packet_trace (out->buf + orig_len +4 , n -4 ,1 ); }
Signature · 1 line
2.9.0
Re: [PATCH v4 01/12] pkt-line: extract set_packet_header()
Quoted 5 lines · [email protected] writes:
From: Lars Schneider <[email protected]>
set_packet_header()converts an integer to a 4 byte hex string. Make this function locally available so that other pkt-line functions can use it.
Didn't I say that this is a bad idea already in an earlier review?
The only reason why you want it, together with direct_packet_write() (which I think is another bad idea), is because you use packet_buf_write() to create a "<header><payload>" in a buf in the usercode in step 11/12 like this:
+ packet_buf_write(&nbuf, "command=%s\n", filter_type);
+ ret = !direct_packet_write(process->in, nbuf.buf, nbuf.len, 1);
which would be totally unnecessary if you just did strbuf_addf() into nbuf and used packet_write() like everybody else does.
Puzzled. Why are steps 01/12 and 02/12 an improvement?
Re: [PATCH v4 01/12] pkt-line: extract set_packet_header()
Quoted 18 lines · Junio C Hamano
Quoted 5 lines · [email protected] writes:
From: Lars Schneider <[email protected]>
set_packet_header()converts an integer to a 4 byte hex string. Make this function locally available so that other pkt-line functions can use it.Didn't I say that this is a bad idea already in an earlier review?
The only reason why you want it, together with
direct_packet_write()(which I think is another bad idea), is because you usepacket_buf_write()to create a "<header><payload>" in a buf in the usercode in step 11/12 like this:(quoted diff) +2
-0+ packet_buf_write(&nbuf, "command=%s\n", filter_type); + ret = !direct_packet_write(process->in, nbuf.buf, nbuf.len, 1);
Quoted 4 lines
which would be totally unnecessary if you just did
strbuf_addf()into nbuf and usedpacket_write()like everybody else does.Puzzled. Why are steps 01/12 and 02/12 an improvement?
I think it is an attempt to avoid the extra memcpy() of the bytes into another packet buffer.
I notice that the solution does still end up a using a double-write() in some cases, though. I was curious if this made any difference, though, so I wrote a short test program:
-- >8 --
#include <unistd.h>
#include <string.h>
int main(int argc, char **argv)
{
int type;
if (argv[1] && !strcmp(argv[1], "prepend"))
type = 0; /* size prepended to buffer */
else if (argv[1] && !strcmp(argv[1], "write"))
type = 1;
else if (argv[1] && !strcmp(argv[1], "memcpy"))
type = 2;
else
return 1;
while (1) {
char buf[65520];
int r = read(0, buf + 4, sizeof(buf));
if (r <= 0)
break;
if (!type) {
memcpy(buf, "1234", 4);
write(1, buf, r + 4);
} else if (type == 1) {
write(1, "1234", 4);
write(1, buf + 4, r);
} else if (type == 2) {
char packet[sizeof(buf) + 4];
memcpy(packet, "1234", 4);
memcpy(packet + 4, buf + 4, r);
write(1, packet, r + 4);
}
}
return 0;
}-- >8 --
We'd expect "prepend" to be the fastest, as it does a single write and zero-copy. And then it is a question of whether the double-write is worse than the extra memcpy.
On Linux, feeding 100MB of zeroes into stdin, I got (best-of-five):
- prepend: 11ms
- write: 11ms
- memcpy: 15ms
So it _does_ make a difference to avoid the memcpy, though 4ms per 100MB does not seem like it is probably worth caring about. The double-write also gets worse if you use a smaller buffer size (e.g., if you drop to 4K, that adds back in about 4ms of overhead because you're calling write() a lot more times).
The cost of write() may vary on other platforms, but the cost of memcpy generally shouldn't. So I'm inclined to say that it is not really worth micro-optimizing the interface.
I think the other issue is that format_packet() only lets you send string data via "%s", so it cannot be used for arbitrary data that may contain NULs. So we do need _some_ other interface to let you send a raw data packet, and it's going to look similar to the direct_packet_write() thing.
The alternative is to hand-code it, which is what send_sideband() does (it uses xsnprintf("%04x") to do the hex formatting, though).
-Peff
Re: [PATCH v4 01/12] pkt-line: extract set_packet_header()
Quoted 2 lines · Jeff King
The alternative is to hand-code it, which is what
send_sideband()does (it usesxsnprintf("%04x")to do the hex formatting, though).
After seeing that, I wondered why we need set_packet_header() at all. But we do for the case when we are filling in the size at the start of a buffer, because xsnprintf() will write an extra NUL that we do not care about. send_sideband() is happy to then overwrite it with data, but code (like format_packet) that computes the buffer, then fills in the size, must avoid overwriting the first byte of the buffer.
-Peff
Re: [PATCH v4 01/12] pkt-line: extract set_packet_header()
In reply to Jeff KingQuoted 9 lines · Jeff King
The cost of
write()may vary on other platforms, but the cost of memcpy generally shouldn't. So I'm inclined to say that it is not really worth micro-optimizing the interface.I think the other issue is that
format_packet()only lets you send string data via "%s", so it cannot be used for arbitrary data that may contain NULs. So we do need _some_ other interface to let you send a raw data packet, and it's going to look similar to thedirect_packet_write()thing.
OK. That is a much better argument than "I already stuff the length bytes in my buffer" (which will invite "How about stop doing that?") to justify a new "I have N bytes of data, send it out", whose signature would look more like write(2) and deserve to be called packet_write() but unfortunately the name is taken by what should have called packet_fmt() or something, but that squats on a good name packet_write(). Sigh.
Re: [PATCH v4 01/12] pkt-line: extract set_packet_header()
Quoted 15 lines · Junio C Hamano
Quoted 9 lines · Jeff King
The cost of
write()may vary on other platforms, but the cost of memcpy generally shouldn't. So I'm inclined to say that it is not really worth micro-optimizing the interface.I think the other issue is that
format_packet()only lets you send string data via "%s", so it cannot be used for arbitrary data that may contain NULs. So we do need _some_ other interface to let you send a raw data packet, and it's going to look similar to thedirect_packet_write()thing.
Quoted 7 lines
OK. That is a much better argument than "I already stuff the length bytes in my buffer" (which will invite "How about stop doing that?") to justify a new "I have N bytes of data, send it out", whose signature would look more like write(2) and deserve to be called
packet_write()but unfortunately the name is taken by what should have calledpacket_fmt()or something, but that squats on a good namepacket_write(). Sigh.
Well, my argument wasn't meant to be offensive. It was just an idea that I published the to get feedback. Now I understand that it wasn't a particular good idea (thanks Peff for the performance test!).
However, besides the bogus performance argument I introduced that function to allow packet writs to fail using the gentle parameter:
http://public-inbox.org/git/D116610C-F33A-43DA-A49D-0B33958822E5%40gmail.com/
Would you be OK if I introduce packet_write_gently() that returns 0 if the write was OK and -1 if it failed?
Thanks,
Lars
Re: [PATCH v4 01/12] pkt-line: extract set_packet_header()
Quoted 6 lines · Lars Schneider
However, besides the bogus performance argument I introduced that function to allow packet writs to fail using the
gentleparameter:
http://public-inbox.org/git/D116610C-F33A-43DA-A49D-0B33958822E5%40gmail.com/Would you be OK if I introduce
packet_write_gently()that returns0if the write was OK and-1if it failed?
Yes, I agree with you that it would be a good thing to have a _gently() variant that lets the caller deal with possible error conditions itself instead of dying.
Thanks.
Re: [PATCH v4 01/12] pkt-line: extract set_packet_header()
In reply to Junio C HamanoQuoted 15 lines · Junio C Hamano
Quoted 9 lines · Jeff King
The cost of
write()may vary on other platforms, but the cost of memcpy generally shouldn't. So I'm inclined to say that it is not really worth micro-optimizing the interface.I think the other issue is that
format_packet()only lets you send string data via "%s", so it cannot be used for arbitrary data that may contain NULs. So we do need _some_ other interface to let you send a raw data packet, and it's going to look similar to thedirect_packet_write()thing.
Quoted 7 lines
OK. That is a much better argument than "I already stuff the length bytes in my buffer" (which will invite "How about stop doing that?") to justify a new "I have N bytes of data, send it out", whose signature would look more like write(2) and deserve to be called
packet_write()but unfortunately the name is taken by what should have calledpacket_fmt()or something, but that squats on a good namepacket_write(). Sigh.
"Sigh" means, a series preparation patch that renames "packet_write()" to "paket_write_fmt()" would not be a good idea? It is used 59 times currently...
- Lars
Re: [PATCH v4 01/12] pkt-line: extract set_packet_header()
Quoted 11 lines · Lars Schneider
Quoted 7 lines · Junio C Hamano
signature would look more like write(2) and deserve to be called
packet_write()but unfortunately the name is taken by what should have calledpacket_fmt()or something, but that squats on a good namepacket_write(). Sigh."Sigh" means, a series preparation patch that renames "
packet_write()" to "paket_write_fmt()" would not be a good idea? It is used 59 times currently...
It would be a good idea in the longer term, I would think. I just wasn't sure if you are willing to volunteer, and in-flight topics will tolerate, such a change right now. I have a feeling that all the current callsites are fairly stable and no in-flight topic touches them, so if you feel like doing so, please go ahead ;-)
Re: [PATCH v4 01/12] pkt-line: extract set_packet_header()
In reply to Junio C HamanoQuoted 12 lines · Junio C Hamano
Quoted 5 lines · [email protected] writes:
From: Lars Schneider <[email protected]>
set_packet_header()converts an integer to a 4 byte hex string. Make this function locally available so that other pkt-line functions can use it.Didn't I say that this is a bad idea already in an earlier review?
Yes, but in that earlier version I made this function *publicly* available. In this patch the function is only available and used within pkt-line.c.
Quoted 10 lines
The only reason why you want it, together with
direct_packet_write()(which I think is another bad idea), is because you usepacket_buf_write()to create a "<header><payload>" in a buf in the usercode in step 11/12 like this:(quoted diff) +2
-0+ packet_buf_write(&nbuf, "command=%s\n", filter_type); + ret = !direct_packet_write(process->in, nbuf.buf, nbuf.len, 1);which would be totally unnecessary if you just did
strbuf_addf()into nbuf and usedpacket_write()like everybody else does.
The usercode in step 11/12 could use packet_buf_write(). I am not worried about performance here. What I am worried about is that packet_buf_write() dies on error. Since direct_packet_write() has a "gentle" parameter in can handle these cases. This is important because a filter might be configured as "required=false" and then errors are OK.
Would you prefer to see a packet_buf_write_gently() instead?
Thanks,
Lars
PATCH v4 2/12 pkt-line: add direct_packet_write() and direct_packet_write_data()
In reply to [email protected]From: Lars Schneider <[email protected]>
Sometimes pkt-line data is already available in a buffer and it would be a waste of resources to write the packet using packet_write() which would copy the existing buffer into a strbuf before writing it.
If the caller has control over the buffer creation then the PKTLINE_DATA_START macro can be used to skip the header and write directly into the data section of a pkt-line (PKTLINE_DATA_LEN bytes would be the maximum). direct_packet_write() would take this buffer, adjust the pkt-line header and write it.
If the caller has no control over the buffer creation then direct_packet_write_data() can be used. This function creates a pkt-line header. Afterwards the header and the data buffer are written using two consecutive write calls.
Both functions have a gentle parameter that indicates if Git should die in case of a write error (gentle set to 0) or return with a error (gentle set to 1).
--- pkt-line.c | 42 ++++++++++++++++++++++++++++++++++++++++++ pkt-line.h | 5 +++++ 2 files changed, 47 insertions(+)
pkt-line.c +42 -0
@@ -136,6 +136,48 @@ void packet_write(int fd, const char *fmt, ...)write_or_die (fd, buf.buf, buf.len); } +int direct_packet_write (int fd,char *buf,size_t size,int gentle) +{ +int ret =0 ; +if (size >LARGE_PACKET_MAX ) { +if (gentle) +return 0 ; +else +die ("protocol error: impossibly long line" ); + } +packet_trace (buf +4 , size -4 ,1 ); +set_packet_header (buf, size); +if (gentle) + ret = !write_or_whine_pipe (fd, buf, size,"pkt-line" ); +else +write_or_die (fd, buf, size); +return ret; +} + +int direct_packet_write_data (int fd,const char *data,size_t size,int gentle) +{ +int ret =0 ; +char hdr[PKTLINE_HEADER_LEN ]; +if (size >PKTLINE_DATA_MAXLEN ) { +if (gentle) +return 0 ; +else +die ("protocol error: impossibly long line" ); + } +set_packet_header (hdr,PKTLINE_HEADER_LEN + size); +packet_trace (data, size,1 ); +if (gentle) { + ret = ( + !write_or_whine_pipe (fd, hdr,PKTLINE_HEADER_LEN ,"pkt-line header" ) || + !write_or_whine_pipe (fd, data, size,"pkt-line data" ) + ); + }else { +write_or_die (fd, hdr,PKTLINE_HEADER_LEN ); +write_or_die (fd, data, size); + } +return ret; +} +void packet_buf_write (struct strbuf *buf,const char *fmt, ...) {va_list args;
pkt-line.h +5 -0
@@ -23,6 +23,8 @@ void packet_flush(int fd);void packet_write (int fd,const char *fmt, ...) __attribute__((format (printf,2 ,3 )));void packet_buf_flush (struct strbuf *buf);void packet_buf_write (struct strbuf *buf,const char *fmt, ...) __attribute__((format (printf,2 ,3 ))); +int direct_packet_write (int fd,char *buf,size_t size,int gentle); +int direct_packet_write_data (int fd,const char *data,size_t size,int gentle);/* * Read a packetized line into the buffer, which must be at least size bytes
@@ -77,6 +79,9 @@ char *packet_read_line_buf(char **src_buf, size_t *src_len, int *size);#define DEFAULT_PACKET_MAX 1000#define LARGE_PACKET_MAX 65520 +#define PKTLINE_HEADER_LEN 4 +#define PKTLINE_DATA_START (pkt) ((pkt) + PKTLINE_HEADER_LEN) +#define PKTLINE_DATA_MAXLEN (LARGE_PACKET_MAX - PKTLINE_HEADER_LEN)extern char packet_buffer[LARGE_PACKET_MAX ];#endif
Signature · 1 line
2.9.0
PATCH v4 3/12 pkt-line: add packet_flush_gentle()
In reply to [email protected]From: Lars Schneider <[email protected]>
packet_flush() would die in case of a write error even though for some callers an error would be acceptable. Add packet_flush_gentle() which writes a pkt-line flush packet and returns 0 for success and 1 for failure.
--- pkt-line.c | 6 ++++++ pkt-line.h | 1 + 2 files changed, 7 insertions(+)
pkt-line.c +6 -0
@@ -91,6 +91,12 @@ void packet_flush(int fd)write_or_die (fd,"0000" ,4 ); } +int packet_flush_gently (int fd) +{ +packet_trace ("0000" ,4 ,1 ); +return !write_or_whine_pipe (fd,"0000" ,4 ,"flush packet" ); +} +void packet_buf_flush (struct strbuf *buf) {packet_trace ("0000" ,4 ,1 );
pkt-line.h +1 -0
@@ -23,6 +23,7 @@ void packet_flush(int fd);void packet_write (int fd,const char *fmt, ...) __attribute__((format (printf,2 ,3 )));void packet_buf_flush (struct strbuf *buf);void packet_buf_write (struct strbuf *buf,const char *fmt, ...) __attribute__((format (printf,2 ,3 ))); +int packet_flush_gently (int fd);int direct_packet_write (int fd,char *buf,size_t size,int gentle);int direct_packet_write_data (int fd,const char *data,size_t size,int gentle);
Signature · 1 line
2.9.0
Re: [PATCH v4 03/12] pkt-line: add packet_flush_gentle()
Quoted 5 lines · On Wed, Aug 03, 2016 at 06:42:16PM +0200, [email protected] wrote:
From: Lars Schneider <[email protected]>
packet_flush()would die in case of a write error even though for some callers an error would be acceptable. Addpacket_flush_gentle()which writes a pkt-line flush packet and returns0for success and1for failure.
Our normal convention would be "0" for success, "-1" for failure.
I see write_or_whine_pipe(), which you use here, has a bizarre "0 for failure, 1 for success", but that nobody actually checks it.
I actually think you probably don't want to use write_or_whine_pipe() here. It does two things:
1. It writes to stderr unconditionally. But if you are doing a "gently" form, then you probably don't want unconditional errors. Since the point of not dying is that you could presumably recover in some way, or do some other more intelligent action.
The existing callers of write_or_whine_pipe() are all in the trace code. Their use is not "let's handle an error", but "we _would_ die except that this is low-priority debugging code that should not interrupt the normal flow". So there it at least makes sense to unconditionally complain to stderr, but not to die().
For your series, I don't think that is true (and especially for most potential callers of a generic "gently flush the packet" function).
2. It calls check_pipe(), which will turn EPIPE into death-by-SIGPIPE (in case you had for some reason ignored SIGPIPE).
But I think that's the opposite of what you want. You know you're writing to a pipe, and I would think EPIPE is the most common reason that your writes would fail (i.e., the helper unexpectedly died while you were writing to it).
So you would want to explicitly ignore SIGPIPE while talking to the helper, and then handle EPIPE just as any other error.
Thinking about (2), I'd go so far as to say that the trace actually should just be using:
if (write_in_full(...) < 0)
warning("unable to write trace to ...: %s", strerror(errno));and we should get rid of write_or_whine_pipe entirely.
-Peff
cover minor trace fixes and cosmetic improvements
Quoted 7 lines · Jeff King
Thinking about (2), I'd go so far as to say that the trace actually should just be using:
if (
write_in_full(...)< 0)warning("unable to write trace to ...: %s", strerror(errno));and we should get rid of
write_or_whine_pipeentirely.
I started to write a patch to do that, but it turns out the trace code is full of bugs (and opportunities for cosmetic improvements).
Here's what I came up with.
[1/7]: trace: handle NULL argument in trace_disable() [2/7]: trace: stop using write_or_whine_pipe() [3/7]: trace: use warning() for printing trace errors [4/7]: trace: cosmetic fixes for error messages [5/7]: trace: correct variable name in write() error message [6/7]: trace: disable key after write error
[7/7]: write_or_die: drop write_or_whine_pipe()
-Peff
PATCH 1/7 trace: handle NULL argument in trace_disable()
All of the trace functions treat a NULL key as a synonym for the default GIT_TRACE key. Except for trace_disable(), which will segfault.
Fortunately, this can't cause any bugs, as the function has no callers. But rather than drop it, let's fix the bug, as I plan to add a caller.
--- trace.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
trace.c +16 -4
@@ -25,15 +25,25 @@#include "cache.h" #include "quote.h" +/* +* "Normalize" a key argument by converting NULL to our trace_default, +* and otherwise passing through the value. All caller-facing functions +* should normalize their inputs in this way, though most get it +* for free by calling get_trace_fd() (directly or indirectly). +*/ +static void normalize_trace_key (struct trace_key **key) +{ +static struct trace_key trace_default = {"GIT_TRACE" }; +if (!*key) + *key = &trace_default; +} +/* Get a trace file descriptor from "key" env variable. */ static int get_trace_fd (struct trace_key *key) { -static struct trace_key trace_default = {"GIT_TRACE" };const char *trace; -/* use default "GIT_TRACE" if NULL */ -if (!key) - key = &trace_default; +normalize_trace_key (&key);/* don't open twice */ if (key->initialized)
@@ -75,6 +85,8 @@ static int get_trace_fd(struct trace_key *key)void trace_disable (struct trace_key *key) { +normalize_trace_key (&key); +if (key->need_close)close (key->fd); key->fd =0 ;
Signature · 1 line
2.9.2.670.g42e63de
PATCH 2/7 trace: stop using write_or_whine_pipe()
In reply to Jeff KingThe write_or_whine_pipe function does two things:
1. it checks for EPIPE and converts it into a signal death
2. it prints a message to stderr on error
The first thing does not help us, and actively hurts. Generally we would simply die from SIGPIPE in this case, unless somebody has taken the time to ignore SIGPIPE for the whole process. And if they _did_ do that, it seems rather silly for the trace code, which otherwise takes pains to continue even in the face of errors (e.g., by not using write_or_die!), to take down the whole process for one specific type of error.
Nor does the second thing help us; it just makes it harder to write our error message, because we have to feed bits of it as an argument to write_or_whine_pipe(). Translators never get to see the full message, and it's hard for us to customize it.
Let's switch to just using write_in_full() and writing our own error string. For now, the error is identical to what write_or_whine_pipe() would say, but now that it's more under our control, we can improve it in future patches.
--- trace.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
trace.c +8 -3
@@ -132,18 +132,23 @@ static int prepare_trace_line(const char *file, int line,return 1 ; } +static void trace_write (struct trace_key *key,const void *buf,unsigned len) +{ +if (write_in_full (get_trace_fd (key), buf, len) <0 ) +fprintf (stderr,"%s: write error (%s)\n" , err_msg,strerror (errno)); +} +void trace_verbatim (struct trace_key *key,const void *buf,unsigned len) {if (!trace_want (key))return ; -write_or_whine_pipe (get_trace_fd (key), buf, len, err_msg); +trace_write (key, buf, len); }static void print_trace_line (struct trace_key *key,struct strbuf *buf) {strbuf_complete_line (buf); - -write_or_whine_pipe (get_trace_fd (key), buf->buf, buf->len, err_msg); +trace_write (key, buf->buf, buf->len);strbuf_release (buf); }
Signature · 1 line
2.9.2.670.g42e63de
PATCH 3/7 trace: use warning() for printing trace errors
In reply to Jeff KingRight now we just fprintf() straight to stderr, which can make the output hard to distinguish. It would be helpful to give it one of our usual prefixes like "error:", "warning:", etc.
It doesn't make sense to use error() here, as the trace code is "optional" debugging code. If something goes wrong, we should warn the user, but saying "error" implies the actual git operation had a problem. So warning() is the only sane choice.
Note that this does end up calling warn_routine() to do the formatting. So in theory, somebody who tries to trace from their warn_routine() could cause a loop. But nobody does this, and in fact nobody in the history of git has ever replaced the default warn_builtin (there isn't even a set_warn_routine function!).
--- trace.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
trace.c +5 -6
@@ -61,9 +61,8 @@ static int get_trace_fd(struct trace_key *key)else if (is_absolute_path (trace)) {int fd =open (trace,O_WRONLY |O_APPEND |O_CREAT ,0666 );if (fd ==-1 ) { -fprintf (stderr, -"Could not open '%s' for tracing: %s\n" -"Defaulting to tracing on stderr...\n" , +warning ("Could not open '%s' for tracing: %s\n" +"Defaulting to tracing on stderr..." , trace,strerror (errno)); key->fd =STDERR_FILENO ; }else {
@@ -71,10 +70,10 @@ static int get_trace_fd(struct trace_key *key) key->need_close =1 ; } }else { -fprintf (stderr,"What does '%s' for %s mean?\n" +warning ("What does '%s' for %s mean?\n" "If you want to trace into a file, then please set " "%s to an absolute pathname (starting with /).\n" -"Defaulting to tracing on stderr...\n" , +"Defaulting to tracing on stderr..." , trace, key->key, key->key); key->fd =STDERR_FILENO ; }
@@ -135,7 +134,7 @@ static int prepare_trace_line(const char *file, int line,static void trace_write (struct trace_key *key,const void *buf,unsigned len) {if (write_in_full (get_trace_fd (key), buf, len) <0 ) -fprintf (stderr,"%s: write error (%s)\n" , err_msg,strerror (errno)); +warning ("%s: write error (%s)" , err_msg,strerror (errno)); }void trace_verbatim (struct trace_key *key,const void *buf,unsigned len)
Signature · 1 line
2.9.2.670.g42e63de
Re: [PATCH 3/7] trace: use warning() for printing trace errors
Quoted 11 lines · Jeff King
Right now we just
fprintf()straight to stderr, which can make the output hard to distinguish. It would be helpful to give it one of our usual prefixes like "error:", "warning:", etc.It doesn't make sense to use
error()here, as the trace code is "optional" debugging code. If something goes wrong, we should warn the user, but saying "error" implies the actual git operation had a problem. Sowarning()is the only sane choice.
Quoted 6 lines
Note that this does end up calling
warn_routine()to do the formatting. So in theory, somebody who tries to trace from theirwarn_routine()could cause a loop. But nobody does this, and in fact nobody in the history of git has ever replaced the defaultwarn_builtin(there isn't even aset_warn_routinefunction!).
I think the last bit is about to change; cf. 545f13c0 (usage: add set_warn_routine(), 2016-07-30) on cc/apply-am topic.
Re: [PATCH 3/7] trace: use warning() for printing trace errors
Quoted 20 lines · Junio C Hamano
Quoted 11 lines · Jeff King
Right now we just
fprintf()straight to stderr, which can make the output hard to distinguish. It would be helpful to give it one of our usual prefixes like "error:", "warning:", etc.It doesn't make sense to use
error()here, as the trace code is "optional" debugging code. If something goes wrong, we should warn the user, but saying "error" implies the actual git operation had a problem. Sowarning()is the only sane choice.Quoted 6 lines
Note that this does end up calling
warn_routine()to do the formatting. So in theory, somebody who tries to trace from theirwarn_routine()could cause a loop. But nobody does this, and in fact nobody in the history of git has ever replaced the defaultwarn_builtin(there isn't even aset_warn_routinefunction!).
Quoted 2 lines
I think the last bit is about to change; cf.
545f13c0(usage: addset_warn_routine(), 2016-07-30) on cc/apply-am topic.
Thanks, I meant to check this series against "pu" to make sure there are no new callers for write_or_whine_pipe(), but forgot to.
It looks like that same topic does add one new caller, and switches the "fprintf" inside it to use warning().
IMHO the call added by 19a73ac (builtin/apply: make try_create_file() return -1 on error, 2016-07-30) should just be a regular:
if (write_in_full(...) < 0)
error(...);We don't care about the weird pipe handling there (we know we're writing to a file we just created), and the way the error message is passed in just makes things weird. Plus it seems more like an error() than a warning (e.g., we call error() immediately below when close() fails!). But 8fab3c6 (write_or_die: use warning() instead of fprintf(stderr, ...), 2016-07-30) makes it an unconditional warning (that commit, btw, has a bug in that it retains the trailing newline of the message, even though warning() will add one itself).
So I'd suggest that series drop the call write_or_whine_pipe() and drop 8fab3c6 entirely.
I wondered if that would then let us drop set_warn_routine(), but it looks like there are other warning() calls it cares about. So that would invalidate the last paragraph here, though I still think converting the trace errors to warning() is a reasonable thing to do.
-Peff
Re: [PATCH 3/7] trace: use warning() for printing trace errors
Quoted 4 lines · Jeff King
I wondered if that would then let us drop
set_warn_routine(), but it looks like there are otherwarning()calls it cares about. So that would invalidate the last paragraph here, though I still think converting the trace errors towarning()is a reasonable thing to do.
Yes. That is why tonight's pushout will have this series in 'jch' (that is a point on a linear history between 'master' and 'pu') and tentatively ejects cc/apply-am topic out of 'pu', expecting it to be rerolled.
Thanks.
Re: [PATCH 3/7] trace: use warning() for printing trace errors
Quoted 11 lines · Junio C Hamano
Quoted 4 lines · Jeff King
I wondered if that would then let us drop
set_warn_routine(), but it looks like there are otherwarning()calls it cares about. So that would invalidate the last paragraph here, though I still think converting the trace errors towarning()is a reasonable thing to do.Yes. That is why tonight's pushout will have this series in 'jch' (that is a point on a linear history between 'master' and 'pu') and tentatively ejects cc/apply-am topic out of 'pu', expecting it to be rerolled.
Here's a replacement patch 3. Same code, but it clarifies the warn_routine situation in the commit message.
-- >8 --
Subject: [PATCH] trace: use warning() for printing trace errors
Right now we just fprintf() straight to stderr, which can make the output hard to distinguish. It would be helpful to give it one of our usual prefixes like "error:", "warning:", etc.
It doesn't make sense to use error() here, as the trace code is "optional" debugging code. If something goes wrong, we should warn the user, but saying "error" implies the actual git operation had a problem. So warning() is the only sane choice.
Note that this does end up calling warn_routine() to do the formatting. This is probably a good thing, since they are clearly trying to hook messages before they make it to stderr. However, it also means that in theory somebody who tries to trace from their warn_routine() could cause a loop. This seems rather unlikely in practice (we've never even overridden the default warn_builtin routine before, and recent discussions to do so would just install a noop routine).
--- trace.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
trace.c +5 -6
@@ -61,9 +61,8 @@ static int get_trace_fd(struct trace_key *key)else if (is_absolute_path (trace)) {int fd =open (trace,O_WRONLY |O_APPEND |O_CREAT ,0666 );if (fd ==-1 ) { -fprintf (stderr, -"Could not open '%s' for tracing: %s\n" -"Defaulting to tracing on stderr...\n" , +warning ("Could not open '%s' for tracing: %s\n" +"Defaulting to tracing on stderr..." , trace,strerror (errno)); key->fd =STDERR_FILENO ; }else {
@@ -71,10 +70,10 @@ static int get_trace_fd(struct trace_key *key) key->need_close =1 ; } }else { -fprintf (stderr,"What does '%s' for %s mean?\n" +warning ("What does '%s' for %s mean?\n" "If you want to trace into a file, then please set " "%s to an absolute pathname (starting with /).\n" -"Defaulting to tracing on stderr...\n" , +"Defaulting to tracing on stderr..." , trace, key->key, key->key); key->fd =STDERR_FILENO ; }
@@ -135,7 +134,7 @@ static int prepare_trace_line(const char *file, int line,static void trace_write (struct trace_key *key,const void *buf,unsigned len) {if (write_in_full (get_trace_fd (key), buf, len) <0 ) -fprintf (stderr,"%s: write error (%s)\n" , err_msg,strerror (errno)); +warning ("%s: write error (%s)" , err_msg,strerror (errno)); }void trace_verbatim (struct trace_key *key,const void *buf,unsigned len)
Signature · 1 line
2.9.2.707.g48ee8b7
Re: [PATCH 3/7] trace: use warning() for printing trace errors
In reply to Junio C HamanoQuoted 11 lines · Junio C Hamano
Quoted 4 lines · Jeff King
I wondered if that would then let us drop
set_warn_routine(), but it looks like there are otherwarning()calls it cares about. So that would invalidate the last paragraph here, though I still think converting the trace errors towarning()is a reasonable thing to do.Yes. That is why tonight's pushout will have this series in 'jch' (that is a point on a linear history between 'master' and 'pu') and tentatively ejects cc/apply-am topic out of 'pu', expecting it to be rerolled.
Ok, I will reroll soon with Peff's suggested changes.
Re: [PATCH 3/7] trace: use warning() for printing trace errors
Quoted 8 lines · Christian Couder
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Jeff King
I wondered if that would then let us drop
set_warn_routine(), but it looks like there are otherwarning()calls it cares about. So that would invalidate the last paragraph here, though I still think converting the trace errors towarning()is a reasonable thing to do.
Quoted 6 lines
Quoted 4 lines
Yes. That is why tonight's pushout will have this series in 'jch' (that is a point on a linear history between 'master' and 'pu') and tentatively ejects cc/apply-am topic out of 'pu', expecting it to be rerolled.
Ok, I will reroll soon with Peff's suggested changes.
Thanks.
PATCH 4/7 trace: cosmetic fixes for error messages
In reply to Jeff KingThe error messages for the trace code are often multi-line; the first line gets a nice "warning:", but the rest are left-aligned. Let's give them an indentation to make sure they stand out as a unit.
While we're here, let's also downcase the first letter of each error (our usual style), and break up a long line of advice (since we're already using multiple lines, one more doesn't hurt).
We also replace "What does 'foo' for GIT_TRACE mean?". While cute, it's probably a good idea to give more context, and follow our usual styles. So it's now "unknown trace value for 'GIT_TRACE': foo".
---
I went with an indent the size of "warning: ", because that string does not actually get translated (it seems like it probably should, though).
I think it would be nicer to still to print:
warning: first line
warning: second line
etc. We do that for "advice:", but not the rest of the vreportf functions. It might be nice to do that, but we'd have to go back to printing into a buffer (since we can't break up the incoming format string that we feed to fprintf).
trace.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
trace.c +8 -8
@@ -61,8 +61,8 @@ static int get_trace_fd(struct trace_key *key)else if (is_absolute_path (trace)) {int fd =open (trace,O_WRONLY |O_APPEND |O_CREAT ,0666 );if (fd ==-1 ) { -warning ("Could not open '%s' for tracing: %s\n" -"Defaulting to tracing on stderr..." , +warning ("could not open '%s' for tracing: %s\n" +" Defaulting to tracing on stderr..." , trace,strerror (errno)); key->fd =STDERR_FILENO ; }else {
@@ -70,11 +70,11 @@ static int get_trace_fd(struct trace_key *key) key->need_close =1 ; } }else { -warning ("What does '%s' for %s mean?\n" -"If you want to trace into a file, then please set " -"%s to an absolute pathname (starting with /).\n" -"Defaulting to tracing on stderr..." , - trace, key->key, key->key); +warning ("unknown trace value for '%s': %s\n" +" If you want to trace into a file, then please set %s\n" +" to an absolute pathname (starting with /)\n" +" Defaulting to tracing on stderr..." , + key->key, trace, key->key); key->fd =STDERR_FILENO ; }
@@ -93,7 +93,7 @@ void trace_disable(struct trace_key *key) key->need_close =0 ; } -static const char err_msg[] ="Could not trace into fd given by " +static const char err_msg[] ="could not trace into fd given by " "GIT_TRACE environment variable" ; static int prepare_trace_line(const char *file, int line,
Signature · 1 line
2.9.2.670.g42e63de
Re: [PATCH 4/7] trace: cosmetic fixes for error messages
Quoted 9 lines · Jeff King
I think it would be nicer to still to print:
warning: first line
warning: second lineetc. We do that for "advice:", but not the rest of the vreportf functions. It might be nice to do that, but we'd have to go back to printing into a buffer (since we can't break up the incoming format string that we feed to fprintf).
Yes, yes.
Re: [PATCH 4/7] trace: cosmetic fixes for error messages
Quoted 7 lines · Junio C Hamano
Quoted 5 lines · Jeff King
I think it would be nicer to still to print:
warning: first line
warning: second line
Quoted 6 lines
Quoted 4 lines
etc. We do that for "advice:", but not the rest of the vreportf functions. It might be nice to do that, but we'd have to go back to printing into a buffer (since we can't break up the incoming format string that we feed to fprintf).
Yes, yes.
Actually, I guess in this case we could easily do:
warning("something");
warning("something else");etc (the lines are fairly stand-alone, so I don't think it runs afoul of the usual translator-lego problem; not to mention that these aren't actually translated). I don't really care that much between that and the indented output, but if there's a preference, I'm happy to re-roll with that.
-Peff
PATCH 5/7 trace: correct variable name in write() error message
In reply to Jeff KingOur error message for write() always mentions GIT_TRACE, even though we may be writing for a different variable entirely. It's also not quite accurate to say "fd given by GIT_TRACE environment variable", as we may hit this error based on a filename the user put in the variable (we do complain and switch to stderr if the file cannot be opened, but it's still possible to hit a write() error on the descriptor later).
So let's fix those things, and switch to our more usual "unable to do X: Y" format for the error.
--- trace.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
trace.c +5 -5
@@ -93,9 +93,6 @@ void trace_disable(struct trace_key *key) key->need_close =0 ; } -static const char err_msg[] ="could not trace into fd given by " -"GIT_TRACE environment variable" ; -static int prepare_trace_line (const char *file,int line,struct trace_key *key,struct strbuf *buf) {
@@ -133,8 +130,11 @@ static int prepare_trace_line(const char *file, int line,static void trace_write (struct trace_key *key,const void *buf,unsigned len) { -if (write_in_full (get_trace_fd (key), buf, len) <0 ) -warning ("%s: write error (%s)" , err_msg,strerror (errno)); +if (write_in_full (get_trace_fd (key), buf, len) <0 ) { +normalize_trace_key (&key); +warning ("unable to write trace for %s: %s" , + key->key,strerror (errno)); + } }void trace_verbatim (struct trace_key *key,const void *buf,unsigned len)
Signature · 1 line
2.9.2.670.g42e63de
Re: [PATCH 0/7] minor trace fixes and cosmetic improvements
In reply to Jeff KingQuoted 15 lines · Jeff King
Quoted 7 lines · Jeff King
Thinking about (2), I'd go so far as to say that the trace actually should just be using:
if (
write_in_full(...)< 0)warning("unable to write trace to ...: %s", strerror(errno));and we should get rid of
write_or_whine_pipeentirely.I started to write a patch to do that, but it turns out the trace code is full of bugs (and opportunities for cosmetic improvements).
Here's what I came up with.
Quoted 7 lines
[1/7]: trace: handle NULL argument in
trace_disable()[2/7]: trace: stop usingwrite_or_whine_pipe()[3/7]: trace: usewarning()for printing trace errors [4/7]: trace: cosmetic fixes for error messages [5/7]: trace: correct variable name inwrite()error message [6/7]: trace: disable key after write error
[7/7]:write_or_die: dropwrite_or_whine_pipe()
Oops, I meant to detach this from the parent thread, but apparently I am incompetent at editing email headers.
This really is totally orthogonal to your series (except that you should obviously not use write_or_whine_pipe(), but that is the case whether I rip it out or not :) ).
-Peff
PATCH 6/7 trace: disable key after write error
In reply to Jeff KingIf we get a write error writing to a trace descriptor, the error isn't likely to go away if we keep writing. Instead, you'll just get the same error over and over. E.g., try:
GIT_TRACE_PACKET=42 git ls-remote >/dev/null
You don't really need to see:
warning: unable to write trace for GIT_TRACE_PACKET: Bad file descriptor
hundreds of times. We could fallback to tracing to stderr, as we do in the error code-path for open(), but there's not much point. If the user fed us a bogus descriptor, they're probably better off fixing their invocation. And if they didn't, and we saw a transient error (e.g., ENOSPC writing to a file), it probably doesn't help anybody to have half of the trace in a file, and half on stderr.
--- trace.c | 1 + 1 file changed, 1 insertion(+)
trace.c +1 -0
@@ -134,6 +134,7 @@ static void trace_write(struct trace_key *key, const void *buf, unsigned len)normalize_trace_key (&key);warning ("unable to write trace for %s: %s" , key->key,strerror (errno)); +trace_disable (key); } }
Signature · 1 line
2.9.2.670.g42e63de
Re: [PATCH 6/7] trace: disable key after write error
Quoted 10 lines · Jeff King
If we get a write error writing to a trace descriptor, the error isn't likely to go away if we keep writing. Instead, you'll just get the same error over and over. E.g., try:
GIT_TRACE_PACKET=42 git ls-remote >/dev/null
You don't really need to see:
warning: unable to write trace for
GIT_TRACE_PACKET: Bad file descriptor
Quoted 7 lines
hundreds of times. We could fallback to tracing to stderr, as we do in the error code-path for
open(), but there's not much point. If the user fed us a bogus descriptor, they're probably better off fixing their invocation. And if they didn't, and we saw a transient error (e.g., ENOSPC writing to a file), it probably doesn't help anybody to have half of the trace in a file, and half on stderr.
Yes, I think I like this better than "we cannot open the named file, so let's trace into standard error stream" that is done in the code in the context of [3/7]. We should do the same over there.
Re: [PATCH 6/7] trace: disable key after write error
Quoted 20 lines · Junio C Hamano
Quoted 10 lines · Jeff King
If we get a write error writing to a trace descriptor, the error isn't likely to go away if we keep writing. Instead, you'll just get the same error over and over. E.g., try:
GIT_TRACE_PACKET=42 git ls-remote >/dev/null
You don't really need to see:
warning: unable to write trace for
GIT_TRACE_PACKET: Bad file descriptorQuoted 7 lines
hundreds of times. We could fallback to tracing to stderr, as we do in the error code-path for
open(), but there's not much point. If the user fed us a bogus descriptor, they're probably better off fixing their invocation. And if they didn't, and we saw a transient error (e.g., ENOSPC writing to a file), it probably doesn't help anybody to have half of the trace in a file, and half on stderr.
Quoted 3 lines
Yes, I think I like this better than "we cannot open the named file, so let's trace into standard error stream" that is done in the code in the context of [3/7]. We should do the same over there.
Yeah, I was tempted to strip that out, too. I'll look into preparing a patch on top.
-Peff
Re: [PATCH 6/7] trace: disable key after write error
Quoted 22 lines · Jeff King
Quoted 20 lines · Junio C Hamano
Quoted 17 lines · Jeff King
If we get a write error writing to a trace descriptor, the error isn't likely to go away if we keep writing. Instead, you'll just get the same error over and over. E.g., try:
GIT_TRACE_PACKET=42 git ls-remote >/dev/null
You don't really need to see:
warning: unable to write trace for
GIT_TRACE_PACKET: Bad file descriptorhundreds of times. We could fallback to tracing to stderr, as we do in the error code-path for
open(), but there's not much point. If the user fed us a bogus descriptor, they're probably better off fixing their invocation. And if they didn't, and we saw a transient error (e.g., ENOSPC writing to a file), it probably doesn't help anybody to have half of the trace in a file, and half on stderr.
Quoted 6 lines
Quoted 3 lines
Yes, I think I like this better than "we cannot open the named file, so let's trace into standard error stream" that is done in the code in the context of [3/7]. We should do the same over there.
Yeah, I was tempted to strip that out, too. I'll look into preparing a patch on top.
Here's a patch that can go on the tip of jk/trace-fixup.
-- >8 --
Subject: [PATCH] trace: do not fall back to stderr
If the trace code cannot open a specified file, or does not understand the contents of the GIT_TRACE variable, it falls back to printing trace output to stderr.
This is an attempt to be helpful, but in practice it just ends up annoying. The user was trying to get the output to go somewhere else, so spewing it to stderr does not really accomplish that. And as it's intended for debugging, they can presumably re-run the command with their error corrected.
So instead of falling back, this patch disables bogus trace keys for the rest of the program, just as we do for write errors. We can drop the "Defaulting to..." part of the error message entirely; after seeing "cannot open '/foo'", the user can assume that tracing is skipped.
--- trace.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
trace.c +4 -6
@@ -61,10 +61,9 @@ static int get_trace_fd(struct trace_key *key)else if (is_absolute_path (trace)) {int fd =open (trace,O_WRONLY |O_APPEND |O_CREAT ,0666 );if (fd ==-1 ) { -warning ("could not open '%s' for tracing: %s\n" -" Defaulting to tracing on stderr..." , +warning ("could not open '%s' for tracing: %s" , trace,strerror (errno)); - key->fd =STDERR_FILENO ; +trace_disable (key); }else { key->fd = fd; key->need_close =1 ;
@@ -72,10 +71,9 @@ static int get_trace_fd(struct trace_key *key) }else {warning ("unknown trace value for '%s': %s\n" " If you want to trace into a file, then please set %s\n" -" to an absolute pathname (starting with /)\n" -" Defaulting to tracing on stderr..." , +" to an absolute pathname (starting with /)" , key->key, trace, key->key); - key->fd =STDERR_FILENO ; +trace_disable (key); } key->initialized =1 ;
Signature · 1 line
2.9.2.707.g48ee8b7
PATCH 7/7 write_or_die: drop write_or_whine_pipe()
In reply to Jeff KingThis function has no callers, and is not likely to gain any because it's confusing to use.
It unconditionally complains to stderr, but _doesn't_ die. Yet any caller which wants a "gentle" write would generally want to suppress the error message, because presumably they're going to write a better one, and/or try the operation again.
And the check_pipe() call leads to confusing behaviors. It means we die for EPIPE, but not for other errors, which is confusing and pointless.
On top of all that, it has unusual error return semantics, which makes it easy for callers to get it wrong.
Let's drop the function, and if somebody ever needs to resurrect something like it, they can fix these warts.
--- cache.h | 1 - write_or_die.c | 12 ------------ 2 files changed, 13 deletions(-)
cache.h +0 -1
@@ -1740,7 +1740,6 @@ extern int copy_file(const char *dst, const char *src, int mode);extern int copy_file_with_time (const char *dst,const char *src,int mode);extern void write_or_die (int fd,const void *buf,size_t count); -extern int write_or_whine_pipe (int fd,const void *buf,size_t count,const char *msg);extern void fsync_or_die (int fd,const char *);extern ssize_t read_in_full (int fd,void *buf,size_t count);
write_or_die.c +0 -12
@@ -82,15 +82,3 @@ void write_or_die(int fd, const void *buf, size_t count)die_errno ("write error" ); } } - -int write_or_whine_pipe (int fd,const void *buf,size_t count,const char *msg) -{ -if (write_in_full (fd, buf, count) <0 ) { -check_pipe (errno); -fprintf (stderr,"%s: write error (%s)\n" , - msg,strerror (errno)); -return 0 ; - } - -return 1 ; -}
Signature · 1 line
2.9.2.670.g42e63de
Re: [PATCH v4 03/12] pkt-line: add packet_flush_gentle()
In reply to Jeff KingQuoted 11 lines · Jeff King
2. It calls
check_pipe(), which will turn EPIPE into death-by-SIGPIPE (in case you had for some reason ignored SIGPIPE).
...Thinking about (2), I'd go so far as to say that the trace actually should just be using:
if (
write_in_full(...)< 0)warning("unable to write trace to ...: %s", strerror(errno));and we should get rid of
write_or_whine_pipeentirely.
I like the simplicity the above suggestion gives us.
PATCH v4 4/12 pkt-line: call packet_trace() only if a packet is actually send
In reply to [email protected]From: Lars Schneider <[email protected]>
The packet_trace() call is not ideal in format_packet() as we would print a trace when a packet is formatted and (potentially) when the packet is actually send. This was no problem up until now because format_packet() was only used by one function. Fix it by moving the trace call into the function that actally sends the packet.
--- pkt-line.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
pkt-line.c +1 -1
@@ -127,7 +127,6 @@ static void format_packet(struct strbuf *out, const char *fmt, va_list args)die ("protocol error: impossibly long line" );set_packet_header (&out->buf[orig_len], n); -packet_trace (out->buf + orig_len +4 , n -4 ,1 ); }void packet_write (int fd,const char *fmt, ...)
@@ -139,6 +138,7 @@ void packet_write(int fd, const char *fmt, ...)va_start (args, fmt);format_packet (&buf, fmt, args);va_end (args); +packet_trace (buf.buf +4 , buf.len -4 ,1 );write_or_die (fd, buf.buf, buf.len); }
Signature · 1 line
2.9.0
PATCH v4 5/12 pkt-line: add functions to read/write flush terminated packet streams
In reply to [email protected]From: Lars Schneider <[email protected]>
packet_write_stream_with_flush_from_fd() and packet_write_stream_with_flush_from_buf() write a stream of packets. All content packets use the maximal packet size except for the last one. After the last content packet a flush control packet is written.
packet_read_till_flush() reads arbitary sized packets until it detects a flush packet.
--- pkt-line.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ pkt-line.h | 7 +++++ 2 files changed, 95 insertions(+)
pkt-line.c +88 -0
@@ -193,6 +193,44 @@ void packet_buf_write(struct strbuf *buf, const char *fmt, ...)va_end (args); } +int packet_write_stream_with_flush_from_fd (const int fd_in,const int fd_out) +{ +int did_fail =0 ; +ssize_t bytes_to_write; +while (!did_fail) { + bytes_to_write =xread (fd_in,PKTLINE_DATA_START (packet_buffer),PKTLINE_DATA_MAXLEN ); +if (bytes_to_write <0 ) +return COPY_READ_ERROR ; +if (bytes_to_write ==0 ) +break ; + did_fail |=direct_packet_write (fd_out, packet_buffer,PKTLINE_HEADER_LEN + bytes_to_write,1 ); + } +if (!did_fail) + did_fail =packet_flush_gently (fd_out); +return (did_fail ?COPY_WRITE_ERROR :0 ); +} + +int packet_write_stream_with_flush_from_buf (const char *src_in,size_t len,int fd_out) +{ +int did_fail =0 ; +size_t bytes_written =0 ; +size_t bytes_to_write; +while (!did_fail) { +if ((len - bytes_written) >PKTLINE_DATA_MAXLEN ) + bytes_to_write =PKTLINE_DATA_MAXLEN ; +else + bytes_to_write = len - bytes_written; +if (bytes_to_write ==0 ) +break ; + did_fail |=direct_packet_write_data (fd_out, src_in + bytes_written, bytes_to_write,1 ); + bytes_written += bytes_to_write; + } +if (!did_fail) + did_fail =packet_flush_gently (fd_out); +return did_fail; +} + +static int get_packet_data (int fd,char **src_buf,size_t *src_size,void *dst,unsigned size,int options) {
@@ -302,3 +340,53 @@ char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len) {return packet_read_line_generic (-1 , src, src_len, dst_len); } + +ssize_t packet_read_till_flush (int fd_in,struct strbuf *sb_out) +{ +int len, ret; +int options =PACKET_READ_GENTLE_ON_EOF ; +char linelen[4 ]; + +size_t oldlen = sb_out->len; +size_t oldalloc = sb_out->alloc; + +for (;;) { +// Read packet header + ret =get_packet_data (fd_in,NULL ,NULL , linelen,4 , options); +if (ret <0 ) + goto done; + len =packet_length (linelen); +if (len <0 ) +die ("protocol error: bad line length character: %.4s" , linelen); +if (!len) { +// Found a flush packet - Done! +packet_trace ("0000" ,4 ,0 ); +break ; + } + len -=4 ; + +// Read packet content +strbuf_grow (sb_out, len); + ret =get_packet_data (fd_in,NULL ,NULL , sb_out->buf + sb_out->len, len, options); +if (ret <0 ) + goto done; + +if (ret != len) { +error ("protocol error: incomplete read (expected %d, got %d)" , len, ret); + goto done; + } + +packet_trace (sb_out->buf + sb_out->len, len,0 ); + sb_out->len += len; + } + +done: +if (ret <0 ) { +if (oldalloc ==0 ) +strbuf_release (sb_out); +else +strbuf_setlen (sb_out, oldlen); +return ret;// unexpected EOF + } +return sb_out->len - oldlen; +}
pkt-line.h +7 -0
@@ -26,6 +26,8 @@ void packet_buf_write(struct strbuf *buf, const char *fmt, ...) __attribute__((fint packet_flush_gently (int fd);int direct_packet_write (int fd,char *buf,size_t size,int gentle);int direct_packet_write_data (int fd,const char *data,size_t size,int gentle); +int packet_write_stream_with_flush_from_fd (const int fd_in,const int fd_out); +int packet_write_stream_with_flush_from_buf (const char *src_in,size_t len,int fd_out);/* * Read a packetized line into the buffer, which must be at least size bytes
@@ -78,6 +80,11 @@ char *packet_read_line(int fd, int *size);*/ char *packet_read_line_buf (char **src_buf,size_t *src_len,int *size); +/* +* Reads a stream of variable sized packets until a flush packet is detected. +*/ +ssize_t packet_read_till_flush (int fd_in,struct strbuf *sb_out); +#define DEFAULT_PACKET_MAX 1000#define LARGE_PACKET_MAX 65520#define PKTLINE_HEADER_LEN 4
Signature · 1 line
2.9.0
PATCH v4 6/12 pack-protocol: fix maximum pkt-line size
In reply to [email protected]From: Lars Schneider <[email protected]>
According to LARGE_PACKET_MAX in pkt-line.h the maximal length of a pkt-line packet is 65520 bytes. The pkt-line header takes 4 bytes and therefore the pkt-line data component must not exceed 65516 bytes.
--- Documentation/technical/protocol-common.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Documentation/technical/protocol-common.txt +3 -3
@@ -67,9 +67,9 @@ with non-binary data the same whether or not they contain the trailing LF (stripping the LF if present, and not complaining when it is missing). -The maximum length of a pkt-line's data component is 65520 bytes. -Implementations MUST NOT send pkt-line whose length exceeds 65524 -(65520 bytes of payload + 4 bytes of length data). +The maximum length of a pkt-line's data component is 65516 bytes. +Implementations MUST NOT send pkt-line whose length exceeds 65520 +(65516 bytes of payload + 4 bytes of length data). Implementations SHOULD NOT send an empty pkt-line ("0004").
Signature · 1 line
2.9.0
PATCH v4 7/12 run-command: add clean_on_exit_handler
In reply to [email protected]From: Lars Schneider <[email protected]>
Some commands might need to perform cleanup tasks on exit. Let's give them an interface for doing this.
Please note, that the cleanup callback is not executed if Git dies of a signal. The reason is that only "async-signal-safe" functions would be allowed to be call in that case. Since we cannot control what functions the callback will use, we will not support the case. See 507d7804 for more details.
--- run-command.c | 12 ++++++++---- run-command.h | 1 + 2 files changed, 9 insertions(+), 4 deletions(-)
run-command.c +8 -4
@@ -21,6 +21,7 @@ void child_process_clear(struct child_process *child)struct child_to_clean {pid_t pid; +void (*clean_on_exit_handler)(pid_t );struct child_to_clean *next; };static struct child_to_clean *children_to_clean;
@@ -30,6 +31,8 @@ static void cleanup_children(int sig, int in_signal) {while (children_to_clean) {struct child_to_clean *p = children_to_clean; +if (!in_signal && p->clean_on_exit_handler) + p->clean_on_exit_handler (p->pid); children_to_clean = p->next;kill (p->pid, sig);if (!in_signal)
@@ -49,10 +52,11 @@ static void cleanup_children_on_exit(void)cleanup_children (SIGTERM ,0 ); } -static void mark_child_for_cleanup (pid_t pid) +static void mark_child_for_cleanup (pid_t pid,void (*clean_on_exit_handler)(pid_t )) {struct child_to_clean *p =xmalloc (sizeof (*p)); p->pid = pid; + p->clean_on_exit_handler = clean_on_exit_handler; p->next = children_to_clean; children_to_clean = p;
@@ -422,7 +426,7 @@ int start_command(struct child_process *cmd)if (cmd->pid <0 )error_errno ("cannot fork() for %s" , cmd->argv[0 ]);else if (cmd->clean_on_exit) -mark_child_for_cleanup (cmd->pid); +mark_child_for_cleanup (cmd->pid, cmd->clean_on_exit_handler);/* * Wait for child's execvp. If the execvp succeeds (or if fork()
@@ -483,7 +487,7 @@ int start_command(struct child_process *cmd)if (cmd->pid <0 && (!cmd->silent_exec_failure || errno !=ENOENT ))error_errno ("cannot spawn %s" , cmd->argv[0 ]);if (cmd->clean_on_exit && cmd->pid >=0 ) -mark_child_for_cleanup (cmd->pid); +mark_child_for_cleanup (cmd->pid, cmd->clean_on_exit_handler);argv_array_clear (&nargv); cmd->argv = sargv;
@@ -752,7 +756,7 @@ int start_async(struct async *async)exit (!!async->proc (proc_in, proc_out, async->data)); } -mark_child_for_cleanup (async->pid); +mark_child_for_cleanup (async->pid,NULL );if (need_in)close (fdin[0 ]);
run-command.h +1 -0
@@ -43,6 +43,7 @@ struct child_process { unsigned stdout_to_stderr:1; unsigned use_shell:1; unsigned clean_on_exit:1; +void (*clean_on_exit_handler)(pid_t ); };#define CHILD_PROCESS_INIT { NULL, ARGV_ARRAY_INIT, ARGV_ARRAY_INIT }
Signature · 1 line
2.9.0
Re: [PATCH v4 07/12] run-command: add clean_on_exit_handler
Quoted 10 lines · On Wed, Aug 03, 2016 at 06:42:20PM +0200, [email protected] wrote:
From: Lars Schneider <[email protected]>
Some commands might need to perform cleanup tasks on exit. Let's give them an interface for doing this.
Please note, that the cleanup callback is not executed if Git dies of a signal. The reason is that only "async-signal-safe" functions would be allowed to be call in that case. Since we cannot control what functions the callback will use, we will not support the case. See
507d7804for more details.
I'm not clear on why we want this cleanup filter. It looks like you use it in the final patch to send an explicit shutdown to any filters we start. But I see two issues with that:
1. This shutdown may come at any time, and you have no idea what state the protocol conversation with the filter is in. You could be in the middle of sending another pkt-line, or in a sequence of non-command pkt-lines where "shutdown" is not recognized.
2. If your protocol does bad things when it is cut off in the middle without an explicit shutdown, then it's a bad protocol. As you note, this patch doesn't cover signal death, nor could it ever cover something like "kill -9", or a bug which prevented git from saying "shutdown".
You're much better off to design the protocol so that a premature EOF is detected as an error. For example, if we're feeding file data to the filter, and we're worried it might be writing it to a data store (like LFS), we would not want it to see EOF and say "well, I guess I got all the data; time to store this!". Instead, it should know how many bytes are coming, or should have some kind of framing so that the sender says "and now you have seen all the bytes" (like a pkt-line flush).
AFAIK, your protocol _does_ do those things sensibly, so this explicit shutdown isn't really accomplishing anything.
-Peff
Re: [PATCH v4 07/12] run-command: add clean_on_exit_handler
Quoted 16 lines · Jeff King
Quoted 10 lines · On Wed, Aug 03, 2016 at 06:42:20PM +0200, [email protected] wrote:
From: Lars Schneider <[email protected]>
Some commands might need to perform cleanup tasks on exit. Let's give them an interface for doing this.
Please note, that the cleanup callback is not executed if Git dies of a signal. The reason is that only "async-signal-safe" functions would be allowed to be call in that case. Since we cannot control what functions the callback will use, we will not support the case. See
507d7804for more details.
Quoted 8 lines
I'm not clear on why we want this cleanup filter. It looks like you use it in the final patch to send an explicit shutdown to any filters we start. But I see two issues with that:
1. This shutdown may come at any time, and you have no idea what state the protocol conversation with the filter is in. You could be in the middle of sending another pkt-line, or in a sequence of non-command pkt-lines where "shutdown" is not recognized.
Maybe I am missing something, but I don't think that can happen because the cleanup callback is *only* executed if Git exits normally without error. In that case we would be in a sane protocol state, no?
Quoted 15 lines
2. If your protocol does bad things when it is cut off in the middle without an explicit shutdown, then it's a bad protocol. As you note, this patch doesn't cover signal death, nor could it ever cover something like "kill -9", or a bug which prevented git from saying "shutdown".
You're much better off to design the protocol so that a premature EOF is detected as an error. For example, if we're feeding file data to the filter, and we're worried it might be writing it to a data store (like LFS), we would not want it to see EOF and say "well, I guess I got all the data; time to store this!". Instead, it should know how many bytes are coming, or should have some kind of framing so that the sender says "and now you have seen all the bytes" (like a pkt-line flush).
Quoted 2 lines
AFAIK, your protocol _does_ do those things sensibly, so this explicit shutdown isn't really accomplishing anything.
Thanks. The shutdown command is not intended to be a mechanism to tell the filter that everything went well. At this point - as you mentioned - the filter already received all data in the right way. The shutdown command is intended to give the filter some time to perform some post processing before Git returns.
See here for some brainstorming how this feature could be useful in filters similar to Git LFS:
https://github.com/github/git-lfs/issues/1401#issuecomment-236133991
- Lars
Re: [PATCH v4 07/12] run-command: add clean_on_exit_handler
Quoted 4 lines · Lars Schneider
Quoted 4 lines
I'm not clear on why we want this cleanup filter. It looks like you use it in the final patch to send an explicit shutdown to any filters we start. But I see two issues with that:
Quoted 8 lines
Quoted 4 lines
1. This shutdown may come at any time, and you have no idea what state the protocol conversation with the filter is in. You could be in the middle of sending another pkt-line, or in a sequence of non-command pkt-lines where "shutdown" is not recognized.
Maybe I am missing something, but I don't think that can happen because the cleanup callback is *only* executed if Git exits normally without error. In that case we would be in a sane protocol state, no?
OK, then maybe I am doubly missing the point. I thought this cleanup was here to hit the case where we call die() and git exits unexpectedly.
If you only want to cover the "we are done, no errors, goodbye" case, then why don't you just write shutdown when we're done?
I realize you may have multiple filters, but I don't think it should be run-command's job to iterate over them. You are presumably keeping a list of active filters, and should have a function to iterate over that.
Or better yet, do not require a shutdown at all. The filter sees EOF and knows there is nothing more to do. If we are in the middle of an operation, then it knows git died. If not, then presumably git had nothing else to say (and really, it is not the filter's business if git saw an error or not).
Though...
Quoted 9 lines
Thanks. The shutdown command is not intended to be a mechanism to tell the filter that everything went well. At this point - as you mentioned - the filter already received all data in the right way. The shutdown command is intended to give the filter some time to perform some post processing before Git returns.
See here for some brainstorming how this feature could be useful in filters similar to Git LFS:
https://github.com/github/git-lfs/issues/1401#issuecomment-236133991
OK, so it is not really "tell the filter to shutdown" but "I am done with you, filter, but I will wait for you to tell me you are all done, so that I can tell the user".
I'm not sure if calling that "shutdown" makes sense, though. It's almost more of a checkpoint (and I wonder if git would ever want to "checkpoint" without hanging up the connection).
-Peff
Re: [PATCH v4 07/12] run-command: add clean_on_exit_handler
Quoted 18 lines · Jeff King
Quoted 4 lines · Lars Schneider
Quoted 4 lines
I'm not clear on why we want this cleanup filter. It looks like you use it in the final patch to send an explicit shutdown to any filters we start. But I see two issues with that:
Quoted 8 lines
Quoted 4 lines
1. This shutdown may come at any time, and you have no idea what state the protocol conversation with the filter is in. You could be in the middle of sending another pkt-line, or in a sequence of non-command pkt-lines where "shutdown" is not recognized.
Maybe I am missing something, but I don't think that can happen because the cleanup callback is *only* executed if Git exits normally without error. In that case we would be in a sane protocol state, no?
Quoted 5 lines
OK, then maybe I am doubly missing the point. I thought this cleanup was here to hit the case where we call
die()and git exits unexpectedly.If you only want to cover the "we are done, no errors, goodbye" case, then why don't you just write shutdown when we're done?
I think I tried that at some point but the filter code is called from multiple places and therefore I looked into atexit() (via run-command) and it seemed easier. Do you have a place in mind where you would call the shutdown after all blobs are processed explicitly?
Quoted 3 lines
I realize you may have multiple filters, but I don't think it should be run-command's job to iterate over them. You are presumably keeping a list of active filters, and should have a function to iterate over that.
Yes, that would be easy.
Quoted 5 lines
Or better yet, do not require a shutdown at all. The filter sees EOF and knows there is nothing more to do. If we are in the middle of an operation, then it knows git died. If not, then presumably git had nothing else to say (and really, it is not the filter's business if git saw an error or not).
EOF? The filter is supposed to process multiple files. How would one EOF indicate that we are done?
Quoted 8 lines
Though...
Quoted 6 lines
Thanks. The shutdown command is not intended to be a mechanism to tell the filter that everything went well. At this point - as you mentioned - the filter already received all data in the right way. The shutdown command is intended to give the filter some time to perform some post processing before Git returns.
Quoted 7 lines
Quoted 3 lines
See here for some brainstorming how this feature could be useful in filters similar to Git LFS:
https://github.com/github/git-lfs/issues/1401#issuecomment-236133991OK, so it is not really "tell the filter to shutdown" but "I am done with you, filter, but I will wait for you to tell me you are all done, so that I can tell the user".
Correct!
Quoted 3 lines
I'm not sure if calling that "shutdown" makes sense, though. It's almost more of a checkpoint (and I wonder if git would ever want to "checkpoint" without hanging up the connection).
OK, I agree that the naming might not be ideal. But "checkpoint" does not convey that it is only executed once after all blobs are filtered?!
I understand that Git might not want to wait for the filter...
- Lars
Re: [PATCH v4 07/12] run-command: add clean_on_exit_handler
Quoted 8 lines · Lars Schneider
Quoted 5 lines
Or better yet, do not require a shutdown at all. The filter sees EOF and knows there is nothing more to do. If we are in the middle of an operation, then it knows git died. If not, then presumably git had nothing else to say (and really, it is not the filter's business if git saw an error or not).
EOF? The filter is supposed to process multiple files. How would one EOF indicate that we are done?
I think we may be talking about two different EOFs.
Git sends a file in pkt-line format, and the flush marks EOF for that file. But the filter keeps running, waiting for more input. This can happen multiple times.
Eventually git calls close() on the descriptor, and the filter sees the "real" EOF (i.e., read() returns 0). That is the signal that git is done.
Quoted 6 lines
Quoted 3 lines
I'm not sure if calling that "shutdown" makes sense, though. It's almost more of a checkpoint (and I wonder if git would ever want to "checkpoint" without hanging up the connection).
OK, I agree that the naming might not be ideal. But "checkpoint" does not convey that it is only executed once after all blobs are filtered?!
Does the filter need to care? It's told to do any deferred work, and to report back when it's done. The fact that git is calling it before it decides to exit is not the filter's business (and you can imagine for something like fast-import, it might want to feed files to something like LFS, too; it already checkpoints occasionally to avoid lost work, and would presumably want to ask LFS to checkpoint, too).
Quoted 1 line
I understand that Git might not want to wait for the filter...
If git _doesn't_ want to wait for the filter, I don't think you need a checkpoint at all. The filter just does its deferred work when it sees git hang up the connection (i.e., the "real" EOF from above).
-Peff
Re: [PATCH v4 07/12] run-command: add clean_on_exit_handler
Quoted 11 lines · Jeff King
Quoted 6 lines · Lars Schneider
Quoted 5 lines
Or better yet, do not require a shutdown at all. The filter sees EOF and knows there is nothing more to do. If we are in the middle of an operation, then it knows git died. If not, then presumably git had nothing else to say (and really, it is not the filter's business if git saw an error or not).
Quoted 8 lines
Quoted 2 lines
EOF? The filter is supposed to process multiple files. How would one EOF indicate that we are done?
I think we may be talking about two different EOFs.
Git sends a file in pkt-line format, and the flush marks EOF for that file. But the filter keeps running, waiting for more input. This can happen multiple times.
Correct.
Quoted 3 lines
Eventually git calls
close()on the descriptor, and the filter sees the "real" EOF (i.e.,read()returns 0). That is the signal that git is done.
Right.
Quoted 15 lines
Quoted 7 lines
Quoted 4 lines
I'm not sure if calling that "shutdown" makes sense, though. It's almost more of a checkpoint (and I wonder if git would ever want to "checkpoint" without hanging up the connection).
OK, I agree that the naming might not be ideal. But "checkpoint" does not convey that it is only executed once after all blobs are filtered?!
Does the filter need to care? It's told to do any deferred work, and to report back when it's done. The fact that git is calling it before it decides to exit is not the filter's business (and you can imagine for something like fast-import, it might want to feed files to something like LFS, too; it already checkpoints occasionally to avoid lost work, and would presumably want to ask LFS to checkpoint, too).
Quoted 4 lines
Quoted 1 line
I understand that Git might not want to wait for the filter...
If git _doesn't_ want to wait for the filter, I don't think you need a checkpoint at all.
True. However, I wonder if it could be useful if the filter is allowed to do some finishing work *before* Git returns to the user.
Quoted 2 lines
The filter just does its deferred work when it sees git hang up the connection (i.e., the "real" EOF from above).
Yeah it could do that. But then the filter cannot do things like modifying the index after the fact... however, that might be considered nasty by the Git community anyways... I am thinking about dropping this patch in the next roll as it is not strictly necessary for my current use case.
Thanks,
Lars
Re: [PATCH v4 07/12] run-command: add clean_on_exit_handler
On 2016-08-05 15.08, Lars Schneider wrote:
[]
Quoted 5 lines
Yeah it could do that. But then the filter cannot do things like modifying the index after the fact... however, that might be considered nasty by the Git community anyways... I am thinking about dropping this patch in the next roll as it is not strictly necessary for my current use case.
(Thanks Peff for helping me out with the EOF explanation)
I would say that a filter is a filter, and should do nothing else than filtering one file,
(or a stream).
When you want to modify the index, a hook may be your friend.
Re: [PATCH v4 07/12] run-command: add clean_on_exit_handler
Quoted 11 lines · Torsten Bögershausen
On 2016-08-05 15.08, Lars Schneider wrote:
[]
Quoted 5 lines
Yeah it could do that. But then the filter cannot do things like modifying the index after the fact... however, that might be considered nasty by the Git community anyways... I am thinking about dropping this patch in the next roll as it is not strictly necessary for my current use case.
Quoted 6 lines
(Thanks Peff for helping me out with the EOF explanation)
I would say that a filter is a filter, and should do nothing else than filtering one file,
(or a stream).
When you want to modify the index, a hook may be your friend.
Agreed. I will remove that feature.
Thanks,
Lars
PATCH v4 8/12 convert: quote filter names in error messages
In reply to [email protected]From: Lars Schneider <[email protected]>
Git filter driver commands with spaces (e.g. filter.sh foo) are hard to read in error messages. Quote them to improve the readability.
--- convert.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
convert.c +6 -6
@@ -397,7 +397,7 @@ static int filter_buffer_or_fd(int in, int out, void *data) child_process.out = out;if (start_command (&child_process)) -return error ("cannot fork to run external filter %s" , params->cmd); +return error ("cannot fork to run external filter '%s'" , params->cmd);sigchain_push (SIGPIPE ,SIG_IGN );
@@ -415,13 +415,13 @@ static int filter_buffer_or_fd(int in, int out, void *data)if (close (child_process.in)) write_err =1 ;if (write_err) -error ("cannot feed the input to external filter %s" , params->cmd); +error ("cannot feed the input to external filter '%s'" , params->cmd);sigchain_pop (SIGPIPE ); status =finish_command (&child_process);if (status) -error ("external filter %s failed %d" , params->cmd, status); +error ("external filter '%s' failed %d" , params->cmd, status);strbuf_release (&cmd);return (write_err || status);
@@ -462,15 +462,15 @@ static int apply_filter(const char *path, const char *src, size_t len, int fd,return 0 ;/* error was already reported */ if (strbuf_read (&nbuf, async.out, len) <0 ) { -error ("read from external filter %s failed" , cmd); +error ("read from external filter '%s' failed" , cmd); ret =0 ; }if (close (async.out)) { -error ("read from external filter %s failed" , cmd); +error ("read from external filter '%s' failed" , cmd); ret =0 ; }if (finish_async (&async)) { -error ("external filter %s failed" , cmd); +error ("external filter '%s' failed" , cmd); ret =0 ; }
Signature · 1 line
2.9.0
PATCH v4 9/12 convert: modernize tests
In reply to [email protected]From: Lars Schneider <[email protected]>
Use test_config to set the config, check that files are empty with test_must_be_empty, compare files with test_cmp, and remove spaces after ">" and "<".
--- t/t0021-conversion.sh | 62 +++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 31 deletions(-)
t/t0021-conversion.sh +31 -31
@@ -13,8 +13,8 @@ EOF chmod +x rot13.sh test_expect_success setup ' - git config filter.rot13.smudge ./rot13.sh && - git config filter.rot13.clean ./rot13.sh && + test_config filter.rot13.smudge ./rot13.sh && + test_config filter.rot13.clean ./rot13.sh && { echo "*.t filter=rot13"
@@ -38,8 +38,8 @@ script='s/^\$Id: \([0-9a-f]*\) \$/\1/p' test_expect_success check ' - cmp test.o test && - cmp test.o test.t && + test_cmp test.o test && + test_cmp test.o test.t && # ident should be stripped in the repository git diff --raw --exit-code :test :test.i &&
@@ -47,10 +47,10 @@ test_expect_success check ' embedded=$(sed -ne "$script" test.i) && test "z$id" = "z$embedded" && - git cat-file blob :test.t > test.r && + git cat-file blob :test.t >test.r && - ./rot13.sh < test.o > test.t && - cmp test.r test.t + ./rot13.sh <test.o >test.t && + test_cmp test.r test.t ' # If an expanded ident ever gets into the repository, we want to make sure that
@@ -130,7 +130,7 @@ test_expect_success 'filter shell-escaped filenames' ' # delete the files and check them out again, using a smudge filter # that will count the args and echo the command-line back to us - git config filter.argc.smudge "sh ./argc.sh %f" && + test_config filter.argc.smudge "sh ./argc.sh %f" && rm "$normal" "$special" && git checkout -- "$normal" "$special" &&
@@ -141,7 +141,7 @@ test_expect_success 'filter shell-escaped filenames' ' test_cmp expect "$special" && # do the same thing, but with more args in the filter expression - git config filter.argc.smudge "sh ./argc.sh %f --my-extra-arg" && + test_config filter.argc.smudge "sh ./argc.sh %f --my-extra-arg" && rm "$normal" "$special" && git checkout -- "$normal" "$special" &&
@@ -154,9 +154,9 @@ test_expect_success 'filter shell-escaped filenames' ' ' test_expect_success 'required filter should filter data' ' - git config filter.required.smudge ./rot13.sh && - git config filter.required.clean ./rot13.sh && - git config filter.required.required true && + test_config filter.required.smudge ./rot13.sh && + test_config filter.required.clean ./rot13.sh && + test_config filter.required.required true && echo "*.r filter=required" >.gitattributes &&
@@ -165,17 +165,17 @@ test_expect_success 'required filter should filter data' ' rm -f test.r && git checkout -- test.r && - cmp test.o test.r && + test_cmp test.o test.r && ./rot13.sh <test.o >expected && git cat-file blob :test.r >actual && - cmp expected actual + test_cmp expected actual ' test_expect_success 'required filter smudge failure' ' - git config filter.failsmudge.smudge false && - git config filter.failsmudge.clean cat && - git config filter.failsmudge.required true && + test_config filter.failsmudge.smudge false && + test_config filter.failsmudge.clean cat && + test_config filter.failsmudge.required true && echo "*.fs filter=failsmudge" >.gitattributes &&
@@ -186,9 +186,9 @@ test_expect_success 'required filter smudge failure' ' ' test_expect_success 'required filter clean failure' ' - git config filter.failclean.smudge cat && - git config filter.failclean.clean false && - git config filter.failclean.required true && + test_config filter.failclean.smudge cat && + test_config filter.failclean.clean false && + test_config filter.failclean.required true && echo "*.fc filter=failclean" >.gitattributes &&
@@ -197,8 +197,8 @@ test_expect_success 'required filter clean failure' ' ' test_expect_success 'filtering large input to small output should use little memory' ' - git config filter.devnull.clean "cat >/dev/null" && - git config filter.devnull.required true && + test_config filter.devnull.clean "cat >/dev/null" && + test_config filter.devnull.required true && for i in $(test_seq 1 30); do printf "%1048576d" 1; done >30MB && echo "30MB filter=devnull" >.gitattributes && GIT_MMAP_LIMIT=1m GIT_ALLOC_LIMIT=1m git add 30MB
@@ -207,7 +207,7 @@ test_expect_success 'filtering large input to small output should use little mem test_expect_success 'filter that does not read is fine' ' test-genrandom foo $((128 * 1024 + 1)) >big && echo "big filter=epipe" >.gitattributes && - git config filter.epipe.clean "echo xyzzy" && + test_config filter.epipe.clean "echo xyzzy" && git add big && git cat-file blob :big >actual && echo xyzzy >expect &&
@@ -215,20 +215,20 @@ test_expect_success 'filter that does not read is fine' ' ' test_expect_success EXPENSIVE 'filter large file' ' - git config filter.largefile.smudge cat && - git config filter.largefile.clean cat && + test_config filter.largefile.smudge cat && + test_config filter.largefile.clean cat && for i in $(test_seq 1 2048); do printf "%1048576d" 1; done >2GB && echo "2GB filter=largefile" >.gitattributes && git add 2GB 2>err && - ! test -s err && + test_must_be_empty err && rm -f 2GB && git checkout -- 2GB 2>err && - ! test -s err + test_must_be_empty err ' test_expect_success "filter: clean empty file" ' - git config filter.in-repo-header.clean "echo cleaned && cat" && - git config filter.in-repo-header.smudge "sed 1d" && + test_config filter.in-repo-header.clean "echo cleaned && cat" && + test_config filter.in-repo-header.smudge "sed 1d" && echo "empty-in-worktree filter=in-repo-header" >>.gitattributes && >empty-in-worktree &&
@@ -240,8 +240,8 @@ test_expect_success "filter: clean empty file" ' ' test_expect_success "filter: smudge empty file" ' - git config filter.empty-in-repo.clean "cat >/dev/null" && - git config filter.empty-in-repo.smudge "echo smudged && cat" && + test_config filter.empty-in-repo.clean "cat >/dev/null" && + test_config filter.empty-in-repo.smudge "echo smudged && cat" && echo "empty-in-repo filter=empty-in-repo" >>.gitattributes && echo dead data walking >empty-in-repo &&
Signature · 1 line
2.9.0
PATCH v4 10/12 convert: generate large test files only once
In reply to [email protected]From: Lars Schneider <[email protected]>
Generate more interesting large test files with pseudo random characters in between and reuse these test files in multiple tests. Run tests formerly marked as EXPENSIVE every time but with a smaller data set.
--- t/t0021-conversion.sh | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-)
t/t0021-conversion.sh +38 -10
@@ -4,6 +4,15 @@ test_description='blob conversion via gitattributes' . ./test-lib.sh +if test_have_prereq EXPENSIVE +then + T0021_LARGE_FILE_SIZE=2048 + T0021_LARGISH_FILE_SIZE=100 +else + T0021_LARGE_FILE_SIZE=30 + T0021_LARGISH_FILE_SIZE=2 +fi + cat <<EOF >rot13.sh #!$SHELL_PATH tr \
@@ -31,7 +40,26 @@ test_expect_success setup ' cat test >test.i && git add test test.t test.i && rm -f test test.t test.i && - git checkout -- test test.t test.i + git checkout -- test test.t test.i && + + mkdir generated-test-data && + for i in $(test_seq 1 $T0021_LARGE_FILE_SIZE) + do + RANDOM_STRING="$(test-genrandom end $i | tr -dc "A-Za-z0-9" )" + ROT_RANDOM_STRING="$(echo $RANDOM_STRING | ./rot13.sh )" + # Generate 1MB of empty data and 100 bytes of random characters + # printf "$(test-genrandom start $i)" + printf "%1048576d" 1 >>generated-test-data/large.file && + printf "$RANDOM_STRING" >>generated-test-data/large.file && + printf "%1048576d" 1 >>generated-test-data/large.file.rot13 && + printf "$ROT_RANDOM_STRING" >>generated-test-data/large.file.rot13 && + + if test $i = $T0021_LARGISH_FILE_SIZE + then + cat generated-test-data/large.file >generated-test-data/largish.file && + cat generated-test-data/large.file.rot13 >generated-test-data/largish.file.rot13 + fi + done ' script='s/^\$Id: \([0-9a-f]*\) \$/\1/p'
@@ -199,9 +227,9 @@ test_expect_success 'required filter clean failure' ' test_expect_success 'filtering large input to small output should use little memory' ' test_config filter.devnull.clean "cat >/dev/null" && test_config filter.devnull.required true && - for i in $(test_seq 1 30); do printf "%1048576d" 1; done >30MB && - echo "30MB filter=devnull" >.gitattributes && - GIT_MMAP_LIMIT=1m GIT_ALLOC_LIMIT=1m git add 30MB + cp generated-test-data/large.file large.file && + echo "large.file filter=devnull" >.gitattributes && + GIT_MMAP_LIMIT=1m GIT_ALLOC_LIMIT=1m git add large.file ' test_expect_success 'filter that does not read is fine' '
@@ -214,15 +242,15 @@ test_expect_success 'filter that does not read is fine' ' test_cmp expect actual ' -test_expect_success EXPENSIVE 'filter large file' ' +test_expect_success 'filter large file' ' test_config filter.largefile.smudge cat && test_config filter.largefile.clean cat && - for i in $(test_seq 1 2048); do printf "%1048576d" 1; done >2GB && - echo "2GB filter=largefile" >.gitattributes && - git add 2GB 2>err && + echo "large.file filter=largefile" >.gitattributes && + cp generated-test-data/large.file large.file && + git add large.file 2>err && test_must_be_empty err && - rm -f 2GB && - git checkout -- 2GB 2>err && + rm -f large.file && + git checkout -- large.file 2>err && test_must_be_empty err '
Signature · 1 line
2.9.0
PATCH v4 11/12 convert: add filter.<driver>.process option
In reply to [email protected]From: Lars Schneider <[email protected]>
Git's clean/smudge mechanism invokes an external filter process for every single blob that is affected by a filter. If Git filters a lot of blobs then the startup time of the external filter processes can become a significant part of the overall Git execution time.
In a preliminary performance test this developer used a clean/smudge filter written in golang to filter 12,000 files. This process took 364s with the existing filter mechanism and 5s with the new mechanism. See details here:
https://github.com/github/git-lfs/pull/1382
This patch adds the filter.<driver>.process string option which, if used, keeps the external filter process running and processes all blobs with the packet format (pkt-line) based protocol over standard input and standard output described below.
Git starts the filter when it encounters the first file that needs to be cleaned or smudged. After the filter started Git expects a welcome message, protocol version number, and filter capabilities separated by spaces:
------------------------
packet: git< git-filter-protocol\n packet: git< version=2\n packet: git< capabilities=clean smudge\n
------------------------
Supported filter capabilities are "clean" and "smudge".
Afterwards Git sends a command (based on the supported capabilities), the pathname of a file relative to the repository root, the content split in zero or more pkt-line packets, and a flush packet at the end:
------------------------
packet: git> command=smudge\n packet: git> pathname=path/testfile.dat\n packet: git> CONTENT packet: git> 0000
------------------------
The filter is expected to respond with the result content in zero or more pkt-line packets and a flush packet at the end. Finally, a "result=success" packet is expected if everything went well.
------------------------
packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< result=success\n
------------------------
If the result content is empty then the filter is expected to respond only with a flush packet and a "result=success" packet.
------------------------
packet: git< 0000 packet: git< result=success\n
------------------------
In case the filter cannot or does not want to process the content, it is expected to respond with a flush packet and a "result=reject" packet. Depending on the filter.<driver>.required flag Git will interpret that as error but it will not stop or restart the filter process.
------------------------
packet: git< 0000 packet: git< result=reject\n
------------------------
If the filter experiences an error during processing, then it can either die or send a flush packet and a "result=error" packet. If Git receives such an error then it will stop and restart the filter with the next file that needs to be processed.
------------------------
packet: git< HALF_WRITTEN_ERRONEOUS_CONTENT packet: git< 0000 packet: git< result=error\n
------------------------
After the filter has processed a blob it is expected to wait for the next command. When the Git process terminates, it will send a kill signal to the filter in that stage.
If a filter.<driver>.clean or filter.<driver>.smudge command is configured then these commands always take precedence over a configured filter.<driver>.process command.
--- Documentation/gitattributes.txt | 98 +++++++++++- convert.c | 277 ++++++++++++++++++++++++++++++---- t/t0021-conversion.sh | 322 ++++++++++++++++++++++++++++++++++++++++ t/t0021/rot13-filter.pl | 148 ++++++++++++++++++ 4 files changed, 815 insertions(+), 30 deletions(-) create mode 100755 t/t0021/rot13-filter.pl
Documentation/gitattributes.txt +97 -1
@@ -300,7 +300,13 @@ checkout, when the `smudge` command is specified, the command is fed the blob object from its standard input, and its standard output is used to update the worktree file. Similarly, the `clean` command is used to convert the contents of worktree file -upon checkin. +upon checkin. By default these commands process only a single +blob and terminate. If a long running `process` filter is used +in place of `clean` and/or `smudge` filters, then Git can process +all blobs with a single filter command invocation for the entire +life of a single Git command, for example `git add --all`. See +section below for the description of the protocol used to +communicate with a `process` filter. One use of the content filtering is to massage the content into a shape that is more convenient for the platform, filesystem, and the user to use.
@@ -375,6 +381,96 @@ substitution. For example: ------------------------ +Long Running Filter Process +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +If the filter command (a string value) is defined via +`filter.<driver>.process` then Git can process all blobs with a +single filter invocation for the entire life of a single Git +command. This is achieved by using the following packet format +(pkt-line, see technical/protocol-common.txt) based protocol over +standard input and standard output. + +Git starts the filter when it encounters the first file +that needs to be cleaned or smudged. After the filter started +Git expects a welcome message, protocol version number, and +filter capabilities separated by spaces: +------------------------ +packet: git< git-filter-protocol\n +packet: git< version=2\n +packet: git< capabilities=clean smudge\n +------------------------ +Supported filter capabilities are "clean" and "smudge". + +Afterwards Git sends a command (based on the supported +capabilities), the pathname of a file relative to the +repository root, the content split in zero or more pkt-line +packets, and a flush packet at the end: +------------------------ +packet: git> command=smudge\n +packet: git> pathname=path/testfile.dat\n +packet: git> CONTENT +packet: git> 0000 +------------------------ + +The filter is expected to respond with the result content in zero +or more pkt-line packets and a flush packet at the end. Finally, a +"result=success" packet is expected if everything went well. +------------------------ +packet: git< SMUDGED_CONTENT +packet: git< 0000 +packet: git< result=success\n +------------------------ + +If the result content is empty then the filter is expected to respond +only with a flush packet and a "result=success" packet. +------------------------ +packet: git< 0000 +packet: git< result=success\n +------------------------ + +In case the filter cannot or does not want to process the content, +it is expected to respond with a flush packet and a "result=reject" +packet. Depending on the `filter.<driver>.required` flag Git will +interpret that as error but it will not stop or restart the filter +process. +------------------------ +packet: git< 0000 +packet: git< result=reject\n +------------------------ + +If the filter experiences an error during processing, then it can +either die or send a flush packet and a "result=error" packet. If +Git receives such an error then it will stop and restart the filter +with the next file that needs to be processed. +------------------------ +packet: git< HALF_WRITTEN_ERRONEOUS_CONTENT +packet: git< 0000 +packet: git< result=error\n +------------------------ + +After the filter has processed a blob it is expected to wait for +the next command. When the Git process terminates, it will send +a kill signal to the filter in that stage. + +A long running filter demo implementation can be found in +`t/t0021/rot13-filter.pl` located in the Git core repository. +If you develop your own long running filter process then the +`GIT_TRACE_PACKET` environment variables can be very helpful +for debugging (see linkgit:git[1]). + +If a `filter.<driver>.clean` or `filter.<driver>.smudge` command +is configured then these commands always take precedence over +a configured `filter.<driver>.process` command. + +Please note that you cannot use an existing `filter.<driver>.clean` +or `filter.<driver>.smudge` command with `filter.<driver>.process` +because the former two use a different inter process communication +protocol than the latter one. As soon as Git would detect a file +that needs to be processed by such an invalid "process" filter, +it would wait for a proper protocol handshake and appear "hanging". + + Interaction between checkin/checkout attributes ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
convert.c +248 -29
366 lines not on this page · raw message
t/t0021-conversion.sh +322 -0
332 lines not on this page · raw message
t/t0021/rot13-filter.pl new file +148 -0
148 lines not on this page · raw message
Signature · 1 line
2.9.0
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
Quoted 3 lines · [email protected] writes:
packet: git< git-filter-protocol\n packet: git< version=2\n packet: git< capabilities=clean smudge\n
During the discussion on the future of pack-protocol, it was pointed out that having to shove all capabilities on a single line/packet was one of the things we would want to fix in the current protocol when we revamp to v2. As this exhange between the convert machinery and an external process is a brand new one, I do not think you want to mimic the limitation in the current pack protocol like this; the limitation mostly came from the constraint that we cannot break existing pack protocol clients and servers before we extended the protocol to add capabilities.
You may not foresee that the caps won't grow very long beyond clean/smudge right now, just like we did not foresee that we would wish to be able to convey a lot longer capability values to the other side when we added the capability exchange to the pack protocol, so "but but but we will never have that many" is not a good counter-argument.
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
Quoted 19 lines · Junio C Hamano
Quoted 3 lines · [email protected] writes:
packet: git< git-filter-protocol\n packet: git< version=2\n packet: git< capabilities=clean smudge\nDuring the discussion on the future of pack-protocol, it was pointed out that having to shove all capabilities on a single line/packet was one of the things we would want to fix in the current protocol when we revamp to v2. As this exhange between the convert machinery and an external process is a brand new one, I do not think you want to mimic the limitation in the current pack protocol like this; the limitation mostly came from the constraint that we cannot break existing pack protocol clients and servers before we extended the protocol to add capabilities.
Quoted 6 lines
You may not foresee that the caps won't grow very long beyond clean/smudge right now, just like we did not foresee that we would wish to be able to convey a lot longer capability values to the other side when we added the capability exchange to the pack protocol, so "but but but we will never have that many" is not a good counter-argument.
OK. Is this the v2 discussion you are referring to?
http://public-inbox.org/git/1461972887-22100-1-git-send-email-sbeller%40google.com/
What format do you suggest?
packet: git< git-filter-protocol\n packet: git< version=2\n packet: git< capability=clean\n packet: git< capability=smudge\n packet: git< 0000
or
packet: git< git-filter-protocol\n packet: git< version=2\n packet: git< capability\n packet: git< clean\n packet: git< smudge\n packet: git< 0000
or ... ?
I would prefer the first one, I think.
- Lars
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
Quoted 20 lines · Lars Schneider
OK. Is this the v2 discussion you are referring to?
http://public-inbox.org/git/1461972887-22100-1-git-send-email-sbeller%40google.com/What format do you suggest?
packet: git< git-filter-protocol\n packet: git< version=2\n packet: git< capability=clean\n packet: git< capability=smudge\n packet: git< 0000or
packet: git< git-filter-protocol\n packet: git< version=2\n packet: git< capability\n packet: git< clean\n packet: git< smudge\n packet: git< 0000
Quoted 3 lines
or ... ?
I would prefer the first one, I think.
How about:
version=2
clean=true
smudge=true
0000
? Then we do not have to care about multiple "capability" keys (so something naively parsing this could just store them in a string list, for example).
You could also make "clean" a synonym for "clean=true" or something, and have:
version=2
clean
smudge
0000
but it's probably better to have the protocol err on the side of verbose-but-unambiguous. It's not like people are typing this routinely.
-Peff
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
Quoted 31 lines · Jeff King
Quoted 20 lines · Lars Schneider
OK. Is this the v2 discussion you are referring to?
http://public-inbox.org/git/1461972887-22100-1-git-send-email-sbeller%40google.com/What format do you suggest?
packet: git< git-filter-protocol\n packet: git< version=2\n packet: git< capability=clean\n packet: git< capability=smudge\n packet: git< 0000or
packet: git< git-filter-protocol\n packet: git< version=2\n packet: git< capability\n packet: git< clean\n packet: git< smudge\n packet: git< 0000Quoted 3 lines
or ... ?
I would prefer the first one, I think.
How about:
Quoted 8 lines
version=2
clean=true
smudge=true
0000? Then we do not have to care about multiple "capability" keys (so something naively parsing this could just store them in a string list, for example).
Alright. I will go with this solution.
Thanks,
Lars
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
In reply to [email protected]convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 482
Quoted 4 lines · [email protected] writes:
convert.c +4
-0+#define FILTER_CAPABILITIES_CLEAN (1u<<0) +#define FILTER_CAPABILITIES_SMUDGE (1u<<1) +#define FILTER_SUPPORTS_CLEAN (type) ((type) & FILTER_CAPABILITIES_CLEAN) +#define FILTER_SUPPORTS_SMUDGE (type) ((type) & FILTER_CAPABILITIES_SMUDGE)
I would expect a lot shorter names as these are file-local; CAP_CLEAN and CAP_SMUDGE, perhaps, _WITHOUT_ "supports BLAH" macros?
if (FILTER_SUPPORTS_CLEAN(type))
is not all that more readable than
if (CAP_CLEAN & type)
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 492
Quoted 9 lines
convert.c +9
-0+struct cmd2process { +struct hashmap_entry ent;/* must be the first member! */ +int supported_capabilities; +const char *cmd; +struct child_process process; +}; + +static int cmd_process_map_initialized =0 ; +static struct hashmap cmd_process_map;
Don't initialize statics to 0 or NULL.
Quoted 14 lines
convert.c +14
-0+static int cmd2process_cmp (const struct cmd2process *e1, +const struct cmd2process *e2, +const void *unused) +{ +return strcmp (e1->cmd, e2->cmd); +} + +static struct cmd2process *find_multi_file_filter_entry (struct hashmap *hashmap,const char *cmd) +{ +struct cmd2process key; +hashmap_entry_init (&key,strhash (cmd)); + key.cmd = cmd; +return hashmap_get (hashmap, &key,NULL ); +}
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 517
Quoted 10 lines
convert.c +10
-0+ +static void kill_multi_file_filter (struct hashmap *hashmap,struct cmd2process *entry) +{ +if (!entry) +return ; +sigchain_push (SIGPIPE ,SIG_IGN ); +close (entry->process.in); +close (entry->process.out); +sigchain_pop (SIGPIPE ); +finish_command (&entry->process);
I wonder if we want to diagnose failures from close(), which is a lot more interesting than usual because these are connected to pipes.
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 629
Quoted 8 lines
convert.c +8
-0+static int apply_multi_file_filter (const char *path,const char *src,size_t len, +int fd,struct strbuf *dst,const char *cmd, +const int wanted_capability) +{ +int ret =1 ; + ... +if (!(wanted_capability & entry->supported_capabilities)) +return 1 ;// it is OK if the wanted capability is not supported
No // comment please.
Quoted 6 lines
convert.c +6
-0+ filter_result =packet_read_line (process->out,NULL ); + ret = !strcmp (filter_result,"result=success" ); + +done: +if (ret) { +strbuf_swap (dst, &nbuf);
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 682
Quoted 10 lines
convert.c +10
-0+ }else { +if (!filter_result ||strcmp (filter_result,"result=reject" )) { +// Something went wrong with the protocol filter. Force shutdown! +error ("external filter '%s' failed" , cmd); +kill_multi_file_filter (&cmd_process_map, entry); + } + } +strbuf_release (&nbuf); +return ret; +}
I think this was already pointed out in the previous review by Peff, but a variable "ret" that says "0 is bad" somehow makes it hard to follow the code. Perhaps rename it to "int error", flip the meaning, and if the caller wants this function to return non-zero on success flip the polarity in the return statement itself, i.e. "return !errors", may make it easier to follow?
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
Quoted 13 lines · Junio C Hamano
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 482
Quoted 4 lines · [email protected] writes:
convert.c +4
-0+#define FILTER_CAPABILITIES_CLEAN (1u<<0) +#define FILTER_CAPABILITIES_SMUDGE (1u<<1) +#define FILTER_SUPPORTS_CLEAN (type) ((type) & FILTER_CAPABILITIES_CLEAN) +#define FILTER_SUPPORTS_SMUDGE (type) ((type) & FILTER_CAPABILITIES_SMUDGE)I would expect a lot shorter names as these are file-local;
CAP_CLEANandCAP_SMUDGE, perhaps, _WITHOUT_ "supports BLAH" macros?
Quoted 5 lines
if (
FILTER_SUPPORTS_CLEAN(type))is not all that more readable than
if (
CAP_CLEAN& type)
OK. I will change that.
Quoted 11 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 492
Quoted 9 lines
convert.c +9
-0+struct cmd2process { +struct hashmap_entry ent;/* must be the first member! */ +int supported_capabilities; +const char *cmd; +struct child_process process; +}; + +static int cmd_process_map_initialized =0 ; +static struct hashmap cmd_process_map;Don't initialize statics to 0 or NULL.
OK, statics are initialized implicitly to 0.
I will fix it.
Quoted 20 lines
Quoted 20 lines
convert.c +20
-0+static int cmd2process_cmp (const struct cmd2process *e1, +const struct cmd2process *e2, +const void *unused) +{ +return strcmp (e1->cmd, e2->cmd); +} + +static struct cmd2process *find_multi_file_filter_entry (struct hashmap *hashmap,const char *cmd) +{ +struct cmd2process key; +hashmap_entry_init (&key,strhash (cmd)); + key.cmd = cmd; +return hashmap_get (hashmap, &key,NULL ); +} + +static void kill_multi_file_filter (struct hashmap *hashmap,struct cmd2process *entry) +{ +if (!entry) +return ; +sigchain_push (SIGPIPE ,SIG_IGN );
Quoted 8 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 517
Quoted 4 lines
convert.c +4
-0+close (entry->process.in); +close (entry->process.out); +sigchain_pop (SIGPIPE ); +finish_command (&entry->process);I wonder if we want to diagnose failures from
close(), which is a lot more interesting than usual because these are connected to pipes.
In this particular case we kill the filter. That means some error already happened, therefore the result wouldn't be of interest anymore, I think. Wrong?
The other case is the proper shutdown (see 12/12). However, in that case Git is already exiting and therefore I wonder what we would do with a "close" error?
Quoted 10 lines
convert.c static int apply_filter(const char *path, const char *src, size_t len, int fd, · line 629
Quoted 8 lines
convert.c +8
-0+static int apply_multi_file_filter (const char *path,const char *src,size_t len, +int fd,struct strbuf *dst,const char *cmd, +const int wanted_capability) +{ +int ret =1 ; + ... +if (!(wanted_capability & entry->supported_capabilities)) +return 1 ;// it is OK if the wanted capability is not supported No // comment please.
OK!
Quoted 15 lines
Quoted 15 lines
convert.c +15
-0+ filter_result =packet_read_line (process->out,NULL ); + ret = !strcmp (filter_result,"result=success" ); + +done: +if (ret) { +strbuf_swap (dst, &nbuf); + }else { +if (!filter_result ||strcmp (filter_result,"result=reject" )) { +// Something went wrong with the protocol filter. Force shutdown! +error ("external filter '%s' failed" , cmd); +kill_multi_file_filter (&cmd_process_map, entry); + } + } +strbuf_release (&nbuf); +return ret;
Quoted 8 lines
Quoted 1 line
+}
I think this was already pointed out in the previous review by Peff, but a variable "ret" that says "0 is bad" somehow makes it hard to follow the code. Perhaps rename it to "int error", flip the meaning, and if the caller wants this function to return non-zero on success flip the polarity in the return statement itself, i.e. "return !errors", may make it easier to follow?
This follows the existing filter function. Please see Peff's later reply here:
"So I'm not sure if changing them is a good idea. I agree with you that it's probably inviting confusion to have the two sets of filter functions have opposite return codes. So I think I retract my suggestion. :)"
http://public-inbox.org/git/20160728133523.GB21311%40sigill.intra.peff.net/
That's why I kept it the way it is. If you prefer the "!errors" approach then I will change that.
Thanks for looking at the patch,
Lars
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
Quoted 10 lines · Lars Schneider
Quoted 7 lines
I think this was already pointed out in the previous review by Peff, but a variable "ret" that says "0 is bad" somehow makes it hard to follow the code. Perhaps rename it to "int error", flip the meaning, and if the caller wants this function to return non-zero on success flip the polarity in the return statement itself, i.e. "return !errors", may make it easier to follow?
This follows the existing filter function. Please see Peff's later reply here:
Which I did before mentioning "pointed out in his review".
Quoted 2 lines
That's why I kept it the way it is. If you prefer the "!errors" approach then I will change that.
I am not suggesting to change the RETURN VALUE from this function. That is why I mentioned "return !errors" to flip the polarity at the end. Inside the function, "ret" variable _forces_ the readers to think "this function unlike the others signal an error with 0" constantly while reading it, and one possible approach to reduce the mental burden is to replace "ret" variable with "errors" variable, which is clear to anybody that it would be non-zero when we saw error(s).
Oh, I am not suggesting to _count_ the number of errors by mentioning a possible variable name "errors"; the only reason why I mentioned that name is because "error" is already taken, and "seen_error" is a bit too long.
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
Quoted 28 lines · Junio C Hamano
Quoted 10 lines · Lars Schneider
Quoted 7 lines
I think this was already pointed out in the previous review by Peff, but a variable "ret" that says "0 is bad" somehow makes it hard to follow the code. Perhaps rename it to "int error", flip the meaning, and if the caller wants this function to return non-zero on success flip the polarity in the return statement itself, i.e. "return !errors", may make it easier to follow?
This follows the existing filter function. Please see Peff's later reply here:
Which I did before mentioning "pointed out in his review".
Quoted 2 lines
That's why I kept it the way it is. If you prefer the "!errors" approach then I will change that.
I am not suggesting to change the RETURN VALUE from this function. That is why I mentioned "return !errors" to flip the polarity at the end. Inside the function, "ret" variable _forces_ the readers to think "this function unlike the others signal an error with 0" constantly while reading it, and one possible approach to reduce the mental burden is to replace "ret" variable with "errors" variable, which is clear to anybody that it would be non-zero when we saw error(s).
Quoted 4 lines
Oh, I am not suggesting to _count_ the number of errors by mentioning a possible variable name "errors"; the only reason why I mentioned that name is because "error" is already taken, and "
seen_error" is a bit too long.
Agreed. I got that you didn't suggest to change the return value :-) In order to be consistent I would also adjust the error handling in the existing apply_filter() function that I renamed to apply_single_file_filter() in 11/12. OK?
Thanks,
Lars
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
In reply to [email protected]Quoted 8 lines · On 2016-08-03 18.42, [email protected] wrote:
The filter is expected to respond with the result content in zero or more pkt-line packets and a flush packet at the end. Finally, a "result=success" packet is expected if everything went well.
------------------------packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< result=success\n------------------------
I would really send the diagnostics/return codes before the content.
Quoted 6 lines
If the result content is empty then the filter is expected to respond only with a flush packet and a "result=success" packet.
------------------------packet: git< 0000 packet: git< result=success\n------------------------
Which may be:
packet: git< result=success\n packet: git< SMUDGED_CONTENT packet: git< 0000
or for an empty file:
packet: git< result=success\n packet: git< SMUDGED_CONTENT packet: git< 0000
or in case of an error:
packet: git< result=reject\n
# And this will not send the "0000" packet
Does this makes sense ?
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
Quoted 27 lines · Torsten Bögershausen
Quoted 8 lines · On 2016-08-03 18.42, [email protected] wrote:
The filter is expected to respond with the result content in zero or more pkt-line packets and a flush packet at the end. Finally, a "result=success" packet is expected if everything went well.
------------------------packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< result=success\n------------------------
I would really send the diagnostics/return codes before the content.
Quoted 6 lines
If the result content is empty then the filter is expected to respond only with a flush packet and a "result=success" packet.
------------------------packet: git< 0000 packet: git< result=success\n------------------------
Which may be:
packet: git< result=success\n packet: git< SMUDGED_CONTENT packet: git< 0000
Quoted 5 lines
or for an empty file:
packet: git< result=success\n packet: git< SMUDGED_CONTENT packet: git< 0000
I think you meant:
packet: git< result=success\n packet: git< 0000
Right?
Quoted 6 lines
or in case of an error:
packet: git< result=reject\n
# And this will not send the "0000" packetDoes this makes sense ?
I see your point. However, I think your suggestion would not work in the true streaming case as the filter wouldn't know upfront if the operation will succeed, right?
Thanks for the review,
Lars
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
In reply to Torsten BögershausenQuoted 10 lines · Torsten Bögershausen
Quoted 8 lines · On 2016-08-03 18.42, [email protected] wrote:
The filter is expected to respond with the result content in zero or more pkt-line packets and a flush packet at the end. Finally, a "result=success" packet is expected if everything went well.
------------------------packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< result=success\n------------------------
I would really send the diagnostics/return codes before the content.
I smell the assumption "by the time the filter starts output, it must have finished everything and knows both size and the status".
I'd prefer to have a protocol that allows us to do streaming I/O on both ends when possible, even if the initial version of the filters (and the code that sits on the Git side) hold everything in-core before starting to talk.
Quoted 9 lines
Quoted 2 lines
If the result content is empty then the filter is expected to respond only with a flush packet and a "result=success" packet.
...
Which may be:packet: git< result=success\n packet: git< SMUDGED_CONTENT packet: git< 0000
Quoted 5 lines
or for an empty file:
packet: git< result=success\n packet: git< SMUDGED_CONTENT packet: git< 0000
The above two look the same to me.
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
Quoted 13 lines · Junio C Hamano
Quoted 10 lines · Torsten Bögershausen
Quoted 8 lines · On 2016-08-03 18.42, [email protected] wrote:
The filter is expected to respond with the result content in zero or more pkt-line packets and a flush packet at the end. Finally, a "result=success" packet is expected if everything went well.
------------------------packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< result=success\n------------------------
I would really send the diagnostics/return codes before the content.
Quoted 7 lines
I smell the assumption "by the time the filter starts output, it must have finished everything and knows both size and the status".
I'd prefer to have a protocol that allows us to do streaming I/O on both ends when possible, even if the initial version of the filters (and the code that sits on the Git side) hold everything in-core before starting to talk.
I think you really want to handle both cases:
- the server says "no, I can't fulfill your request" (e.g., HTTP 404)
- the server can abort an in-progress response to indicate that it could not be fulfilled completely (in HTTP chunked encoding, this requires hanging up before sending the final EOF chunk)
If we expect the second case to be rare, then hanging up before sending the flush packet is probably OK. But we could also have a trailing error code after the data to say "ignore that, we saw an error, but I can still handle more requests".
It is true that you don't need the up-front status code in that case (you can send an empty body and say "ignore that, we saw an error") but that feels a little weird. And I expect it makes the lives of the client easier to get a code up front, before it starts taking steps to handle what it _thinks_ is probably a valid response.
-Peff
PS I haven't followed HTTP/2 development much, but I think it solves the
"hangup" issue by putting each request/response in its own framed stream. I actually wonder if that is a direction we will want to go eventually, too, or the same reason that HTTP/2 did: multiple async requests across a single connection.
We already have some precedent in the sideband protocol. So imagine, for example, that we could ask the filter to work on several files simultaneously, by sending
git> \1[file1 content]
git> \2[file2 content]
git> \1[file1 content]and so on. I don't think this is something that needs to happen in the initial protocol (it's not like git can do parallel checkout right now anyway). If there's a capability negotiation at the front of the protocol, then an async feature can be worked out later. Just food for thought at this point.
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
Quoted 21 lines · Jeff King
Quoted 16 lines · Junio C Hamano
Quoted 10 lines · Torsten Bögershausen
Quoted 8 lines · On 2016-08-03 18.42, [email protected] wrote:
The filter is expected to respond with the result content in zero or more pkt-line packets and a flush packet at the end. Finally, a "result=success" packet is expected if everything went well.
------------------------packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< result=success\n------------------------
I would really send the diagnostics/return codes before the content.
I smell the assumption "by the time the filter starts output, it must have finished everything and knows both size and the status".
Quoted 8 lines
Quoted 4 lines
I'd prefer to have a protocol that allows us to do streaming I/O on both ends when possible, even if the initial version of the filters (and the code that sits on the Git side) hold everything in-core before starting to talk.
I think you really want to handle both cases:
- the server says "no, I can't fulfill your request" (e.g., HTTP 404)
You can do this with the current protocol:
packet: git< 0000 packet: git< result=reject\n
Admittedly the flush packet could be consider overhead but I think that is neglectable.
Quoted 3 lines
- the server can abort an in-progress response to indicate that it could not be fulfilled completely (in HTTP chunked encoding, this requires hanging up before sending the final EOF chunk)
Also already supported with the following sequence:
packet: git< HALF_WRITTEN_ERRONEOUS_CONTENT packet: git< 0000 packet: git< result=error\n
Quoted 8 lines
If we expect the second case to be rare, then hanging up before sending the flush packet is probably OK. But we could also have a trailing error code after the data to say "ignore that, we saw an error, but I can still handle more requests".
It is true that you don't need the up-front status code in that case (you can send an empty body and say "ignore that, we saw an error") but that feels a little weird.
I understand your argument. However, I think "0000" indicates "I have nothing for you" and therefore I think it would be OK in the reject case.
Quoted 3 lines
And I expect it makes the lives of the client
easier to get a code up front, before it starts taking steps to handle what it _thinks_ is probably a valid response.
I am not sure I can follow you here. Which actor are you referring to when you write "client" -- Git, right? If the response is rejected right away then Git just needs to read a single flush. If the response experiences an error only later, then the filter wouldn't know about the error when it starts sending. Therefore I don't see how an error code up front could make it easier for Git.
- Lars
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
Quoted 10 lines · Lars Schneider
Quoted 3 lines
And I expect it makes the lives of the client
easier to get a code up front, before it starts taking steps to handle what it _thinks_ is probably a valid response.I am not sure I can follow you here. Which actor are you referring to when you write "client" -- Git, right? If the response is rejected right away then Git just needs to read a single flush. If the response experiences an error only later, then the filter wouldn't know about the error when it starts sending. Therefore I don't see how an error code up front could make it easier for Git.
Yes, I mean git (I see it as the "client" side of the connection in that it is making requests of the filter, which will then provide responses).
What I mean is that the git code could look something like:
status == send_filter_request();
if (status == OK) {
prepare_storage();
read_response_into_storage();
} else {
complain();}
But if there's no status up front, then you probably have:
send_filter_request();
prepare_storage();
status = read_response_into_storage();
if (status != OK) {
rollback_storage();
complain();}
In the first case, we could easily avoid preparing the storage if our request wasn't going to be filled, whereas in the second we have to do it unconditionally. That's not a big deal if preparing the storage is initializing a strbuf. It's more so if you're opening a temporary object file to stream into.
You _do_ still have to deal with rollback in the first one (for the case that the stream ends prematurely for whatever reason). So it's really a question of where and how often we expect the failures to come, and whether it is worth git knowing up front that the request is not going to be fulfilled.
I dunno. It's not _that_ big a deal to code around. I was just surprised not to see an up-front status when responding to a request. It seems like the normal thing in just about every protocol I've ever used.
-Peff
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
Quoted 51 lines · Jeff King
Quoted 10 lines · Lars Schneider
Quoted 3 lines
And I expect it makes the lives of the client
easier to get a code up front, before it starts taking steps to handle what it _thinks_ is probably a valid response.I am not sure I can follow you here. Which actor are you referring to when you write "client" -- Git, right? If the response is rejected right away then Git just needs to read a single flush. If the response experiences an error only later, then the filter wouldn't know about the error when it starts sending. Therefore I don't see how an error code up front could make it easier for Git.
Yes, I mean git (I see it as the "client" side of the connection in that it is making requests of the filter, which will then provide responses).
What I mean is that the git code could look something like:
status ==
send_filter_request();if (status == OK) { prepare_storage(); read_response_into_storage(); } else { complain();}
But if there's no status up front, then you probably have:
send_filter_request();
prepare_storage();
status =read_response_into_storage();if (status != OK) { rollback_storage(); complain();}
In the first case, we could easily avoid preparing the storage if our request wasn't going to be filled, whereas in the second we have to do it unconditionally. That's not a big deal if preparing the storage is initializing a strbuf. It's more so if you're opening a temporary object file to stream into.
You _do_ still have to deal with rollback in the first one (for the case that the stream ends prematurely for whatever reason). So it's really a question of where and how often we expect the failures to come, and whether it is worth git knowing up front that the request is not going to be fulfilled.
Quoted 3 lines
I dunno. It's not _that_ big a deal to code around. I was just surprised not to see an up-front status when responding to a request. It seems like the normal thing in just about every protocol I've ever used.
Alright. The fact that it "surprised" you is a bad sign. How about this:
Happy answer:
------------------------
packet: git< status=accept\n packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< status=success\n
------------------------
Happy answer with no content:
------------------------
packet: git< status=success\n
------------------------
Rejected content:
------------------------
packet: git< status=reject\n
------------------------
Error during content response:
------------------------
packet: git< status=accept\n packet: git< HALF_WRITTEN_ERRONEOUS_CONTENT packet: git< 0000 packet: git< status=error\n
------------------------
Cheers,
Lars
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
Quoted 7 lines · Lars Schneider
Quoted 3 lines
I dunno. It's not _that_ big a deal to code around. I was just surprised not to see an up-front status when responding to a request. It seems like the normal thing in just about every protocol I've ever used.
Alright. The fact that it "surprised" you is a bad sign. How about this:
Quoted 7 lines
Happy answer:
------------------------packet: git< status=accept\n packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< status=success\n------------------------
I notice that the status pkt-lines are by themselves. I had assumed we'd be sending other data, too (presumably before, but I guess possibly after, too). Something like:
git< status=accept
git< 0000
git< SMUDGED_CONTENT
git< 0000
git< status=success
git< 0000
I don't have any particular meta-information in mind, but I thought stuff like the tentative "size" field would be here.
I had imagined it at the front, but I guess it could go in either place. I wonder if keys at the end could simply replace ones from the beginning (so if you say "foo=bar" at the front, that is tentative, but if you then say "foo=revised" at the end, that takes precedence).
And so the happy answer is really:
git< status=success
git< 0000
git< SMUDGED_CONTENT
git< 0000
git< 0000 # empty list!
i.e., no second status. The original "success" still holds.
And then:
Quoted 4 lines
Happy answer with no content:
------------------------
packet: git< status=success\n
------------------------
This can just be spelled:
git< status=success
git< 0000
git< 0000 # empty content!
git< 0000 # empty list!
Quoted 4 lines
Rejected content:
------------------------
packet: git< status=reject\n
------------------------
I'd assume that an error status would end the output for that file immediately, no empty lists necessary (so what you have here). I'd probably just call this "error" (see below).
Quoted 7 lines
Error during content response:
------------------------packet: git< status=accept\n packet: git< HALF_WRITTEN_ERRONEOUS_CONTENT packet: git< 0000 packet: git< status=error\n------------------------
And then this would be:
git< status=success
git< 0000
git< HALF_OF_CONTENT
git< 0000
git< status=error
git< 0000
And then you have only two status codes: success and error. Which keeps things simple.
There's one other case, which is when the filter dies halfway through the conversation, like:
git< status=success
git< 0000
git< CONTENT
git< 0000
... EOF on pipe ...
Any time git does not get the conversation all the way to the final flush after the trailers, it should be considered an error (because we can never know if the filter was about to say "whoops, status=error").
-Peff
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
Quoted 41 lines · Jeff King
Quoted 7 lines · Lars Schneider
Quoted 3 lines
I dunno. It's not _that_ big a deal to code around. I was just surprised not to see an up-front status when responding to a request. It seems like the normal thing in just about every protocol I've ever used.
Alright. The fact that it "surprised" you is a bad sign. How about this:
Quoted 7 lines
Happy answer:
------------------------packet: git< status=accept\n packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< status=success\n------------------------
I notice that the status pkt-lines are by themselves. I had assumed we'd be sending other data, too (presumably before, but I guess possibly after, too). Something like:
git< status=accept
git< 0000
git<SMUDGED_CONTENT
git< 0000
git< status=success
git< 0000I don't have any particular meta-information in mind, but I thought stuff like the tentative "size" field would be here.
I had imagined it at the front, but I guess it could go in either place. I wonder if keys at the end could simply replace ones from the beginning (so if you say "foo=bar" at the front, that is tentative, but if you then say "foo=revised" at the end, that takes precedence).
And so the happy answer is really:
Quoted 7 lines
git< status=success
git< 0000
git<SMUDGED_CONTENT
git< 0000
git< 0000 # empty list!i.e., no second status. The original "success" still holds.
OK, that sounds sensible to me.
Quoted 7 lines
And then:
Quoted 4 lines
Happy answer with no content:
------------------------
packet: git< status=success\n
------------------------
Quoted 6 lines
This can just be spelled:
git< status=success
git< 0000
git< 0000 # empty content!
git< 0000 # empty list!
Is the first flush packet one too many?
If there is nothing then I think we shouldn't
send any packets?!
I agree with the remaining two flush packets.
Quoted 8 lines
Quoted 4 lines
Rejected content:
------------------------
packet: git< status=reject\n
------------------------I'd assume that an error status would end the output for that file immediately, no empty lists necessary (so what you have here). I'd probably just call this "error" (see below).
OK!
Quoted 29 lines
Quoted 7 lines
Error during content response:
------------------------packet: git< status=accept\n packet: git< HALF_WRITTEN_ERRONEOUS_CONTENT packet: git< 0000 packet: git< status=error\n------------------------
And then this would be:
git< status=success
git< 0000
git<HALF_OF_CONTENT
git< 0000
git< status=error
git< 0000And then you have only two status codes: success and error. Which keeps things simple.
There's one other case, which is when the filter dies halfway through the conversation, like:
git< status=success
git< 0000
git< CONTENT
git< 0000
... EOF on pipe ...
Quoted 3 lines
Any time git does not get the conversation all the way to the final flush after the trailers, it should be considered an error (because we can never know if the filter was about to say "whoops, status=error").
Right. I agree with the protocol above and I will implement it that way.
There is one more thing: I introduced a return value "status=error-all". Using this the filter can signal Git that it does not want to process any other file using the particular command.
Jakub came up with this idea here:
"Another response, which I think should be standarized, or at least described in the documentation, is filter driver refusing to filter further (e.g. git-LFS and network is down), to be not restarted by Git."
http://public-inbox.org/git/607c07fe-5b6f-fd67-13e1-705020c267ee%40gmail.com/
I think it is a good idea. Do you see arguments against it?
Thanks,
Lars
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
Quoted 12 lines · Lars Schneider
Quoted 11 lines
Quoted 4 lines
Happy answer with no content:
------------------------
packet: git< status=success\n
------------------------This can just be spelled:
git< status=success
git< 0000
git< 0000 # empty content!
git< 0000 # empty list!
Quoted 5 lines
Is the first flush packet one too many?
If there is nothing then I think we shouldn't
send any packets?!I agree with the remaining two flush packets.
There isn't nothing, there is a "status" field (though I think that should probably be required, so I guess you could imagine it as a stand-alone pkt, separate from the list terminated by the flush). But regardless, you need the first flush to say "I am done telling you up-front keys, now I am starting the content".
Otherwise, what would:
git< status=success
git< foo=bar
git< 0000
be parsed as? Is "foo=bar" the first line of content, or the rest of the pre-content header? (You could guess if you could see the total conversation, but you can't; you have to parse it as it comes).
Quoted 6 lines
There is one more thing: I introduced a return value "status=error-all". Using this the filter can signal Git that it does not want to process any other file using the particular command.
Jakub came up with this idea here:
Quoted 8 lines
"Another response, which I think should be standarized, or at least described in the documentation, is filter driver refusing to filter further (e.g. git-LFS and network is down), to be not restarted by Git."
http://public-inbox.org/git/607c07fe-5b6f-fd67-13e1-705020c267ee%40gmail.com/
I think it is a good idea. Do you see arguments against it?
No, that seems reasonable (I would have just implemented that by hanging up the connection, but explicitly communicating is more robust).
-Peff
Re: [PATCH v4 11/12] convert: add filter.<driver>.process option
In reply to Junio C HamanoQuoted 29 lines · Junio C Hamano
Quoted 10 lines · Torsten Bögershausen
Quoted 8 lines · On 2016-08-03 18.42, [email protected] wrote:
The filter is expected to respond with the result content in zero or more pkt-line packets and a flush packet at the end. Finally, a "result=success" packet is expected if everything went well.
------------------------packet: git< SMUDGED_CONTENT packet: git< 0000 packet: git< result=success\n------------------------
I would really send the diagnostics/return codes before the content.
I smell the assumption "by the time the filter starts output, it must have finished everything and knows both size and the status".
I'd prefer to have a protocol that allows us to do streaming I/O on both ends when possible, even if the initial version of the filters (and the code that sits on the Git side) hold everything in-core before starting to talk.
Quoted 9 lines
Quoted 2 lines
If the result content is empty then the filter is expected to respond only with a flush packet and a "result=success" packet.
...
Which may be:packet: git< result=success\n packet: git< SMUDGED_CONTENT packet: git< 0000
Quoted 6 lines
Quoted 5 lines
or for an empty file:
packet: git< result=success\n packet: git< SMUDGED_CONTENT packet: git< 0000The above two look the same to me.
Copy-paste error.
i see that we need a status after the complete transfer, and after some thinking I would like to take back my comment.
PATCH v4 12/12 convert: add filter.<driver>.process shutdown command option
In reply to [email protected]From: Lars Schneider <[email protected]>
Add the "shutdown" capability to the filter.<driver>.process filter protocol. If a filter supports this capability then Git will send the "shutdown" command and wait until the filter answers. This gives the filter the opportunity to perform cleanup tasks. Afterwards the filter is expected to exit.
--- Documentation/gitattributes.txt | 12 ++++++- convert.c | 35 ++++++++++++++++++++ t/t0021-conversion.sh | 71 +++++++++++++++++++++++++++++++++++++++++ t/t0021/rot13-filter.pl | 7 ++++ 4 files changed, 124 insertions(+), 1 deletion(-)
Documentation/gitattributes.txt +11 -1
@@ -400,7 +400,8 @@ packet: git< git-filter-protocol\n packet: git< version=2\n packet: git< capabilities=clean smudge\n ------------------------ -Supported filter capabilities are "clean" and "smudge". +Supported filter capabilities are "clean", "smudge", and +"shutdown". Afterwards Git sends a command (based on the supported capabilities), the pathname of a file relative to the
@@ -453,6 +454,15 @@ After the filter has processed a blob it is expected to wait for the next command. When the Git process terminates, it will send a kill signal to the filter in that stage. +If the filter supports the "shutdown" capability then Git will +send the "shutdown" command and wait until the filter answers +with "done". This gives the filter the opportunity to perform +cleanup tasks. Afterwards the filter is expected to exit. +------------------------ +packet: git> command=shutdown\n +packet: git< result=success\n +------------------------ + A long running filter demo implementation can be found in `t/t0021/rot13-filter.pl` located in the Git core repository. If you develop your own long running filter process then the
convert.c +35 -0
@@ -478,8 +478,10 @@ static int apply_single_file_filter(const char *path, const char *src, size_t le#define FILTER_CAPABILITIES_CLEAN (1u<<0)#define FILTER_CAPABILITIES_SMUDGE (1u<<1) +#define FILTER_CAPABILITIES_SHUTDOWN (1u<<2)#define FILTER_SUPPORTS_CLEAN (type) ((type) & FILTER_CAPABILITIES_CLEAN)#define FILTER_SUPPORTS_SMUDGE (type) ((type) & FILTER_CAPABILITIES_SMUDGE) +#define FILTER_SUPPORTS_SHUTDOWN (type) ((type) & FILTER_CAPABILITIES_SHUTDOWN)struct cmd2process {struct hashmap_entry ent;/* must be the first member! */
@@ -520,6 +522,35 @@ static void kill_multi_file_filter(struct hashmap *hashmap, struct cmd2process *free (entry); } +void shutdown_multi_file_filter (pid_t pid) +{ +int did_fail; +struct cmd2process *entry; +struct hashmap_iter iter; +static const char shutdown[] ="command=shutdown\n" ; +char *result =NULL ; + +if (!cmd_process_map_initialized) +return ; + +hashmap_iter_init (&cmd_process_map, &iter); +while ((entry =hashmap_iter_next (&iter))) { +if (entry->process.pid == pid && +FILTER_SUPPORTS_SHUTDOWN (entry->supported_capabilities) + ) { + did_fail =direct_packet_write_data ( + entry->process.in, shutdown,strlen (shutdown),1 ); +if (!did_fail) + result =packet_read_line (entry->process.out,NULL ); +close (entry->process.in); +close (entry->process.out); + +if (did_fail || !result ||strcmp (result,"result=success" )) +error ("shutdown of external filter '%s' failed" , entry->cmd); + } + } +} +static struct cmd2process *start_multi_file_filter (struct hashmap *hashmap,const char *cmd) {int did_fail;
@@ -543,6 +574,8 @@ static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, cons process->use_shell =1 ; process->in =-1 ; process->out =-1 ; + process->clean_on_exit =1 ; + process->clean_on_exit_handler = shutdown_multi_file_filter;if (start_command (process)) {error ("cannot fork to run external filter '%s'" , cmd);
@@ -575,6 +608,8 @@ static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, cons entry->supported_capabilities |=FILTER_CAPABILITIES_CLEAN ; }else if (!strcmp (requested,"smudge" )) { entry->supported_capabilities |=FILTER_CAPABILITIES_SMUDGE ; + }else if (!strcmp (requested,"shutdown" )) { + entry->supported_capabilities |=FILTER_CAPABILITIES_SHUTDOWN ; }else { warning( "external filter '%s' requested unsupported filter capability '%s'",
t/t0021-conversion.sh +71 -0
77 lines not on this page · raw message
t/t0021/rot13-filter.pl +7 -0
@@ -84,6 +84,13 @@ while (1) { print $debug "IN: $command"; $debug->flush(); + if ( $command eq "shutdown" ) { + print $debug " -- [OK]"; + $debug->flush(); + packet_write("result=success\n"); + exit(); + } + my ($pathname) = packet_read() =~ /^pathname=([^=]+)\n$/; print $debug " $pathname"; $debug->flush();
Signature · 1 line
2.9.0