{"thread":{"id":"44216","subject":"[PATCH v9 02/14] convert: modernize tests","startedAt":"2016-10-04T13:00:02Z","lastAt":"2016-10-06T17:26:00Z","messageCount":28,"participants":["larsxschneider@gmail.com","Junio C Hamano","Lars Schneider","Jakub Narębski","Johannes Schindelin"],"isPatch":true,"patchVersion":9,"patchTotal":14},"messages":[{"id":"303209","messageId":"20161004125947.67104-3-larsxschneider@gmail.com","threadId":"44216","inReplyTo":"20161004125947.67104-1-larsxschneider@gmail.com","subject":"[PATCH v9 02/14] convert: modernize tests","fromName":"","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-04T12:59:35Z","receivedAt":"2016-10-04T13:00:02Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nUse `test_config` to set the config, check that files are empty with\n`test_must_be_empty`, compare files with `test_cmp`, and remove spaces\nafter \">\" and \"<\".\n\nPlease note that the \"rot13\" filter configured in \"setup\" keeps using\n`git config` instead of `test_config` because subsequent tests might\ndepend on it.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n t/t0021-conversion.sh | 58 +++++++++++++++++++++++++--------------------------\n 1 file changed, 29 insertions(+), 29 deletions(-)\n\ndiff --git a/t/t0021-conversion.sh b/t/t0021-conversion.sh\nindex e799e59..dc50938 100755\n--- a/t/t0021-conversion.sh\n+++ b/t/t0021-conversion.sh\n@@ -38,8 +38,8 @@ script='s/^\\$Id: \\([0-9a-f]*\\) \\$/\\1/p'\n \n test_expect_success check '\n \n-\tcmp test.o test &&\n-\tcmp test.o test.t &&\n+\ttest_cmp test.o test &&\n+\ttest_cmp test.o test.t &&\n \n \t# ident should be stripped in the repository\n \tgit diff --raw --exit-code :test :test.i &&\n@@ -47,10 +47,10 @@ test_expect_success check '\n \tembedded=$(sed -ne \"$script\" test.i) &&\n \ttest \"z$id\" = \"z$embedded\" &&\n \n-\tgit cat-file blob :test.t > test.r &&\n+\tgit cat-file blob :test.t >test.r &&\n \n-\t./rot13.sh < test.o > test.t &&\n-\tcmp test.r test.t\n+\t./rot13.sh <test.o >test.t &&\n+\ttest_cmp test.r test.t\n '\n \n # If an expanded ident ever gets into the repository, we want to make sure that\n@@ -130,7 +130,7 @@ test_expect_success 'filter shell-escaped filenames' '\n \n \t# delete the files and check them out again, using a smudge filter\n \t# that will count the args and echo the command-line back to us\n-\tgit config filter.argc.smudge \"sh ./argc.sh %f\" &&\n+\ttest_config filter.argc.smudge \"sh ./argc.sh %f\" &&\n \trm \"$normal\" \"$special\" &&\n \tgit checkout -- \"$normal\" \"$special\" &&\n \n@@ -141,7 +141,7 @@ test_expect_success 'filter shell-escaped filenames' '\n \ttest_cmp expect \"$special\" &&\n \n \t# do the same thing, but with more args in the filter expression\n-\tgit config filter.argc.smudge \"sh ./argc.sh %f --my-extra-arg\" &&\n+\ttest_config filter.argc.smudge \"sh ./argc.sh %f --my-extra-arg\" &&\n \trm \"$normal\" \"$special\" &&\n \tgit checkout -- \"$normal\" \"$special\" &&\n \n@@ -154,9 +154,9 @@ test_expect_success 'filter shell-escaped filenames' '\n '\n \n test_expect_success 'required filter should filter data' '\n-\tgit config filter.required.smudge ./rot13.sh &&\n-\tgit config filter.required.clean ./rot13.sh &&\n-\tgit config filter.required.required true &&\n+\ttest_config filter.required.smudge ./rot13.sh &&\n+\ttest_config filter.required.clean ./rot13.sh &&\n+\ttest_config filter.required.required true &&\n \n \techo \"*.r filter=required\" >.gitattributes &&\n \n@@ -165,17 +165,17 @@ test_expect_success 'required filter should filter data' '\n \n \trm -f test.r &&\n \tgit checkout -- test.r &&\n-\tcmp test.o test.r &&\n+\ttest_cmp test.o test.r &&\n \n \t./rot13.sh <test.o >expected &&\n \tgit cat-file blob :test.r >actual &&\n-\tcmp expected actual\n+\ttest_cmp expected actual\n '\n \n test_expect_success 'required filter smudge failure' '\n-\tgit config filter.failsmudge.smudge false &&\n-\tgit config filter.failsmudge.clean cat &&\n-\tgit config filter.failsmudge.required true &&\n+\ttest_config filter.failsmudge.smudge false &&\n+\ttest_config filter.failsmudge.clean cat &&\n+\ttest_config filter.failsmudge.required true &&\n \n \techo \"*.fs filter=failsmudge\" >.gitattributes &&\n \n@@ -186,9 +186,9 @@ test_expect_success 'required filter smudge failure' '\n '\n \n test_expect_success 'required filter clean failure' '\n-\tgit config filter.failclean.smudge cat &&\n-\tgit config filter.failclean.clean false &&\n-\tgit config filter.failclean.required true &&\n+\ttest_config filter.failclean.smudge cat &&\n+\ttest_config filter.failclean.clean false &&\n+\ttest_config filter.failclean.required true &&\n \n \techo \"*.fc filter=failclean\" >.gitattributes &&\n \n@@ -197,8 +197,8 @@ test_expect_success 'required filter clean failure' '\n '\n \n test_expect_success 'filtering large input to small output should use little memory' '\n-\tgit config filter.devnull.clean \"cat >/dev/null\" &&\n-\tgit config filter.devnull.required true &&\n+\ttest_config filter.devnull.clean \"cat >/dev/null\" &&\n+\ttest_config filter.devnull.required true &&\n \tfor i in $(test_seq 1 30); do printf \"%1048576d\" 1; done >30MB &&\n \techo \"30MB filter=devnull\" >.gitattributes &&\n \tGIT_MMAP_LIMIT=1m GIT_ALLOC_LIMIT=1m git add 30MB\n@@ -207,7 +207,7 @@ test_expect_success 'filtering large input to small output should use little mem\n test_expect_success 'filter that does not read is fine' '\n \ttest-genrandom foo $((128 * 1024 + 1)) >big &&\n \techo \"big filter=epipe\" >.gitattributes &&\n-\tgit config filter.epipe.clean \"echo xyzzy\" &&\n+\ttest_config filter.epipe.clean \"echo xyzzy\" &&\n \tgit add big &&\n \tgit cat-file blob :big >actual &&\n \techo xyzzy >expect &&\n@@ -215,20 +215,20 @@ test_expect_success 'filter that does not read is fine' '\n '\n \n test_expect_success EXPENSIVE 'filter large file' '\n-\tgit config filter.largefile.smudge cat &&\n-\tgit config filter.largefile.clean cat &&\n+\ttest_config filter.largefile.smudge cat &&\n+\ttest_config filter.largefile.clean cat &&\n \tfor i in $(test_seq 1 2048); do printf \"%1048576d\" 1; done >2GB &&\n \techo \"2GB filter=largefile\" >.gitattributes &&\n \tgit add 2GB 2>err &&\n-\t! test -s err &&\n+\ttest_must_be_empty err &&\n \trm -f 2GB &&\n \tgit checkout -- 2GB 2>err &&\n-\t! test -s err\n+\ttest_must_be_empty err\n '\n \n test_expect_success \"filter: clean empty file\" '\n-\tgit config filter.in-repo-header.clean  \"echo cleaned && cat\" &&\n-\tgit config filter.in-repo-header.smudge \"sed 1d\" &&\n+\ttest_config filter.in-repo-header.clean  \"echo cleaned && cat\" &&\n+\ttest_config filter.in-repo-header.smudge \"sed 1d\" &&\n \n \techo \"empty-in-worktree    filter=in-repo-header\" >>.gitattributes &&\n \t>empty-in-worktree &&\n@@ -240,8 +240,8 @@ test_expect_success \"filter: clean empty file\" '\n '\n \n test_expect_success \"filter: smudge empty file\" '\n-\tgit config filter.empty-in-repo.clean \"cat >/dev/null\" &&\n-\tgit config filter.empty-in-repo.smudge \"echo smudged && cat\" &&\n+\ttest_config filter.empty-in-repo.clean \"cat >/dev/null\" &&\n+\ttest_config filter.empty-in-repo.smudge \"echo smudged && cat\" &&\n \n \techo \"empty-in-repo filter=empty-in-repo\" >>.gitattributes &&\n \techo dead data walking >empty-in-repo &&\n-- \n2.10.0\n\n"},{"id":"303210","messageId":"20161004125947.67104-7-larsxschneider@gmail.com","threadId":"44216","inReplyTo":"20161004125947.67104-1-larsxschneider@gmail.com","subject":"[PATCH v9 06/14] pkt-line: extract set_packet_header()","fromName":"","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-04T12:59:39Z","receivedAt":"2016-10-04T13:00:10Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nExtracted set_packet_header() function converts an integer to a 4 byte\nhex string. Make this function locally available so that other pkt-line\nfunctions could use it.\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n pkt-line.c | 19 +++++++++++++------\n 1 file changed, 13 insertions(+), 6 deletions(-)\n\ndiff --git a/pkt-line.c b/pkt-line.c\nindex 0a9b61c..e8adc0f 100644\n--- a/pkt-line.c\n+++ b/pkt-line.c\n@@ -97,10 +97,20 @@ void packet_buf_flush(struct strbuf *buf)\n \tstrbuf_add(buf, \"0000\", 4);\n }\n \n-#define hex(a) (hexchar[(a) & 15])\n-static void format_packet(struct strbuf *out, const char *fmt, va_list args)\n+static void set_packet_header(char *buf, const int size)\n {\n \tstatic char hexchar[] = \"0123456789abcdef\";\n+\n+\t#define hex(a) (hexchar[(a) & 15])\n+\tbuf[0] = hex(size >> 12);\n+\tbuf[1] = hex(size >> 8);\n+\tbuf[2] = hex(size >> 4);\n+\tbuf[3] = hex(size);\n+\t#undef hex\n+}\n+\n+static void format_packet(struct strbuf *out, const char *fmt, va_list args)\n+{\n \tsize_t orig_len, n;\n \n \torig_len = out->len;\n@@ -111,10 +121,7 @@ static void format_packet(struct strbuf *out, const char *fmt, va_list args)\n \tif (n > LARGE_PACKET_MAX)\n \t\tdie(\"protocol error: impossibly long line\");\n \n-\tout->buf[orig_len + 0] = hex(n >> 12);\n-\tout->buf[orig_len + 1] = hex(n >> 8);\n-\tout->buf[orig_len + 2] = hex(n >> 4);\n-\tout->buf[orig_len + 3] = hex(n);\n+\tset_packet_header(&out->buf[orig_len], n);\n \tpacket_trace(out->buf + orig_len + 4, n - 4, 1);\n }\n \n-- \n2.10.0\n\n"},{"id":"303211","messageId":"20161004125947.67104-8-larsxschneider@gmail.com","threadId":"44216","inReplyTo":"20161004125947.67104-1-larsxschneider@gmail.com","subject":"[PATCH v9 07/14] pkt-line: add packet_write_fmt_gently()","fromName":"","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-04T12:59:40Z","receivedAt":"2016-10-04T13:00:13Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\npacket_write_fmt() would die in case of a write error even though for\nsome callers an error would be acceptable. Add packet_write_fmt_gently()\nwhich writes a formatted pkt-line like packet_write_fmt() but does not\ndie in case of an error. The function is used in a subsequent patch.\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n pkt-line.c | 34 ++++++++++++++++++++++++++++++----\n pkt-line.h |  1 +\n 2 files changed, 31 insertions(+), 4 deletions(-)\n\ndiff --git a/pkt-line.c b/pkt-line.c\nindex e8adc0f..56915f0 100644\n--- a/pkt-line.c\n+++ b/pkt-line.c\n@@ -125,16 +125,42 @@ static void format_packet(struct strbuf *out, const char *fmt, va_list args)\n \tpacket_trace(out->buf + orig_len + 4, n - 4, 1);\n }\n \n+static int packet_write_fmt_1(int fd, int gently,\n+\t\t\t      const char *fmt, va_list args)\n+{\n+\tstruct strbuf buf = STRBUF_INIT;\n+\tssize_t count;\n+\n+\tformat_packet(&buf, fmt, args);\n+\tcount = write_in_full(fd, buf.buf, buf.len);\n+\tif (count == buf.len)\n+\t\treturn 0;\n+\n+\tif (!gently) {\n+\t\tcheck_pipe(errno);\n+\t\tdie_errno(\"packet write with format failed\");\n+\t}\n+\treturn error(\"packet write with format failed\");\n+}\n+\n void packet_write_fmt(int fd, const char *fmt, ...)\n {\n-\tstatic struct strbuf buf = STRBUF_INIT;\n \tva_list args;\n \n-\tstrbuf_reset(&buf);\n \tva_start(args, fmt);\n-\tformat_packet(&buf, fmt, args);\n+\tpacket_write_fmt_1(fd, 0, fmt, args);\n+\tva_end(args);\n+}\n+\n+int packet_write_fmt_gently(int fd, const char *fmt, ...)\n+{\n+\tint status;\n+\tva_list args;\n+\n+\tva_start(args, fmt);\n+\tstatus = packet_write_fmt_1(fd, 1, fmt, args);\n \tva_end(args);\n-\twrite_or_die(fd, buf.buf, buf.len);\n+\treturn status;\n }\n \n void packet_buf_write(struct strbuf *buf, const char *fmt, ...)\ndiff --git a/pkt-line.h b/pkt-line.h\nindex 1902fb3..3caea77 100644\n--- a/pkt-line.h\n+++ b/pkt-line.h\n@@ -23,6 +23,7 @@ void packet_flush(int fd);\n void packet_write_fmt(int fd, const char *fmt, ...) __attribute__((format (printf, 2, 3)));\n void packet_buf_flush(struct strbuf *buf);\n void packet_buf_write(struct strbuf *buf, const char *fmt, ...) __attribute__((format (printf, 2, 3)));\n+int packet_write_fmt_gently(int fd, const char *fmt, ...) __attribute__((format (printf, 2, 3)));\n \n /*\n  * Read a packetized line into the buffer, which must be at least size bytes\n-- \n2.10.0\n\n"},{"id":"303212","messageId":"20161004125947.67104-10-larsxschneider@gmail.com","threadId":"44216","inReplyTo":"20161004125947.67104-1-larsxschneider@gmail.com","subject":"[PATCH v9 09/14] pkt-line: add packet_write_gently()","fromName":"","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-04T12:59:42Z","receivedAt":"2016-10-04T13:00:14Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\npacket_write_fmt_gently() uses format_packet() which lets the caller\nonly send string data via \"%s\". That means it cannot be used for\narbitrary data that may contain NULs.\n\nAdd packet_write_gently() which writes arbitrary data and does not die\nin case of an error. The function is used by other pkt-line functions in\na subsequent patch.\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n pkt-line.c | 16 ++++++++++++++++\n 1 file changed, 16 insertions(+)\n\ndiff --git a/pkt-line.c b/pkt-line.c\nindex 286eb09..3fd4dc0 100644\n--- a/pkt-line.c\n+++ b/pkt-line.c\n@@ -171,6 +171,22 @@ int packet_write_fmt_gently(int fd, const char *fmt, ...)\n \treturn status;\n }\n \n+static int packet_write_gently(const int fd_out, const char *buf, size_t size)\n+{\n+\tstatic char packet_write_buffer[LARGE_PACKET_MAX];\n+\tconst size_t packet_size = size + 4;\n+\n+\tif (packet_size > sizeof(packet_write_buffer))\n+\t\treturn error(\"packet write failed - data exceeds max packet size\");\n+\n+\tpacket_trace(buf, size, 1);\n+\tset_packet_header(packet_write_buffer, packet_size);\n+\tmemcpy(packet_write_buffer + 4, buf, size);\n+\tif (write_in_full(fd_out, packet_write_buffer, packet_size) == packet_size)\n+\t\treturn 0;\n+\treturn error(\"packet write failed\");\n+}\n+\n void packet_buf_write(struct strbuf *buf, const char *fmt, ...)\n {\n \tva_list args;\n-- \n2.10.0\n\n"},{"id":"303213","messageId":"20161004125947.67104-12-larsxschneider@gmail.com","threadId":"44216","inReplyTo":"20161004125947.67104-1-larsxschneider@gmail.com","subject":"[PATCH v9 11/14] convert: make apply_filter() adhere to standard Git error handling","fromName":"","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-04T12:59:44Z","receivedAt":"2016-10-04T13:00:21Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\napply_filter() returns a boolean that tells the caller if it\n\"did convert or did not convert\". The variable `ret` was used throughout\nthe function to track errors whereas `1` denoted success and `0`\nfailure. This is unusual for the Git source where `0` denotes success.\n\nRename the variable and flip its value to make the function easier\nreadable for Git developers.\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n convert.c | 15 ++++++---------\n 1 file changed, 6 insertions(+), 9 deletions(-)\n\ndiff --git a/convert.c b/convert.c\nindex 986c239..597f561 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -451,7 +451,7 @@ static int apply_filter(const char *path, const char *src, size_t len, int fd,\n \t *\n \t * (child --> cmd) --> us\n \t */\n-\tint ret = 1;\n+\tint err = 0;\n \tstruct strbuf nbuf = STRBUF_INIT;\n \tstruct async async;\n \tstruct filter_params params;\n@@ -477,23 +477,20 @@ static int apply_filter(const char *path, const char *src, size_t len, int fd,\n \t\treturn 0;\t/* error was already reported */\n \n \tif (strbuf_read(&nbuf, async.out, len) < 0) {\n-\t\terror(\"read from external filter '%s' failed\", cmd);\n-\t\tret = 0;\n+\t\terr = error(\"read from external filter '%s' failed\", cmd);\n \t}\n \tif (close(async.out)) {\n-\t\terror(\"read from external filter '%s' failed\", cmd);\n-\t\tret = 0;\n+\t\terr = error(\"read from external filter '%s' failed\", cmd);\n \t}\n \tif (finish_async(&async)) {\n-\t\terror(\"external filter '%s' failed\", cmd);\n-\t\tret = 0;\n+\t\terr = error(\"external filter '%s' failed\", cmd);\n \t}\n \n-\tif (ret) {\n+\tif (!err) {\n \t\tstrbuf_swap(dst, &nbuf);\n \t}\n \tstrbuf_release(&nbuf);\n-\treturn ret;\n+\treturn !err;\n }\n \n static struct convert_driver {\n-- \n2.10.0\n\n"},{"id":"303214","messageId":"20161004125947.67104-11-larsxschneider@gmail.com","threadId":"44216","inReplyTo":"20161004125947.67104-1-larsxschneider@gmail.com","subject":"[PATCH v9 10/14] pkt-line: add functions to read/write flush terminated packet streams","fromName":"","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-04T12:59:43Z","receivedAt":"2016-10-04T13:00:24Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nwrite_packetized_from_fd() and write_packetized_from_buf() write a\nstream of packets. All content packets use the maximal packet size\nexcept for the last one. After the last content packet a `flush` control\npacket is written.\n\nread_packetized_to_strbuf() reads arbitrary sized packets until it\ndetects a `flush` packet.\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n pkt-line.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n pkt-line.h |  8 ++++++++\n 2 files changed, 77 insertions(+)\n\ndiff --git a/pkt-line.c b/pkt-line.c\nindex 3fd4dc0..8ffde22 100644\n--- a/pkt-line.c\n+++ b/pkt-line.c\n@@ -196,6 +196,47 @@ void packet_buf_write(struct strbuf *buf, const char *fmt, ...)\n \tva_end(args);\n }\n \n+int write_packetized_from_fd(int fd_in, int fd_out)\n+{\n+\tstatic char buf[LARGE_PACKET_DATA_MAX];\n+\tint err = 0;\n+\tssize_t bytes_to_write;\n+\n+\twhile (!err) {\n+\t\tbytes_to_write = xread(fd_in, buf, sizeof(buf));\n+\t\tif (bytes_to_write < 0)\n+\t\t\treturn COPY_READ_ERROR;\n+\t\tif (bytes_to_write == 0)\n+\t\t\tbreak;\n+\t\terr = packet_write_gently(fd_out, buf, bytes_to_write);\n+\t}\n+\tif (!err)\n+\t\terr = packet_flush_gently(fd_out);\n+\treturn err;\n+}\n+\n+int write_packetized_from_buf(const char *src_in, size_t len, int fd_out)\n+{\n+\tstatic char buf[LARGE_PACKET_DATA_MAX];\n+\tint err = 0;\n+\tsize_t bytes_written = 0;\n+\tsize_t bytes_to_write;\n+\n+\twhile (!err) {\n+\t\tif ((len - bytes_written) > sizeof(buf))\n+\t\t\tbytes_to_write = sizeof(buf);\n+\t\telse\n+\t\t\tbytes_to_write = len - bytes_written;\n+\t\tif (bytes_to_write == 0)\n+\t\t\tbreak;\n+\t\terr = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);\n+\t\tbytes_written += bytes_to_write;\n+\t}\n+\tif (!err)\n+\t\terr = packet_flush_gently(fd_out);\n+\treturn err;\n+}\n+\n static int get_packet_data(int fd, char **src_buf, size_t *src_size,\n \t\t\t   void *dst, unsigned size, int options)\n {\n@@ -305,3 +346,31 @@ char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)\n {\n \treturn packet_read_line_generic(-1, src, src_len, dst_len);\n }\n+\n+ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out)\n+{\n+\tint packet_len;\n+\n+\tsize_t orig_len = sb_out->len;\n+\tsize_t orig_alloc = sb_out->alloc;\n+\n+\tfor (;;) {\n+\t\tstrbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);\n+\t\tpacket_len = packet_read(fd_in, NULL, NULL,\n+\t\t\t// TODO: explain + 1\n+\t\t\tsb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,\n+\t\t\tPACKET_READ_GENTLE_ON_EOF);\n+\t\tif (packet_len <= 0)\n+\t\t\tbreak;\n+\t\tsb_out->len += packet_len;\n+\t}\n+\n+\tif (packet_len < 0) {\n+\t\tif (orig_alloc == 0)\n+\t\t\tstrbuf_release(sb_out);\n+\t\telse\n+\t\t\tstrbuf_setlen(sb_out, orig_len);\n+\t\treturn packet_len;\n+\t}\n+\treturn sb_out->len - orig_len;\n+}\ndiff --git a/pkt-line.h b/pkt-line.h\nindex 3fa0899..18eac64 100644\n--- a/pkt-line.h\n+++ b/pkt-line.h\n@@ -25,6 +25,8 @@ void packet_buf_flush(struct strbuf *buf);\n void packet_buf_write(struct strbuf *buf, const char *fmt, ...) __attribute__((format (printf, 2, 3)));\n int packet_flush_gently(int fd);\n int packet_write_fmt_gently(int fd, const char *fmt, ...) __attribute__((format (printf, 2, 3)));\n+int write_packetized_from_fd(int fd_in, int fd_out);\n+int write_packetized_from_buf(const char *src_in, size_t len, int fd_out);\n \n /*\n  * Read a packetized line into the buffer, which must be at least size bytes\n@@ -77,8 +79,14 @@ char *packet_read_line(int fd, int *size);\n  */\n char *packet_read_line_buf(char **src_buf, size_t *src_len, int *size);\n \n+/*\n+ * Reads a stream of variable sized packets until a flush packet is detected.\n+ */\n+ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out);\n+\n #define DEFAULT_PACKET_MAX 1000\n #define LARGE_PACKET_MAX 65520\n+#define LARGE_PACKET_DATA_MAX (LARGE_PACKET_MAX - 4)\n extern char packet_buffer[LARGE_PACKET_MAX];\n \n #endif\n-- \n2.10.0\n\n"},{"id":"303215","messageId":"20161004125947.67104-13-larsxschneider@gmail.com","threadId":"44216","inReplyTo":"20161004125947.67104-1-larsxschneider@gmail.com","subject":"[PATCH v9 12/14] convert: prepare filter.<driver>.process option","fromName":"","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-04T12:59:45Z","receivedAt":"2016-10-04T13:00:25Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nRefactor the existing 'single shot filter mechanism' and prepare the\nnew 'long running filter mechanism'.\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\n---\n convert.c | 60 ++++++++++++++++++++++++++++++++++--------------------------\n 1 file changed, 34 insertions(+), 26 deletions(-)\n\ndiff --git a/convert.c b/convert.c\nindex 597f561..71e11ff 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -442,7 +442,7 @@ static int filter_buffer_or_fd(int in, int out, void *data)\n \treturn (write_err || status);\n }\n \n-static int apply_filter(const char *path, const char *src, size_t len, int fd,\n+static int apply_single_file_filter(const char *path, const char *src, size_t len, int fd,\n                         struct strbuf *dst, const char *cmd)\n {\n \t/*\n@@ -456,12 +456,6 @@ static int apply_filter(const char *path, const char *src, size_t len, int fd,\n \tstruct async async;\n \tstruct filter_params params;\n \n-\tif (!cmd || !*cmd)\n-\t\treturn 0;\n-\n-\tif (!dst)\n-\t\treturn 1;\n-\n \tmemset(&async, 0, sizeof(async));\n \tasync.proc = filter_buffer_or_fd;\n \tasync.data = &params;\n@@ -493,6 +487,9 @@ static int apply_filter(const char *path, const char *src, size_t len, int fd,\n \treturn !err;\n }\n \n+#define CAP_CLEAN    (1u<<0)\n+#define CAP_SMUDGE   (1u<<1)\n+\n static struct convert_driver {\n \tconst char *name;\n \tstruct convert_driver *next;\n@@ -501,6 +498,29 @@ static struct convert_driver {\n \tint required;\n } *user_convert, **user_convert_tail;\n \n+static int apply_filter(const char *path, const char *src, size_t len,\n+\t\t\tint fd, struct strbuf *dst, struct convert_driver *drv,\n+\t\t\tconst unsigned int wanted_capability)\n+{\n+\tconst char *cmd = NULL;\n+\n+\tif (!drv)\n+\t\treturn 0;\n+\n+\tif (!dst)\n+\t\treturn 1;\n+\n+\tif ((CAP_CLEAN & wanted_capability) && drv->clean)\n+\t\tcmd = drv->clean;\n+\telse if ((CAP_SMUDGE & wanted_capability) && drv->smudge)\n+\t\tcmd = drv->smudge;\n+\n+\tif (cmd && *cmd)\n+\t\treturn apply_single_file_filter(path, src, len, fd, dst, cmd);\n+\n+\treturn 0;\n+}\n+\n static int read_convert_config(const char *var, const char *value, void *cb)\n {\n \tconst char *key, *name;\n@@ -839,7 +859,7 @@ int would_convert_to_git_filter_fd(const char *path)\n \tif (!ca.drv->required)\n \t\treturn 0;\n \n-\treturn apply_filter(path, NULL, 0, -1, NULL, ca.drv->clean);\n+\treturn apply_filter(path, NULL, 0, -1, NULL, ca.drv, CAP_CLEAN);\n }\n \n const char *get_convert_attr_ascii(const char *path)\n@@ -872,18 +892,12 @@ int convert_to_git(const char *path, const char *src, size_t len,\n                    struct strbuf *dst, enum safe_crlf checksafe)\n {\n \tint ret = 0;\n-\tconst char *filter = NULL;\n-\tint required = 0;\n \tstruct conv_attrs ca;\n \n \tconvert_attrs(&ca, path);\n-\tif (ca.drv) {\n-\t\tfilter = ca.drv->clean;\n-\t\trequired = ca.drv->required;\n-\t}\n \n-\tret |= apply_filter(path, src, len, -1, dst, filter);\n-\tif (!ret && required)\n+\tret |= apply_filter(path, src, len, -1, dst, ca.drv, CAP_CLEAN);\n+\tif (!ret && ca.drv && ca.drv->required)\n \t\tdie(\"%s: clean filter '%s' failed\", path, ca.drv->name);\n \n \tif (ret && dst) {\n@@ -907,7 +921,7 @@ void convert_to_git_filter_fd(const char *path, int fd, struct strbuf *dst,\n \tassert(ca.drv);\n \tassert(ca.drv->clean);\n \n-\tif (!apply_filter(path, NULL, 0, fd, dst, ca.drv->clean))\n+\tif (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN))\n \t\tdie(\"%s: clean filter '%s' failed\", path, ca.drv->name);\n \n \tcrlf_to_git(path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);\n@@ -919,15 +933,9 @@ static int convert_to_working_tree_internal(const char *path, const char *src,\n \t\t\t\t\t    int normalizing)\n {\n \tint ret = 0, ret_filter = 0;\n-\tconst char *filter = NULL;\n-\tint required = 0;\n \tstruct conv_attrs ca;\n \n \tconvert_attrs(&ca, path);\n-\tif (ca.drv) {\n-\t\tfilter = ca.drv->smudge;\n-\t\trequired = ca.drv->required;\n-\t}\n \n \tret |= ident_to_worktree(path, src, len, dst, ca.ident);\n \tif (ret) {\n@@ -938,7 +946,7 @@ static int convert_to_working_tree_internal(const char *path, const char *src,\n \t * CRLF conversion can be skipped if normalizing, unless there\n \t * is a smudge filter.  The filter might expect CRLFs.\n \t */\n-\tif (filter || !normalizing) {\n+\tif ((ca.drv && ca.drv->smudge) || !normalizing) {\n \t\tret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action);\n \t\tif (ret) {\n \t\t\tsrc = dst->buf;\n@@ -946,8 +954,8 @@ static int convert_to_working_tree_internal(const char *path, const char *src,\n \t\t}\n \t}\n \n-\tret_filter = apply_filter(path, src, len, -1, dst, filter);\n-\tif (!ret_filter && required)\n+\tret_filter = apply_filter(path, src, len, -1, dst, ca.drv, CAP_SMUDGE);\n+\tif (!ret_filter && ca.drv && ca.drv->required)\n \t\tdie(\"%s: smudge filter %s failed\", path, ca.drv->name);\n \n \treturn ret | ret_filter;\n-- \n2.10.0\n\n"},{"id":"303216","messageId":"20161004125947.67104-14-larsxschneider@gmail.com","threadId":"44216","inReplyTo":"20161004125947.67104-1-larsxschneider@gmail.com","subject":"[PATCH v9 13/14] convert: add filter.<driver>.process option","fromName":"","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-04T12:59:46Z","receivedAt":"2016-10-04T13:00:29Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nGit's clean/smudge mechanism invokes an external filter process for\nevery single blob that is affected by a filter. If Git filters a lot of\nblobs then the startup time of the external filter processes can become\na significant part of the overall Git execution time.\n\nIn a preliminary performance test this developer used a clean/smudge\nfilter written in golang to filter 12,000 files. This process took 364s\nwith the existing filter mechanism and 5s with the new mechanism. See\ndetails here: https://github.com/github/git-lfs/pull/1382\n\nThis patch adds the `filter.<driver>.process` string option which, if\nused, keeps the external filter process running and processes all blobs\nwith the packet format (pkt-line) based protocol over standard input and\nstandard output. The full protocol is explained in detail in\n`Documentation/gitattributes.txt`.\n\nA few key decisions:\n\n* The long running filter process is referred to as filter protocol\n  version 2 because the existing single shot filter invocation is\n  considered version 1.\n* Git sends a welcome message and expects a response right after the\n  external filter process has started. This ensures that Git will not\n  hang if a version 1 filter is incorrectly used with the\n  filter.<driver>.process option for version 2 filters. In addition,\n  Git can detect this kind of error and warn the user.\n* The status of a filter operation (e.g. \"success\" or \"error) is set\n  before the actual response and (if necessary!) re-set after the\n  response. The advantage of this two step status response is that if\n  the filter detects an error early, then the filter can communicate\n  this and Git does not even need to create structures to read the\n  response.\n* All status responses are pkt-line lists terminated with a flush\n  packet. This allows us to send other status fields with the same\n  protocol in the future.\n\nHelped-by: Martin-Louis Bright <mlbright@gmail.com>\nReviewed-by: Jakub Narebski <jnareb@gmail.com>\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n Documentation/gitattributes.txt | 157 +++++++++++++-\n convert.c                       | 295 +++++++++++++++++++++++++-\n t/t0021-conversion.sh           | 447 +++++++++++++++++++++++++++++++++++++++-\n t/t0021/rot13-filter.pl         | 191 +++++++++++++++++\n 4 files changed, 1080 insertions(+), 10 deletions(-)\n create mode 100755 t/t0021/rot13-filter.pl\n\ndiff --git a/Documentation/gitattributes.txt b/Documentation/gitattributes.txt\nindex 7aff940..5868f00 100644\n--- a/Documentation/gitattributes.txt\n+++ b/Documentation/gitattributes.txt\n@@ -293,7 +293,13 @@ checkout, when the `smudge` command is specified, the command is\n fed the blob object from its standard input, and its standard\n output is used to update the worktree file.  Similarly, the\n `clean` command is used to convert the contents of worktree file\n-upon checkin.\n+upon checkin. By default these commands process only a single\n+blob and terminate.  If a long running `process` filter is used\n+in place of `clean` and/or `smudge` filters, then Git can process\n+all blobs with a single filter command invocation for the entire\n+life of a single Git command, for example `git add --all`.  See\n+section below for the description of the protocol used to\n+communicate with a `process` filter.\n \n One use of the content filtering is to massage the content into a shape\n that is more convenient for the platform, filesystem, and the user to use.\n@@ -373,6 +379,155 @@ not exist, or may have different contents. So, smudge and clean commands\n should not try to access the file on disk, but only act as filters on the\n content provided to them on standard input.\n \n+Long Running Filter Process\n+^^^^^^^^^^^^^^^^^^^^^^^^^^^\n+\n+If the filter command (a string value) is defined via\n+`filter.<driver>.process` then Git can process all blobs with a\n+single filter invocation for the entire life of a single Git\n+command. This is achieved by using a packet format (pkt-line,\n+see technical/protocol-common.txt) based protocol over standard\n+input and standard output as follows. All packets, except for the\n+\"*CONTENT\" packets and the \"0000\" flush packet, are considered\n+text and therefore are terminated by a LF.\n+\n+Git starts the filter when it encounters the first file\n+that needs to be cleaned or smudged. After the filter started\n+Git sends a welcome message (\"git-filter-client\"), a list of\n+supported protocol version numbers, and a flush packet. Git expects\n+to read a welcome response message (\"git-filter-server\") and exactly\n+one protocol version number from the previously sent list. All further\n+communication will be based on the selected version. The remaining\n+protocol description below documents \"version=2\". Please note that\n+\"version=42\" in the example below does not exist and is only there\n+to illustrate how the protocol would look like with more than one\n+version.\n+\n+After the version negotiation Git sends a list of all capabilities that\n+it supports and a flush packet. Git expects to read a list of desired\n+capabilities, which must be a subset of the supported capabilities list,\n+and a flush packet as response:\n+------------------------\n+packet:          git> git-filter-client\n+packet:          git> version=2\n+packet:          git> version=42\n+packet:          git> 0000\n+packet:          git< git-filter-server\n+packet:          git< version=2\n+packet:          git> clean=true\n+packet:          git> smudge=true\n+packet:          git> not-yet-invented=true\n+packet:          git> 0000\n+packet:          git< clean=true\n+packet:          git< smudge=true\n+packet:          git< 0000\n+------------------------\n+Supported filter capabilities in version 2 are \"clean\" and\n+\"smudge\".\n+\n+Afterwards Git sends a list of \"key=value\" pairs terminated with\n+a flush packet. The list will contain at least the filter command\n+(based on the supported capabilities) and the pathname of the file\n+to filter relative to the repository root. Right after these packets\n+Git sends the content split in zero or more pkt-line packets and a\n+flush packet to terminate content. Please note, that the filter\n+must not send any response before it received the content and the\n+final flush packet.\n+------------------------\n+packet:          git> command=smudge\n+packet:          git> pathname=path/testfile.dat\n+packet:          git> 0000\n+packet:          git> CONTENT\n+packet:          git> 0000\n+------------------------\n+\n+The filter is expected to respond with a list of \"key=value\" pairs\n+terminated with a flush packet. If the filter does not experience\n+problems then the list must contain a \"success\" status. Right after\n+these packets the filter is expected to send the content in zero\n+or more pkt-line packets and a flush packet at the end. Finally, a\n+second list of \"key=value\" pairs terminated with a flush packet\n+is expected. The filter can change the status in the second list.\n+------------------------\n+packet:          git< status=success\n+packet:          git< 0000\n+packet:          git< SMUDGED_CONTENT\n+packet:          git< 0000\n+packet:          git< 0000  # empty list, keep \"status=success\" unchanged!\n+------------------------\n+\n+If the result content is empty then the filter is expected to respond\n+with a \"success\" status and an empty list.\n+------------------------\n+packet:          git< status=success\n+packet:          git< 0000\n+packet:          git< 0000  # empty content!\n+packet:          git< 0000  # empty list, keep \"status=success\" unchanged!\n+------------------------\n+\n+In case the filter cannot or does not want to process the content,\n+it is expected to respond with an \"error\" status. Depending on the\n+`filter.<driver>.required` flag Git will interpret that as error\n+but it will not stop or restart the filter process.\n+------------------------\n+packet:          git< status=error\n+packet:          git< 0000\n+------------------------\n+\n+If the filter experiences an error during processing, then it can\n+send the status \"error\" after the content was (partially or\n+completely) sent. Depending on the `filter.<driver>.required` flag\n+Git will interpret that as error but it will not stop or restart the\n+filter process.\n+------------------------\n+packet:          git< status=success\n+packet:          git< 0000\n+packet:          git< HALF_WRITTEN_ERRONEOUS_CONTENT\n+packet:          git< 0000\n+packet:          git< status=error\n+packet:          git< 0000\n+------------------------\n+\n+If the filter dies during the communication or does not adhere to\n+the protocol then Git will stop the filter process and restart it\n+with the next file that needs to be processed. Depending on the\n+`filter.<driver>.required` flag Git will interpret that as error.\n+\n+The error handling for all cases above mimic the behavior of\n+the `filter.<driver>.clean` / `filter.<driver>.smudge` error\n+handling.\n+\n+In case the filter cannot or does not want to process the content\n+as well as any future content for the lifetime of the Git process,\n+it is expected to respond with an \"abort\" status at any point in\n+the protocol. Depending on the `filter.<driver>.required` flag Git\n+will interpret that as error for the content as well as any future\n+content for the lifetime of the Git process but it will not stop or\n+restart the filter process.\n+------------------------\n+packet:          git< status=abort\n+packet:          git< 0000\n+------------------------\n+\n+After the filter has processed a blob it is expected to wait for\n+the next \"key=value\" list containing a command. Git will close\n+the command pipe on exit. The filter is expected to detect EOF\n+and exit gracefully on its own.\n+\n+If you develop your own long running filter\n+process then the `GIT_TRACE_PACKET` environment variables can be\n+very helpful for debugging (see linkgit:git[1]).\n+\n+If a `filter.<driver>.process` command is configured then it\n+always takes precedence over a configured `filter.<driver>.clean`\n+or `filter.<driver>.smudge` command.\n+\n+Please note that you cannot use an existing `filter.<driver>.clean`\n+or `filter.<driver>.smudge` command with `filter.<driver>.process`\n+because the former two use a different inter process communication\n+protocol than the latter one.\n+\n+\n Interaction between checkin/checkout attributes\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n \ndiff --git a/convert.c b/convert.c\nindex 71e11ff..88581d6 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -3,6 +3,7 @@\n #include \"run-command.h\"\n #include \"quote.h\"\n #include \"sigchain.h\"\n+#include \"pkt-line.h\"\n \n /*\n  * convert.c - convert a file when checking it out and checking it in.\n@@ -490,11 +491,287 @@ static int apply_single_file_filter(const char *path, const char *src, size_t le\n #define CAP_CLEAN    (1u<<0)\n #define CAP_SMUDGE   (1u<<1)\n \n+struct cmd2process {\n+\tstruct hashmap_entry ent; /* must be the first member! */\n+\tunsigned int supported_capabilities;\n+\tconst char *cmd;\n+\tstruct child_process process;\n+};\n+\n+static int cmd_process_map_initialized;\n+static struct hashmap cmd_process_map;\n+\n+static int cmd2process_cmp(const struct cmd2process *e1,\n+\t\t\t   const struct cmd2process *e2,\n+\t\t\t   const void *unused)\n+{\n+\treturn strcmp(e1->cmd, e2->cmd);\n+}\n+\n+static struct cmd2process *find_multi_file_filter_entry(struct hashmap *hashmap, const char *cmd)\n+{\n+\tstruct cmd2process key;\n+\thashmap_entry_init(&key, strhash(cmd));\n+\tkey.cmd = cmd;\n+\treturn hashmap_get(hashmap, &key, NULL);\n+}\n+\n+static void kill_multi_file_filter(struct hashmap *hashmap, struct cmd2process *entry)\n+{\n+\tif (!entry)\n+\t\treturn;\n+\tsigchain_push(SIGPIPE, SIG_IGN);\n+\t/*\n+\t * We kill the filter most likely because an error happened already.\n+\t * That's why we are not interested in any error code here.\n+\t */\n+\tclose(entry->process.in);\n+\tclose(entry->process.out);\n+\tsigchain_pop(SIGPIPE);\n+\tfinish_command(&entry->process);\n+\thashmap_remove(hashmap, entry, NULL);\n+\tfree(entry);\n+}\n+\n+static int packet_write_list(int fd, const char *line, ...)\n+{\n+\tva_list args;\n+\tint err;\n+\tva_start(args, line);\n+\tfor (;;) {\n+\t\tif (!line)\n+\t\t\tbreak;\n+\t\tif (strlen(line) > LARGE_PACKET_DATA_MAX)\n+\t\t\treturn -1;\n+\t\terr = packet_write_fmt_gently(fd, \"%s\\n\", line);\n+\t\tif (err)\n+\t\t\treturn err;\n+\t\tline = va_arg(args, const char*);\n+\t}\n+\tva_end(args);\n+\treturn packet_flush_gently(fd);\n+}\n+\n+static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, const char *cmd)\n+{\n+\tint err;\n+\tstruct cmd2process *entry;\n+\tstruct child_process *process;\n+\tconst char *argv[] = { cmd, NULL };\n+\tstruct string_list cap_list = STRING_LIST_INIT_NODUP;\n+\tchar *cap_buf;\n+\tconst char *cap_name;\n+\n+\tentry = xmalloc(sizeof(*entry));\n+\thashmap_entry_init(entry, strhash(cmd));\n+\tentry->cmd = cmd;\n+\tentry->supported_capabilities = 0;\n+\tprocess = &entry->process;\n+\n+\tchild_process_init(process);\n+\tprocess->argv = argv;\n+\tprocess->use_shell = 1;\n+\tprocess->in = -1;\n+\tprocess->out = -1;\n+\tprocess->wait_on_exit = 1;\n+\n+\tif (start_command(process)) {\n+\t\terror(\"cannot fork to run external filter '%s'\", cmd);\n+\t\tkill_multi_file_filter(hashmap, entry);\n+\t\treturn NULL;\n+\t}\n+\n+\tsigchain_push(SIGPIPE, SIG_IGN);\n+\n+\terr = packet_write_list(process->in, \"git-filter-client\", \"version=2\", NULL);\n+\tif (err)\n+\t\tgoto done;\n+\n+\terr = strcmp(packet_read_line(process->out, NULL), \"git-filter-server\");\n+\tif (err) {\n+\t\terror(\"external filter '%s' does not support filter protocol version 2\", cmd);\n+\t\tgoto done;\n+\t}\n+\terr = strcmp(packet_read_line(process->out, NULL), \"version=2\");\n+\tif (err)\n+\t\tgoto done;\n+\n+\terr = packet_write_list(process->in, \"clean=true\", \"smudge=true\", NULL);\n+\n+\tfor (;;) {\n+\t\tcap_buf = packet_read_line(process->out, NULL);\n+\t\tif (!cap_buf)\n+\t\t\tbreak;\n+\t\tstring_list_split_in_place(&cap_list, cap_buf, '=', 1);\n+\n+\t\tif (cap_list.nr != 2 || strcmp(cap_list.items[1].string, \"true\"))\n+\t\t\tcontinue;\n+\n+\t\tcap_name = cap_list.items[0].string;\n+\t\tif (!strcmp(cap_name, \"clean\")) {\n+\t\t\tentry->supported_capabilities |= CAP_CLEAN;\n+\t\t} else if (!strcmp(cap_name, \"smudge\")) {\n+\t\t\tentry->supported_capabilities |= CAP_SMUDGE;\n+\t\t} else {\n+\t\t\twarning(\n+\t\t\t\t\"external filter '%s' requested unsupported filter capability '%s'\",\n+\t\t\t\tcmd, cap_name\n+\t\t\t);\n+\t\t}\n+\n+\t\tstring_list_clear(&cap_list, 0);\n+\t}\n+\n+done:\n+\tsigchain_pop(SIGPIPE);\n+\n+\tif (err || errno == EPIPE) {\n+\t\terror(\"initialization for external filter '%s' failed\", cmd);\n+\t\tkill_multi_file_filter(hashmap, entry);\n+\t\treturn NULL;\n+\t}\n+\n+\thashmap_add(hashmap, entry);\n+\treturn entry;\n+}\n+\n+static void read_multi_file_filter_status(int fd, struct strbuf *status) {\n+\tstruct strbuf **pair;\n+\tchar *line;\n+\tfor (;;) {\n+\t\tline = packet_read_line(fd, NULL);\n+\t\tif (!line)\n+\t\t\tbreak;\n+\t\tpair = strbuf_split_str(line, '=', 2);\n+\t\tif (pair[0] && pair[0]->len && pair[1]) {\n+\t\t\tif (!strcmp(pair[0]->buf, \"status=\")) {\n+\t\t\t\tstrbuf_reset(status);\n+\t\t\t\tstrbuf_addbuf(status, pair[1]);\n+\t\t\t}\n+\t\t}\n+\t\tstrbuf_list_free(pair);\n+\t}\n+}\n+\n+static int apply_multi_file_filter(const char *path, const char *src, size_t len,\n+\t\t\t\t   int fd, struct strbuf *dst, const char *cmd,\n+\t\t\t\t   const unsigned int wanted_capability)\n+{\n+\tint err;\n+\tstruct cmd2process *entry;\n+\tstruct child_process *process;\n+\tstruct stat file_stat;\n+\tstruct strbuf nbuf = STRBUF_INIT;\n+\tstruct strbuf filter_status = STRBUF_INIT;\n+\tchar *filter_type;\n+\n+\tif (!cmd_process_map_initialized) {\n+\t\tcmd_process_map_initialized = 1;\n+\t\thashmap_init(&cmd_process_map, (hashmap_cmp_fn) cmd2process_cmp, 0);\n+\t\tentry = NULL;\n+\t} else {\n+\t\tentry = find_multi_file_filter_entry(&cmd_process_map, cmd);\n+\t}\n+\n+\tfflush(NULL);\n+\n+\tif (!entry) {\n+\t\tentry = start_multi_file_filter(&cmd_process_map, cmd);\n+\t\tif (!entry)\n+\t\t\treturn 0;\n+\t}\n+\tprocess = &entry->process;\n+\n+\tif (!(wanted_capability & entry->supported_capabilities))\n+\t\treturn 0;\n+\n+\tif (CAP_CLEAN & wanted_capability)\n+\t\tfilter_type = \"clean\";\n+\telse if (CAP_SMUDGE & wanted_capability)\n+\t\tfilter_type = \"smudge\";\n+\telse\n+\t\tdie(\"unexpected filter type\");\n+\n+\tif (fd >= 0 && !src) {\n+\t\tif (fstat(fd, &file_stat) == -1)\n+\t\t\treturn 0;\n+\t\tlen = xsize_t(file_stat.st_size);\n+\t}\n+\n+\tsigchain_push(SIGPIPE, SIG_IGN);\n+\n+\tassert(strlen(filter_type) < LARGE_PACKET_DATA_MAX - strlen(\"command=\\n\"));\n+\terr = packet_write_fmt_gently(process->in, \"command=%s\\n\", filter_type);\n+\tif (err)\n+\t\tgoto done;\n+\n+\terr = strlen(path) > LARGE_PACKET_DATA_MAX - strlen(\"pathname=\\n\");\n+\tif (err) {\n+\t\terror(\"path name too long for external filter\");\n+\t\tgoto done;\n+\t}\n+\n+\terr = packet_write_fmt_gently(process->in, \"pathname=%s\\n\", path);\n+\tif (err)\n+\t\tgoto done;\n+\n+\terr = packet_flush_gently(process->in);\n+\tif (err)\n+\t\tgoto done;\n+\n+\tif (fd >= 0)\n+\t\terr = write_packetized_from_fd(fd, process->in);\n+\telse\n+\t\terr = write_packetized_from_buf(src, len, process->in);\n+\tif (err)\n+\t\tgoto done;\n+\n+\tread_multi_file_filter_status(process->out, &filter_status);\n+\terr = strcmp(filter_status.buf, \"success\");\n+\tif (err)\n+\t\tgoto done;\n+\n+\terr = read_packetized_to_strbuf(process->out, &nbuf) < 0;\n+\tif (err)\n+\t\tgoto done;\n+\n+\tread_multi_file_filter_status(process->out, &filter_status);\n+\terr = strcmp(filter_status.buf, \"success\");\n+\n+done:\n+\tsigchain_pop(SIGPIPE);\n+\n+\tif (err || errno == EPIPE) {\n+\t\tif (!strcmp(filter_status.buf, \"error\")) {\n+\t\t\t/* The filter signaled a problem with the file. */\n+\t\t} else if (!strcmp(filter_status.buf, \"abort\")) {\n+\t\t\t/*\n+\t\t\t * The filter signaled a permanent problem. Don't try to filter\n+\t\t\t * files with the same command for the lifetime of the current\n+\t\t\t * Git process.\n+\t\t\t */\n+\t\t\t entry->supported_capabilities &= ~wanted_capability;\n+\t\t} else {\n+\t\t\t/*\n+\t\t\t * Something went wrong with the protocol filter.\n+\t\t\t * Force shutdown and restart if another blob requires filtering.\n+\t\t\t */\n+\t\t\terror(\"external filter '%s' failed\", cmd);\n+\t\t\tkill_multi_file_filter(&cmd_process_map, entry);\n+\t\t}\n+\t} else {\n+\t\tstrbuf_swap(dst, &nbuf);\n+\t}\n+\tstrbuf_release(&nbuf);\n+\treturn !err;\n+}\n+\n static struct convert_driver {\n \tconst char *name;\n \tstruct convert_driver *next;\n \tconst char *smudge;\n \tconst char *clean;\n+\tconst char *process;\n \tint required;\n } *user_convert, **user_convert_tail;\n \n@@ -510,13 +787,15 @@ static int apply_filter(const char *path, const char *src, size_t len,\n \tif (!dst)\n \t\treturn 1;\n \n-\tif ((CAP_CLEAN & wanted_capability) && drv->clean)\n+\tif ((CAP_CLEAN & wanted_capability) && !drv->process && drv->clean)\n \t\tcmd = drv->clean;\n-\telse if ((CAP_SMUDGE & wanted_capability) && drv->smudge)\n+\telse if ((CAP_SMUDGE & wanted_capability) && !drv->process && drv->smudge)\n \t\tcmd = drv->smudge;\n \n \tif (cmd && *cmd)\n \t\treturn apply_single_file_filter(path, src, len, fd, dst, cmd);\n+\telse if (drv->process && *drv->process)\n+\t\treturn apply_multi_file_filter(path, src, len, fd, dst, drv->process, wanted_capability);\n \n \treturn 0;\n }\n@@ -558,6 +837,9 @@ static int read_convert_config(const char *var, const char *value, void *cb)\n \tif (!strcmp(\"clean\", key))\n \t\treturn git_config_string(&drv->clean, var, value);\n \n+\tif (!strcmp(\"process\", key))\n+\t\treturn git_config_string(&drv->process, var, value);\n+\n \tif (!strcmp(\"required\", key)) {\n \t\tdrv->required = git_config_bool(var, value);\n \t\treturn 0;\n@@ -919,7 +1201,7 @@ void convert_to_git_filter_fd(const char *path, int fd, struct strbuf *dst,\n \tconvert_attrs(&ca, path);\n \n \tassert(ca.drv);\n-\tassert(ca.drv->clean);\n+\tassert(ca.drv->clean || ca.drv->process);\n \n \tif (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN))\n \t\tdie(\"%s: clean filter '%s' failed\", path, ca.drv->name);\n@@ -944,9 +1226,10 @@ static int convert_to_working_tree_internal(const char *path, const char *src,\n \t}\n \t/*\n \t * CRLF conversion can be skipped if normalizing, unless there\n-\t * is a smudge filter.  The filter might expect CRLFs.\n+\t * is a smudge or process filter (even if the process filter doesn't\n+\t * support smudge).  The filters might expect CRLFs.\n \t */\n-\tif ((ca.drv && ca.drv->smudge) || !normalizing) {\n+\tif ((ca.drv && (ca.drv->smudge || ca.drv->process)) || !normalizing) {\n \t\tret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action);\n \t\tif (ret) {\n \t\t\tsrc = dst->buf;\n@@ -1407,7 +1690,7 @@ struct stream_filter *get_stream_filter(const char *path, const unsigned char *s\n \tstruct stream_filter *filter = NULL;\n \n \tconvert_attrs(&ca, path);\n-\tif (ca.drv && (ca.drv->smudge || ca.drv->clean))\n+\tif (ca.drv && (ca.drv->process || ca.drv->smudge || ca.drv->clean))\n \t\treturn NULL;\n \n \tif (ca.crlf_action == CRLF_AUTO || ca.crlf_action == CRLF_AUTO_CRLF)\ndiff --git a/t/t0021-conversion.sh b/t/t0021-conversion.sh\nindex dc50938..52b7fe9 100755\n--- a/t/t0021-conversion.sh\n+++ b/t/t0021-conversion.sh\n@@ -4,13 +4,77 @@ test_description='blob conversion via gitattributes'\n \n . ./test-lib.sh\n \n-cat <<EOF >rot13.sh\n+TEST_ROOT=\"$(pwd)\"\n+\n+cat <<EOF >\"$TEST_ROOT/rot13.sh\"\n #!$SHELL_PATH\n tr \\\n   'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' \\\n   'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'\n EOF\n-chmod +x rot13.sh\n+chmod +x \"$TEST_ROOT/rot13.sh\"\n+\n+generate_random_characters () {\n+\tLEN=$1\n+\tNAME=$2\n+\ttest-genrandom some-seed $LEN |\n+\t\tperl -pe \"s/./chr((ord($&) % 26) + ord('a'))/sge\" >\"$TEST_ROOT/$NAME\"\n+}\n+\n+file_size () {\n+\tcat \"$1\" | wc -c | sed \"s/^[ ]*//\"\n+}\n+\n+filter_git () {\n+\trm -f rot13-filter.log &&\n+\tgit \"$@\" 2>git-stderr.log &&\n+\tsed '/Waiting for/d' git-stderr.log >git-stderr-clean.log &&\n+\ttest_must_be_empty git-stderr-clean.log &&\n+\trm -f git-stderr.log git-stderr-clean.log\n+}\n+\n+# Count unique lines in two files and compare them.\n+test_cmp_count () {\n+\tfor FILE in $@\n+\tdo\n+\t\tsort $FILE | uniq -c | sed \"s/^[ ]*//\" >$FILE.tmp\n+\t\tcat $FILE.tmp >$FILE\n+\tdone &&\n+\ttest_cmp $@\n+}\n+\n+# Count unique lines except clean invocations in two files and compare\n+# them. Clean invocations are not counted because their number can vary.\n+# c.f. http://public-inbox.org/git/xmqqshv18i8i.fsf@gitster.mtv.corp.google.com/\n+test_cmp_count_except_clean () {\n+\tfor FILE in $@\n+\tdo\n+\t\tsort $FILE | uniq -c | sed \"s/^[ ]*//\" |\n+\t\t\tsed \"s/^\\([0-9]\\) IN: clean/x IN: clean/\" >$FILE.tmp\n+\t\tcat $FILE.tmp >$FILE\n+\tdone &&\n+\ttest_cmp $@\n+}\n+\n+# Compare two files but exclude clean invocations because they can vary.\n+# c.f. http://public-inbox.org/git/xmqqshv18i8i.fsf@gitster.mtv.corp.google.com/\n+test_cmp_exclude_clean () {\n+\tfor FILE in $@\n+\tdo\n+\t\tgrep -v \"IN: clean\" $FILE >$FILE.tmp\n+\t\tcat $FILE.tmp >$FILE\n+\tdone &&\n+\ttest_cmp $@\n+}\n+\n+# Check that the contents of two files are equal and that their rot13 version\n+# is equal to the committed content.\n+test_cmp_committed_rot13 () {\n+\ttest_cmp \"$1\" \"$2\" &&\n+\t\"$TEST_ROOT/rot13.sh\" <\"$1\" >expected &&\n+\tgit cat-file blob :\"$2\" >actual &&\n+\ttest_cmp expected actual\n+}\n \n test_expect_success setup '\n \tgit config filter.rot13.smudge ./rot13.sh &&\n@@ -31,7 +95,10 @@ test_expect_success setup '\n \tcat test >test.i &&\n \tgit add test test.t test.i &&\n \trm -f test test.t test.i &&\n-\tgit checkout -- test test.t test.i\n+\tgit checkout -- test test.t test.i &&\n+\n+\techo \"content-test2\" >test2.o &&\n+\techo \"content-test3 - filename with special characters\" >\"test3 '\\''sq'\\'',\\$x.o\"\n '\n \n script='s/^\\$Id: \\([0-9a-f]*\\) \\$/\\1/p'\n@@ -279,4 +346,378 @@ test_expect_success 'diff does not reuse worktree files that need cleaning' '\n \ttest_line_count = 0 count\n '\n \n+test_expect_success PERL 'required process filter should filter data' '\n+\ttest_config_global filter.protocol.process \"$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge\" &&\n+\ttest_config_global filter.protocol.required true &&\n+\trm -rf repo &&\n+\tmkdir repo &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit init &&\n+\n+\t\techo \"git-stderr.log\" >.gitignore &&\n+\t\techo \"*.r filter=protocol\" >.gitattributes &&\n+\t\tgit add . &&\n+\t\tgit commit . -m \"test commit 1\" &&\n+\t\tgit branch empty-branch &&\n+\n+\t\tcp \"$TEST_ROOT/test.o\" test.r &&\n+\t\tcp \"$TEST_ROOT/test2.o\" test2.r &&\n+\t\tmkdir testsubdir &&\n+\t\tcp \"$TEST_ROOT/test3 '\\''sq'\\'',\\$x.o\" \"testsubdir/test3 '\\''sq'\\'',\\$x.r\" &&\n+\t\t>test4-empty.r &&\n+\n+\t\tS=$(file_size test.r) &&\n+\t\tS2=$(file_size test2.r) &&\n+\t\tS3=$(file_size \"testsubdir/test3 '\\''sq'\\'',\\$x.r\") &&\n+\n+\t\tfilter_git add . &&\n+\t\tcat >expected.log <<-EOF &&\n+\t\t\tSTART\n+\t\t\tinit handshake complete\n+\t\t\tIN: clean test.r $S [OK] -- OUT: $S . [OK]\n+\t\t\tIN: clean test2.r $S2 [OK] -- OUT: $S2 . [OK]\n+\t\t\tIN: clean test4-empty.r 0 [OK] -- OUT: 0  [OK]\n+\t\t\tIN: clean testsubdir/test3 '\\''sq'\\'',\\$x.r $S3 [OK] -- OUT: $S3 . [OK]\n+\t\t\tSTOP\n+\t\tEOF\n+\t\ttest_cmp_count expected.log rot13-filter.log &&\n+\n+\t\tfilter_git commit . -m \"test commit 2\" &&\n+\t\tcat >expected.log <<-EOF &&\n+\t\t\tSTART\n+\t\t\tinit handshake complete\n+\t\t\tIN: clean test.r $S [OK] -- OUT: $S . [OK]\n+\t\t\tIN: clean test2.r $S2 [OK] -- OUT: $S2 . [OK]\n+\t\t\tIN: clean test4-empty.r 0 [OK] -- OUT: 0  [OK]\n+\t\t\tIN: clean testsubdir/test3 '\\''sq'\\'',\\$x.r $S3 [OK] -- OUT: $S3 . [OK]\n+\t\t\tIN: clean test.r $S [OK] -- OUT: $S . [OK]\n+\t\t\tIN: clean test2.r $S2 [OK] -- OUT: $S2 . [OK]\n+\t\t\tIN: clean test4-empty.r 0 [OK] -- OUT: 0  [OK]\n+\t\t\tIN: clean testsubdir/test3 '\\''sq'\\'',\\$x.r $S3 [OK] -- OUT: $S3 . [OK]\n+\t\t\tSTOP\n+\t\tEOF\n+\t\ttest_cmp_count_except_clean expected.log rot13-filter.log &&\n+\n+\t\trm -f test2.r \"testsubdir/test3 '\\''sq'\\'',\\$x.r\" &&\n+\n+\t\tfilter_git checkout --quiet --no-progress . &&\n+\t\tcat >expected.log <<-EOF &&\n+\t\t\tSTART\n+\t\t\tinit handshake complete\n+\t\t\tIN: smudge test2.r $S2 [OK] -- OUT: $S2 . [OK]\n+\t\t\tIN: smudge testsubdir/test3 '\\''sq'\\'',\\$x.r $S3 [OK] -- OUT: $S3 . [OK]\n+\t\t\tSTOP\n+\t\tEOF\n+\t\ttest_cmp_exclude_clean expected.log rot13-filter.log &&\n+\n+\t\tfilter_git checkout --quiet --no-progress empty-branch &&\n+\t\tcat >expected.log <<-EOF &&\n+\t\t\tSTART\n+\t\t\tinit handshake complete\n+\t\t\tIN: clean test.r $S [OK] -- OUT: $S . [OK]\n+\t\t\tSTOP\n+\t\tEOF\n+\t\ttest_cmp_exclude_clean expected.log rot13-filter.log &&\n+\n+\t\tfilter_git checkout --quiet --no-progress master &&\n+\t\tcat >expected.log <<-EOF &&\n+\t\t\tSTART\n+\t\t\tinit handshake complete\n+\t\t\tIN: smudge test.r $S [OK] -- OUT: $S . [OK]\n+\t\t\tIN: smudge test2.r $S2 [OK] -- OUT: $S2 . [OK]\n+\t\t\tIN: smudge test4-empty.r 0 [OK] -- OUT: 0  [OK]\n+\t\t\tIN: smudge testsubdir/test3 '\\''sq'\\'',\\$x.r $S3 [OK] -- OUT: $S3 . [OK]\n+\t\t\tSTOP\n+\t\tEOF\n+\t\ttest_cmp_exclude_clean expected.log rot13-filter.log &&\n+\n+\t\ttest_cmp_committed_rot13 \"$TEST_ROOT/test.o\" test.r &&\n+\t\ttest_cmp_committed_rot13 \"$TEST_ROOT/test2.o\" test2.r &&\n+\t\ttest_cmp_committed_rot13 \"$TEST_ROOT/test3 '\\''sq'\\'',\\$x.o\" \"testsubdir/test3 '\\''sq'\\'',\\$x.r\"\n+\t)\n+'\n+\n+test_expect_success PERL 'required process filter takes precedence' '\n+\ttest_config_global filter.protocol.clean false &&\n+\ttest_config_global filter.protocol.process \"$TEST_DIRECTORY/t0021/rot13-filter.pl clean\" &&\n+\ttest_config_global filter.protocol.required true &&\n+\trm -rf repo &&\n+\tmkdir repo &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit init &&\n+\n+\t\techo \"*.r filter=protocol\" >.gitattributes &&\n+\t\tcp \"$TEST_ROOT/test.o\" test.r &&\n+\t\tS=$(file_size test.r) &&\n+\n+\t\t# Check that the process filter is invoked here\n+\t\tfilter_git add . &&\n+\t\tcat >expected.log <<-EOF &&\n+\t\t\tSTART\n+\t\t\tinit handshake complete\n+\t\t\tIN: clean test.r $S [OK] -- OUT: $S . [OK]\n+\t\t\tSTOP\n+\t\tEOF\n+\t\ttest_cmp_count expected.log rot13-filter.log\n+\t)\n+'\n+\n+test_expect_success PERL 'required process filter should be used only for \"clean\" operation only' '\n+\ttest_config_global filter.protocol.process \"$TEST_DIRECTORY/t0021/rot13-filter.pl clean\" &&\n+\trm -rf repo &&\n+\tmkdir repo &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit init &&\n+\n+\t\techo \"*.r filter=protocol\" >.gitattributes &&\n+\t\tcp \"$TEST_ROOT/test.o\" test.r &&\n+\t\tS=$(file_size test.r) &&\n+\n+\t\tfilter_git add . &&\n+\t\tcat >expected.log <<-EOF &&\n+\t\t\tSTART\n+\t\t\tinit handshake complete\n+\t\t\tIN: clean test.r $S [OK] -- OUT: $S . [OK]\n+\t\t\tSTOP\n+\t\tEOF\n+\t\ttest_cmp_count expected.log rot13-filter.log &&\n+\n+\t\trm test.r &&\n+\n+\t\tfilter_git checkout --quiet --no-progress . &&\n+\t\t# If the filter would be used for \"smudge\", too, we would see\n+\t\t# \"IN: smudge test.r 57 [OK] -- OUT: 57 . [OK]\" here\n+\t\tcat >expected.log <<-EOF &&\n+\t\t\tSTART\n+\t\t\tinit handshake complete\n+\t\t\tSTOP\n+\t\tEOF\n+\t\ttest_cmp_exclude_clean expected.log rot13-filter.log\n+\t)\n+'\n+\n+test_expect_success PERL 'required process filter should process multiple packets' '\n+\ttest_config_global filter.protocol.process \"$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge\" &&\n+\ttest_config_global filter.protocol.required true &&\n+\n+\trm -rf repo &&\n+\tmkdir repo &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit init &&\n+\n+\t\t# Generate data requiring 1, 2, 3 packets\n+\t\tS=65516 && # PKTLINE_DATA_MAXLEN -> Maximal size of a packet\n+\t\tgenerate_random_characters $(($S    )) 1pkt_1__.file &&\n+\t\tgenerate_random_characters $(($S  +1)) 2pkt_1+1.file &&\n+\t\tgenerate_random_characters $(($S*2-1)) 2pkt_2-1.file &&\n+\t\tgenerate_random_characters $(($S*2  )) 2pkt_2__.file &&\n+\t\tgenerate_random_characters $(($S*2+1)) 3pkt_2+1.file &&\n+\n+\t\tfor FILE in \"$TEST_ROOT\"/*.file\n+\t\tdo\n+\t\t\tcp \"$FILE\" . &&\n+\t\t\t\"$TEST_ROOT/rot13.sh\" <\"$FILE\" >\"$FILE.rot13\"\n+\t\tdone &&\n+\n+\t\techo \"*.file filter=protocol\" >.gitattributes &&\n+\t\tfilter_git add *.file .gitattributes &&\n+\t\tcat >expected.log <<-EOF &&\n+\t\t\tSTART\n+\t\t\tinit handshake complete\n+\t\t\tIN: clean 1pkt_1__.file $(($S    )) [OK] -- OUT: $(($S    )) . [OK]\n+\t\t\tIN: clean 2pkt_1+1.file $(($S  +1)) [OK] -- OUT: $(($S  +1)) .. [OK]\n+\t\t\tIN: clean 2pkt_2-1.file $(($S*2-1)) [OK] -- OUT: $(($S*2-1)) .. [OK]\n+\t\t\tIN: clean 2pkt_2__.file $(($S*2  )) [OK] -- OUT: $(($S*2  )) .. [OK]\n+\t\t\tIN: clean 3pkt_2+1.file $(($S*2+1)) [OK] -- OUT: $(($S*2+1)) ... [OK]\n+\t\t\tSTOP\n+\t\tEOF\n+\t\ttest_cmp_count expected.log rot13-filter.log &&\n+\n+\t\trm -f *.file &&\n+\n+\t\tfilter_git checkout --quiet --no-progress -- *.file &&\n+\t\tcat >expected.log <<-EOF &&\n+\t\t\tSTART\n+\t\t\tinit handshake complete\n+\t\t\tIN: smudge 1pkt_1__.file $(($S    )) [OK] -- OUT: $(($S    )) . [OK]\n+\t\t\tIN: smudge 2pkt_1+1.file $(($S  +1)) [OK] -- OUT: $(($S  +1)) .. [OK]\n+\t\t\tIN: smudge 2pkt_2-1.file $(($S*2-1)) [OK] -- OUT: $(($S*2-1)) .. [OK]\n+\t\t\tIN: smudge 2pkt_2__.file $(($S*2  )) [OK] -- OUT: $(($S*2  )) .. [OK]\n+\t\t\tIN: smudge 3pkt_2+1.file $(($S*2+1)) [OK] -- OUT: $(($S*2+1)) ... [OK]\n+\t\t\tSTOP\n+\t\tEOF\n+\t\ttest_cmp_exclude_clean expected.log rot13-filter.log &&\n+\n+\t\tfor FILE in *.file\n+\t\tdo\n+\t\t\ttest_cmp_committed_rot13 \"$TEST_ROOT/$FILE\" $FILE\n+\t\tdone\n+\t)\n+'\n+\n+test_expect_success PERL 'required process filter with clean error should fail' '\n+\ttest_config_global filter.protocol.process \"$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge\" &&\n+\ttest_config_global filter.protocol.required true &&\n+\trm -rf repo &&\n+\tmkdir repo &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit init &&\n+\n+\t\techo \"*.r filter=protocol\" >.gitattributes &&\n+\n+\t\tcp \"$TEST_ROOT/test.o\" test.r &&\n+\t\techo \"this is going to fail\" >clean-write-fail.r &&\n+\t\techo \"content-test3-subdir\" >test3.r &&\n+\n+\t\ttest_must_fail git add .\n+\t)\n+'\n+\n+test_expect_success PERL 'process filter should restart after unexpected write failure' '\n+\ttest_config_global filter.protocol.process \"$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge\" &&\n+\trm -rf repo &&\n+\tmkdir repo &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit init &&\n+\n+\t\techo \"*.r filter=protocol\" >.gitattributes &&\n+\n+\t\tcp \"$TEST_ROOT/test.o\" test.r &&\n+\t\tcp \"$TEST_ROOT/test2.o\" test2.r &&\n+\t\techo \"this is going to fail\" >smudge-write-fail.o &&\n+\t\tcp smudge-write-fail.o smudge-write-fail.r &&\n+\n+\t\tS=$(file_size test.r) &&\n+\t\tS2=$(file_size test2.r) &&\n+\t\tSF=$(file_size smudge-write-fail.r) &&\n+\n+\t\tgit add . &&\n+\t\trm -f *.r &&\n+\n+\t\trm -f rot13-filter.log &&\n+\t\tgit checkout --quiet --no-progress . 2>git-stderr.log &&\n+\n+\t\tgrep \"smudge write error at\" git-stderr.log &&\n+\t\tgrep \"error: external filter\" git-stderr.log &&\n+\n+\t\tcat >expected.log <<-EOF &&\n+\t\t\tSTART\n+\t\t\tinit handshake complete\n+\t\t\tIN: smudge smudge-write-fail.r $SF [OK] -- OUT: $SF [WRITE FAIL]\n+\t\t\tSTART\n+\t\t\tinit handshake complete\n+\t\t\tIN: smudge test.r $S [OK] -- OUT: $S . [OK]\n+\t\t\tIN: smudge test2.r $S2 [OK] -- OUT: $S2 . [OK]\n+\t\t\tSTOP\n+\t\tEOF\n+\t\ttest_cmp_exclude_clean expected.log rot13-filter.log &&\n+\n+\t\ttest_cmp_committed_rot13 \"$TEST_ROOT/test.o\" test.r &&\n+\t\ttest_cmp_committed_rot13 \"$TEST_ROOT/test2.o\" test2.r &&\n+\n+\t\t# Smudge failed\n+\t\t! test_cmp smudge-write-fail.o smudge-write-fail.r &&\n+\t\t\"$TEST_ROOT/rot13.sh\" <smudge-write-fail.o >expected &&\n+\t\tgit cat-file blob :smudge-write-fail.r >actual &&\n+\t\ttest_cmp expected actual\n+\t)\n+'\n+\n+test_expect_success PERL 'process filter should not be restarted if it signals an error' '\n+\ttest_config_global filter.protocol.process \"$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge\" &&\n+\trm -rf repo &&\n+\tmkdir repo &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit init &&\n+\n+\t\techo \"*.r filter=protocol\" >.gitattributes &&\n+\n+\t\tcp \"$TEST_ROOT/test.o\" test.r &&\n+\t\tcp \"$TEST_ROOT/test2.o\" test2.r &&\n+\t\techo \"this will cause an error\" >error.o &&\n+\t\tcp error.o error.r &&\n+\n+\t\tS=$(file_size test.r) &&\n+\t\tS2=$(file_size test2.r) &&\n+\t\tSE=$(file_size error.r) &&\n+\n+\t\tgit add . &&\n+\t\trm -f *.r &&\n+\n+\t\tfilter_git checkout --quiet --no-progress . &&\n+\t\tcat >expected.log <<-EOF &&\n+\t\t\tSTART\n+\t\t\tinit handshake complete\n+\t\t\tIN: smudge error.r $SE [OK] -- OUT: 0 [ERROR]\n+\t\t\tIN: smudge test.r $S [OK] -- OUT: $S . [OK]\n+\t\t\tIN: smudge test2.r $S2 [OK] -- OUT: $S2 . [OK]\n+\t\t\tSTOP\n+\t\tEOF\n+\t\ttest_cmp_exclude_clean expected.log rot13-filter.log &&\n+\n+\t\ttest_cmp_committed_rot13 \"$TEST_ROOT/test.o\" test.r &&\n+\t\ttest_cmp_committed_rot13 \"$TEST_ROOT/test2.o\" test2.r &&\n+\t\ttest_cmp error.o error.r\n+\t)\n+'\n+\n+test_expect_success PERL 'process filter signals abort once to abort processing of all future files' '\n+\ttest_config_global filter.protocol.process \"$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge\" &&\n+\trm -rf repo &&\n+\tmkdir repo &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit init &&\n+\n+\t\techo \"*.r filter=protocol\" >.gitattributes &&\n+\n+\t\tcp \"$TEST_ROOT/test.o\" test.r &&\n+\t\tcp \"$TEST_ROOT/test2.o\" test2.r &&\n+\t\techo \"error this blob and all future blobs\" >abort.o &&\n+\t\tcp abort.o abort.r &&\n+\n+\t\tSA=$(file_size abort.r) &&\n+\n+\t\tgit add . &&\n+\t\trm -f *.r &&\n+\n+\t\tfilter_git checkout --quiet --no-progress . &&\n+\t\tcat >expected.log <<-EOF &&\n+\t\t\tSTART\n+\t\t\tinit handshake complete\n+\t\t\tIN: smudge abort.r $SA [OK] -- OUT: 0 [ABORT]\n+\t\t\tSTOP\n+\t\tEOF\n+\t\ttest_cmp_exclude_clean expected.log rot13-filter.log &&\n+\n+\t\ttest_cmp \"$TEST_ROOT/test.o\" test.r &&\n+\t\ttest_cmp \"$TEST_ROOT/test2.o\" test2.r &&\n+\t\ttest_cmp abort.o abort.r\n+\t)\n+'\n+\n+test_expect_success PERL 'invalid process filter must fail (and not hang!)' '\n+\ttest_config_global filter.protocol.process cat &&\n+\ttest_config_global filter.protocol.required true &&\n+\trm -rf repo &&\n+\tmkdir repo &&\n+\t(\n+\t\tcd repo &&\n+\t\tgit init &&\n+\n+\t\techo \"*.r filter=protocol\" >.gitattributes &&\n+\n+\t\tcp \"$TEST_ROOT/test.o\" test.r &&\n+\t\ttest_must_fail git add . 2>git-stderr.log &&\n+\t\tgrep \"does not support filter protocol version\" git-stderr.log\n+\t)\n+'\n+\n test_done\ndiff --git a/t/t0021/rot13-filter.pl b/t/t0021/rot13-filter.pl\nnew file mode 100755\nindex 0000000..1a6959c\n--- /dev/null\n+++ b/t/t0021/rot13-filter.pl\n@@ -0,0 +1,191 @@\n+#!/usr/bin/perl\n+#\n+# Example implementation for the Git filter protocol version 2\n+# See Documentation/gitattributes.txt, section \"Filter Protocol\"\n+#\n+# The script takes the list of supported protocol capabilities as\n+# arguments (\"clean\", \"smudge\", etc).\n+#\n+# This implementation supports special test cases:\n+# (1) If data with the pathname \"clean-write-fail.r\" is processed with\n+#     a \"clean\" operation then the write operation will die.\n+# (2) If data with the pathname \"smudge-write-fail.r\" is processed with\n+#     a \"smudge\" operation then the write operation will die.\n+# (3) If data with the pathname \"error.r\" is processed with any\n+#     operation then the filter signals that it cannot or does not want\n+#     to process the file.\n+# (4) If data with the pathname \"abort.r\" is processed with any\n+#     operation then the filter signals that it cannot or does not want\n+#     to process the file and any file after that is processed with the\n+#     same command.\n+#\n+\n+use strict;\n+use warnings;\n+\n+my $MAX_PACKET_CONTENT_SIZE = 65516;\n+my @capabilities            = @ARGV;\n+\n+open my $debug, \">>\", \"rot13-filter.log\" or die \"cannot open log file: $!\";\n+\n+sub rot13 {\n+\tmy $str = shift;\n+\t$str =~ y/A-Za-z/N-ZA-Mn-za-m/;\n+\treturn $str;\n+}\n+\n+sub packet_bin_read {\n+\tmy $buffer;\n+\tmy $bytes_read = read STDIN, $buffer, 4;\n+\tif ( $bytes_read == 0 ) {\n+\t\t# EOF - Git stopped talking to us!\n+\t\tprint $debug \"STOP\\n\";\n+\t\texit();\n+\t}\n+\telsif ( $bytes_read != 4 ) {\n+\t\tdie \"invalid packet: '$buffer'\";\n+\t}\n+\tmy $pkt_size = hex($buffer);\n+\tif ( $pkt_size == 0 ) {\n+\t\treturn ( 1, \"\" );\n+\t}\n+\telsif ( $pkt_size > 4 ) {\n+\t\tmy $content_size = $pkt_size - 4;\n+\t\t$bytes_read = read STDIN, $buffer, $content_size;\n+\t\tif ( $bytes_read != $content_size ) {\n+\t\t\tdie \"invalid packet ($content_size bytes expected; $bytes_read bytes read)\";\n+\t\t}\n+\t\treturn ( 0, $buffer );\n+\t}\n+\telse {\n+\t\tdie \"invalid packet size: $pkt_size\";\n+\t}\n+}\n+\n+sub packet_txt_read {\n+\tmy ( $res, $buf ) = packet_bin_read();\n+\tunless ( $buf =~ s/\\n$// ) {\n+\t\tdie \"A non-binary line MUST be terminated by an LF.\";\n+\t}\n+\treturn ( $res, $buf );\n+}\n+\n+sub packet_bin_write {\n+\tmy $buf = shift;\n+\tprint STDOUT sprintf( \"%04x\", length($buf) + 4 );\n+\tprint STDOUT $buf;\n+\tSTDOUT->flush();\n+}\n+\n+sub packet_txt_write {\n+\tpacket_bin_write( $_[0] . \"\\n\" );\n+}\n+\n+sub packet_flush {\n+\tprint STDOUT sprintf( \"%04x\", 0 );\n+\tSTDOUT->flush();\n+}\n+\n+print $debug \"START\\n\";\n+$debug->flush();\n+\n+( packet_txt_read() eq ( 0, \"git-filter-client\" ) ) || die \"bad initialize\";\n+( packet_txt_read() eq ( 0, \"version=2\" ) )         || die \"bad version\";\n+( packet_bin_read() eq ( 1, \"\" ) )                  || die \"bad version end\";\n+\n+packet_txt_write(\"git-filter-server\");\n+packet_txt_write(\"version=2\");\n+\n+( packet_txt_read() eq ( 0, \"clean=true\" ) )  || die \"bad capability\";\n+( packet_txt_read() eq ( 0, \"smudge=true\" ) ) || die \"bad capability\";\n+( packet_bin_read() eq ( 1, \"\" ) )            || die \"bad capability end\";\n+\n+foreach (@capabilities) {\n+\tpacket_txt_write( $_ . \"=true\" );\n+}\n+packet_flush();\n+print $debug \"init handshake complete\\n\";\n+$debug->flush();\n+\n+while (1) {\n+\tmy ($command) = packet_txt_read() =~ /^command=([^=]+)$/;\n+\tprint $debug \"IN: $command\";\n+\t$debug->flush();\n+\n+\tmy ($pathname) = packet_txt_read() =~ /^pathname=([^=]+)$/;\n+\tprint $debug \" $pathname\";\n+\t$debug->flush();\n+\n+\t# Flush\n+\tpacket_bin_read();\n+\n+\tmy $input = \"\";\n+\t{\n+\t\tbinmode(STDIN);\n+\t\tmy $buffer;\n+\t\tmy $done = 0;\n+\t\twhile ( !$done ) {\n+\t\t\t( $done, $buffer ) = packet_bin_read();\n+\t\t\t$input .= $buffer;\n+\t\t}\n+\t\tprint $debug \" \" . length($input) . \" [OK] -- \";\n+\t\t$debug->flush();\n+\t}\n+\n+\tmy $output;\n+\tif ( $pathname eq \"error.r\" or $pathname eq \"abort.r\" ) {\n+\t\t$output = \"\";\n+\t}\n+\telsif ( $command eq \"clean\" and grep( /^clean$/, @capabilities ) ) {\n+\t\t$output = rot13($input);\n+\t}\n+\telsif ( $command eq \"smudge\" and grep( /^smudge$/, @capabilities ) ) {\n+\t\t$output = rot13($input);\n+\t}\n+\telse {\n+\t\tdie \"bad command '$command'\";\n+\t}\n+\n+\tprint $debug \"OUT: \" . length($output) . \" \";\n+\t$debug->flush();\n+\n+\tif ( $pathname eq \"error.r\" ) {\n+\t\tprint $debug \"[ERROR]\\n\";\n+\t\t$debug->flush();\n+\t\tpacket_txt_write(\"status=error\");\n+\t\tpacket_flush();\n+\t}\n+\telsif ( $pathname eq \"abort.r\" ) {\n+\t\tprint $debug \"[ABORT]\\n\";\n+\t\t$debug->flush();\n+\t\tpacket_txt_write(\"status=abort\");\n+\t\tpacket_flush();\n+\t}\n+\telse {\n+\t\tpacket_txt_write(\"status=success\");\n+\t\tpacket_flush();\n+\n+\t\tif ( $pathname eq \"${command}-write-fail.r\" ) {\n+\t\t\tprint $debug \"[WRITE FAIL]\\n\";\n+\t\t\t$debug->flush();\n+\t\t\tdie \"${command} write error\";\n+\t\t}\n+\n+\t\twhile ( length($output) > 0 ) {\n+\t\t\tmy $packet = substr( $output, 0, $MAX_PACKET_CONTENT_SIZE );\n+\t\t\tpacket_bin_write($packet);\n+\t\t\t# dots represent the number of packets\n+\t\t\tprint $debug \".\";\n+\t\t\tif ( length($output) > $MAX_PACKET_CONTENT_SIZE ) {\n+\t\t\t\t$output = substr( $output, $MAX_PACKET_CONTENT_SIZE );\n+\t\t\t}\n+\t\t\telse {\n+\t\t\t\t$output = \"\";\n+\t\t\t}\n+\t\t}\n+\t\tpacket_flush();\n+\t\tprint $debug \" [OK]\\n\";\n+\t\t$debug->flush();\n+\t\tpacket_flush();\n+\t}\n+}\n-- \n2.10.0\n\n"},{"id":"303217","messageId":"20161004125947.67104-1-larsxschneider@gmail.com","threadId":"44216","inReplyTo":null,"subject":"[PATCH v9 00/14] Git filter protocol","fromName":"","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-04T12:59:33Z","receivedAt":"2016-10-04T13:00:35Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nThe goal of this series is to avoid launching a new clean/smudge filter\nprocess for each file that is filtered.\n\nA short summary about v1 to v5 can be found here:\nhttps://git.github.io/rev_news/2016/08/17/edition-18/\n\nThis series is also published on web:\nhttps://github.com/larsxschneider/git/pull/13\n\nPatches 1 and 2 are cleanups and not strictly necessary for the series.\nPatches 3 to 12 are required preparation. Patch 13 is the main patch.\nPatch 14 adds an example how to use the Git filter protocol in contrib.\n\nThanks a lot to\n  Ramsay, Jakub, Junio, Johannes, Torsten, and Peff\nfor very helpful reviews,\nLars\n\n\n\n## Major changes since v8\n\n  * refactor test cases to make them more robust and more readable\n  * split main \"convert: add filter\" patch into three:\n      1. preparation patch\n      2. main patch\n      3. contrib patch\n  * add run-command \"wait_on_exit\" flag to fix flaky t0021, see discussion:\n      http://public-inbox.org/git/xmqq8tubitjs.fsf@gitster.mtv.corp.google.com/\n\n\n\n## All changes since v8\n\n### Ramsay\n  * make async_exit()\n      http://public-inbox.org/git/78f2bdd0-f6ad-db5c-f9f2-f90528bc4f77@ramsayjones.plus.com/\n\n### Jacub\n  * improve commit message for:\n      pkt-line: rename packet_write() to packet_write_fmt\n      pkt-line: extract set_packet_header()\n  * remove unnecessary brackets in packet_write_gently\n  * introduce variable 'packet_size' to make packet_write_gently()\n    more readable\n  * replace 'PKTLINE_DATA_MAXLEN' with 'LARGE_PACKET_DATA_MAX'\n  * rename 'read_packetized_to_buf' to 'read_packetized_to_strbuf'\n  * use 'ssize_t' as proper return value for write_in_full()\n  * rename 'paket_len' to 'packet_len'\n  * remove unnecessary options variable in read_packetized_to_strbuf()\n  * rename variables in read_packetized_to_strbuf()\n  * fix +1 in read_packetized_to_strbuf()\n  * fix length comparison in packet_read()\n  * replace spaces with tabs in Perl scripts\n  * ... many smaller changes. See interdiff below.\n\n\n\n## Interdiff (v8..v9)\n\n```diff\ndiff --git a/Documentation/gitattributes.txt b/Documentation/gitattributes.txt\nindex 946dcad..a182ef2 100644\n--- a/Documentation/gitattributes.txt\n+++ b/Documentation/gitattributes.txt\n@@ -387,9 +387,9 @@ If the filter command (a string value) is defined via\n single filter invocation for the entire life of a single Git\n command. This is achieved by using a packet format (pkt-line,\n see technical/protocol-common.txt) based protocol over standard\n-input and standard output as follows. All packets are considered\n-text and therefore are terminated by an LF. Exceptions are the\n-\"*CONTENT\" packets and the flush packet.\n+input and standard output as follows. All packets, except for the\n+\"*CONTENT\" packets and the \"0000\" flush packet, are considered\n+text and therefore are terminated by a LF.\n\n Git starts the filter when it encounters the first file\n that needs to be cleaned or smudged. After the filter started\n@@ -403,10 +403,10 @@ protocol description below documents \"version=2\". Please note that\n to illustrate how the protocol would look like with more than one\n version.\n\n-After the version negotiation Git sends a list of supported capabilities\n-and a flush packet. Git expects to read a list of desired capabilities,\n-which must be a subset of the supported capabilities list, and a flush\n-packet as response:\n+After the version negotiation Git sends a list of all capabilities that\n+it supports and a flush packet. Git expects to read a list of desired\n+capabilities, which must be a subset of the supported capabilities list,\n+and a flush packet as response:\n ------------------------\n packet:          git> git-filter-client\n packet:          git> version=2\n@@ -430,7 +430,9 @@ a flush packet. The list will contain at least the filter command\n (based on the supported capabilities) and the pathname of the file\n to filter relative to the repository root. Right after these packets\n Git sends the content split in zero or more pkt-line packets and a\n-flush packet to terminate content.\n+flush packet to terminate content. Please note, that the filter\n+must not send any response before it received the content and the\n+final flush packet.\n ------------------------\n packet:          git> command=smudge\n packet:          git> pathname=path/testfile.dat\n@@ -451,16 +453,16 @@ packet:          git< status=success\n packet:          git< 0000\n packet:          git< SMUDGED_CONTENT\n packet:          git< 0000\n-packet:          git< 0000  # empty list!\n+packet:          git< 0000  # empty list, keep \"status=success\" unchanged!\n ------------------------\n\n If the result content is empty then the filter is expected to respond\n-with a success status and an empty list.\n+with a \"success\" status and an empty list.\n ------------------------\n packet:          git< status=success\n packet:          git< 0000\n packet:          git< 0000  # empty content!\n-packet:          git< 0000  # empty list!\n+packet:          git< 0000  # empty list, keep \"status=success\" unchanged!\n ------------------------\n\n In case the filter cannot or does not want to process the content,\n@@ -497,10 +499,11 @@ handling.\n\n In case the filter cannot or does not want to process the content\n as well as any future content for the lifetime of the Git process,\n-it is expected to respond with an \"abort\" status. Depending on\n-the `filter.<driver>.required` flag Git will interpret that as error\n-for the content as well as any future content for the lifetime of the\n-Git process but it will not stop or restart the filter process.\n+it is expected to respond with an \"abort\" status at any point in\n+the protocol. Depending on the `filter.<driver>.required` flag Git\n+will interpret that as error for the content as well as any future\n+content for the lifetime of the Git process but it will not stop or\n+restart the filter process.\n ------------------------\n packet:          git< status=abort\n packet:          git< 0000\ndiff --git a/contrib/long-running-filter/example.pl b/contrib/long-running-filter/example.pl\nindex c13a631..f4102d2 100755\n--- a/contrib/long-running-filter/example.pl\n+++ b/contrib/long-running-filter/example.pl\n@@ -3,6 +3,9 @@\n # Example implementation for the Git filter protocol version 2\n # See Documentation/gitattributes.txt, section \"Filter Protocol\"\n #\n+# Please note, this pass-thru filter is a minimal skeleton. No proper\n+# error handling was implemented.\n+#\n\n use strict;\n use warnings;\n@@ -18,7 +21,7 @@ sub packet_bin_read {\n    exit();\n  }\n  elsif ( $bytes_read != 4 ) {\n-        die \"invalid packet size '$bytes_read' field\";\n+   die \"invalid packet: '$buffer'\";\n  }\n  my $pkt_size = hex($buffer);\n  if ( $pkt_size == 0 ) {\n@@ -28,27 +31,27 @@ sub packet_bin_read {\n    my $content_size = $pkt_size - 4;\n    $bytes_read = read STDIN, $buffer, $content_size;\n    if ( $bytes_read != $content_size ) {\n-            die \"invalid packet ($content_size expected; $bytes_read read)\";\n+     die \"invalid packet ($content_size bytes expected; $bytes_read bytes read)\";\n    }\n    return ( 0, $buffer );\n  }\n  else {\n-        die \"invalid packet size\";\n+   die \"invalid packet size: $pkt_size\";\n  }\n }\n\n sub packet_txt_read {\n  my ( $res, $buf ) = packet_bin_read();\n-    unless ( $buf =~ /\\n$/ ) {\n-        die \"A non-binary line SHOULD BE terminated by an LF.\";\n+ unless ( $buf =~ s/\\n$// ) {\n+   die \"A non-binary line MUST be terminated by an LF.\";\n  }\n-    return ( $res, substr( $buf, 0, -1 ) );\n+ return ( $res, $buf );\n }\n\n sub packet_bin_write {\n-    my ($packet) = @_;\n-    print STDOUT sprintf( \"%04x\", length($packet) + 4 );\n-    print STDOUT $packet;\n+ my $buf = shift;\n+ print STDOUT sprintf( \"%04x\", length($buf) + 4 );\n+ print STDOUT $buf;\n  STDOUT->flush();\n }\n\n@@ -119,5 +122,6 @@ while (1) {\n    }\n  }\n  packet_flush();    # flush content!\n-    packet_flush();    # empty list!\n+ packet_flush();    # empty list, keep \"status=success\" unchanged!\n+\n }\ndiff --git a/convert.c b/convert.c\nindex bd66257..88581d6 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -541,7 +541,7 @@ static int packet_write_list(int fd, const char *line, ...)\n  for (;;) {\n    if (!line)\n      break;\n-   if (strlen(line) > PKTLINE_DATA_MAXLEN)\n+   if (strlen(line) > LARGE_PACKET_DATA_MAX)\n      return -1;\n    err = packet_write_fmt_gently(fd, \"%s\\n\", line);\n    if (err)\n@@ -573,6 +573,7 @@ static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, cons\n  process->use_shell = 1;\n  process->in = -1;\n  process->out = -1;\n+ process->wait_on_exit = 1;\n\n  if (start_command(process)) {\n    error(\"cannot fork to run external filter '%s'\", cmd);\n@@ -588,7 +589,7 @@ static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, cons\n\n  err = strcmp(packet_read_line(process->out, NULL), \"git-filter-server\");\n  if (err) {\n-   error(\"external filter '%s' does not support long running filter protocol\", cmd);\n+   error(\"external filter '%s' does not support filter protocol version 2\", cmd);\n    goto done;\n  }\n  err = strcmp(packet_read_line(process->out, NULL), \"version=2\");\n@@ -634,7 +635,7 @@ static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, cons\n  return entry;\n }\n\n-static void read_multi_file_filter_values(int fd, struct strbuf *status) {\n+static void read_multi_file_filter_status(int fd, struct strbuf *status) {\n  struct strbuf **pair;\n  char *line;\n  for (;;) {\n@@ -648,6 +649,7 @@ static void read_multi_file_filter_values(int fd, struct strbuf *status) {\n        strbuf_addbuf(status, pair[1]);\n      }\n    }\n+   strbuf_list_free(pair);\n  }\n }\n\n@@ -698,17 +700,16 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len\n\n  sigchain_push(SIGPIPE, SIG_IGN);\n\n- err = strlen(filter_type) > PKTLINE_DATA_MAXLEN;\n- if (err)\n-   goto done;\n-\n+ assert(strlen(filter_type) < LARGE_PACKET_DATA_MAX - strlen(\"command=\\n\"));\n  err = packet_write_fmt_gently(process->in, \"command=%s\\n\", filter_type);\n  if (err)\n    goto done;\n\n- err = strlen(path) > PKTLINE_DATA_MAXLEN;\n- if (err)\n+ err = strlen(path) > LARGE_PACKET_DATA_MAX - strlen(\"pathname=\\n\");\n+ if (err) {\n+   error(\"path name too long for external filter\");\n    goto done;\n+ }\n\n  err = packet_write_fmt_gently(process->in, \"pathname=%s\\n\", path);\n  if (err)\n@@ -725,16 +726,16 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len\n  if (err)\n    goto done;\n\n- read_multi_file_filter_values(process->out, &filter_status);\n+ read_multi_file_filter_status(process->out, &filter_status);\n  err = strcmp(filter_status.buf, \"success\");\n  if (err)\n    goto done;\n\n- err = read_packetized_to_buf(process->out, &nbuf) < 0;\n+ err = read_packetized_to_strbuf(process->out, &nbuf) < 0;\n  if (err)\n    goto done;\n\n- read_multi_file_filter_values(process->out, &filter_status);\n+ read_multi_file_filter_status(process->out, &filter_status);\n  err = strcmp(filter_status.buf, \"success\");\n\n done:\n@@ -753,7 +754,7 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len\n    } else {\n      /*\n       * Something went wrong with the protocol filter.\n-      * Force shutdown and restart if another blob requires filtering!\n+      * Force shutdown and restart if another blob requires filtering.\n       */\n      error(\"external filter '%s' failed\", cmd);\n      kill_multi_file_filter(&cmd_process_map, entry);\n@@ -786,9 +787,9 @@ static int apply_filter(const char *path, const char *src, size_t len,\n  if (!dst)\n    return 1;\n\n- if (!drv->process && (CAP_CLEAN & wanted_capability) && drv->clean)\n+ if ((CAP_CLEAN & wanted_capability) && !drv->process && drv->clean)\n    cmd = drv->clean;\n- else if (!drv->process && (CAP_SMUDGE & wanted_capability) && drv->smudge)\n+ else if ((CAP_SMUDGE & wanted_capability) && !drv->process && drv->smudge)\n    cmd = drv->smudge;\n\n  if (cmd && *cmd)\ndiff --git a/pkt-line.c b/pkt-line.c\nindex a0a8543..b82aaca 100644\n--- a/pkt-line.c\n+++ b/pkt-line.c\n@@ -137,7 +137,7 @@ static int packet_write_fmt_1(int fd, int gently,\n            const char *fmt, va_list args)\n {\n  struct strbuf buf = STRBUF_INIT;\n- size_t count;\n+ ssize_t count;\n\n  format_packet(&buf, fmt, args);\n  count = write_in_full(fd, buf.buf, buf.len);\n@@ -174,15 +174,15 @@ int packet_write_fmt_gently(int fd, const char *fmt, ...)\n static int packet_write_gently(const int fd_out, const char *buf, size_t size)\n {\n  static char packet_write_buffer[LARGE_PACKET_MAX];\n+ const size_t packet_size = size + 4;\n\n- if (size > sizeof(packet_write_buffer) - 4) {\n+ if (packet_size > sizeof(packet_write_buffer))\n    return error(\"packet write failed - data exceeds max packet size\");\n- }\n+\n  packet_trace(buf, size, 1);\n- size += 4;\n- set_packet_header(packet_write_buffer, size);\n- memcpy(packet_write_buffer + 4, buf, size - 4);\n- if (write_in_full(fd_out, packet_write_buffer, size) == size)\n+ set_packet_header(packet_write_buffer, packet_size);\n+ memcpy(packet_write_buffer + 4, buf, size);\n+ if (write_in_full(fd_out, packet_write_buffer, packet_size) == packet_size)\n    return 0;\n  return error(\"packet write failed\");\n }\n@@ -198,7 +198,7 @@ void packet_buf_write(struct strbuf *buf, const char *fmt, ...)\n\n int write_packetized_from_fd(int fd_in, int fd_out)\n {\n- static char buf[PKTLINE_DATA_MAXLEN];\n+ static char buf[LARGE_PACKET_DATA_MAX];\n  int err = 0;\n  ssize_t bytes_to_write;\n\n@@ -217,7 +217,7 @@ int write_packetized_from_fd(int fd_in, int fd_out)\n\n int write_packetized_from_buf(const char *src_in, size_t len, int fd_out)\n {\n- static char buf[PKTLINE_DATA_MAXLEN];\n+ static char buf[LARGE_PACKET_DATA_MAX];\n  int err = 0;\n  size_t bytes_written = 0;\n  size_t bytes_to_write;\n@@ -347,29 +347,34 @@ char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)\n  return packet_read_line_generic(-1, src, src_len, dst_len);\n }\n\n-ssize_t read_packetized_to_buf(int fd_in, struct strbuf *sb_out)\n+ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out)\n {\n- int paket_len;\n- int options = PACKET_READ_GENTLE_ON_EOF;\n+ int packet_len;\n\n- size_t oldlen = sb_out->len;\n- size_t oldalloc = sb_out->alloc;\n+ size_t orig_len = sb_out->len;\n+ size_t orig_alloc = sb_out->alloc;\n\n  for (;;) {\n-   strbuf_grow(sb_out, PKTLINE_DATA_MAXLEN+1);\n-   paket_len = packet_read(fd_in, NULL, NULL,\n-     sb_out->buf + sb_out->len, PKTLINE_DATA_MAXLEN+1, options);\n-   if (paket_len <= 0)\n+   strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);\n+   packet_len = packet_read(fd_in, NULL, NULL,\n+     /* strbuf_grow() above always allocates one extra byte to\n+      * store a '\\0' at the end of the string. packet_read()\n+      * writes a '\\0' extra byte at the end, too. Let it know\n+      * that there is already room for the extra byte.\n+      */\n+     sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,\n+     PACKET_READ_GENTLE_ON_EOF);\n+   if (packet_len <= 0)\n      break;\n-   sb_out->len += paket_len;\n+   sb_out->len += packet_len;\n  }\n\n- if (paket_len < 0) {\n-   if (oldalloc == 0)\n+ if (packet_len < 0) {\n+   if (orig_alloc == 0)\n      strbuf_release(sb_out);\n    else\n-     strbuf_setlen(sb_out, oldlen);\n-   return paket_len;\n+     strbuf_setlen(sb_out, orig_len);\n+   return packet_len;\n  }\n- return sb_out->len - oldlen;\n+ return sb_out->len - orig_len;\n }\ndiff --git a/pkt-line.h b/pkt-line.h\nindex 3d873f3..18eac64 100644\n--- a/pkt-line.h\n+++ b/pkt-line.h\n@@ -82,11 +82,11 @@ char *packet_read_line_buf(char **src_buf, size_t *src_len, int *size);\n /*\n  * Reads a stream of variable sized packets until a flush packet is detected.\n  */\n-ssize_t read_packetized_to_buf(int fd_in, struct strbuf *sb_out);\n+ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out);\n\n #define DEFAULT_PACKET_MAX 1000\n #define LARGE_PACKET_MAX 65520\n-#define PKTLINE_DATA_MAXLEN (LARGE_PACKET_MAX - 4)\n+#define LARGE_PACKET_DATA_MAX (LARGE_PACKET_MAX - 4)\n extern char packet_buffer[LARGE_PACKET_MAX];\n\n #endif\ndiff --git a/run-command.c b/run-command.c\nindex b72f6d1..96c54fe 100644\n--- a/run-command.c\n+++ b/run-command.c\n@@ -6,19 +6,6 @@\n #include \"thread-utils.h\"\n #include \"strbuf.h\"\n\n-void check_pipe(int err)\n-{\n- if (err == EPIPE) {\n-   if (in_async())\n-     async_exit(141);\n-\n-   signal(SIGPIPE, SIG_DFL);\n-   raise(SIGPIPE);\n-   /* Should never happen, but just in case... */\n-   exit(141);\n- }\n-}\n-\n void child_process_init(struct child_process *child)\n {\n  memset(child, 0, sizeof(*child));\n@@ -34,6 +21,9 @@ void child_process_clear(struct child_process *child)\n\n struct child_to_clean {\n  pid_t pid;\n+ char *name;\n+ int stdin;\n+ int wait;\n  struct child_to_clean *next;\n };\n static struct child_to_clean *children_to_clean;\n@@ -41,14 +31,35 @@ static int installed_child_cleanup_handler;\n\n static void cleanup_children(int sig, int in_signal)\n {\n- while (children_to_clean) {\n+ int status;\n  struct child_to_clean *p = children_to_clean;\n+\n+ /* Close the the child's stdin as indicator that Git will exit soon */\n+ while (p) {\n+   if (p->wait)\n+     if (p->stdin > 0)\n+       close(p->stdin);\n+   p = p->next;\n+ }\n+\n+ while (children_to_clean) {\n+   p = children_to_clean;\n    children_to_clean = p->next;\n+\n+   if (p->wait) {\n+     fprintf(stderr, _(\"Waiting for '%s' to finish...\"), p->name);\n+     while ((waitpid(p->pid, &status, 0)) < 0 && errno == EINTR)\n+       ; /* nothing */\n+     fprintf(stderr, _(\"done!\\n\"));\n+   }\n+\n    kill(p->pid, sig);\n-   if (!in_signal)\n+   if (!in_signal) {\n+     free(p->name);\n      free(p);\n    }\n  }\n+}\n\n static void cleanup_children_on_signal(int sig)\n {\n@@ -62,10 +73,16 @@ static void cleanup_children_on_exit(void)\n  cleanup_children(SIGTERM, 0);\n }\n\n-static void mark_child_for_cleanup(pid_t pid)\n+static void mark_child_for_cleanup(pid_t pid, const char *name, int stdin, int wait)\n {\n  struct child_to_clean *p = xmalloc(sizeof(*p));\n  p->pid = pid;\n+ p->wait = wait;\n+ p->stdin = stdin;\n+ if (name)\n+   p->name = xstrdup(name);\n+ else\n+   p->name = \"process\";\n  p->next = children_to_clean;\n  children_to_clean = p;\n\n@@ -76,6 +93,13 @@ static void mark_child_for_cleanup(pid_t pid)\n  }\n }\n\n+#ifdef NO_PTHREADS\n+static void mark_child_for_cleanup_no_wait(pid_t pid, const char *name, int timeout, int stdin)\n+{\n+ mark_child_for_cleanup(pid, NULL, 0, 0);\n+}\n+#endif\n+\n static void clear_child_for_cleanup(pid_t pid)\n {\n  struct child_to_clean **pp;\n@@ -434,8 +458,9 @@ int start_command(struct child_process *cmd)\n  }\n  if (cmd->pid < 0)\n    error_errno(\"cannot fork() for %s\", cmd->argv[0]);\n- else if (cmd->clean_on_exit)\n-   mark_child_for_cleanup(cmd->pid);\n+ else if (cmd->clean_on_exit || cmd->wait_on_exit)\n+   mark_child_for_cleanup(\n+     cmd->pid, cmd->argv[0], cmd->in, cmd->wait_on_exit);\n\n  /*\n   * Wait for child's execvp. If the execvp succeeds (or if fork()\n@@ -495,8 +520,9 @@ int start_command(struct child_process *cmd)\n  failed_errno = errno;\n  if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))\n    error_errno(\"cannot spawn %s\", cmd->argv[0]);\n- if (cmd->clean_on_exit && cmd->pid >= 0)\n-   mark_child_for_cleanup(cmd->pid);\n+ if ((cmd->clean_on_exit || cmd->wait_on_exit) && cmd->pid >= 0)\n+   mark_child_for_cleanup(\n+     cmd->pid, cmd->argv[0], cmd->in, cmd->clean_on_exit_timeout);\n\n  argv_array_clear(&nargv);\n  cmd->argv = sargv;\n@@ -647,7 +673,7 @@ int in_async(void)\n  return !pthread_equal(main_thread, pthread_self());\n }\n\n-void NORETURN async_exit(int code)\n+static void NORETURN async_exit(int code)\n {\n  pthread_exit((void *)(intptr_t)code);\n }\n@@ -697,13 +723,26 @@ int in_async(void)\n  return process_is_async;\n }\n\n-void NORETURN async_exit(int code)\n+static void NORETURN async_exit(int code)\n {\n  exit(code);\n }\n\n #endif\n\n+void check_pipe(int err)\n+{\n+ if (err == EPIPE) {\n+   if (in_async())\n+     async_exit(141);\n+\n+   signal(SIGPIPE, SIG_DFL);\n+   raise(SIGPIPE);\n+   /* Should never happen, but just in case... */\n+   exit(141);\n+ }\n+}\n+\n int start_async(struct async *async)\n {\n  int need_in, need_out;\n@@ -765,7 +804,7 @@ int start_async(struct async *async)\n    exit(!!async->proc(proc_in, proc_out, async->data));\n  }\n\n- mark_child_for_cleanup(async->pid);\n+ mark_child_for_cleanup_no_wait(async->pid);\n\n  if (need_in)\n    close(fdin[0]);\ndiff --git a/run-command.h b/run-command.h\nindex e7c5f71..f7b9907 100644\n--- a/run-command.h\n+++ b/run-command.h\n@@ -42,7 +42,10 @@ struct child_process {\n  unsigned silent_exec_failure:1;\n  unsigned stdout_to_stderr:1;\n  unsigned use_shell:1;\n+  /* kill the child on Git exit */\n  unsigned clean_on_exit:1;\n+ /* close the child's stdin on Git exit and wait until it terminates */\n+ unsigned wait_on_exit:1;\n };\n\n #define CHILD_PROCESS_INIT { NULL, ARGV_ARRAY_INIT, ARGV_ARRAY_INIT }\n@@ -54,8 +57,6 @@ int finish_command(struct child_process *);\n int finish_command_in_signal(struct child_process *);\n int run_command(struct child_process *);\n\n-void check_pipe(int err);\n-\n /*\n  * Returns the path to the hook file, or NULL if the hook is missing\n  * or disabled. Note that this points to static storage that will be\n@@ -141,7 +142,7 @@ struct async {\n int start_async(struct async *async);\n int finish_async(struct async *async);\n int in_async(void);\n-void NORETURN async_exit(int code);\n+void check_pipe(int err);\n\n /**\n  * This callback should initialize the child process and preload the\ndiff --git a/t/t0021-conversion.sh b/t/t0021-conversion.sh\nindex 210c4f6..52b7fe9 100755\n--- a/t/t0021-conversion.sh\n+++ b/t/t0021-conversion.sh\n@@ -4,13 +4,77 @@ test_description='blob conversion via gitattributes'\n\n . ./test-lib.sh\n\n-cat <<EOF >rot13.sh\n+TEST_ROOT=\"$(pwd)\"\n+\n+cat <<EOF >\"$TEST_ROOT/rot13.sh\"\n #!$SHELL_PATH\n tr \\\n   'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' \\\n   'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'\n EOF\n-chmod +x rot13.sh\n+chmod +x \"$TEST_ROOT/rot13.sh\"\n+\n+generate_random_characters () {\n+ LEN=$1\n+ NAME=$2\n+ test-genrandom some-seed $LEN |\n+   perl -pe \"s/./chr((ord($&) % 26) + ord('a'))/sge\" >\"$TEST_ROOT/$NAME\"\n+}\n+\n+file_size () {\n+ cat \"$1\" | wc -c | sed \"s/^[ ]*//\"\n+}\n+\n+filter_git () {\n+ rm -f rot13-filter.log &&\n+ git \"$@\" 2>git-stderr.log &&\n+ sed '/Waiting for/d' git-stderr.log >git-stderr-clean.log &&\n+ test_must_be_empty git-stderr-clean.log &&\n+ rm -f git-stderr.log git-stderr-clean.log\n+}\n+\n+# Count unique lines in two files and compare them.\n+test_cmp_count () {\n+ for FILE in $@\n+ do\n+   sort $FILE | uniq -c | sed \"s/^[ ]*//\" >$FILE.tmp\n+   cat $FILE.tmp >$FILE\n+ done &&\n+ test_cmp $@\n+}\n+\n+# Count unique lines except clean invocations in two files and compare\n+# them. Clean invocations are not counted because their number can vary.\n+# c.f. http://public-inbox.org/git/xmqqshv18i8i.fsf@gitster.mtv.corp.google.com/\n+test_cmp_count_except_clean () {\n+ for FILE in $@\n+ do\n+   sort $FILE | uniq -c | sed \"s/^[ ]*//\" |\n+     sed \"s/^\\([0-9]\\) IN: clean/x IN: clean/\" >$FILE.tmp\n+   cat $FILE.tmp >$FILE\n+ done &&\n+ test_cmp $@\n+}\n+\n+# Compare two files but exclude clean invocations because they can vary.\n+# c.f. http://public-inbox.org/git/xmqqshv18i8i.fsf@gitster.mtv.corp.google.com/\n+test_cmp_exclude_clean () {\n+ for FILE in $@\n+ do\n+   grep -v \"IN: clean\" $FILE >$FILE.tmp\n+   cat $FILE.tmp >$FILE\n+ done &&\n+ test_cmp $@\n+}\n+\n+# Check that the contents of two files are equal and that their rot13 version\n+# is equal to the committed content.\n+test_cmp_committed_rot13 () {\n+ test_cmp \"$1\" \"$2\" &&\n+ \"$TEST_ROOT/rot13.sh\" <\"$1\" >expected &&\n+ git cat-file blob :\"$2\" >actual &&\n+ test_cmp expected actual\n+}\n\n test_expect_success setup '\n  git config filter.rot13.smudge ./rot13.sh &&\n@@ -34,7 +98,7 @@ test_expect_success setup '\n  git checkout -- test test.t test.i &&\n\n  echo \"content-test2\" >test2.o &&\n- echo \"content-test3 - subdir\" >\"test3 - subdir.o\"\n+ echo \"content-test3 - filename with special characters\" >\"test3 '\\''sq'\\'',\\$x.o\"\n '\n\n script='s/^\\$Id: \\([0-9a-f]*\\) \\$/\\1/p'\n@@ -282,47 +346,6 @@ test_expect_success 'diff does not reuse worktree files that need cleaning' '\n  test_line_count = 0 count\n '\n\n-check_filter () {\n- rm -f rot13-filter.log actual.log &&\n- \"$@\" 2> git_stderr.log &&\n- test_must_be_empty git_stderr.log &&\n- cat >expected.log &&\n- sort rot13-filter.log | uniq -c | sed \"s/^[ ]*//\" >actual.log &&\n- test_cmp expected.log actual.log\n-}\n-\n-check_filter_count_clean () {\n- rm -f rot13-filter.log actual.log &&\n- \"$@\" 2> git_stderr.log &&\n- test_must_be_empty git_stderr.log &&\n- cat >expected.log &&\n- sort rot13-filter.log | uniq -c | sed \"s/^[ ]*//\" |\n-   sed \"s/^\\([0-9]\\) IN: clean/x IN: clean/\" >actual.log &&\n- test_cmp expected.log actual.log\n-}\n-\n-check_filter_ignore_clean () {\n- rm -f rot13-filter.log actual.log &&\n- \"$@\" &&\n- cat >expected.log &&\n- grep -v \"IN: clean\" rot13-filter.log >actual.log &&\n- test_cmp expected.log actual.log\n-}\n-\n-check_filter_no_call () {\n- rm -f rot13-filter.log &&\n- \"$@\" 2> git_stderr.log &&\n- test_must_be_empty git_stderr.log &&\n- test_must_be_empty rot13-filter.log\n-}\n-\n-check_rot13 () {\n- test_cmp \"$1\" \"$2\" &&\n- ./../rot13.sh <\"$1\" >expected &&\n- git cat-file blob :\"$2\" >actual &&\n- test_cmp expected actual\n-}\n-\n test_expect_success PERL 'required process filter should filter data' '\n  test_config_global filter.protocol.process \"$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge\" &&\n  test_config_global filter.protocol.required true &&\n@@ -332,81 +355,91 @@ test_expect_success PERL 'required process filter should filter data' '\n    cd repo &&\n    git init &&\n\n+   echo \"git-stderr.log\" >.gitignore &&\n    echo \"*.r filter=protocol\" >.gitattributes &&\n    git add . &&\n-   git commit . -m \"test commit\" &&\n-   git branch empty &&\n+   git commit . -m \"test commit 1\" &&\n+   git branch empty-branch &&\n\n-   cp ../test.o test.r &&\n-   cp ../test2.o test2.r &&\n+   cp \"$TEST_ROOT/test.o\" test.r &&\n+   cp \"$TEST_ROOT/test2.o\" test2.r &&\n    mkdir testsubdir &&\n-   cp \"../test3 - subdir.o\" \"testsubdir/test3 - subdir.r\" &&\n+   cp \"$TEST_ROOT/test3 '\\''sq'\\'',\\$x.o\" \"testsubdir/test3 '\\''sq'\\'',\\$x.r\" &&\n    >test4-empty.r &&\n\n-   check_filter \\\n-     git add . \\\n-       <<-\\EOF &&\n-         1 IN: clean test.r 57 [OK] -- OUT: 57 . [OK]\n-         1 IN: clean test2.r 14 [OK] -- OUT: 14 . [OK]\n-         1 IN: clean test4-empty.r 0 [OK] -- OUT: 0  [OK]\n-         1 IN: clean testsubdir/test3 - subdir.r 23 [OK] -- OUT: 23 . [OK]\n-         1 START\n-         1 STOP\n-         1 wrote filter header\n+   S=$(file_size test.r) &&\n+   S2=$(file_size test2.r) &&\n+   S3=$(file_size \"testsubdir/test3 '\\''sq'\\'',\\$x.r\") &&\n+\n+   filter_git add . &&\n+   cat >expected.log <<-EOF &&\n+     START\n+     init handshake complete\n+     IN: clean test.r $S [OK] -- OUT: $S . [OK]\n+     IN: clean test2.r $S2 [OK] -- OUT: $S2 . [OK]\n+     IN: clean test4-empty.r 0 [OK] -- OUT: 0  [OK]\n+     IN: clean testsubdir/test3 '\\''sq'\\'',\\$x.r $S3 [OK] -- OUT: $S3 . [OK]\n+     STOP\n    EOF\n+   test_cmp_count expected.log rot13-filter.log &&\n\n-   check_filter_count_clean \\\n-     git commit . -m \"test commit\" \\\n-       <<-\\EOF &&\n-         x IN: clean test.r 57 [OK] -- OUT: 57 . [OK]\n-         x IN: clean test2.r 14 [OK] -- OUT: 14 . [OK]\n-         x IN: clean test4-empty.r 0 [OK] -- OUT: 0  [OK]\n-         x IN: clean testsubdir/test3 - subdir.r 23 [OK] -- OUT: 23 . [OK]\n-         1 START\n-         1 STOP\n-         1 wrote filter header\n+   filter_git commit . -m \"test commit 2\" &&\n+   cat >expected.log <<-EOF &&\n+     START\n+     init handshake complete\n+     IN: clean test.r $S [OK] -- OUT: $S . [OK]\n+     IN: clean test2.r $S2 [OK] -- OUT: $S2 . [OK]\n+     IN: clean test4-empty.r 0 [OK] -- OUT: 0  [OK]\n+     IN: clean testsubdir/test3 '\\''sq'\\'',\\$x.r $S3 [OK] -- OUT: $S3 . [OK]\n+     IN: clean test.r $S [OK] -- OUT: $S . [OK]\n+     IN: clean test2.r $S2 [OK] -- OUT: $S2 . [OK]\n+     IN: clean test4-empty.r 0 [OK] -- OUT: 0  [OK]\n+     IN: clean testsubdir/test3 '\\''sq'\\'',\\$x.r $S3 [OK] -- OUT: $S3 . [OK]\n+     STOP\n    EOF\n+   test_cmp_count_except_clean expected.log rot13-filter.log &&\n\n-   rm -f test?.r \"testsubdir/test3 - subdir.r\" &&\n+   rm -f test2.r \"testsubdir/test3 '\\''sq'\\'',\\$x.r\" &&\n\n-   check_filter_ignore_clean \\\n-     git checkout . \\\n-       <<-\\EOF &&\n+   filter_git checkout --quiet --no-progress . &&\n+   cat >expected.log <<-EOF &&\n      START\n-         wrote filter header\n-         IN: smudge test2.r 14 [OK] -- OUT: 14 . [OK]\n-         IN: smudge testsubdir/test3 - subdir.r 23 [OK] -- OUT: 23 . [OK]\n+     init handshake complete\n+     IN: smudge test2.r $S2 [OK] -- OUT: $S2 . [OK]\n+     IN: smudge testsubdir/test3 '\\''sq'\\'',\\$x.r $S3 [OK] -- OUT: $S3 . [OK]\n      STOP\n    EOF\n+   test_cmp_exclude_clean expected.log rot13-filter.log &&\n\n-   check_filter_ignore_clean \\\n-     git checkout empty \\\n-       <<-\\EOF &&\n+   filter_git checkout --quiet --no-progress empty-branch &&\n+   cat >expected.log <<-EOF &&\n      START\n-         wrote filter header\n+     init handshake complete\n+     IN: clean test.r $S [OK] -- OUT: $S . [OK]\n      STOP\n    EOF\n+   test_cmp_exclude_clean expected.log rot13-filter.log &&\n\n-   check_filter_ignore_clean \\\n-     git checkout master \\\n-       <<-\\EOF &&\n+   filter_git checkout --quiet --no-progress master &&\n+   cat >expected.log <<-EOF &&\n      START\n-         wrote filter header\n-         IN: smudge test.r 57 [OK] -- OUT: 57 . [OK]\n-         IN: smudge test2.r 14 [OK] -- OUT: 14 . [OK]\n+     init handshake complete\n+     IN: smudge test.r $S [OK] -- OUT: $S . [OK]\n+     IN: smudge test2.r $S2 [OK] -- OUT: $S2 . [OK]\n      IN: smudge test4-empty.r 0 [OK] -- OUT: 0  [OK]\n-         IN: smudge testsubdir/test3 - subdir.r 23 [OK] -- OUT: 23 . [OK]\n+     IN: smudge testsubdir/test3 '\\''sq'\\'',\\$x.r $S3 [OK] -- OUT: $S3 . [OK]\n      STOP\n    EOF\n+   test_cmp_exclude_clean expected.log rot13-filter.log &&\n\n-   check_rot13 ../test.o test.r &&\n-   check_rot13 ../test2.o test2.r &&\n-   check_rot13 \"../test3 - subdir.o\" \"testsubdir/test3 - subdir.r\"\n+   test_cmp_committed_rot13 \"$TEST_ROOT/test.o\" test.r &&\n+   test_cmp_committed_rot13 \"$TEST_ROOT/test2.o\" test2.r &&\n+   test_cmp_committed_rot13 \"$TEST_ROOT/test3 '\\''sq'\\'',\\$x.o\" \"testsubdir/test3 '\\''sq'\\'',\\$x.r\"\n  )\n '\n\n-test_expect_success PERL 'required process filter should clean only and take precedence' '\n- test_config_global filter.protocol.clean ./../rot13.sh &&\n+test_expect_success PERL 'required process filter takes precedence' '\n+ test_config_global filter.protocol.clean false &&\n  test_config_global filter.protocol.process \"$TEST_DIRECTORY/t0021/rot13-filter.pl clean\" &&\n  test_config_global filter.protocol.required true &&\n  rm -rf repo &&\n@@ -416,40 +449,55 @@ test_expect_success PERL 'required process filter should clean only and take pre\n    git init &&\n\n    echo \"*.r filter=protocol\" >.gitattributes &&\n-   git add . &&\n-   git commit . -m \"test commit\" &&\n-   git branch empty &&\n-\n-   cp ../test.o test.r &&\n-\n-   check_filter \\\n-     git add . \\\n-       <<-\\EOF &&\n-         1 IN: clean test.r 57 [OK] -- OUT: 57 . [OK]\n-         1 START\n-         1 STOP\n-         1 wrote filter header\n-       EOF\n+   cp \"$TEST_ROOT/test.o\" test.r &&\n+   S=$(file_size test.r) &&\n\n-   check_filter_count_clean \\\n-     git commit . -m \"test commit\" \\\n-       <<-\\EOF\n-         x IN: clean test.r 57 [OK] -- OUT: 57 . [OK]\n-         1 START\n-         1 STOP\n-         1 wrote filter header\n+   # Check that the process filter is invoked here\n+   filter_git add . &&\n+   cat >expected.log <<-EOF &&\n+     START\n+     init handshake complete\n+     IN: clean test.r $S [OK] -- OUT: $S . [OK]\n+     STOP\n    EOF\n+   test_cmp_count expected.log rot13-filter.log\n  )\n '\n\n-generate_test_data () {\n- LEN=$1\n- NAME=$2\n- test-genrandom end $LEN |\n-   perl -pe \"s/./chr((ord($&) % 26) + 97)/sge\" >../$NAME.file &&\n- cp ../$NAME.file . &&\n- ./../rot13.sh <../$NAME.file >../$NAME.file.rot13\n-}\n+test_expect_success PERL 'required process filter should be used only for \"clean\" operation only' '\n+ test_config_global filter.protocol.process \"$TEST_DIRECTORY/t0021/rot13-filter.pl clean\" &&\n+ rm -rf repo &&\n+ mkdir repo &&\n+ (\n+   cd repo &&\n+   git init &&\n+\n+   echo \"*.r filter=protocol\" >.gitattributes &&\n+   cp \"$TEST_ROOT/test.o\" test.r &&\n+   S=$(file_size test.r) &&\n+\n+   filter_git add . &&\n+   cat >expected.log <<-EOF &&\n+     START\n+     init handshake complete\n+     IN: clean test.r $S [OK] -- OUT: $S . [OK]\n+     STOP\n+   EOF\n+   test_cmp_count expected.log rot13-filter.log &&\n+\n+   rm test.r &&\n+\n+   filter_git checkout --quiet --no-progress . &&\n+   # If the filter would be used for \"smudge\", too, we would see\n+   # \"IN: smudge test.r 57 [OK] -- OUT: 57 . [OK]\" here\n+   cat >expected.log <<-EOF &&\n+     START\n+     init handshake complete\n+     STOP\n+   EOF\n+   test_cmp_exclude_clean expected.log rot13-filter.log\n+ )\n+'\n\n test_expect_success PERL 'required process filter should process multiple packets' '\n  test_config_global filter.protocol.process \"$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge\" &&\n@@ -461,42 +509,57 @@ test_expect_success PERL 'required process filter should process multiple packet\n    cd repo &&\n    git init &&\n\n-   # Generate data that requires 3 packets\n-   PKTLINE_DATA_MAXLEN=65516 &&\n+   # Generate data requiring 1, 2, 3 packets\n+   S=65516 && # PKTLINE_DATA_MAXLEN -> Maximal size of a packet\n+   generate_random_characters $(($S    )) 1pkt_1__.file &&\n+   generate_random_characters $(($S  +1)) 2pkt_1+1.file &&\n+   generate_random_characters $(($S*2-1)) 2pkt_2-1.file &&\n+   generate_random_characters $(($S*2  )) 2pkt_2__.file &&\n+   generate_random_characters $(($S*2+1)) 3pkt_2+1.file &&\n\n-   generate_test_data $(($PKTLINE_DATA_MAXLEN        )) 1pkt_1__ &&\n-   generate_test_data $(($PKTLINE_DATA_MAXLEN     + 1)) 2pkt_1+1 &&\n-   generate_test_data $(($PKTLINE_DATA_MAXLEN * 2 - 1)) 2pkt_2-1 &&\n-   generate_test_data $(($PKTLINE_DATA_MAXLEN * 2    )) 2pkt_2__ &&\n-   generate_test_data $(($PKTLINE_DATA_MAXLEN * 2 + 1)) 3pkt_2+1 &&\n+   for FILE in \"$TEST_ROOT\"/*.file\n+   do\n+     cp \"$FILE\" . &&\n+     \"$TEST_ROOT/rot13.sh\" <\"$FILE\" >\"$FILE.rot13\"\n+   done &&\n\n    echo \"*.file filter=protocol\" >.gitattributes &&\n-   check_filter \\\n-     git add *.file .gitattributes \\\n-       <<-\\EOF &&\n-         1 IN: clean 1pkt_1__.file 65516 [OK] -- OUT: 65516 . [OK]\n-         1 IN: clean 2pkt_1+1.file 65517 [OK] -- OUT: 65517 .. [OK]\n-         1 IN: clean 2pkt_2-1.file 131031 [OK] -- OUT: 131031 .. [OK]\n-         1 IN: clean 2pkt_2__.file 131032 [OK] -- OUT: 131032 .. [OK]\n-         1 IN: clean 3pkt_2+1.file 131033 [OK] -- OUT: 131033 ... [OK]\n-         1 START\n-         1 STOP\n-         1 wrote filter header\n+   filter_git add *.file .gitattributes &&\n+   cat >expected.log <<-EOF &&\n+     START\n+     init handshake complete\n+     IN: clean 1pkt_1__.file $(($S    )) [OK] -- OUT: $(($S    )) . [OK]\n+     IN: clean 2pkt_1+1.file $(($S  +1)) [OK] -- OUT: $(($S  +1)) .. [OK]\n+     IN: clean 2pkt_2-1.file $(($S*2-1)) [OK] -- OUT: $(($S*2-1)) .. [OK]\n+     IN: clean 2pkt_2__.file $(($S*2  )) [OK] -- OUT: $(($S*2  )) .. [OK]\n+     IN: clean 3pkt_2+1.file $(($S*2+1)) [OK] -- OUT: $(($S*2+1)) ... [OK]\n+     STOP\n    EOF\n-   git commit . -m \"test commit\" &&\n+   test_cmp_count expected.log rot13-filter.log &&\n\n    rm -f *.file &&\n-   git checkout -- *.file &&\n\n-   for f in *.file\n+   filter_git checkout --quiet --no-progress -- *.file &&\n+   cat >expected.log <<-EOF &&\n+     START\n+     init handshake complete\n+     IN: smudge 1pkt_1__.file $(($S    )) [OK] -- OUT: $(($S    )) . [OK]\n+     IN: smudge 2pkt_1+1.file $(($S  +1)) [OK] -- OUT: $(($S  +1)) .. [OK]\n+     IN: smudge 2pkt_2-1.file $(($S*2-1)) [OK] -- OUT: $(($S*2-1)) .. [OK]\n+     IN: smudge 2pkt_2__.file $(($S*2  )) [OK] -- OUT: $(($S*2  )) .. [OK]\n+     IN: smudge 3pkt_2+1.file $(($S*2+1)) [OK] -- OUT: $(($S*2+1)) ... [OK]\n+     STOP\n+   EOF\n+   test_cmp_exclude_clean expected.log rot13-filter.log &&\n+\n+   for FILE in *.file\n    do\n-     git cat-file blob :$f >actual &&\n-     test_cmp ../$f.rot13 actual\n+     test_cmp_committed_rot13 \"$TEST_ROOT/$FILE\" $FILE\n    done\n  )\n '\n\n-test_expect_success PERL 'required process filter should with clean error should fail' '\n+test_expect_success PERL 'required process filter with clean error should fail' '\n  test_config_global filter.protocol.process \"$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge\" &&\n  test_config_global filter.protocol.required true &&\n  rm -rf repo &&\n@@ -507,11 +570,10 @@ test_expect_success PERL 'required process filter should with clean error should\n\n    echo \"*.r filter=protocol\" >.gitattributes &&\n\n-   cp ../test.o test.r &&\n+   cp \"$TEST_ROOT/test.o\" test.r &&\n    echo \"this is going to fail\" >clean-write-fail.r &&\n    echo \"content-test3-subdir\" >test3.r &&\n\n-   # Note: There are three clean paths in convert.c we just test one here.\n    test_must_fail git add .\n  )\n '\n@@ -526,38 +588,48 @@ test_expect_success PERL 'process filter should restart after unexpected write f\n\n    echo \"*.r filter=protocol\" >.gitattributes &&\n\n-   cp ../test.o test.r &&\n-   cp ../test2.o test2.r &&\n+   cp \"$TEST_ROOT/test.o\" test.r &&\n+   cp \"$TEST_ROOT/test2.o\" test2.r &&\n    echo \"this is going to fail\" >smudge-write-fail.o &&\n-   cat smudge-write-fail.o >smudge-write-fail.r &&\n+   cp smudge-write-fail.o smudge-write-fail.r &&\n+\n+   S=$(file_size test.r) &&\n+   S2=$(file_size test2.r) &&\n+   SF=$(file_size smudge-write-fail.r) &&\n+\n    git add . &&\n-   git commit . -m \"test commit\" &&\n    rm -f *.r &&\n\n-   check_filter_ignore_clean \\\n-     git checkout . \\\n-       <<-\\EOF &&\n+   rm -f rot13-filter.log &&\n+   git checkout --quiet --no-progress . 2>git-stderr.log &&\n+\n+   grep \"smudge write error at\" git-stderr.log &&\n+   grep \"error: external filter\" git-stderr.log &&\n+\n+   cat >expected.log <<-EOF &&\n      START\n-         wrote filter header\n-         IN: smudge smudge-write-fail.r 22 [OK] -- OUT: 22 [WRITE FAIL]\n+     init handshake complete\n+     IN: smudge smudge-write-fail.r $SF [OK] -- OUT: $SF [WRITE FAIL]\n      START\n-         wrote filter header\n-         IN: smudge test.r 57 [OK] -- OUT: 57 . [OK]\n-         IN: smudge test2.r 14 [OK] -- OUT: 14 . [OK]\n+     init handshake complete\n+     IN: smudge test.r $S [OK] -- OUT: $S . [OK]\n+     IN: smudge test2.r $S2 [OK] -- OUT: $S2 . [OK]\n      STOP\n    EOF\n+   test_cmp_exclude_clean expected.log rot13-filter.log &&\n\n-   check_rot13 ../test.o test.r &&\n-   check_rot13 ../test2.o test2.r &&\n+   test_cmp_committed_rot13 \"$TEST_ROOT/test.o\" test.r &&\n+   test_cmp_committed_rot13 \"$TEST_ROOT/test2.o\" test2.r &&\n\n-   ! test_cmp smudge-write-fail.o smudge-write-fail.r && # Smudge failed!\n-   ./../rot13.sh <smudge-write-fail.o >expected &&\n+   # Smudge failed\n+   ! test_cmp smudge-write-fail.o smudge-write-fail.r &&\n+   \"$TEST_ROOT/rot13.sh\" <smudge-write-fail.o >expected &&\n    git cat-file blob :smudge-write-fail.r >actual &&\n-   test_cmp expected actual                # Clean worked!\n+   test_cmp expected actual\n  )\n '\n\n-test_expect_success PERL 'process filter should not restart in case of an error' '\n+test_expect_success PERL 'process filter should not be restarted if it signals an error' '\n  test_config_global filter.protocol.process \"$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge\" &&\n  rm -rf repo &&\n  mkdir repo &&\n@@ -567,32 +639,36 @@ test_expect_success PERL 'process filter should not restart in case of an error'\n\n    echo \"*.r filter=protocol\" >.gitattributes &&\n\n-   cp ../test.o test.r &&\n-   cp ../test2.o test2.r &&\n+   cp \"$TEST_ROOT/test.o\" test.r &&\n+   cp \"$TEST_ROOT/test2.o\" test2.r &&\n    echo \"this will cause an error\" >error.o &&\n    cp error.o error.r &&\n+\n+   S=$(file_size test.r) &&\n+   S2=$(file_size test2.r) &&\n+   SE=$(file_size error.r) &&\n+\n    git add . &&\n-   git commit . -m \"test commit\" &&\n    rm -f *.r &&\n\n-   check_filter_ignore_clean \\\n-     git checkout . \\\n-       <<-\\EOF &&\n+   filter_git checkout --quiet --no-progress . &&\n+   cat >expected.log <<-EOF &&\n      START\n-         wrote filter header\n-         IN: smudge error.r 25 [OK] -- OUT: 0 [ERROR]\n-         IN: smudge test.r 57 [OK] -- OUT: 57 . [OK]\n-         IN: smudge test2.r 14 [OK] -- OUT: 14 . [OK]\n+     init handshake complete\n+     IN: smudge error.r $SE [OK] -- OUT: 0 [ERROR]\n+     IN: smudge test.r $S [OK] -- OUT: $S . [OK]\n+     IN: smudge test2.r $S2 [OK] -- OUT: $S2 . [OK]\n      STOP\n    EOF\n+   test_cmp_exclude_clean expected.log rot13-filter.log &&\n\n-   check_rot13 ../test.o test.r &&\n-   check_rot13 ../test2.o test2.r &&\n+   test_cmp_committed_rot13 \"$TEST_ROOT/test.o\" test.r &&\n+   test_cmp_committed_rot13 \"$TEST_ROOT/test2.o\" test2.r &&\n    test_cmp error.o error.r\n  )\n '\n\n-test_expect_success PERL 'process filter should be able to signal an error for all future files' '\n+test_expect_success PERL 'process filter signals abort once to abort processing of all future files' '\n  test_config_global filter.protocol.process \"$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge\" &&\n  rm -rf repo &&\n  mkdir repo &&\n@@ -602,25 +678,27 @@ test_expect_success PERL 'process filter should be able to signal an error for a\n\n    echo \"*.r filter=protocol\" >.gitattributes &&\n\n-   cp ../test.o test.r &&\n-   cp ../test2.o test2.r &&\n+   cp \"$TEST_ROOT/test.o\" test.r &&\n+   cp \"$TEST_ROOT/test2.o\" test2.r &&\n    echo \"error this blob and all future blobs\" >abort.o &&\n    cp abort.o abort.r &&\n+\n+   SA=$(file_size abort.r) &&\n+\n    git add . &&\n-   git commit . -m \"test commit\" &&\n    rm -f *.r &&\n\n-   check_filter_ignore_clean \\\n-     git checkout . \\\n-       <<-\\EOF &&\n+   filter_git checkout --quiet --no-progress . &&\n+   cat >expected.log <<-EOF &&\n      START\n-         wrote filter header\n-         IN: smudge abort.r 37 [OK] -- OUT: 0 [ABORT]\n+     init handshake complete\n+     IN: smudge abort.r $SA [OK] -- OUT: 0 [ABORT]\n      STOP\n    EOF\n+   test_cmp_exclude_clean expected.log rot13-filter.log &&\n\n-   test_cmp ../test.o test.r &&\n-   test_cmp ../test2.o test2.r &&\n+   test_cmp \"$TEST_ROOT/test.o\" test.r &&\n+   test_cmp \"$TEST_ROOT/test2.o\" test2.r &&\n    test_cmp abort.o abort.r\n  )\n '\n@@ -636,9 +714,9 @@ test_expect_success PERL 'invalid process filter must fail (and not hang!)' '\n\n    echo \"*.r filter=protocol\" >.gitattributes &&\n\n-   cp ../test.o test.r &&\n-   test_must_fail git add . 2> git_stderr.log &&\n-   grep \"not support long running filter protocol\" git_stderr.log\n+   cp \"$TEST_ROOT/test.o\" test.r &&\n+   test_must_fail git add . 2>git-stderr.log &&\n+   grep \"does not support filter protocol version\" git-stderr.log\n  )\n '\n\ndiff --git a/t/t0021/rot13-filter.pl b/t/t0021/rot13-filter.pl\nindex 8958f71..1a6959c 100755\n--- a/t/t0021/rot13-filter.pl\n+++ b/t/t0021/rot13-filter.pl\n@@ -26,10 +26,10 @@ use warnings;\n my $MAX_PACKET_CONTENT_SIZE = 65516;\n my @capabilities            = @ARGV;\n\n-open my $debug, \">>\", \"rot13-filter.log\";\n+open my $debug, \">>\", \"rot13-filter.log\" or die \"cannot open log file: $!\";\n\n sub rot13 {\n-    my ($str) = @_;\n+ my $str = shift;\n  $str =~ y/A-Za-z/N-ZA-Mn-za-m/;\n  return $str;\n }\n@@ -38,13 +38,12 @@ sub packet_bin_read {\n  my $buffer;\n  my $bytes_read = read STDIN, $buffer, 4;\n  if ( $bytes_read == 0 ) {\n-\n    # EOF - Git stopped talking to us!\n    print $debug \"STOP\\n\";\n    exit();\n  }\n  elsif ( $bytes_read != 4 ) {\n-        die \"invalid packet size '$bytes_read' field\";\n+   die \"invalid packet: '$buffer'\";\n  }\n  my $pkt_size = hex($buffer);\n  if ( $pkt_size == 0 ) {\n@@ -54,27 +53,27 @@ sub packet_bin_read {\n    my $content_size = $pkt_size - 4;\n    $bytes_read = read STDIN, $buffer, $content_size;\n    if ( $bytes_read != $content_size ) {\n-            die \"invalid packet ($content_size expected; $bytes_read read)\";\n+     die \"invalid packet ($content_size bytes expected; $bytes_read bytes read)\";\n    }\n    return ( 0, $buffer );\n  }\n  else {\n-        die \"invalid packet size\";\n+   die \"invalid packet size: $pkt_size\";\n  }\n }\n\n sub packet_txt_read {\n  my ( $res, $buf ) = packet_bin_read();\n-    unless ( $buf =~ /\\n$/ ) {\n-        die \"A non-binary line SHOULD BE terminated by an LF.\";\n+ unless ( $buf =~ s/\\n$// ) {\n+   die \"A non-binary line MUST be terminated by an LF.\";\n  }\n-    return ( $res, substr( $buf, 0, -1 ) );\n+ return ( $res, $buf );\n }\n\n sub packet_bin_write {\n-    my ($packet) = @_;\n-    print STDOUT sprintf( \"%04x\", length($packet) + 4 );\n-    print STDOUT $packet;\n+ my $buf = shift;\n+ print STDOUT sprintf( \"%04x\", length($buf) + 4 );\n+ print STDOUT $buf;\n  STDOUT->flush();\n }\n\n@@ -105,7 +104,7 @@ foreach (@capabilities) {\n  packet_txt_write( $_ . \"=true\" );\n }\n packet_flush();\n-print $debug \"wrote filter header\\n\";\n+print $debug \"init handshake complete\\n\";\n $debug->flush();\n\n while (1) {\n@@ -175,6 +174,7 @@ while (1) {\n    while ( length($output) > 0 ) {\n      my $packet = substr( $output, 0, $MAX_PACKET_CONTENT_SIZE );\n      packet_bin_write($packet);\n+     # dots represent the number of packets\n      print $debug \".\";\n      if ( length($output) > $MAX_PACKET_CONTENT_SIZE ) {\n        $output = substr( $output, $MAX_PACKET_CONTENT_SIZE );\n\n\nLars Schneider (14):\n  convert: quote filter names in error messages\n  convert: modernize tests\n  run-command: move check_pipe() from write_or_die to run_command\n  run-command: add wait_on_exit\n  pkt-line: rename packet_write() to packet_write_fmt()\n  pkt-line: extract set_packet_header()\n  pkt-line: add packet_write_fmt_gently()\n  pkt-line: add packet_flush_gently()\n  pkt-line: add packet_write_gently()\n  pkt-line: add functions to read/write flush terminated packet streams\n  convert: make apply_filter() adhere to standard Git error handling\n  convert: prepare filter.<driver>.process option\n  convert: add filter.<driver>.process option\n  contrib/long-running-filter: add long running filter example\n\n Documentation/gitattributes.txt        | 159 ++++++++++-\n builtin/archive.c                      |   4 +-\n builtin/receive-pack.c                 |   4 +-\n builtin/remote-ext.c                   |   4 +-\n builtin/upload-archive.c               |   4 +-\n connect.c                              |   2 +-\n contrib/long-running-filter/example.pl | 127 +++++++++\n convert.c                              | 370 +++++++++++++++++++++---\n daemon.c                               |   2 +-\n http-backend.c                         |   2 +-\n pkt-line.c                             | 148 +++++++++-\n pkt-line.h                             |  12 +-\n run-command.c                          |  72 ++++-\n run-command.h                          |   5 +-\n shallow.c                              |   2 +-\n t/t0021-conversion.sh                  | 505 ++++++++++++++++++++++++++++++---\n t/t0021/rot13-filter.pl                | 191 +++++++++++++\n upload-pack.c                          |  30 +-\n write_or_die.c                         |  13 -\n 19 files changed, 1519 insertions(+), 137 deletions(-)\n create mode 100755 contrib/long-running-filter/example.pl\n create mode 100755 t/t0021/rot13-filter.pl\n\n--\n2.10.0\n\n"},{"id":"303218","messageId":"20161004125947.67104-2-larsxschneider@gmail.com","threadId":"44216","inReplyTo":"20161004125947.67104-1-larsxschneider@gmail.com","subject":"[PATCH v9 01/14] convert: quote filter names in error messages","fromName":"","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-04T12:59:34Z","receivedAt":"2016-10-04T13:00:40Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nGit filter driver commands with spaces (e.g. `filter.sh foo`) are hard\nto read in error messages. Quote them to improve the readability.\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n convert.c | 12 ++++++------\n 1 file changed, 6 insertions(+), 6 deletions(-)\n\ndiff --git a/convert.c b/convert.c\nindex 077f5e6..986c239 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -412,7 +412,7 @@ static int filter_buffer_or_fd(int in, int out, void *data)\n \tchild_process.out = out;\n \n \tif (start_command(&child_process))\n-\t\treturn error(\"cannot fork to run external filter %s\", params->cmd);\n+\t\treturn error(\"cannot fork to run external filter '%s'\", params->cmd);\n \n \tsigchain_push(SIGPIPE, SIG_IGN);\n \n@@ -430,13 +430,13 @@ static int filter_buffer_or_fd(int in, int out, void *data)\n \tif (close(child_process.in))\n \t\twrite_err = 1;\n \tif (write_err)\n-\t\terror(\"cannot feed the input to external filter %s\", params->cmd);\n+\t\terror(\"cannot feed the input to external filter '%s'\", params->cmd);\n \n \tsigchain_pop(SIGPIPE);\n \n \tstatus = finish_command(&child_process);\n \tif (status)\n-\t\terror(\"external filter %s failed %d\", params->cmd, status);\n+\t\terror(\"external filter '%s' failed %d\", params->cmd, status);\n \n \tstrbuf_release(&cmd);\n \treturn (write_err || status);\n@@ -477,15 +477,15 @@ static int apply_filter(const char *path, const char *src, size_t len, int fd,\n \t\treturn 0;\t/* error was already reported */\n \n \tif (strbuf_read(&nbuf, async.out, len) < 0) {\n-\t\terror(\"read from external filter %s failed\", cmd);\n+\t\terror(\"read from external filter '%s' failed\", cmd);\n \t\tret = 0;\n \t}\n \tif (close(async.out)) {\n-\t\terror(\"read from external filter %s failed\", cmd);\n+\t\terror(\"read from external filter '%s' failed\", cmd);\n \t\tret = 0;\n \t}\n \tif (finish_async(&async)) {\n-\t\terror(\"external filter %s failed\", cmd);\n+\t\terror(\"external filter '%s' failed\", cmd);\n \t\tret = 0;\n \t}\n \n-- \n2.10.0\n\n"},{"id":"303219","messageId":"20161004125947.67104-9-larsxschneider@gmail.com","threadId":"44216","inReplyTo":"20161004125947.67104-1-larsxschneider@gmail.com","subject":"[PATCH v9 08/14] pkt-line: add packet_flush_gently()","fromName":"","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-04T12:59:41Z","receivedAt":"2016-10-04T13:00:44Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\npacket_flush() would die in case of a write error even though for some\ncallers an error would be acceptable. Add packet_flush_gently() which\nwrites a pkt-line flush packet like packet_flush() but does not die in\ncase of an error. The function is used in a subsequent patch.\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n pkt-line.c | 8 ++++++++\n pkt-line.h | 1 +\n 2 files changed, 9 insertions(+)\n\ndiff --git a/pkt-line.c b/pkt-line.c\nindex 56915f0..286eb09 100644\n--- a/pkt-line.c\n+++ b/pkt-line.c\n@@ -91,6 +91,14 @@ void packet_flush(int fd)\n \twrite_or_die(fd, \"0000\", 4);\n }\n \n+int packet_flush_gently(int fd)\n+{\n+\tpacket_trace(\"0000\", 4, 1);\n+\tif (write_in_full(fd, \"0000\", 4) == 4)\n+\t\treturn 0;\n+\treturn error(\"flush packet write failed\");\n+}\n+\n void packet_buf_flush(struct strbuf *buf)\n {\n \tpacket_trace(\"0000\", 4, 1);\ndiff --git a/pkt-line.h b/pkt-line.h\nindex 3caea77..3fa0899 100644\n--- a/pkt-line.h\n+++ b/pkt-line.h\n@@ -23,6 +23,7 @@ void packet_flush(int fd);\n void packet_write_fmt(int fd, const char *fmt, ...) __attribute__((format (printf, 2, 3)));\n void packet_buf_flush(struct strbuf *buf);\n void packet_buf_write(struct strbuf *buf, const char *fmt, ...) __attribute__((format (printf, 2, 3)));\n+int packet_flush_gently(int fd);\n int packet_write_fmt_gently(int fd, const char *fmt, ...) __attribute__((format (printf, 2, 3)));\n \n /*\n-- \n2.10.0\n\n"},{"id":"303220","messageId":"20161004125947.67104-15-larsxschneider@gmail.com","threadId":"44216","inReplyTo":"20161004125947.67104-1-larsxschneider@gmail.com","subject":"[PATCH v9 14/14] contrib/long-running-filter: add long running filter example","fromName":"","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-04T12:59:47Z","receivedAt":"2016-10-04T13:00:48Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nAdd a simple pass-thru filter as example implementation for the Git\nfilter protocol version 2. See Documentation/gitattributes.txt, section\n\"Filter Protocol\" for more info.\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\n---\n Documentation/gitattributes.txt        |   4 +-\n contrib/long-running-filter/example.pl | 127 +++++++++++++++++++++++++++++++++\n 2 files changed, 130 insertions(+), 1 deletion(-)\n create mode 100755 contrib/long-running-filter/example.pl\n\ndiff --git a/Documentation/gitattributes.txt b/Documentation/gitattributes.txt\nindex 5868f00..a182ef2 100644\n--- a/Documentation/gitattributes.txt\n+++ b/Documentation/gitattributes.txt\n@@ -514,7 +514,9 @@ the next \"key=value\" list containing a command. Git will close\n the command pipe on exit. The filter is expected to detect EOF\n and exit gracefully on its own.\n \n-If you develop your own long running filter\n+A long running filter demo implementation can be found in\n+`contrib/long-running-filter/example.pl` located in the Git\n+core repository. If you develop your own long running filter\n process then the `GIT_TRACE_PACKET` environment variables can be\n very helpful for debugging (see linkgit:git[1]).\n \ndiff --git a/contrib/long-running-filter/example.pl b/contrib/long-running-filter/example.pl\nnew file mode 100755\nindex 0000000..f4102d2\n--- /dev/null\n+++ b/contrib/long-running-filter/example.pl\n@@ -0,0 +1,127 @@\n+#!/usr/bin/perl\n+#\n+# Example implementation for the Git filter protocol version 2\n+# See Documentation/gitattributes.txt, section \"Filter Protocol\"\n+#\n+# Please note, this pass-thru filter is a minimal skeleton. No proper\n+# error handling was implemented.\n+#\n+\n+use strict;\n+use warnings;\n+\n+my $MAX_PACKET_CONTENT_SIZE = 65516;\n+\n+sub packet_bin_read {\n+\tmy $buffer;\n+\tmy $bytes_read = read STDIN, $buffer, 4;\n+\tif ( $bytes_read == 0 ) {\n+\n+\t\t# EOF - Git stopped talking to us!\n+\t\texit();\n+\t}\n+\telsif ( $bytes_read != 4 ) {\n+\t\tdie \"invalid packet: '$buffer'\";\n+\t}\n+\tmy $pkt_size = hex($buffer);\n+\tif ( $pkt_size == 0 ) {\n+\t\treturn ( 1, \"\" );\n+\t}\n+\telsif ( $pkt_size > 4 ) {\n+\t\tmy $content_size = $pkt_size - 4;\n+\t\t$bytes_read = read STDIN, $buffer, $content_size;\n+\t\tif ( $bytes_read != $content_size ) {\n+\t\t\tdie \"invalid packet ($content_size bytes expected; $bytes_read bytes read)\";\n+\t\t}\n+\t\treturn ( 0, $buffer );\n+\t}\n+\telse {\n+\t\tdie \"invalid packet size: $pkt_size\";\n+\t}\n+}\n+\n+sub packet_txt_read {\n+\tmy ( $res, $buf ) = packet_bin_read();\n+\tunless ( $buf =~ s/\\n$// ) {\n+\t\tdie \"A non-binary line MUST be terminated by an LF.\";\n+\t}\n+\treturn ( $res, $buf );\n+}\n+\n+sub packet_bin_write {\n+\tmy $buf = shift;\n+\tprint STDOUT sprintf( \"%04x\", length($buf) + 4 );\n+\tprint STDOUT $buf;\n+\tSTDOUT->flush();\n+}\n+\n+sub packet_txt_write {\n+\tpacket_bin_write( $_[0] . \"\\n\" );\n+}\n+\n+sub packet_flush {\n+\tprint STDOUT sprintf( \"%04x\", 0 );\n+\tSTDOUT->flush();\n+}\n+\n+( packet_txt_read() eq ( 0, \"git-filter-client\" ) ) || die \"bad initialize\";\n+( packet_txt_read() eq ( 0, \"version=2\" ) )         || die \"bad version\";\n+( packet_bin_read() eq ( 1, \"\" ) )                  || die \"bad version end\";\n+\n+packet_txt_write(\"git-filter-server\");\n+packet_txt_write(\"version=2\");\n+\n+( packet_txt_read() eq ( 0, \"clean=true\" ) )  || die \"bad capability\";\n+( packet_txt_read() eq ( 0, \"smudge=true\" ) ) || die \"bad capability\";\n+( packet_bin_read() eq ( 1, \"\" ) )            || die \"bad capability end\";\n+\n+packet_txt_write(\"clean=true\");\n+packet_txt_write(\"smudge=true\");\n+packet_flush();\n+\n+while (1) {\n+\tmy ($command)  = packet_txt_read() =~ /^command=([^=]+)$/;\n+\tmy ($pathname) = packet_txt_read() =~ /^pathname=([^=]+)$/;\n+\n+\tpacket_bin_read();\n+\n+\tmy $input = \"\";\n+\t{\n+\t\tbinmode(STDIN);\n+\t\tmy $buffer;\n+\t\tmy $done = 0;\n+\t\twhile ( !$done ) {\n+\t\t\t( $done, $buffer ) = packet_bin_read();\n+\t\t\t$input .= $buffer;\n+\t\t}\n+\t}\n+\n+\tmy $output;\n+\tif ( $command eq \"clean\" ) {\n+\t\t### Perform clean here ###\n+\t\t$output = $input;\n+\t}\n+\telsif ( $command eq \"smudge\" ) {\n+\t\t### Perform smudge here ###\n+\t\t$output = $input;\n+\t}\n+\telse {\n+\t\tdie \"bad command '$command'\";\n+\t}\n+\n+\tpacket_txt_write(\"status=success\");\n+\tpacket_flush();\n+\twhile ( length($output) > 0 ) {\n+\t\tmy $packet = substr( $output, 0, $MAX_PACKET_CONTENT_SIZE );\n+\t\tpacket_bin_write($packet);\n+\t\tif ( length($output) > $MAX_PACKET_CONTENT_SIZE ) {\n+\t\t\t$output = substr( $output, $MAX_PACKET_CONTENT_SIZE );\n+\t\t}\n+\t\telse {\n+\t\t\t$output = \"\";\n+\t\t}\n+\t}\n+\tpacket_flush();    # flush content!\n+\tpacket_flush();    # empty list, keep \"status=success\" unchanged!\n+\n+}\n-- \n2.10.0\n\n"},{"id":"303221","messageId":"20161004125947.67104-5-larsxschneider@gmail.com","threadId":"44216","inReplyTo":"20161004125947.67104-1-larsxschneider@gmail.com","subject":"[PATCH v9 04/14] run-command: add wait_on_exit","fromName":"","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-04T12:59:37Z","receivedAt":"2016-10-04T13:00:51Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nThe flag 'clean_on_exit' kills child processes spawned by Git on exit.\nA hard kill like this might not be desired in all cases.\n\nAdd 'wait_on_exit' which closes the child's stdin on Git exit and waits\nuntil the child process has terminated.\n\nThe flag is used in a subsequent patch.\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\n---\n run-command.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++--------\n run-command.h |  3 +++\n 2 files changed, 50 insertions(+), 8 deletions(-)\n\ndiff --git a/run-command.c b/run-command.c\nindex 3269362..96c54fe 100644\n--- a/run-command.c\n+++ b/run-command.c\n@@ -21,6 +21,9 @@ void child_process_clear(struct child_process *child)\n\n struct child_to_clean {\n \tpid_t pid;\n+\tchar *name;\n+\tint stdin;\n+\tint wait;\n \tstruct child_to_clean *next;\n };\n static struct child_to_clean *children_to_clean;\n@@ -28,12 +31,33 @@ static int installed_child_cleanup_handler;\n\n static void cleanup_children(int sig, int in_signal)\n {\n+\tint status;\n+\tstruct child_to_clean *p = children_to_clean;\n+\n+\t/* Close the the child's stdin as indicator that Git will exit soon */\n+\twhile (p) {\n+\t\tif (p->wait)\n+\t\t\tif (p->stdin > 0)\n+\t\t\t\tclose(p->stdin);\n+\t\tp = p->next;\n+\t}\n+\n \twhile (children_to_clean) {\n-\t\tstruct child_to_clean *p = children_to_clean;\n+\t\tp = children_to_clean;\n \t\tchildren_to_clean = p->next;\n+\n+\t\tif (p->wait) {\n+\t\t\tfprintf(stderr, _(\"Waiting for '%s' to finish...\"), p->name);\n+\t\t\twhile ((waitpid(p->pid, &status, 0)) < 0 && errno == EINTR)\n+\t\t\t\t;\t/* nothing */\n+\t\t\tfprintf(stderr, _(\"done!\\n\"));\n+\t\t}\n+\n \t\tkill(p->pid, sig);\n-\t\tif (!in_signal)\n+\t\tif (!in_signal) {\n+\t\t\tfree(p->name);\n \t\t\tfree(p);\n+\t\t}\n \t}\n }\n\n@@ -49,10 +73,16 @@ static void cleanup_children_on_exit(void)\n \tcleanup_children(SIGTERM, 0);\n }\n\n-static void mark_child_for_cleanup(pid_t pid)\n+static void mark_child_for_cleanup(pid_t pid, const char *name, int stdin, int wait)\n {\n \tstruct child_to_clean *p = xmalloc(sizeof(*p));\n \tp->pid = pid;\n+\tp->wait = wait;\n+\tp->stdin = stdin;\n+\tif (name)\n+\t\tp->name = xstrdup(name);\n+\telse\n+\t\tp->name = \"process\";\n \tp->next = children_to_clean;\n \tchildren_to_clean = p;\n\n@@ -63,6 +93,13 @@ static void mark_child_for_cleanup(pid_t pid)\n \t}\n }\n\n+#ifdef NO_PTHREADS\n+static void mark_child_for_cleanup_no_wait(pid_t pid, const char *name, int timeout, int stdin)\n+{\n+\tmark_child_for_cleanup(pid, NULL, 0, 0);\n+}\n+#endif\n+\n static void clear_child_for_cleanup(pid_t pid)\n {\n \tstruct child_to_clean **pp;\n@@ -421,8 +458,9 @@ int start_command(struct child_process *cmd)\n \t}\n \tif (cmd->pid < 0)\n \t\terror_errno(\"cannot fork() for %s\", cmd->argv[0]);\n-\telse if (cmd->clean_on_exit)\n-\t\tmark_child_for_cleanup(cmd->pid);\n+\telse if (cmd->clean_on_exit || cmd->wait_on_exit)\n+\t\tmark_child_for_cleanup(\n+\t\t\tcmd->pid, cmd->argv[0], cmd->in, cmd->wait_on_exit);\n\n \t/*\n \t * Wait for child's execvp. If the execvp succeeds (or if fork()\n@@ -482,8 +520,9 @@ int start_command(struct child_process *cmd)\n \tfailed_errno = errno;\n \tif (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))\n \t\terror_errno(\"cannot spawn %s\", cmd->argv[0]);\n-\tif (cmd->clean_on_exit && cmd->pid >= 0)\n-\t\tmark_child_for_cleanup(cmd->pid);\n+\tif ((cmd->clean_on_exit || cmd->wait_on_exit) && cmd->pid >= 0)\n+\t\tmark_child_for_cleanup(\n+\t\t\tcmd->pid, cmd->argv[0], cmd->in, cmd->clean_on_exit_timeout);\n\n \targv_array_clear(&nargv);\n \tcmd->argv = sargv;\n@@ -765,7 +804,7 @@ int start_async(struct async *async)\n \t\texit(!!async->proc(proc_in, proc_out, async->data));\n \t}\n\n-\tmark_child_for_cleanup(async->pid);\n+\tmark_child_for_cleanup_no_wait(async->pid);\n\n \tif (need_in)\n \t\tclose(fdin[0]);\ndiff --git a/run-command.h b/run-command.h\nindex cf29a31..f7b9907 100644\n--- a/run-command.h\n+++ b/run-command.h\n@@ -42,7 +42,10 @@ struct child_process {\n \tunsigned silent_exec_failure:1;\n \tunsigned stdout_to_stderr:1;\n \tunsigned use_shell:1;\n+\t /* kill the child on Git exit */\n \tunsigned clean_on_exit:1;\n+\t/* close the child's stdin on Git exit and wait until it terminates */\n+\tunsigned wait_on_exit:1;\n };\n\n #define CHILD_PROCESS_INIT { NULL, ARGV_ARRAY_INIT, ARGV_ARRAY_INIT }\n--\n2.10.0\n\n"},{"id":"303222","messageId":"20161004125947.67104-4-larsxschneider@gmail.com","threadId":"44216","inReplyTo":"20161004125947.67104-1-larsxschneider@gmail.com","subject":"[PATCH v9 03/14] run-command: move check_pipe() from write_or_die to run_command","fromName":"","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-04T12:59:36Z","receivedAt":"2016-10-04T13:00:55Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\nMove check_pipe() to run_command and make it public. This is necessary\nto call the function from pkt-line in a subsequent patch.\n\nWhile at it, make async_exit() static to run_command.c as it is no\nlonger used from outside.\n\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\nSigned-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n run-command.c  | 17 +++++++++++++++--\n run-command.h  |  2 +-\n write_or_die.c | 13 -------------\n 3 files changed, 16 insertions(+), 16 deletions(-)\n\ndiff --git a/run-command.c b/run-command.c\nindex 5a4dbb6..3269362 100644\n--- a/run-command.c\n+++ b/run-command.c\n@@ -634,7 +634,7 @@ int in_async(void)\n \treturn !pthread_equal(main_thread, pthread_self());\n }\n \n-void NORETURN async_exit(int code)\n+static void NORETURN async_exit(int code)\n {\n \tpthread_exit((void *)(intptr_t)code);\n }\n@@ -684,13 +684,26 @@ int in_async(void)\n \treturn process_is_async;\n }\n \n-void NORETURN async_exit(int code)\n+static void NORETURN async_exit(int code)\n {\n \texit(code);\n }\n \n #endif\n \n+void check_pipe(int err)\n+{\n+\tif (err == EPIPE) {\n+\t\tif (in_async())\n+\t\t\tasync_exit(141);\n+\n+\t\tsignal(SIGPIPE, SIG_DFL);\n+\t\traise(SIGPIPE);\n+\t\t/* Should never happen, but just in case... */\n+\t\texit(141);\n+\t}\n+}\n+\n int start_async(struct async *async)\n {\n \tint need_in, need_out;\ndiff --git a/run-command.h b/run-command.h\nindex 5066649..cf29a31 100644\n--- a/run-command.h\n+++ b/run-command.h\n@@ -139,7 +139,7 @@ struct async {\n int start_async(struct async *async);\n int finish_async(struct async *async);\n int in_async(void);\n-void NORETURN async_exit(int code);\n+void check_pipe(int err);\n \n /**\n  * This callback should initialize the child process and preload the\ndiff --git a/write_or_die.c b/write_or_die.c\nindex 0734432..eab8c8d 100644\n--- a/write_or_die.c\n+++ b/write_or_die.c\n@@ -1,19 +1,6 @@\n #include \"cache.h\"\n #include \"run-command.h\"\n \n-static void check_pipe(int err)\n-{\n-\tif (err == EPIPE) {\n-\t\tif (in_async())\n-\t\t\tasync_exit(141);\n-\n-\t\tsignal(SIGPIPE, SIG_DFL);\n-\t\traise(SIGPIPE);\n-\t\t/* Should never happen, but just in case... */\n-\t\texit(141);\n-\t}\n-}\n-\n /*\n  * Some cases use stdio, but want to flush after the write\n  * to get error handling (and to get better interactive\n-- \n2.10.0\n\n"},{"id":"303223","messageId":"20161004125947.67104-6-larsxschneider@gmail.com","threadId":"44216","inReplyTo":"20161004125947.67104-1-larsxschneider@gmail.com","subject":"[PATCH v9 05/14] pkt-line: rename packet_write() to packet_write_fmt()","fromName":"","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-04T12:59:38Z","receivedAt":"2016-10-04T13:00:59Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"From: Lars Schneider <larsxschneider@gmail.com>\n\npacket_write() should be called packet_write_fmt() because it is a\nprintf-like function that takes a format string as first parameter.\n\npacket_write_fmt() should be used for text strings only. Arbitrary\nbinary data should use a new packet_write() function that is introduced\nin a subsequent patch.\n\nSuggested-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Lars Schneider <larsxschneider@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin/archive.c        |  4 ++--\n builtin/receive-pack.c   |  4 ++--\n builtin/remote-ext.c     |  4 ++--\n builtin/upload-archive.c |  4 ++--\n connect.c                |  2 +-\n daemon.c                 |  2 +-\n http-backend.c           |  2 +-\n pkt-line.c               |  2 +-\n pkt-line.h               |  2 +-\n shallow.c                |  2 +-\n upload-pack.c            | 30 +++++++++++++++---------------\n 11 files changed, 29 insertions(+), 29 deletions(-)\n\ndiff --git a/builtin/archive.c b/builtin/archive.c\nindex a1e3b94..49f4914 100644\n--- a/builtin/archive.c\n+++ b/builtin/archive.c\n@@ -47,10 +47,10 @@ static int run_remote_archiver(int argc, const char **argv,\n \tif (name_hint) {\n \t\tconst char *format = archive_format_from_filename(name_hint);\n \t\tif (format)\n-\t\t\tpacket_write(fd[1], \"argument --format=%s\\n\", format);\n+\t\t\tpacket_write_fmt(fd[1], \"argument --format=%s\\n\", format);\n \t}\n \tfor (i = 1; i < argc; i++)\n-\t\tpacket_write(fd[1], \"argument %s\\n\", argv[i]);\n+\t\tpacket_write_fmt(fd[1], \"argument %s\\n\", argv[i]);\n \tpacket_flush(fd[1]);\n \n \tbuf = packet_read_line(fd[0], NULL);\ndiff --git a/builtin/receive-pack.c b/builtin/receive-pack.c\nindex 011db00..1ce7682 100644\n--- a/builtin/receive-pack.c\n+++ b/builtin/receive-pack.c\n@@ -218,7 +218,7 @@ static int receive_pack_config(const char *var, const char *value, void *cb)\n static void show_ref(const char *path, const unsigned char *sha1)\n {\n \tif (sent_capabilities) {\n-\t\tpacket_write(1, \"%s %s\\n\", sha1_to_hex(sha1), path);\n+\t\tpacket_write_fmt(1, \"%s %s\\n\", sha1_to_hex(sha1), path);\n \t} else {\n \t\tstruct strbuf cap = STRBUF_INIT;\n \n@@ -233,7 +233,7 @@ static void show_ref(const char *path, const unsigned char *sha1)\n \t\tif (advertise_push_options)\n \t\t\tstrbuf_addstr(&cap, \" push-options\");\n \t\tstrbuf_addf(&cap, \" agent=%s\", git_user_agent_sanitized());\n-\t\tpacket_write(1, \"%s %s%c%s\\n\",\n+\t\tpacket_write_fmt(1, \"%s %s%c%s\\n\",\n \t\t\t     sha1_to_hex(sha1), path, 0, cap.buf);\n \t\tstrbuf_release(&cap);\n \t\tsent_capabilities = 1;\ndiff --git a/builtin/remote-ext.c b/builtin/remote-ext.c\nindex 88eb8f9..11b48bf 100644\n--- a/builtin/remote-ext.c\n+++ b/builtin/remote-ext.c\n@@ -128,9 +128,9 @@ static void send_git_request(int stdin_fd, const char *serv, const char *repo,\n \tconst char *vhost)\n {\n \tif (!vhost)\n-\t\tpacket_write(stdin_fd, \"%s %s%c\", serv, repo, 0);\n+\t\tpacket_write_fmt(stdin_fd, \"%s %s%c\", serv, repo, 0);\n \telse\n-\t\tpacket_write(stdin_fd, \"%s %s%chost=%s%c\", serv, repo, 0,\n+\t\tpacket_write_fmt(stdin_fd, \"%s %s%chost=%s%c\", serv, repo, 0,\n \t\t\t     vhost, 0);\n }\n \ndiff --git a/builtin/upload-archive.c b/builtin/upload-archive.c\nindex 2caedf1..dc872f6 100644\n--- a/builtin/upload-archive.c\n+++ b/builtin/upload-archive.c\n@@ -88,11 +88,11 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix)\n \twriter.git_cmd = 1;\n \tif (start_command(&writer)) {\n \t\tint err = errno;\n-\t\tpacket_write(1, \"NACK unable to spawn subprocess\\n\");\n+\t\tpacket_write_fmt(1, \"NACK unable to spawn subprocess\\n\");\n \t\tdie(\"upload-archive: %s\", strerror(err));\n \t}\n \n-\tpacket_write(1, \"ACK\\n\");\n+\tpacket_write_fmt(1, \"ACK\\n\");\n \tpacket_flush(1);\n \n \twhile (1) {\ndiff --git a/connect.c b/connect.c\nindex 722dc3f..5330d9c 100644\n--- a/connect.c\n+++ b/connect.c\n@@ -730,7 +730,7 @@ struct child_process *git_connect(int fd[2], const char *url,\n \t\t * Note: Do not add any other headers here!  Doing so\n \t\t * will cause older git-daemon servers to crash.\n \t\t */\n-\t\tpacket_write(fd[1],\n+\t\tpacket_write_fmt(fd[1],\n \t\t\t     \"%s %s%chost=%s%c\",\n \t\t\t     prog, path, 0,\n \t\t\t     target_host, 0);\ndiff --git a/daemon.c b/daemon.c\nindex 425aad0..afce1b9 100644\n--- a/daemon.c\n+++ b/daemon.c\n@@ -281,7 +281,7 @@ static int daemon_error(const char *dir, const char *msg)\n {\n \tif (!informative_errors)\n \t\tmsg = \"access denied or repository not exported\";\n-\tpacket_write(1, \"ERR %s: %s\", msg, dir);\n+\tpacket_write_fmt(1, \"ERR %s: %s\", msg, dir);\n \treturn -1;\n }\n \ndiff --git a/http-backend.c b/http-backend.c\nindex adc8c8c..eef0a36 100644\n--- a/http-backend.c\n+++ b/http-backend.c\n@@ -464,7 +464,7 @@ static void get_info_refs(struct strbuf *hdr, char *arg)\n \t\thdr_str(hdr, content_type, buf.buf);\n \t\tend_headers(hdr);\n \n-\t\tpacket_write(1, \"# service=git-%s\\n\", svc->name);\n+\t\tpacket_write_fmt(1, \"# service=git-%s\\n\", svc->name);\n \t\tpacket_flush(1);\n \n \t\targv[0] = svc->name;\ndiff --git a/pkt-line.c b/pkt-line.c\nindex 62fdb37..0a9b61c 100644\n--- a/pkt-line.c\n+++ b/pkt-line.c\n@@ -118,7 +118,7 @@ static void format_packet(struct strbuf *out, const char *fmt, va_list args)\n \tpacket_trace(out->buf + orig_len + 4, n - 4, 1);\n }\n \n-void packet_write(int fd, const char *fmt, ...)\n+void packet_write_fmt(int fd, const char *fmt, ...)\n {\n \tstatic struct strbuf buf = STRBUF_INIT;\n \tva_list args;\ndiff --git a/pkt-line.h b/pkt-line.h\nindex 3cb9d91..1902fb3 100644\n--- a/pkt-line.h\n+++ b/pkt-line.h\n@@ -20,7 +20,7 @@\n  * side can't, we stay with pure read/write interfaces.\n  */\n void packet_flush(int fd);\n-void packet_write(int fd, const char *fmt, ...) __attribute__((format (printf, 2, 3)));\n+void packet_write_fmt(int fd, const char *fmt, ...) __attribute__((format (printf, 2, 3)));\n void packet_buf_flush(struct strbuf *buf);\n void packet_buf_write(struct strbuf *buf, const char *fmt, ...) __attribute__((format (printf, 2, 3)));\n \ndiff --git a/shallow.c b/shallow.c\nindex 54e2db7..d666e24 100644\n--- a/shallow.c\n+++ b/shallow.c\n@@ -260,7 +260,7 @@ static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *c\n {\n \tint fd = *(int *)cb;\n \tif (graft->nr_parent == -1)\n-\t\tpacket_write(fd, \"shallow %s\\n\", oid_to_hex(&graft->oid));\n+\t\tpacket_write_fmt(fd, \"shallow %s\\n\", oid_to_hex(&graft->oid));\n \treturn 0;\n }\n \ndiff --git a/upload-pack.c b/upload-pack.c\nindex ca7f941..cd47de6 100644\n--- a/upload-pack.c\n+++ b/upload-pack.c\n@@ -393,13 +393,13 @@ static int get_common_commits(void)\n \t\t\tif (multi_ack == 2 && got_common\n \t\t\t    && !got_other && ok_to_give_up()) {\n \t\t\t\tsent_ready = 1;\n-\t\t\t\tpacket_write(1, \"ACK %s ready\\n\", last_hex);\n+\t\t\t\tpacket_write_fmt(1, \"ACK %s ready\\n\", last_hex);\n \t\t\t}\n \t\t\tif (have_obj.nr == 0 || multi_ack)\n-\t\t\t\tpacket_write(1, \"NAK\\n\");\n+\t\t\t\tpacket_write_fmt(1, \"NAK\\n\");\n \n \t\t\tif (no_done && sent_ready) {\n-\t\t\t\tpacket_write(1, \"ACK %s\\n\", last_hex);\n+\t\t\t\tpacket_write_fmt(1, \"ACK %s\\n\", last_hex);\n \t\t\t\treturn 0;\n \t\t\t}\n \t\t\tif (stateless_rpc)\n@@ -416,20 +416,20 @@ static int get_common_commits(void)\n \t\t\t\t\tconst char *hex = sha1_to_hex(sha1);\n \t\t\t\t\tif (multi_ack == 2) {\n \t\t\t\t\t\tsent_ready = 1;\n-\t\t\t\t\t\tpacket_write(1, \"ACK %s ready\\n\", hex);\n+\t\t\t\t\t\tpacket_write_fmt(1, \"ACK %s ready\\n\", hex);\n \t\t\t\t\t} else\n-\t\t\t\t\t\tpacket_write(1, \"ACK %s continue\\n\", hex);\n+\t\t\t\t\t\tpacket_write_fmt(1, \"ACK %s continue\\n\", hex);\n \t\t\t\t}\n \t\t\t\tbreak;\n \t\t\tdefault:\n \t\t\t\tgot_common = 1;\n \t\t\t\tmemcpy(last_hex, sha1_to_hex(sha1), 41);\n \t\t\t\tif (multi_ack == 2)\n-\t\t\t\t\tpacket_write(1, \"ACK %s common\\n\", last_hex);\n+\t\t\t\t\tpacket_write_fmt(1, \"ACK %s common\\n\", last_hex);\n \t\t\t\telse if (multi_ack)\n-\t\t\t\t\tpacket_write(1, \"ACK %s continue\\n\", last_hex);\n+\t\t\t\t\tpacket_write_fmt(1, \"ACK %s continue\\n\", last_hex);\n \t\t\t\telse if (have_obj.nr == 1)\n-\t\t\t\t\tpacket_write(1, \"ACK %s\\n\", last_hex);\n+\t\t\t\t\tpacket_write_fmt(1, \"ACK %s\\n\", last_hex);\n \t\t\t\tbreak;\n \t\t\t}\n \t\t\tcontinue;\n@@ -437,10 +437,10 @@ static int get_common_commits(void)\n \t\tif (!strcmp(line, \"done\")) {\n \t\t\tif (have_obj.nr > 0) {\n \t\t\t\tif (multi_ack)\n-\t\t\t\t\tpacket_write(1, \"ACK %s\\n\", last_hex);\n+\t\t\t\t\tpacket_write_fmt(1, \"ACK %s\\n\", last_hex);\n \t\t\t\treturn 0;\n \t\t\t}\n-\t\t\tpacket_write(1, \"NAK\\n\");\n+\t\t\tpacket_write_fmt(1, \"NAK\\n\");\n \t\t\treturn -1;\n \t\t}\n \t\tdie(\"git upload-pack: expected SHA1 list, got '%s'\", line);\n@@ -650,7 +650,7 @@ static void receive_needs(void)\n \t\twhile (result) {\n \t\t\tstruct object *object = &result->item->object;\n \t\t\tif (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {\n-\t\t\t\tpacket_write(1, \"shallow %s\",\n+\t\t\t\tpacket_write_fmt(1, \"shallow %s\",\n \t\t\t\t\t\toid_to_hex(&object->oid));\n \t\t\t\tregister_shallow(object->oid.hash);\n \t\t\t\tshallow_nr++;\n@@ -662,7 +662,7 @@ static void receive_needs(void)\n \t\t\tstruct object *object = shallows.objects[i].item;\n \t\t\tif (object->flags & NOT_SHALLOW) {\n \t\t\t\tstruct commit_list *parents;\n-\t\t\t\tpacket_write(1, \"unshallow %s\",\n+\t\t\t\tpacket_write_fmt(1, \"unshallow %s\",\n \t\t\t\t\toid_to_hex(&object->oid));\n \t\t\t\tobject->flags &= ~CLIENT_SHALLOW;\n \t\t\t\t/* make sure the real parents are parsed */\n@@ -741,7 +741,7 @@ static int send_ref(const char *refname, const struct object_id *oid,\n \t\tstruct strbuf symref_info = STRBUF_INIT;\n \n \t\tformat_symref_info(&symref_info, cb_data);\n-\t\tpacket_write(1, \"%s %s%c%s%s%s%s%s agent=%s\\n\",\n+\t\tpacket_write_fmt(1, \"%s %s%c%s%s%s%s%s agent=%s\\n\",\n \t\t\t     oid_to_hex(oid), refname_nons,\n \t\t\t     0, capabilities,\n \t\t\t     (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?\n@@ -753,11 +753,11 @@ static int send_ref(const char *refname, const struct object_id *oid,\n \t\t\t     git_user_agent_sanitized());\n \t\tstrbuf_release(&symref_info);\n \t} else {\n-\t\tpacket_write(1, \"%s %s\\n\", oid_to_hex(oid), refname_nons);\n+\t\tpacket_write_fmt(1, \"%s %s\\n\", oid_to_hex(oid), refname_nons);\n \t}\n \tcapabilities = NULL;\n \tif (!peel_ref(refname, peeled.hash))\n-\t\tpacket_write(1, \"%s %s^{}\\n\", oid_to_hex(&peeled), refname_nons);\n+\t\tpacket_write_fmt(1, \"%s %s^{}\\n\", oid_to_hex(&peeled), refname_nons);\n \treturn 0;\n }\n \n-- \n2.10.0\n\n"},{"id":"303288","messageId":"xmqqh98rud20.fsf@gitster.mtv.corp.google.com","threadId":"44216","inReplyTo":"20161004125947.67104-5-larsxschneider@gmail.com","subject":"Re: [PATCH v9 04/14] run-command: add wait_on_exit","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-10-04T19:30:47Z","receivedAt":"2016-10-04T19:30:55Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"larsxschneider@gmail.com writes:\n\n> From: Lars Schneider <larsxschneider@gmail.com>\n>\n> The flag 'clean_on_exit' kills child processes spawned by Git on exit.\n> A hard kill like this might not be desired in all cases.\n>\n> Add 'wait_on_exit' which closes the child's stdin on Git exit and waits\n> until the child process has terminated.\n>\n> The flag is used in a subsequent patch.\n>\n> Signed-off-by: Lars Schneider <larsxschneider@gmail.com>\n> ---\n>  run-command.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++--------\n>  run-command.h |  3 +++\n>  2 files changed, 50 insertions(+), 8 deletions(-)\n>\n> diff --git a/run-command.c b/run-command.c\n> index 3269362..96c54fe 100644\n> --- a/run-command.c\n> +++ b/run-command.c\n> @@ -21,6 +21,9 @@ void child_process_clear(struct child_process *child)\n>\n>  struct child_to_clean {\n>  \tpid_t pid;\n> +\tchar *name;\n> +\tint stdin;\n> +\tint wait;\n>  \tstruct child_to_clean *next;\n>  };\n>  static struct child_to_clean *children_to_clean;\n> @@ -28,12 +31,33 @@ static int installed_child_cleanup_handler;\n>\n>  static void cleanup_children(int sig, int in_signal)\n>  {\n> +\tint status;\n> +\tstruct child_to_clean *p = children_to_clean;\n> +\n> +\t/* Close the the child's stdin as indicator that Git will exit soon */\n> +\twhile (p) {\n> +\t\tif (p->wait)\n> +\t\t\tif (p->stdin > 0)\n> +\t\t\t\tclose(p->stdin);\n> +\t\tp = p->next;\n> +\t}\n\nThis part and the \"stdin\" field feels a bit too specific to the\ncaller you are adding.  Allowing the user of the API to specify what\nclean-up cation needs to be taken in the form of a callback function\nmay not be that much more effort and would be more flexible and\nuseful, I would imagine?\n"},{"id":"303289","messageId":"xmqqd1jfucwo.fsf@gitster.mtv.corp.google.com","threadId":"44216","inReplyTo":"20161004125947.67104-10-larsxschneider@gmail.com","subject":"Re: [PATCH v9 09/14] pkt-line: add packet_write_gently()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-10-04T19:33:59Z","receivedAt":"2016-10-04T19:34:08Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"larsxschneider@gmail.com writes:\n\n> From: Lars Schneider <larsxschneider@gmail.com>\n>\n> packet_write_fmt_gently() uses format_packet() which lets the caller\n> only send string data via \"%s\". That means it cannot be used for\n> arbitrary data that may contain NULs.\n>\n> Add packet_write_gently() which writes arbitrary data and does not die\n> in case of an error. The function is used by other pkt-line functions in\n> a subsequent patch.\n>\n> Signed-off-by: Lars Schneider <larsxschneider@gmail.com>\n> Signed-off-by: Junio C Hamano <gitster@pobox.com>\n> ---\n>  pkt-line.c | 16 ++++++++++++++++\n>  1 file changed, 16 insertions(+)\n>\n> diff --git a/pkt-line.c b/pkt-line.c\n> index 286eb09..3fd4dc0 100644\n> --- a/pkt-line.c\n> +++ b/pkt-line.c\n> @@ -171,6 +171,22 @@ int packet_write_fmt_gently(int fd, const char *fmt, ...)\n>  \treturn status;\n>  }\n>  \n> +static int packet_write_gently(const int fd_out, const char *buf, size_t size)\n> +{\n> +\tstatic char packet_write_buffer[LARGE_PACKET_MAX];\n> +\tconst size_t packet_size = size + 4;\n> +\n> +\tif (packet_size > sizeof(packet_write_buffer))\n> +\t\treturn error(\"packet write failed - data exceeds max packet size\");\n\nHmph, in the previous round, this used to be \"is the size larger\nthan sizeof(..) - 4?\", which avoided integer overflow issue rather\nnicely and more idiomatic.  If size is near the size_t's max,\npacket_size may wrap around to become very small, and we won't hit\nthis error, will we?\n\n> +\tpacket_trace(buf, size, 1);\n> +\tset_packet_header(packet_write_buffer, packet_size);\n> +\tmemcpy(packet_write_buffer + 4, buf, size);\n> +\tif (write_in_full(fd_out, packet_write_buffer, packet_size) == packet_size)\n> +\t\treturn 0;\n> +\treturn error(\"packet write failed\");\n> +}\n> +\n>  void packet_buf_write(struct strbuf *buf, const char *fmt, ...)\n>  {\n>  \tva_list args;\n"},{"id":"303292","messageId":"xmqq8tu3ubzl.fsf@gitster.mtv.corp.google.com","threadId":"44216","inReplyTo":"20161004125947.67104-11-larsxschneider@gmail.com","subject":"Re: [PATCH v9 10/14] pkt-line: add functions to read/write flush terminated packet streams","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-10-04T19:53:50Z","receivedAt":"2016-10-04T19:53:59Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"larsxschneider@gmail.com writes:\n\n> From: Lars Schneider <larsxschneider@gmail.com>\n>\n> write_packetized_from_fd() and write_packetized_from_buf() write a\n> stream of packets. All content packets use the maximal packet size\n> except for the last one. After the last content packet a `flush` control\n> packet is written.\n>\n> read_packetized_to_strbuf() reads arbitrary sized packets until it\n> detects a `flush` packet.\n>\n> Signed-off-by: Lars Schneider <larsxschneider@gmail.com>\n> Signed-off-by: Junio C Hamano <gitster@pobox.com>\n> ---\n>  pkt-line.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n>  pkt-line.h |  8 ++++++++\n>  2 files changed, 77 insertions(+)\n>\n> diff --git a/pkt-line.c b/pkt-line.c\n> index 3fd4dc0..8ffde22 100644\n> --- a/pkt-line.c\n> +++ b/pkt-line.c\n> @@ -196,6 +196,47 @@ void packet_buf_write(struct strbuf *buf, const char *fmt, ...)\n>  \tva_end(args);\n>  }\n>  \n> +int write_packetized_from_fd(int fd_in, int fd_out)\n> +{\n> +\tstatic char buf[LARGE_PACKET_DATA_MAX];\n> +\tint err = 0;\n> +\tssize_t bytes_to_write;\n> +\n> +\twhile (!err) {\n> +\t\tbytes_to_write = xread(fd_in, buf, sizeof(buf));\n> +\t\tif (bytes_to_write < 0)\n> +\t\t\treturn COPY_READ_ERROR;\n> +\t\tif (bytes_to_write == 0)\n> +\t\t\tbreak;\n> +\t\terr = packet_write_gently(fd_out, buf, bytes_to_write);\n> +\t}\n> +\tif (!err)\n> +\t\terr = packet_flush_gently(fd_out);\n> +\treturn err;\n> +}\n\nOK.\n\n> +int write_packetized_from_buf(const char *src_in, size_t len, int fd_out)\n> +{\n> +\tstatic char buf[LARGE_PACKET_DATA_MAX];\n> +\tint err = 0;\n> +\tsize_t bytes_written = 0;\n> +\tsize_t bytes_to_write;\n> +\n> +\twhile (!err) {\n> +\t\tif ((len - bytes_written) > sizeof(buf))\n> +\t\t\tbytes_to_write = sizeof(buf);\n> +\t\telse\n> +\t\t\tbytes_to_write = len - bytes_written;\n> +\t\tif (bytes_to_write == 0)\n> +\t\t\tbreak;\n> +\t\terr = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);\n> +\t\tbytes_written += bytes_to_write;\n> +\t}\n> +\tif (!err)\n> +\t\terr = packet_flush_gently(fd_out);\n> +\treturn err;\n> +}\n\nHmph, what is buf[] used for, other than its sizeof() taken to yield\na constant LARGE_PACKET_DATA_MAX?\n\n> @@ -305,3 +346,31 @@ char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)\n>  {\n>  \treturn packet_read_line_generic(-1, src, src_len, dst_len);\n>  }\n> +\n> +ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out)\n> +{\n> +\tint packet_len;\n> +\n> +\tsize_t orig_len = sb_out->len;\n> +\tsize_t orig_alloc = sb_out->alloc;\n> +\n> +\tfor (;;) {\n> +\t\tstrbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);\n> +\t\tpacket_len = packet_read(fd_in, NULL, NULL,\n> +\t\t\t// TODO: explain + 1\n\nNo // C99 comment please.\n\nAnd I agree that the +1 needs to be explained.\n\n> +\t\t\tsb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,\n> +\t\t\tPACKET_READ_GENTLE_ON_EOF);\n> +\t\tif (packet_len <= 0)\n> +\t\t\tbreak;\n\nHmph.  So at the end of a data stream, we ask packet_read() to read\n64kB or so, packet_read() gets the packet length by calling\nget_packet_data() and then another get_packet_data() reads that much\nand return.  What happens during the next round?  The first call to\nget_packet_data() in packet_read() will find that the stream has\nended and returns -1, which is stored in packet_len here?  But then\nthe data is discarded after the loop when packet_len is negative.\n\nI must be missing something.  Is the other side always supposed to\ngive a flush packet or something?  Perhaps that is what is happening\nhere.  If so, I am OK with that, even though it somehow sounds a bit\nwasteful.\n\n"},{"id":"303293","messageId":"xmqq1szvubsb.fsf@gitster.mtv.corp.google.com","threadId":"44216","inReplyTo":"xmqq8tu3ubzl.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v9 10/14] pkt-line: add functions to read/write flush terminated packet streams","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-10-04T19:58:12Z","receivedAt":"2016-10-04T19:58:20Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> I must be missing something.  Is the other side always supposed to\n> give a flush packet or something?  Perhaps that is what is happening\n> here.  If so, I am OK with that, even though it somehow sounds a bit\n> wasteful.\n\nAh, scratch that.  What I was missing was that this channel is\ndesigned to carry multiple interactions and delimiting with EOF\nwhich by definition can happen only once cannot be a norm.\nExpecting a flush to declare the current stream terminated is\nperfectly fine.\n"},{"id":"303410","messageId":"40D4374E-3784-4AF8-B9F3-E0C117A05631@gmail.com","threadId":"44216","inReplyTo":"xmqq8tu3ubzl.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v9 10/14] pkt-line: add functions to read/write flush terminated packet streams","fromName":"Lars Schneider","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-05T17:35:38Z","receivedAt":"2016-10-05T17:36:16Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"\n> On 04 Oct 2016, at 21:53, Junio C Hamano <gitster@pobox.com> wrote:\n> \n> larsxschneider@gmail.com writes:\n> \n>> From: Lars Schneider <larsxschneider@gmail.com>\n>> \n> \n>> +int write_packetized_from_buf(const char *src_in, size_t len, int fd_out)\n>> +{\n>> +\tstatic char buf[LARGE_PACKET_DATA_MAX];\n>> +\tint err = 0;\n>> +\tsize_t bytes_written = 0;\n>> +\tsize_t bytes_to_write;\n>> +\n>> +\twhile (!err) {\n>> +\t\tif ((len - bytes_written) > sizeof(buf))\n>> +\t\t\tbytes_to_write = sizeof(buf);\n>> +\t\telse\n>> +\t\t\tbytes_to_write = len - bytes_written;\n>> +\t\tif (bytes_to_write == 0)\n>> +\t\t\tbreak;\n>> +\t\terr = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);\n>> +\t\tbytes_written += bytes_to_write;\n>> +\t}\n>> +\tif (!err)\n>> +\t\terr = packet_flush_gently(fd_out);\n>> +\treturn err;\n>> +}\n> \n> Hmph, what is buf[] used for, other than its sizeof() taken to yield\n> a constant LARGE_PACKET_DATA_MAX?\n\nAgreed. This is stupid. I will fix it.\n\n\n>> \n>> +\tfor (;;) {\n>> +\t\tstrbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);\n>> +\t\tpacket_len = packet_read(fd_in, NULL, NULL,\n>> +\t\t\t// TODO: explain + 1\n> \n> No // C99 comment please.\n> \n> And I agree that the +1 needs to be explained.\n\nOh. I did not send the very latest version :-(\n\nIs this explanation OK?\n\n+       strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);\n+       packet_len = packet_read(fd_in, NULL, NULL,\n+           /* strbuf_grow() above always allocates one extra byte to\n+            * store a '\\0' at the end of the string. packet_read()\n+            * writes a '\\0' extra byte at the end, too. Let it know\n+            * that there is already room for the extra byte.\n+            */\n+           sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,\n+           PACKET_READ_GENTLE_ON_EOF);\n\n\nThanks,\nLars"},{"id":"303425","messageId":"B1968DDD-7EAA-477B-B088-5F2A83F29C8C@gmail.com","threadId":"44216","inReplyTo":"xmqqd1jfucwo.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v9 09/14] pkt-line: add packet_write_gently()","fromName":"Lars Schneider","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-05T19:06:23Z","receivedAt":"2016-10-05T19:06:31Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"\n> On 04 Oct 2016, at 21:33, Junio C Hamano <gitster@pobox.com> wrote:\n> \n> larsxschneider@gmail.com writes:\n> \n>> From: Lars Schneider <larsxschneider@gmail.com>\n>> \n>> \n>> +static int packet_write_gently(const int fd_out, const char *buf, size_t size)\n>> +{\n>> +\tstatic char packet_write_buffer[LARGE_PACKET_MAX];\n>> +\tconst size_t packet_size = size + 4;\n>> +\n>> +\tif (packet_size > sizeof(packet_write_buffer))\n>> +\t\treturn error(\"packet write failed - data exceeds max packet size\");\n> \n> Hmph, in the previous round, this used to be \"is the size larger\n> than sizeof(..) - 4?\", which avoided integer overflow issue rather\n> nicely and more idiomatic.  If size is near the size_t's max,\n> packet_size may wrap around to become very small, and we won't hit\n> this error, will we?\n\nYou are right. Would the solution below be acceptable?\nI would like to keep the `packet_size` variable as it eases the rest\nof the function.\n\n \n \tconst size_t packet_size = size + 4;\n \n-\tif (packet_size > sizeof(packet_write_buffer))\n+\tif (size > sizeof(packet_write_buffer) - 4)\n \t\treturn error(\"packet write failed - data exceeds max packet size\");\n\nThanks,\nLars"},{"id":"303437","messageId":"1FD7FB64-0F40-47F0-A047-25B91B170E66@gmail.com","threadId":"44216","inReplyTo":"xmqqh98rud20.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v9 04/14] run-command: add wait_on_exit","fromName":"Lars Schneider","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-05T20:57:07Z","receivedAt":"2016-10-05T20:57:14Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"\n> On 04 Oct 2016, at 21:30, Junio C Hamano <gitster@pobox.com> wrote:\n> \n> larsxschneider@gmail.com writes:\n> \n>> From: Lars Schneider <larsxschneider@gmail.com>\n>> \n>> The flag 'clean_on_exit' kills child processes spawned by Git on exit.\n>> A hard kill like this might not be desired in all cases.\n>> \n>> Add 'wait_on_exit' which closes the child's stdin on Git exit and waits\n>> until the child process has terminated.\n>> \n>> The flag is used in a subsequent patch.\n>> \n>> Signed-off-by: Lars Schneider <larsxschneider@gmail.com>\n>> ---\n>> ...\n>> static void cleanup_children(int sig, int in_signal)\n>> {\n>> +\tint status;\n>> +\tstruct child_to_clean *p = children_to_clean;\n>> +\n>> +\t/* Close the the child's stdin as indicator that Git will exit soon */\n>> +\twhile (p) {\n>> +\t\tif (p->wait)\n>> +\t\t\tif (p->stdin > 0)\n>> +\t\t\t\tclose(p->stdin);\n>> +\t\tp = p->next;\n>> +\t}\n> \n> This part and the \"stdin\" field feels a bit too specific to the\n> caller you are adding.  Allowing the user of the API to specify what\n> clean-up cation needs to be taken in the form of a callback function\n> may not be that much more effort and would be more flexible and\n> useful, I would imagine?\n\nOK. Something like the patch below would work nicely.\nDoes this look acceptable?\n\nThanks,\nLars\n\n\ndiff --git a/run-command.c b/run-command.c\nindex 3269362..a0256a6 100644\n--- a/run-command.c\n+++ b/run-command.c\n@@ -21,6 +21,7 @@ void child_process_clear(struct child_process *child)\n \n struct child_to_clean {\n \tpid_t pid;\n+\tvoid (*clean_on_exit_handler)(pid_t, int);\n \tstruct child_to_clean *next;\n };\n static struct child_to_clean *children_to_clean;\n@@ -31,6 +32,11 @@ static void cleanup_children(int sig, int in_signal)\n \twhile (children_to_clean) {\n \t\tstruct child_to_clean *p = children_to_clean;\n \t\tchildren_to_clean = p->next;\n+\n+\t\tif (p->clean_on_exit_handler) {\n+\t\t\tp->clean_on_exit_handler(p->pid, in_signal);\n+\t\t}\n+\n \t\tkill(p->pid, sig);\n \t\tif (!in_signal)\n \t\t\tfree(p);\n@@ -49,10 +55,11 @@ static void cleanup_children_on_exit(void)\n \tcleanup_children(SIGTERM, 0);\n }\n \n-static void mark_child_for_cleanup(pid_t pid)\n+static void mark_child_for_cleanup(pid_t pid, void (*clean_on_exit_handler)(pid_t, int))\n {\n \tstruct child_to_clean *p = xmalloc(sizeof(*p));\n \tp->pid = pid;\n+\tp->clean_on_exit_handler = clean_on_exit_handler;\n \tp->next = children_to_clean;\n \tchildren_to_clean = p;\n \n@@ -421,8 +428,8 @@ int start_command(struct child_process *cmd)\n \t}\n \tif (cmd->pid < 0)\n \t\terror_errno(\"cannot fork() for %s\", cmd->argv[0]);\n-\telse if (cmd->clean_on_exit)\n-\t\tmark_child_for_cleanup(cmd->pid);\n+\telse if (cmd->clean_on_exit || cmd->clean_on_exit_handler)\n+\t\tmark_child_for_cleanup(cmd->pid, cmd->clean_on_exit_handler);\n \n \t/*\n \t * Wait for child's execvp. If the execvp succeeds (or if fork()\n@@ -482,8 +489,8 @@ int start_command(struct child_process *cmd)\n \tfailed_errno = errno;\n \tif (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))\n \t\terror_errno(\"cannot spawn %s\", cmd->argv[0]);\n-\tif (cmd->clean_on_exit && cmd->pid >= 0)\n-\t\tmark_child_for_cleanup(cmd->pid);\n+\tif ((cmd->clean_on_exit || cmd->clean_on_exit_handler) && cmd->pid >= 0)\n+\t\tmark_child_for_cleanup(cmd->pid, cmd->clean_on_exit_handler);\n \n \targv_array_clear(&nargv);\n \tcmd->argv = sargv;\n@@ -765,7 +772,7 @@ int start_async(struct async *async)\n \t\texit(!!async->proc(proc_in, proc_out, async->data));\n \t}\n \n-\tmark_child_for_cleanup(async->pid);\n+\tmark_child_for_cleanup(async->pid, NULL);\n \n \tif (need_in)\n \t\tclose(fdin[0]);\ndiff --git a/run-command.h b/run-command.h\nindex cf29a31..3630733 100644\n--- a/run-command.h\n+++ b/run-command.h\n@@ -43,6 +43,7 @@ struct child_process {\n \tunsigned stdout_to_stderr:1;\n \tunsigned use_shell:1;\n \tunsigned clean_on_exit:1;\n+\tvoid (*clean_on_exit_handler)(pid_t, int);\n };\n \n #define CHILD_PROCESS_INIT { NULL, ARGV_ARRAY_INIT, ARGV_ARRAY_INIT }\n \n"},{"id":"303441","messageId":"xmqqeg3uo5yt.fsf@gitster.mtv.corp.google.com","threadId":"44216","inReplyTo":"1FD7FB64-0F40-47F0-A047-25B91B170E66@gmail.com","subject":"Re: [PATCH v9 04/14] run-command: add wait_on_exit","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-10-05T21:12:42Z","receivedAt":"2016-10-05T21:12:51Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Lars Schneider <larsxschneider@gmail.com> writes:\n\n> OK. Something like the patch below would work nicely.\n\nYeah, something along that line; it would eliminate the need to\nworry about a field named \"stdin\" ;-)\n\nBut ...\n\n>  \twhile (children_to_clean) {\n>  \t\tstruct child_to_clean *p = children_to_clean;\n>  \t\tchildren_to_clean = p->next;\n> +\n> +\t\tif (p->clean_on_exit_handler) {\n> +\t\t\tp->clean_on_exit_handler(p->pid, in_signal);\n> +\t\t}\n\n... the application that used run_command() API would want to be\nable to pass extra piece of data that is appliation-specific for the\nchild being killed, so it may make sense to extend the function\nsignature to take a pointer to \"struct child_process\" for the child\nprocess being killed, together with a new field added to \"struct\nchild_process\" that is \"void *exit_handler_cbdata;\", perhaps?\n\n"},{"id":"303452","messageId":"4de1976c-3b76-1131-e728-057b143c9988@gmail.com","threadId":"44216","inReplyTo":"20161004125947.67104-1-larsxschneider@gmail.com","subject":"Re: [PATCH v9 00/14] Git filter protocol","fromName":"Jakub Narębski","fromEmail":"jnareb@gmail.com","sentAt":"2016-10-05T22:31:14Z","receivedAt":"2016-10-05T22:31:39Z","isPatch":true,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"W dniu 04.10.2016 o 14:59, larsxschneider@gmail.com pisze:\n> From: Lars Schneider <larsxschneider@gmail.com>\n> \n> The goal of this series is to avoid launching a new clean/smudge filter\n> process for each file that is filtered.\n> \n> A short summary about v1 to v5 can be found here:\n> https://git.github.io/rev_news/2016/08/17/edition-18/\n> \n> This series is also published on web:\n> https://github.com/larsxschneider/git/pull/13\n> \n> Patches 1 and 2 are cleanups and not strictly necessary for the series.\n> Patches 3 to 12 are required preparation. Patch 13 is the main patch.\n> Patch 14 adds an example how to use the Git filter protocol in contrib.\n> \n> Thanks a lot to\n>   Ramsay, Jakub, Junio, Johannes, Torsten, and Peff\n> for very helpful reviews,\n> Lars\n\nI'll try to review it before the end of the week.\n\n-- \nJakub Narębski\n\n"},{"id":"303470","messageId":"alpine.DEB.2.20.1610061129430.35196@virtualbox","threadId":"44216","inReplyTo":"xmqqeg3uo5yt.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v9 04/14] run-command: add wait_on_exit","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2016-10-06T09:32:47Z","receivedAt":"2016-10-06T09:33:35Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Junio & Lars,\n\nOn Wed, 5 Oct 2016, Junio C Hamano wrote:\n\n> Lars Schneider <larsxschneider@gmail.com> writes:\n> \n> > OK. Something like the patch below would work nicely.\n> \n> Yeah, something along that line; it would eliminate the need to\n> worry about a field named \"stdin\" ;-)\n\nNot only a need to worry. Git for Windows' SDK's headers define\n\n\t#define stdin (&__iob_func()[0])\n\nleading to the compile error\n\n\tIn file included from git-compat-util.h:159:0,\n                 from cache.h:4,\n                 from run-command.c:1:\n\trun-command.c:25:6: error: expected identifier or '(' before '&' token\n\t  int stdin;\n\t      ^\n\nI meant to investigate this build failure of `pu` earlier but only got\naround to do it today.\n\nCiao,\nDscho\n"},{"id":"303471","messageId":"6FEA8850-1031-4FCA-8DF2-765B752973DF@gmail.com","threadId":"44216","inReplyTo":"alpine.DEB.2.20.1610061129430.35196@virtualbox","subject":"Re: [PATCH v9 04/14] run-command: add wait_on_exit","fromName":"Lars Schneider","fromEmail":"larsxschneider@gmail.com","sentAt":"2016-10-06T09:35:56Z","receivedAt":"2016-10-06T09:36:05Z","isPatch":true,"sender":{"key":"larsxschneider@gmail.com","avatar":"https://avatars.githubusercontent.com/u/477434?v=4"},"body":"\n> On 06 Oct 2016, at 11:32, Johannes Schindelin <johannes.schindelin@gmx.de> wrote:\n> \n> Hi Junio & Lars,\n> \n> On Wed, 5 Oct 2016, Junio C Hamano wrote:\n> \n>> Lars Schneider <larsxschneider@gmail.com> writes:\n>> \n>>> OK. Something like the patch below would work nicely.\n>> \n>> Yeah, something along that line; it would eliminate the need to\n>> worry about a field named \"stdin\" ;-)\n> \n> Not only a need to worry. Git for Windows' SDK's headers define\n> \n> \t#define stdin (&__iob_func()[0])\n> \n> leading to the compile error\n> \n> \tIn file included from git-compat-util.h:159:0,\n>                 from cache.h:4,\n>                 from run-command.c:1:\n> \trun-command.c:25:6: error: expected identifier or '(' before '&' token\n> \t  int stdin;\n> \t      ^\n> \n> I meant to investigate this build failure of `pu` earlier but only got\n> around to do it today.\n> \n> Ciao,\n> Dscho\n\nSorry for the trouble. The \"stdin\" will go away in the next round\nas we agreed on a more generic solution:\nhttp://public-inbox.org/git/1FD7FB64-0F40-47F0-A047-25B91B170E66@gmail.com/\n\n- Lars\n\n"},{"id":"303491","messageId":"xmqqk2dlmq2x.fsf@gitster.mtv.corp.google.com","threadId":"44216","inReplyTo":"alpine.DEB.2.20.1610061129430.35196@virtualbox","subject":"Re: [PATCH v9 04/14] run-command: add wait_on_exit","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-10-06T15:53:26Z","receivedAt":"2016-10-06T15:53:36Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:\n\n> Hi Junio & Lars,\n>\n> On Wed, 5 Oct 2016, Junio C Hamano wrote:\n>\n>> Lars Schneider <larsxschneider@gmail.com> writes:\n>> \n>> > OK. Something like the patch below would work nicely.\n>> \n>> Yeah, something along that line; it would eliminate the need to\n>> worry about a field named \"stdin\" ;-)\n>\n> Not only a need to worry....\n\nThanks, but I (and more importantly Lars, too) knew that stdin is\nproblematic when I sent the message you are responding to, as raised\nby Ramsay in a separate thread:\n\n    <f9b6abf6-8305-4a32-29af-765f08fee94b@ramsayjones.plus.com>\n\n"},{"id":"303505","messageId":"xmqqy421l78m.fsf@gitster.mtv.corp.google.com","threadId":"44216","inReplyTo":"B1968DDD-7EAA-477B-B088-5F2A83F29C8C@gmail.com","subject":"Re: [PATCH v9 09/14] pkt-line: add packet_write_gently()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-10-06T17:25:45Z","receivedAt":"2016-10-06T17:26:00Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Lars Schneider <larsxschneider@gmail.com> writes:\n\n> You are right. Would the solution below be acceptable?\n> I would like to keep the `packet_size` variable as it eases the rest\n> of the function.\n>\n>  \n>  \tconst size_t packet_size = size + 4;\n>  \n> -\tif (packet_size > sizeof(packet_write_buffer))\n> +\tif (size > sizeof(packet_write_buffer) - 4)\n>  \t\treturn error(\"packet write failed - data exceeds max packet size\");\n\nSounds fine; packet_size may have invalid value if size is large\nenough but in such a case the function would return without using\nit, so no harm is expected, I would say.  I'd prefer to see the\ndefinition of packet_size separate from the assignment of size + 4\nto it in a case like this, though.\n\n"}]}