{"thread":{"id":"42642","subject":"[PATCH v2 0/4] extend smudge/clean filters with direct file access","startedAt":"2016-06-17T20:31:58Z","lastAt":"2016-06-17T22:43:58Z","messageCount":8,"participants":["Joey Hess","Junio C Hamano"],"isPatch":true,"patchVersion":2,"patchTotal":4},"messages":[{"id":"289505","messageId":"1466195481-23209-1-git-send-email-joeyh@joeyh.name","threadId":"42642","inReplyTo":null,"subject":"[PATCH v2 0/4] extend smudge/clean filters with direct file access","fromName":"Joey Hess","fromEmail":"joeyh@joeyh.name","sentAt":"2016-06-17T20:31:17Z","receivedAt":"2016-06-17T20:31:58Z","isPatch":true,"sender":{"key":"joeyh@joeyh.name","avatar":"https://avatars.githubusercontent.com/u/16392?v=4"},"body":"Reroll of this patch set with changes:\n\n* Renamed the new filter drivers for consistency with other configs.\n* Improved documentation with feedback from Junio and others.\n* Eliminated %p and instead append the filename to the commands\n  (separated by a space).\n* Fixed an FD leak and a space leak.\n* Only use smudgeToFile with regular files, not symlinks.\n* After running the smudgeToFile command, double-check that the\n  expected file is present, in case the command was buggy and deleted it.\n* Added a warning message when the new filter commands are configured\n  but the old ones are not, so that the user knows it's refusing to use\n  their configuration.\n\nThere's been good and helpful documentation and interface review,\nbut some more code review would be good! Also, git-annex has a\nimproved-smudge-filters branch now that demonstrates this interface.\n\nJoey Hess (4):\n  add smudgeToFile and cleanFromFile filter configs\n  use cleanFromFile in git add\n  use smudgeToFile in git checkout etc\n  warn on unusable smudgeToFile/cleanFromFile config\n\n Documentation/config.txt        |  18 +++++-\n Documentation/gitattributes.txt |  37 ++++++++++++\n convert.c                       | 126 +++++++++++++++++++++++++++++++++++-----\n convert.h                       |  10 ++++\n entry.c                         |  37 +++++++++---\n sha1_file.c                     |  42 ++++++++++++--\n t/t0021-conversion.sh           |  64 ++++++++++++++++++++\n 7 files changed, 304 insertions(+), 30 deletions(-)\n\n-- \n2.8.1\n\n"},{"id":"289506","messageId":"1466195481-23209-5-git-send-email-joeyh@joeyh.name","threadId":"42642","inReplyTo":"1466195481-23209-1-git-send-email-joeyh@joeyh.name","subject":"[PATCH v2 4/4] warn on unusable smudgeToFile/cleanFromFile config","fromName":"Joey Hess","fromEmail":"joeyh@joeyh.name","sentAt":"2016-06-17T20:31:21Z","receivedAt":"2016-06-17T20:32:06Z","isPatch":true,"sender":{"key":"joeyh@joeyh.name","avatar":"https://avatars.githubusercontent.com/u/16392?v=4"},"body":"Let the user know when they have a smudgeToFile/cleanFromFile config\nthat cannot be used because the corresponding smudge/clean config\nis missing.\n\nThe warning is only displayed a maximum of once per git invocation,\nand only when doing an operation that would use the filter.\n\nSigned-off-by: Joey Hess <joeyh@joeyh.name>\n---\n convert.c | 34 ++++++++++++++++++++++++++--------\n 1 file changed, 26 insertions(+), 8 deletions(-)\n\ndiff --git a/convert.c b/convert.c\nindex bf63ba0..84f6bc5 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -847,32 +847,50 @@ int would_convert_to_git_filter_fd(const char *path)\n \treturn apply_filter(path, NULL, NULL, 0, -1, NULL, ca.drv->clean);\n }\n \n+static int can_filter_file(const char *filefilter, const char *filefiltername,\n+\t\t\t   const char *stdiofilter, const char *stdiofiltername,\n+\t\t\t   const struct conv_attrs *ca,\n+\t\t\t   int *warncount)\n+{\n+\tif (! filefilter)\n+\t\treturn 0;\n+\n+\tif (stdiofilter)\n+\t\treturn 1;\n+\n+\tif (*warncount == 0)\n+\t\twarning(\"Not running your configured filter.%s.%s command, because filter.%s.%s is not configured\",\n+\t\t\tca->drv->name, filefiltername,\n+\t\t\tca->drv->name, stdiofiltername);\n+\t\t*warncount=*warncount+1;\n+\t\n+\treturn 0;\n+}\n+\n int can_clean_from_file(const char *path)\n {\n \tstruct conv_attrs ca;\n+\tstatic int warncount = 0;\n \n \tconvert_attrs(&ca, path);\n \tif (!ca.drv)\n \t\treturn 0;\n \n-\t/* Only use the cleanFromFile filter when the clean filter is also\n-\t * configured.\n-\t */\n-\treturn (ca.drv->clean_from_file && ca.drv->clean);\n+\treturn can_filter_file(ca.drv->clean_from_file, \"cleanFromFile\",\n+\t\t\t       ca.drv->clean, \"clean\", &ca, &warncount);\n }\n \n int can_smudge_to_file(const char *path)\n {\n \tstruct conv_attrs ca;\n+\tstatic int warncount = 0;\n \n \tconvert_attrs(&ca, path);\n \tif (!ca.drv)\n \t\treturn 0;\n \n-\t/* Only use the smudgeToFile filter when the smudge filter is also\n-\t * configured.\n-\t */\n-\treturn (ca.drv->smudge_to_file && ca.drv->smudge);\n+\treturn can_filter_file(ca.drv->smudge_to_file, \"smudgeToFile\",\n+\t\t\t       ca.drv->smudge, \"smudge\", &ca, &warncount);\n }\n \n const char *get_convert_attr_ascii(const char *path)\n-- \n2.8.1\n\n"},{"id":"289507","messageId":"1466195481-23209-4-git-send-email-joeyh@joeyh.name","threadId":"42642","inReplyTo":"1466195481-23209-1-git-send-email-joeyh@joeyh.name","subject":"[PATCH v2 3/4] use smudgeToFile in git checkout etc","fromName":"Joey Hess","fromEmail":"joeyh@joeyh.name","sentAt":"2016-06-17T20:31:20Z","receivedAt":"2016-06-17T20:32:11Z","isPatch":true,"sender":{"key":"joeyh@joeyh.name","avatar":"https://avatars.githubusercontent.com/u/16392?v=4"},"body":"This makes git checkout, git reset, etc use smudgeToFile.\n\nIncludes test cases.\n\n(There's a call to convert_to_working_tree in merge-recursive.c\nthat could also be made to use smudgeToFile as well.)\n\nSigned-off-by: Joey Hess <joeyh@joeyh.name>\n---\n entry.c               | 37 +++++++++++++++++++++++++++++--------\n t/t0021-conversion.sh | 38 +++++++++++++++++++++++++++++++++-----\n 2 files changed, 62 insertions(+), 13 deletions(-)\n\ndiff --git a/entry.c b/entry.c\nindex 519e042..97975e5 100644\n--- a/entry.c\n+++ b/entry.c\n@@ -175,8 +175,13 @@ static int write_entry(struct cache_entry *ce,\n \n \t\t/*\n \t\t * Convert from git internal format to working tree format\n+\t\t * unless the smudgeToFile filter can write to the\n+\t\t * file directly.\n \t\t */\n-\t\tif (ce_mode_s_ifmt == S_IFREG &&\n+\t\tint regular_file = ce_mode_s_ifmt == S_IFREG;\n+\t\tint smudge_to_file = regular_file\n+\t\t\t&& can_smudge_to_file(ce->name);\n+\t\tif (regular_file && ! smudge_to_file &&\n \t\t    convert_to_working_tree(ce->name, new, size, &buf)) {\n \t\t\tfree(new);\n \t\t\tnew = strbuf_detach(&buf, &newsize);\n@@ -189,13 +194,29 @@ static int write_entry(struct cache_entry *ce,\n \t\t\treturn error_errno(\"unable to create file %s\", path);\n \t\t}\n \n-\t\twrote = write_in_full(fd, new, size);\n-\t\tif (!to_tempfile)\n-\t\t\tfstat_done = fstat_output(fd, state, &st);\n-\t\tclose(fd);\n-\t\tfree(new);\n-\t\tif (wrote != size)\n-\t\t\treturn error(\"unable to write file %s\", path);\n+\t\tif (! smudge_to_file) {\n+\t\t\twrote = write_in_full(fd, new, size);\n+\t\t\tif (!to_tempfile)\n+\t\t\t\tfstat_done = fstat_output(fd, state, &st);\n+\t\t\tclose(fd);\n+\t\t\tfree(new);\n+\t\t\tif (wrote != size)\n+\t\t\t\treturn error(\"unable to write file %s\", path);\n+\t\t}\n+\t\telse {\n+\t\t\tclose(fd);\n+\t\t\tconvert_to_working_tree_filter_to_file(ce->name, path, new, size);\n+\t\t\tfree(new);\n+\t\t\t/* The smudgeToFile filter may have replaced the\n+\t\t\t * file; open it to make sure that the file\n+\t\t\t * exists. */\n+\t\t\tfd = open(path, O_RDONLY);\n+\t\t\tif (fd < 0)\n+\t\t\t\treturn error_errno(\"unable to create file %s\", path);\n+\t\t\tif (!to_tempfile)\n+\t\t\t\tfstat_done = fstat_output(fd, state, &st);\n+\t\t\tclose(fd);\n+\t\t}\n \t\tbreak;\n \tcase S_IFGITLINK:\n \t\tif (to_tempfile)\ndiff --git a/t/t0021-conversion.sh b/t/t0021-conversion.sh\nindex 399f92b..a8042d1 100755\n--- a/t/t0021-conversion.sh\n+++ b/t/t0021-conversion.sh\n@@ -14,12 +14,20 @@ chmod +x rot13.sh\n \n cat <<EOF >rot13-from-file.sh\n #!$SHELL_PATH\n-fsfile=\"\\$1\"\n+srcfile=\"\\$1\"\n touch rot13-from-file.ran\n-cat \"\\$fsfile\" | ./rot13.sh\n+cat \"\\$srcfile\" | ./rot13.sh\n EOF\n chmod +x rot13-from-file.sh\n \n+cat <<EOF >rot13-to-file.sh\n+#!$SHELL_PATH\n+destfile=\"\\$1\"\n+touch rot13-to-file.ran\n+./rot13.sh > \"\\$destfile\"\n+EOF\n+chmod +x rot13-to-file.sh\n+\n test_expect_success setup '\n \tgit config filter.rot13.smudge ./rot13.sh &&\n \tgit config filter.rot13.clean ./rot13.sh &&\n@@ -291,6 +299,17 @@ test_expect_success 'cleanFromFile filter is used when adding a file' '\n \tcmp test.t fstest.t\n '\n \n+test_expect_success 'smudgeToFile filter is used when checking out a file' '\n+\ttest_config filter.rot13.smudgeToFile ./rot13-to-file.sh &&\n+\n+\trm -f fstest.t &&\n+\tgit checkout -- fstest.t &&\n+\tcmp test.t fstest.t &&\n+\n+\ttest -e rot13-to-file.ran &&\n+\trm -f rot13-to-file.ran\n+'\n+\n test_expect_success 'cleanFromFile filter is not used when clean filter is not configured' '\n \ttest_config filter.noclean.smudge ./rot13.sh &&\n \ttest_config filter.noclean.cleanFromFile ./rot13-from-file.sh &&\n@@ -299,9 +318,18 @@ test_expect_success 'cleanFromFile filter is not used when clean filter is not c\n \n \tcat test.t >test.no &&\n \tgit add test.no &&\n-\ttest ! -e rot13-from-file.ran &&\n-\tgit cat-file blob :test.no >actual &&\n-\tcmp test.t actual\n+\ttest ! -e rot13-from-file.ran\n+'\n+\n+test_expect_success 'smudgeToFile filter is not used when smudge filter is not configured' '\n+\ttest_config filter.nosmudge.clean ./rot13.sh &&\n+\ttest_config filter.nosmudge.smudgeToFile ./rot13-to-file.sh &&\n+\n+\techo \"*.no filter=nosmudge\" >.gitattributes &&\n+\n+\trm -f fstest.t &&\n+\tgit checkout -- fstest.t &&\n+\ttest ! -e rot13-to-file.ran\n '\n \n test_done\n-- \n2.8.1\n\n"},{"id":"289508","messageId":"1466195481-23209-2-git-send-email-joeyh@joeyh.name","threadId":"42642","inReplyTo":"1466195481-23209-1-git-send-email-joeyh@joeyh.name","subject":"[PATCH v2 1/4] add smudgeToFile and cleanFromFile filter configs","fromName":"Joey Hess","fromEmail":"joeyh@joeyh.name","sentAt":"2016-06-17T20:31:18Z","receivedAt":"2016-06-17T20:32:17Z","isPatch":true,"sender":{"key":"joeyh@joeyh.name","avatar":"https://avatars.githubusercontent.com/u/16392?v=4"},"body":"This adds new smudgeToFile and cleanFromFile filter commands,\nwhich are similar to smudge and clean but allow direct access to files on\ndisk.\n\nThis interface can be much more efficient when operating on large files,\nbecause the whole file content does not need to be streamed through the\nfilter. It even allows for things like cleanFromFile commands that avoid\nreading the whole content of the file, and for smudgeToFile commands that\npopulate a work tree file using an efficient Copy On Write operation.\n\nThe new filter commands will not be used for all filtering. They are\nefficient to use when git add is adding a file, or when the work tree is\nbeing updated, but not a good fit when git is internally filtering blob\nobjects in memory for eg, a diff.\n\nSo, a user who wants to use smudgeToFile should also provide a smudge\ncommand to be used in cases where smudgeToFile is not used. And ditto\nwith cleanFromFile and clean. To avoid foot-shooting configurations, the\nnew commands are not used unless the old commands are also configured.\n\nThat also ensures that a filter driver configuration that includes these\nnew commands will work, although less efficiently, when used with an older\nversion of git that does not support them.\n\nSigned-off-by: Joey Hess <joeyh@joeyh.name>\n---\n Documentation/config.txt        |  18 ++++++-\n Documentation/gitattributes.txt |  37 ++++++++++++++\n convert.c                       | 108 ++++++++++++++++++++++++++++++++++------\n convert.h                       |  10 ++++\n 4 files changed, 157 insertions(+), 16 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex 2e1b2e4..38f54c1 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -1299,15 +1299,29 @@ format.useAutoBase::\n \tformat-patch by default.\n \n filter.<driver>.clean::\n-\tThe command which is used to convert the content of a worktree\n+\tThe command which is used as a filter to convert the content of a worktree\n \tfile to a blob upon checkin.  See linkgit:gitattributes[5] for\n \tdetails.\n \n filter.<driver>.smudge::\n-\tThe command which is used to convert the content of a blob\n+\tThe command which is used as a filter to convert the content of a blob\n \tobject to a worktree file upon checkout.  See\n \tlinkgit:gitattributes[5] for details.\n \n+filter.<driver>.cleanFromFile::\n+\tSimilar to filter.<driver>.clean but the specified command \n+\tdirectly accesses a worktree file on disk, rather than\n+\treceiving the file content from standard input. \n+\tOnly used when filter.<driver>.clean is also configured.\n+\tSee linkgit:gitattributes[5] for details.\n+\n+filter.<driver>.smudgeToFile::\n+\tSimilar to filter.<driver>.smudge but the specified command\n+\twrites the content of a blob directly to a worktree file,\n+\trather than to standard output.\n+\tOnly used when filter.<driver>.smudge is also configured.\n+\tSee linkgit:gitattributes[5] for details.\n+\n fsck.<msg-id>::\n \tAllows overriding the message type (error, warn or ignore) of a\n \tspecific message ID such as `missingEmail`.\ndiff --git a/Documentation/gitattributes.txt b/Documentation/gitattributes.txt\nindex 145dd10..5ae0783 100644\n--- a/Documentation/gitattributes.txt\n+++ b/Documentation/gitattributes.txt\n@@ -380,6 +380,43 @@ 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+There are two extra commands \"cleanFromFile\" and \"smudgeToFile\", which\n+can optionally be set in a filter driver. These are similar to the \"clean\"\n+and \"smudge\" commands, but avoid needing to pipe the contents of files\n+through the filters, and instead read/write files in the filesystem.\n+This can be more efficient when using filters with large files that are not\n+directly stored in the repository.\n+\n+Both \"cleanFromFile\" and \"smudgeToFile\" are provided a path as an\n+added parameter after the configured command line.\n+\n+The \"cleanFromFile\" command is provided the path to the file that\n+it should clean. Like the \"clean\" command, it should output the cleaned\n+version to standard output.\n+\n+The \"smudgeToFile\" command is provided a path to the file that it\n+should write to. (This file will already exist, as an empty file that can\n+be written to or replaced.) Like the \"smudge\" command, \"smudgeToFile\"\n+is fed the blob object from its standard input.\n+\n+Some git operations that need to apply filters cannot use \"cleanFromFile\"\n+and \"smudgeToFile\", since the files are not present to disk. So, to avoid\n+inconsistent behavior, \"cleanFromFile\" will only be used if \"clean\" is\n+also configured, and \"smudgeToFile\" will only be used if \"smudge\" is also\n+configured.\n+\n+An example large file storage filter driver using cleanFromFile and\n+smudgeToFile follows:\n+\n+------------------------\n+[filter \"bigfiles\"]\n+\tclean = store-bigfile --from-stdin\n+\tcleanFromFile = store-bigfile --from-file\n+\tsmudge = retrieve-bigfile --to-stdout\n+\tsmudgeToFile = retrieve-bigfile --to-file\n+\trequired\n+------------------------\n+\n Interaction between checkin/checkout attributes\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n \ndiff --git a/convert.c b/convert.c\nindex b1614bf..bf63ba0 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -360,7 +360,8 @@ struct filter_params {\n \tunsigned long size;\n \tint fd;\n \tconst char *cmd;\n-\tconst char *path;\n+\tconst char *path; /* Path within the git repository */\n+\tconst char *fspath; /* Path to file on disk */\n };\n \n static int filter_buffer_or_fd(int in, int out, void *data)\n@@ -389,6 +390,15 @@ static int filter_buffer_or_fd(int in, int out, void *data)\n \tstrbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);\n \tstrbuf_release(&path);\n \n+\t/* append fspath to the command if it's set, separated with a space */\n+\tif (params->fspath) {\n+\t\tstruct strbuf fspath = STRBUF_INIT;\n+\t\tsq_quote_buf(&fspath, params->fspath);\n+\t\tstrbuf_addstr(&cmd, \" \");\n+\t\tstrbuf_addbuf(&cmd, &fspath);\n+\t\tstrbuf_release(&fspath);\n+\t}\n+\n \targv[0] = cmd.buf;\n \n \tchild_process.argv = argv;\n@@ -427,7 +437,8 @@ 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_filter(const char *path, const char *fspath,\n+\t\t\tconst char *src, size_t len, int fd,\n                         struct strbuf *dst, const char *cmd)\n {\n \t/*\n@@ -456,6 +467,7 @@ static int apply_filter(const char *path, const char *src, size_t len, int fd,\n \tparams.fd = fd;\n \tparams.cmd = cmd;\n \tparams.path = path;\n+\tparams.fspath = fspath;\n \n \tfflush(NULL);\n \tif (start_async(&async))\n@@ -486,6 +498,8 @@ static struct convert_driver {\n \tstruct convert_driver *next;\n \tconst char *smudge;\n \tconst char *clean;\n+\tconst char *smudge_to_file;\n+\tconst char *clean_from_file;\n \tint required;\n } *user_convert, **user_convert_tail;\n \n@@ -512,8 +526,9 @@ static int read_convert_config(const char *var, const char *value, void *cb)\n \t}\n \n \t/*\n-\t * filter.<name>.smudge and filter.<name>.clean specifies\n-\t * the command line:\n+\t * filter.<name>.smudge, filter.<name>.clean,\n+\t * filter.<name>.smudgeToFile, filter.<name>.cleanFromFile\n+\t * specifies the command line:\n \t *\n \t *\tcommand-line\n \t *\n@@ -526,6 +541,12 @@ 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(\"smudgetofile\", key))\n+\t\treturn git_config_string(&drv->smudge_to_file, var, value);\n+\n+\tif (!strcmp(\"cleanfromfile\", key))\n+\t\treturn git_config_string(&drv->clean_from_file, var, value);\n+\n \tif (!strcmp(\"required\", key)) {\n \t\tdrv->required = git_config_bool(var, value);\n \t\treturn 0;\n@@ -823,7 +844,35 @@ 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, NULL, 0, -1, NULL, ca.drv->clean);\n+}\n+\n+int can_clean_from_file(const char *path)\n+{\n+\tstruct conv_attrs ca;\n+\n+\tconvert_attrs(&ca, path);\n+\tif (!ca.drv)\n+\t\treturn 0;\n+\n+\t/* Only use the cleanFromFile filter when the clean filter is also\n+\t * configured.\n+\t */\n+\treturn (ca.drv->clean_from_file && ca.drv->clean);\n+}\n+\n+int can_smudge_to_file(const char *path)\n+{\n+\tstruct conv_attrs ca;\n+\n+\tconvert_attrs(&ca, path);\n+\tif (!ca.drv)\n+\t\treturn 0;\n+\n+\t/* Only use the smudgeToFile filter when the smudge filter is also\n+\t * configured.\n+\t */\n+\treturn (ca.drv->smudge_to_file && ca.drv->smudge);\n }\n \n const char *get_convert_attr_ascii(const char *path)\n@@ -866,7 +915,7 @@ int convert_to_git(const char *path, const char *src, size_t len,\n \t\trequired = ca.drv->required;\n \t}\n \n-\tret |= apply_filter(path, src, len, -1, dst, filter);\n+\tret |= apply_filter(path, NULL, src, len, -1, dst, filter);\n \tif (!ret && required)\n \t\tdie(\"%s: clean filter '%s' failed\", path, ca.drv->name);\n \n@@ -891,14 +940,33 @@ 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, NULL, 0, fd, dst, ca.drv->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 \tident_to_git(path, dst->buf, dst->len, dst, ca.ident);\n }\n \n-static int convert_to_working_tree_internal(const char *path, const char *src,\n+void convert_to_git_filter_from_file(const char *path, struct strbuf *dst,\n+\t\t\t\t   enum safe_crlf checksafe)\n+{\n+\tstruct conv_attrs ca;\n+\tconvert_attrs(&ca, path);\n+\n+\tassert(ca.drv);\n+\tassert(ca.drv->clean);\n+\tassert(ca.drv->clean_from_file);\n+\n+\tif (!apply_filter(path, path, \"\", 0, -1, dst, ca.drv->clean_from_file))\n+\t\tdie(\"%s: cleanFromFile filter '%s' failed\", path, ca.drv->name);\n+\n+\tcrlf_to_git(path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);\n+\tident_to_git(path, dst->buf, dst->len, dst, ca.ident);\n+}\n+\n+static int convert_to_working_tree_internal(const char *path,\n+\t\t\t\t\t    const char *destpath,\n+\t\t\t\t\t    const char *src,\n \t\t\t\t\t    size_t len, struct strbuf *dst,\n \t\t\t\t\t    int normalizing)\n {\n@@ -909,7 +977,10 @@ static int convert_to_working_tree_internal(const char *path, const char *src,\n \n \tconvert_attrs(&ca, path);\n \tif (ca.drv) {\n-\t\tfilter = ca.drv->smudge;\n+\t\tif (destpath)\n+\t\t\tfilter = ca.drv->smudge_to_file;\n+\t\telse\n+\t\t\tfilter = ca.drv->smudge;\n \t\trequired = ca.drv->required;\n \t}\n \n@@ -920,7 +991,7 @@ 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 filter.  The filter might expect CRLFs.\n \t */\n \tif (filter || !normalizing) {\n \t\tret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action);\n@@ -930,21 +1001,30 @@ 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+\tret_filter = apply_filter(path, destpath, src, len, -1, dst, filter);\n \tif (!ret_filter && required)\n-\t\tdie(\"%s: smudge filter %s failed\", path, ca.drv->name);\n+\t\tdie(\"%s: %s filter %s failed\", path, destpath ? \"smudgeToFile\" : \"smudge\", ca.drv->name);\n \n \treturn ret | ret_filter;\n }\n \n int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)\n {\n-\treturn convert_to_working_tree_internal(path, src, len, dst, 0);\n+\treturn convert_to_working_tree_internal(path, NULL, src, len, dst, 0);\n+}\n+\n+int convert_to_working_tree_filter_to_file(const char *path, const char *destpath, const char *src, size_t len)\n+{\n+\tstruct strbuf output = STRBUF_INIT;\n+\tint ret = convert_to_working_tree_internal(path, destpath, src, len, &output, 0);\n+\t/* The smudgeToFile filter stdout is not used. */\n+\tstrbuf_release(&output);\n+\treturn ret;\n }\n \n int renormalize_buffer(const char *path, const char *src, size_t len, struct strbuf *dst)\n {\n-\tint ret = convert_to_working_tree_internal(path, src, len, dst, 1);\n+\tint ret = convert_to_working_tree_internal(path, NULL, src, len, dst, 1);\n \tif (ret) {\n \t\tsrc = dst->buf;\n \t\tlen = dst->len;\ndiff --git a/convert.h b/convert.h\nindex ccf436b..53e1474 100644\n--- a/convert.h\n+++ b/convert.h\n@@ -41,6 +41,10 @@ extern int convert_to_git(const char *path, const char *src, size_t len,\n \t\t\t  struct strbuf *dst, enum safe_crlf checksafe);\n extern int convert_to_working_tree(const char *path, const char *src,\n \t\t\t\t   size_t len, struct strbuf *dst);\n+extern int convert_to_working_tree_filter_to_file(const char *path,\n+\t\t\t\t\t\t  const char *destpath,\n+\t\t\t\t\t\t  const char *src,\n+\t\t\t\t\t\t  size_t len);\n extern int renormalize_buffer(const char *path, const char *src, size_t len,\n \t\t\t      struct strbuf *dst);\n static inline int would_convert_to_git(const char *path)\n@@ -52,6 +56,12 @@ extern void convert_to_git_filter_fd(const char *path, int fd,\n \t\t\t\t     struct strbuf *dst,\n \t\t\t\t     enum safe_crlf checksafe);\n extern int would_convert_to_git_filter_fd(const char *path);\n+/* Precondition: can_clean_from_file(path) == true */\n+extern void convert_to_git_filter_from_file(const char *path,\n+\t\t\t\t\t    struct strbuf *dst,\n+\t\t\t\t\t    enum safe_crlf checksafe);\n+extern int can_clean_from_file(const char *path);\n+extern int can_smudge_to_file(const char *path);\n \n /*****************************************************************\n  *\n-- \n2.8.1\n\n"},{"id":"289509","messageId":"1466195481-23209-3-git-send-email-joeyh@joeyh.name","threadId":"42642","inReplyTo":"1466195481-23209-1-git-send-email-joeyh@joeyh.name","subject":"[PATCH v2 2/4] use cleanFromFile in git add","fromName":"Joey Hess","fromEmail":"joeyh@joeyh.name","sentAt":"2016-06-17T20:31:19Z","receivedAt":"2016-06-17T20:32:19Z","isPatch":true,"sender":{"key":"joeyh@joeyh.name","avatar":"https://avatars.githubusercontent.com/u/16392?v=4"},"body":"Includes test cases.\n\nSigned-off-by: Joey Hess <joeyh@joeyh.name>\n---\n sha1_file.c           | 42 ++++++++++++++++++++++++++++++++++++------\n t/t0021-conversion.sh | 36 ++++++++++++++++++++++++++++++++++++\n 2 files changed, 72 insertions(+), 6 deletions(-)\n\ndiff --git a/sha1_file.c b/sha1_file.c\nindex d5e1121..8df86a0 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -3329,6 +3329,29 @@ static int index_stream_convert_blob(unsigned char *sha1, int fd,\n \treturn ret;\n }\n \n+static int index_from_file_convert_blob(unsigned char *sha1,\n+\t\t\t\t      const char *path, unsigned flags)\n+{\n+\tint ret;\n+\tconst int write_object = flags & HASH_WRITE_OBJECT;\n+\tstruct strbuf sbuf = STRBUF_INIT;\n+\n+\tassert(path);\n+\tassert(can_clean_from_file(path));\n+\n+\tconvert_to_git_filter_from_file(path, &sbuf,\n+\t\t\t\t write_object ? safe_crlf : SAFE_CRLF_FALSE);\n+\n+\tif (write_object)\n+\t\tret = write_sha1_file(sbuf.buf, sbuf.len, typename(OBJ_BLOB),\n+\t\t\t\t      sha1);\n+\telse\n+\t\tret = hash_sha1_file(sbuf.buf, sbuf.len, typename(OBJ_BLOB),\n+\t\t\t\t     sha1);\n+\tstrbuf_release(&sbuf);\n+\treturn ret;\n+}\n+\n static int index_pipe(unsigned char *sha1, int fd, enum object_type type,\n \t\t      const char *path, unsigned flags)\n {\n@@ -3421,12 +3444,19 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, unsigned\n \n \tswitch (st->st_mode & S_IFMT) {\n \tcase S_IFREG:\n-\t\tfd = open(path, O_RDONLY);\n-\t\tif (fd < 0)\n-\t\t\treturn error_errno(\"open(\\\"%s\\\")\", path);\n-\t\tif (index_fd(sha1, fd, st, OBJ_BLOB, path, flags) < 0)\n-\t\t\treturn error(\"%s: failed to insert into database\",\n-\t\t\t\t     path);\n+\t\tif (can_clean_from_file(path)) {\n+\t\t\tif (index_from_file_convert_blob(sha1, path, flags) < 0)\n+\t\t\t\treturn error(\"%s: failed to insert into database\",\n+\t\t\t\t\t     path);\n+\t\t}\n+\t\telse {\n+\t\t\tfd = open(path, O_RDONLY);\n+\t\t\tif (fd < 0)\n+\t\t\t\treturn error_errno(\"open(\\\"%s\\\")\", path);\n+\t\t\tif (index_fd(sha1, fd, st, OBJ_BLOB, path, flags) < 0)\n+\t\t\t\treturn error(\"%s: failed to insert into database\",\n+\t\t\t\t\t     path);\n+\t\t}\n \t\tbreak;\n \tcase S_IFLNK:\n \t\tif (strbuf_readlink(&sb, path, st->st_size))\ndiff --git a/t/t0021-conversion.sh b/t/t0021-conversion.sh\nindex 7bac2bc..399f92b 100755\n--- a/t/t0021-conversion.sh\n+++ b/t/t0021-conversion.sh\n@@ -12,6 +12,14 @@ tr \\\n EOF\n chmod +x rot13.sh\n \n+cat <<EOF >rot13-from-file.sh\n+#!$SHELL_PATH\n+fsfile=\"\\$1\"\n+touch rot13-from-file.ran\n+cat \"\\$fsfile\" | ./rot13.sh\n+EOF\n+chmod +x rot13-from-file.sh\n+\n test_expect_success setup '\n \tgit config filter.rot13.smudge ./rot13.sh &&\n \tgit config filter.rot13.clean ./rot13.sh &&\n@@ -268,4 +276,32 @@ test_expect_success 'disable filter with empty override' '\n \ttest_must_be_empty err\n '\n \n+test_expect_success 'cleanFromFile filter is used when adding a file' '\n+\ttest_config filter.rot13.cleanFromFile ./rot13-from-file.sh &&\n+\n+\techo \"*.t filter=rot13\" >.gitattributes &&\n+\n+\tcat test.t >fstest.t &&\n+\tgit add fstest.t &&\n+\ttest -e rot13-from-file.ran &&\n+\trm -f rot13-from-file.ran &&\n+\n+\trm -f fstest.t &&\n+\tgit checkout -- fstest.t &&\n+\tcmp test.t fstest.t\n+'\n+\n+test_expect_success 'cleanFromFile filter is not used when clean filter is not configured' '\n+\ttest_config filter.noclean.smudge ./rot13.sh &&\n+\ttest_config filter.noclean.cleanFromFile ./rot13-from-file.sh &&\n+\n+\techo \"*.no filter=noclean\" >.gitattributes &&\n+\n+\tcat test.t >test.no &&\n+\tgit add test.no &&\n+\ttest ! -e rot13-from-file.ran &&\n+\tgit cat-file blob :test.no >actual &&\n+\tcmp test.t actual\n+'\n+\n test_done\n-- \n2.8.1\n\n"},{"id":"289511","messageId":"20160617210550.GA25887@kitenet.net","threadId":"42642","inReplyTo":"1466195481-23209-1-git-send-email-joeyh@joeyh.name","subject":"Re: [PATCH v2 0/4] extend smudge/clean filters with direct file access","fromName":"Joey Hess","fromEmail":"id@joeyh.name","sentAt":"2016-06-17T21:05:50Z","receivedAt":"2016-06-17T21:06:02Z","isPatch":true,"sender":{"key":"id@joeyh.name","avatar":"https://avatars.githubusercontent.com/u/16392?v=4"},"body":"Doing a quick benchmark of this new interface and git-annex's use of it, git\ncheckout of a 1 gigabyte file with git-annex providing the smudge filter took:\n\n    19 seconds using the smudge interface\n    11 seconds using smudgeToFile\n    0.1 seconds with smudgeToFile and annex.thin set\n         (while also saving 1 gb of disk space!)\n\nSo around 2x speed improvement due to not needing to pipe the file content\nthrough the filter, even without git-annex's annex.thin tricks.\n\n-- \nsee shy jo\n"},{"id":"289513","messageId":"xmqqshwbzfab.fsf@gitster.mtv.corp.google.com","threadId":"42642","inReplyTo":"1466195481-23209-1-git-send-email-joeyh@joeyh.name","subject":"Re: [PATCH v2 0/4] extend smudge/clean filters with direct file access","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-06-17T21:17:16Z","receivedAt":"2016-06-17T21:17:23Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Joey Hess <joeyh@joeyh.name> writes:\n\n> Reroll of this patch set with changes:\n\n... where is this 4-patch series designed to apply?  The first one\nalready fails...\n\nApplying: add smudgeToFile and cleanFromFile filter configs\n.git/rebase-apply/patch:28: trailing whitespace.\n\tSimilar to filter.<driver>.clean but the specified command \n.git/rebase-apply/patch:30: trailing whitespace.\n\treceiving the file content from standard input. \n.git/rebase-apply/patch:129: indent with spaces.\n                        struct strbuf *dst, const char *cmd)\nfatal: sha1 information is lacking or useless (Documentation/gitattributes.txt).\nerror: could not build fake ancestor\nPatch failed at 0001 add smudgeToFile and cleanFromFile filter configs\nThe copy of the patch that failed is found in: .git/rebase-apply/patch\nWhen you have resolved this problem, run \"git am --continue\".\nIf you prefer to skip this patch, run \"git am --skip\" instead.\nTo restore the original branch and stop patching, run \"git am --abort\".\n\n\n>\n> * Renamed the new filter drivers for consistency with other configs.\n> * Improved documentation with feedback from Junio and others.\n> * Eliminated %p and instead append the filename to the commands\n>   (separated by a space).\n> * Fixed an FD leak and a space leak.\n> * Only use smudgeToFile with regular files, not symlinks.\n> * After running the smudgeToFile command, double-check that the\n>   expected file is present, in case the command was buggy and deleted it.\n> * Added a warning message when the new filter commands are configured\n>   but the old ones are not, so that the user knows it's refusing to use\n>   their configuration.\n>\n> There's been good and helpful documentation and interface review,\n> but some more code review would be good! Also, git-annex has a\n> improved-smudge-filters branch now that demonstrates this interface.\n>\n> Joey Hess (4):\n>   add smudgeToFile and cleanFromFile filter configs\n>   use cleanFromFile in git add\n>   use smudgeToFile in git checkout etc\n>   warn on unusable smudgeToFile/cleanFromFile config\n>\n>  Documentation/config.txt        |  18 +++++-\n>  Documentation/gitattributes.txt |  37 ++++++++++++\n>  convert.c                       | 126 +++++++++++++++++++++++++++++++++++-----\n>  convert.h                       |  10 ++++\n>  entry.c                         |  37 +++++++++---\n>  sha1_file.c                     |  42 ++++++++++++--\n>  t/t0021-conversion.sh           |  64 ++++++++++++++++++++\n>  7 files changed, 304 insertions(+), 30 deletions(-)\n"},{"id":"289527","messageId":"20160617224345.GA26764@kitenet.net","threadId":"42642","inReplyTo":"xmqqshwbzfab.fsf@gitster.mtv.corp.google.com","subject":"[PATCH v2 0/4] clarify %f documentation","fromName":"Joey Hess","fromEmail":"joeyh@joeyh.name","sentAt":"2016-06-17T22:43:45Z","receivedAt":"2016-06-17T22:43:58Z","isPatch":true,"sender":{"key":"joeyh@joeyh.name","avatar":"https://avatars.githubusercontent.com/u/16392?v=4"},"body":"It's natural to expect %f to be an actual file on disk; help avoid that\nmistake.\n\nSigned-off-by: Joey Hess <joeyh@joeyh.name>\n---\n\nThis patch series was meant to contain 5 patches; here's the missing\none. This patch will apply cleanly on top of v2.9.0.\n\n Documentation/gitattributes.txt | 5 +++++\n 1 file changed, 5 insertions(+)\n\ndiff --git a/Documentation/gitattributes.txt b/Documentation/gitattributes.txt\nindex e3b1de8..145dd10 100644\n--- a/Documentation/gitattributes.txt\n+++ b/Documentation/gitattributes.txt\n@@ -374,6 +374,11 @@ substitution.  For example:\n \tsmudge = git-p4-filter --smudge %f\n ------------------------\n \n+Note that \"%f\" is the name of the path that is being worked on. Depending\n+on the version that is being filtered, the corresponding file on disk may\n+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 Interaction between checkin/checkout attributes\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n-- \n2.8.1\n"}]}