Add an option to require a filter to be successful
PATCH 1/1 Add an option to require a filter to be successful
By default, if a filter driver fails, the unfiltered content will be used. This patch adds a "filter.<name>.required" config option. When set to true, git will abort if the filter fails.
A typical usage would be for a "bigfile" filter, where the smudge command can fail if the file is not available locally. Without the "required", the content of repository, i.e. a reference to the real content, will be checked out. Unless one saves the output logs, it then fairly easy to lose track of what "bigfile" wasn't checked out correctly.
Another example would be for an encrypted repository if the clean command (encryption) fails. Without the "required", an unencrypted content could be stored in the repository by mistake.
--- Documentation/gitattributes.txt | 14 ++++++++++++ convert.c | 28 +++++++++++++++++++++--- t/t0021-conversion.sh | 43 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 4 deletions(-)
Documentation/gitattributes.txt +14 -0
@@ -305,6 +305,10 @@ intent is that if someone unsets the filter driver definition, or does not have the appropriate filter program, the project should still be usable. +The exception is if the filter definition has the `required` +attribute set to `true`. In that case, the filter must apply +successfully or git will abort the current operation. + For example, in .gitattributes, you would assign the `filter` attribute for paths.
@@ -335,6 +339,16 @@ input that is already correctly indented. In this case, the lack of a smudge filter means that the clean filter _must_ accept its own output without modifying it. +If you do not wish git to continue if `clean` or `smudge` fail, you can +add a `required` attribute to the filter: + +------------------------ +[filter "crypt"] + clean = openssl enc ... + smudge = openssl enc -d ... + required = true +------------------------ + Sequence "%f" on the filter command line is replaced with the name of the file the filter is working on. A filter might use this in keyword substitution. For example:
convert.c +24 -4
@@ -429,6 +429,7 @@ static struct convert_driver {struct convert_driver *next;const char *smudge;const char *clean; +int required; } *user_convert, **user_convert_tail;static int read_convert_config (const char *var,const char *value,void *cb)
@@ -472,6 +473,11 @@ static int read_convert_config(const char *var, const char *value, void *cb)if (!strcmp ("clean" , ep))return git_config_string (&drv->clean, var, value); +if (!strcmp ("required" , ep)) { + drv->required =git_config_bool (var, value); +return 0 ; + } +return 0 ; }
@@ -747,13 +753,19 @@ int convert_to_git(const char *path, const char *src, size_t len, {int ret =0 ;const char *filter =NULL ; +int required =0 ;struct conv_attrs ca;convert_attrs (&ca, path); -if (ca.drv) +if (ca.drv) { filter = ca.drv->clean; + required = ca.drv->required; + } ret |=apply_filter (path, src, len, dst, filter); +if (!ret && required) +die ("required filter '%s' failed" , ca.drv->name); +if (ret) { src = dst->buf; len = dst->len;
@@ -771,13 +783,16 @@ static int convert_to_working_tree_internal(const char *path, const char *src, size_t len, struct strbuf *dst, int normalizing) { - int ret = 0; + int ret = 0, ret_filter = 0;const char *filter =NULL ; +int required =0 ;struct conv_attrs ca;convert_attrs (&ca, path); -if (ca.drv) +if (ca.drv) { filter = ca.drv->smudge; + required = ca.drv->required; + } ret |=ident_to_worktree (path, src, len, dst, ca.ident);if (ret) {
@@ -796,7 +811,12 @@ static int convert_to_working_tree_internal(const char *path, const char *src, len = dst->len; } } -return ret |apply_filter (path, src, len, dst, filter); + + ret_filter =apply_filter (path, src, len, dst, filter); +if (!ret_filter && required) +die ("required filter %s failed" , ca.drv->name); + +return ret | ret_filter; }int convert_to_working_tree (const char *path,const char *src,size_t len,struct strbuf *dst)
t/t0021-conversion.sh +43 -0
47 lines not on this page · raw message
Signature · 1 line
1.7.9
Quoted 11 lines · Jehan Bing
By default, if a filter driver fails, the unfiltered content will be used. This patch adds a "filter.<name>.required" config option. When set to true, git will abort if the filter fails.
A typical usage would be for a "bigfile" filter, where the smudge command can fail if the file is not available locally. Without the "required", the content of repository, i.e. a reference to the real content, will be checked out. Unless one saves the output logs, it then fairly easy to lose track of what "bigfile" wasn't checked out correctly.
Quoted 3 lines
Another example would be for an encrypted repository if the clean command (encryption) fails. Without the "required", an unencrypted content could be stored in the repository by mistake.
The above describes sample situations where setting the "required" may be very useful, without saying anything about in what situation it might be useful to set it to "optional".
Which makes the reader wonder why this is not done as a bugfix patch that unconditionally propagates the failure from the filter up the callchain.
That is because the first sentence in the message is too weak. It needs to be followed by something like:
This is because the content filtering is done to massage the content into a shape that is more convenient for the platform, filesystem, and the user to use. The key phrase here is "more convenient" and not "turning something unusable into usable".
which is what the part of gitattributes documentation shown in the context says.
That is a statement of principle. And according to that principle, your configuration option should never exist.
If we are changing that principle and making it configurable, I think the update to the existing documentation should state things a bit stronger. We shouldn't be saying "Do not use filter to turn unusable contents to usable" and in the next breath "But you can use it if you set this at the same time". That is simply too confusing.
Here is an attempt to rephrase the part that updates the documentation.
Note that filter.<driver>.required is *NOT* an attribute. An attribute is something you attach to paths.
Documentation/gitattributes.txt | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-)
Documentation/gitattributes.txt +26 -9
@@ -294,16 +294,23 @@ output is used to update the worktree file. Similarly, the `clean` command is used to convert the contents of worktree file upon checkin. -A missing filter driver definition in the config is not an error -but makes the filter a no-op passthru. +One use of the content filtering is to massage the content into a shape +that is more convenient for the platform, filesystem, and the user to use. -The content filtering is done to massage the content into a -shape that is more convenient for the platform, filesystem, and -the user to use. The key phrase here is "more convenient" and not -"turning something unusable into usable". In other words, the -intent is that if someone unsets the filter driver definition, -or does not have the appropriate filter program, the project -should still be usable. +Another use of the content filtering is to store the content that cannot +be directly used in the repository (e.g. an UUID that refers to the true +content stored outside git, or an encrypted content) and turn it into a +usable form upon checkout (e.g. download the external content, decrypt the +encrypted content). + +These two filters behave differently, and by default, a filter is taken as +the former, massaging the contents into more convenient shape. A missing +filter driver definition in the config, or a filter driver that exits with +a non-zero status, is not an error but makes the filter a no-op passthru. + +You can declare that a filter turns a content that by itself is unusable +into usable by setting filter.<drivername>.required configuration variable +to `true`. For example, in .gitattributes, you would assign the `filter` attribute for paths.
@@ -335,6 +342,16 @@ input that is already correctly indented. In this case, the lack of a smudge filter means that the clean filter _must_ accept its own output without modifying it. +If a filter _must_ succeed in order to make the stored contents usable, +you can declare that the filter is `required`, in the configuration: + +------------------------ +[filter "crypt"] + clean = openssl enc ... + smudge = openssl enc -d ... + required +------------------------ + Sequence "%f" on the filter command line is replaced with the name of the file the filter is working on. A filter might use this in keyword substitution. For example:
PATCH v2 1/1 Add a setting to require a filter to be successful
From: Jehan Bing <[email protected]>
By default, if a filter driver fails, the unfiltered content will be used. This is because the content filtering is done to massage the content into a shape that is more convenient for the platform, filesystem, and the user to use. The key phrase here is "more convenient" and not "turning something unusable into usable".
However, another use of the content filtering is to store the content that cannot be directly used in the repository (e.g. an UUID that refers to the true content stored outside git, or an encrypted content) and turn it into a usable form upon checkout (e.g. download the external content, decrypt the encrypted content).
In this situation, it is preferable to have git fail instead of using the unfiltered content.
This patch adds an optional "filter.<filtername>.required" configuration variable. When missing or set to false, git will use the unfiltered content if the filter driver fails (old behavior). When set to true, git will instead abort the current operation.
---
Thanks Junio for your comment. This version use your version of gitattributes.txt and I rewrote the commit message to be stronger.
-Jehan
Documentation/gitattributes.txt | 35 +++++++++++++++++++++++-------- convert.c | 28 +++++++++++++++++++++--- t/t0021-conversion.sh | 43 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 13 deletions(-)
Documentation/gitattributes.txt +26 -9
@@ -294,16 +294,23 @@ output is used to update the worktree file. Similarly, the `clean` command is used to convert the contents of worktree file upon checkin. -A missing filter driver definition in the config is not an error -but makes the filter a no-op passthru. +One use of the content filtering is to massage the content into a shape +that is more convenient for the platform, filesystem, and the user to use. -The content filtering is done to massage the content into a -shape that is more convenient for the platform, filesystem, and -the user to use. The key phrase here is "more convenient" and not -"turning something unusable into usable". In other words, the -intent is that if someone unsets the filter driver definition, -or does not have the appropriate filter program, the project -should still be usable. +Another use of the content filtering is to store the content that cannot +be directly used in the repository (e.g. an UUID that refers to the true +content stored outside git, or an encrypted content) and turn it into a +usable form upon checkout (e.g. download the external content, decrypt the +encrypted content). + +These two filters behave differently, and by default, a filter is taken as +the former, massaging the contents into more convenient shape. A missing +filter driver definition in the config, or a filter driver that exits with +a non-zero status, is not an error but makes the filter a no-op passthru. + +You can declare that a filter turns a content that by itself is unusable +into usable by setting filter.<drivername>.required configuration variable +to `true`. For example, in .gitattributes, you would assign the `filter` attribute for paths.
@@ -335,6 +342,16 @@ input that is already correctly indented. In this case, the lack of a smudge filter means that the clean filter _must_ accept its own output without modifying it. +If a filter _must_ succeed in order to make the stored contents usable, +you can declare that the filter is `required`, in the configuration: + +------------------------ +[filter "crypt"] + clean = openssl enc ... + smudge = openssl enc -d ... + required +------------------------ + Sequence "%f" on the filter command line is replaced with the name of the file the filter is working on. A filter might use this in keyword substitution. For example:
convert.c +24 -4
@@ -429,6 +429,7 @@ static struct convert_driver {struct convert_driver *next;const char *smudge;const char *clean; +int required; } *user_convert, **user_convert_tail;static int read_convert_config (const char *var,const char *value,void *cb)
@@ -472,6 +473,11 @@ static int read_convert_config(const char *var, const char *value, void *cb)if (!strcmp ("clean" , ep))return git_config_string (&drv->clean, var, value); +if (!strcmp ("required" , ep)) { + drv->required =git_config_bool (var, value); +return 0 ; + } +return 0 ; }
@@ -747,13 +753,19 @@ int convert_to_git(const char *path, const char *src, size_t len, {int ret =0 ;const char *filter =NULL ; +int required =0 ;struct conv_attrs ca;convert_attrs (&ca, path); -if (ca.drv) +if (ca.drv) { filter = ca.drv->clean; + required = ca.drv->required; + } ret |=apply_filter (path, src, len, dst, filter); +if (!ret && required) +die ("required filter '%s' failed" , ca.drv->name); +if (ret) { src = dst->buf; len = dst->len;
@@ -771,13 +783,16 @@ static int convert_to_working_tree_internal(const char *path, const char *src, size_t len, struct strbuf *dst, int normalizing) { - int ret = 0; + int ret = 0, ret_filter = 0;const char *filter =NULL ; +int required =0 ;struct conv_attrs ca;convert_attrs (&ca, path); -if (ca.drv) +if (ca.drv) { filter = ca.drv->smudge; + required = ca.drv->required; + } ret |=ident_to_worktree (path, src, len, dst, ca.ident);if (ret) {
@@ -796,7 +811,12 @@ static int convert_to_working_tree_internal(const char *path, const char *src, len = dst->len; } } -return ret |apply_filter (path, src, len, dst, filter); + + ret_filter =apply_filter (path, src, len, dst, filter); +if (!ret_filter && required) +die ("required filter %s failed" , ca.drv->name); + +return ret | ret_filter; }int convert_to_working_tree (const char *path,const char *src,size_t len,struct strbuf *dst)
t/t0021-conversion.sh +43 -0
47 lines not on this page · raw message
Signature · 1 line
1.7.9
Re: [PATCH v2] Add a setting to require a filter to be successful
Am 2/17/2012 2:19, schrieb [email protected]:
Quoted 1 line
(quoted diff) +0
-0@@ -747,13 +753,19 @@ int convert_to_git(const char *path, const char *src, size_t len,
...
convert.c int convert_to_git(const char *path, const char *src, size_t len, · line 767
Quoted 3 lines
convert.c +2
-0ret |=apply_filter (path, src, len, dst, filter); +if (!ret && required) +die ("required filter '%s' failed" , ca.drv->name);
Wouldn't it be much more helpful if this were:
die("%s: clean filter '%s' failed", path, ca.drv->name);Likewise (with s/clean/smudge/) in convert_to_working_tree_internal().
t/t0021-conversion.sh test_expect_success 'filter shell-escaped filenames' ' · line 183
Quoted 1 line
t/t0021-conversion.sh +1
-0+ ! git checkout -- test.fs
test_must_fail git checkout -- test.fs
t/t0021-conversion.sh test_expect_success 'filter shell-escaped filenames' ' · line 196
Quoted 1 line
t/t0021-conversion.sh +1
-0+ ! git add test.fc
test_must_fail git add test.fc
-- Hannes
Re: [PATCH v2] Add a setting to require a filter to be successful
Quoted 17 lines · Johannes Sixt
Am 2/17/2012 2:19, schrieb [email protected]:
Quoted 1 line
(quoted diff) +0
-0@@ -747,13 +753,19 @@ int convert_to_git(const char *path, const char *src, size_t len,...
convert.c int convert_to_git(const char *path, const char *src, size_t len, · line 767
Quoted 3 lines
convert.c +2
-0ret |=apply_filter (path, src, len, dst, filter); +if (!ret && required) +die ("required filter '%s' failed" , ca.drv->name);Wouldn't it be much more helpful if this were:
die("%s: clean filter '%s' failed", path, ca.drv->name);Likewise (with s/clean/smudge/) in
convert_to_working_tree_internal().t/t0021-conversion.sh test_expect_success 'filter shell-escaped filenames' ' · line 183
Quoted 1 line
t/t0021-conversion.sh +1
-0+ ! git checkout -- test.fs
test_must_failgit checkout -- test.fs
Quoted 5 lines
t/t0021-conversion.sh test_expect_success 'filter shell-escaped filenames' ' · line 196
Quoted 1 line
t/t0021-conversion.sh +1
-0+ ! git add test.fc
test_must_failgit add test.fc-- Hannes
Thanks; I'll just squash these in in-place.
Re: [PATCH v2] Add a setting to require a filter to be successful
In reply to Johannes SixtA few test in t0021 use 'false' as the filter, which can exit without reading any byte from us, before we start writing and causes us to die with SIGPIPE, leading to intermittent test failure. I think treating this as a failure of running the filter (the end user's filter should read what is fed in full, produce its output and write the result back to us) is the right thing to do, and this patch needs more work to handle such a situation better, probably by using sigchain_push(SIGPIPE) or something.
Re: [PATCH v2] Add a setting to require a filter to be successful
Quoted 7 lines · Junio C Hamano
A few test in t0021 use 'false' as the filter, which can exit without reading any byte from us, before we start writing and causes us to die with SIGPIPE, leading to intermittent test failure. I think treating this as a failure of running the filter (the end user's filter should read what is fed in full, produce its output and write the result back to us) is the right thing to do, and this patch needs more work to handle such a situation better, probably by using
sigchain_push(SIGPIPE)or something.
If I understand what you're saying, current version of git already have the problem: if a filter fails without reading anything, git will die instead of using the unfiltered content. My patch has only made the issue apparent by testing with a failing filter. Am I understanding correctly?
Re: [PATCH v2] Add a setting to require a filter to be successful
Quoted 5 lines · Jehan Bing
If I understand what you're saying, current version of git already have the problem: if a filter fails without reading anything, git will die instead of using the unfiltered content. My patch has only made the issue apparent by testing with a failing filter.
Am I understanding correctly?
Yes.