Code

Add a setting to require a filter to be successful
authorJehan Bing <jehan@orb.com>
Fri, 17 Feb 2012 01:19:03 +0000 (17:19 -0800)
committerJunio C Hamano <gitster@pobox.com>
Fri, 17 Feb 2012 15:37:08 +0000 (07:37 -0800)
By default, a missing filter driver or a failure from the filter driver is
not an error, but merely makes the filter operation a no-op pass through.
This is useful to massage the content into a shape that is more convenient
for the platform, filesystem, and the user to use, and the content filter
mechanism is not used to turn something unusable into usable.

However, we could also use of the content filtering mechanism and store
the content that cannot be directly used in the repository (e.g. a 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, or decrypt the encrypted content).  For such a use case,
the content cannot be used when filter driver fails, and we need a way to
tell Git to abort the whole operation for such a failing or missing filter
driver.

Add a new "filter.<driver>.required" configuration variable to mark the
second use case.  When it is set, git will abort the operation when the
filter driver does not exist or exits with a non-zero status code.

Signed-off-by: Jehan Bing <jehan@orb.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/gitattributes.txt
convert.c
t/t0021-conversion.sh

index a85b187e0479b99e137160c1190f174d80675fa1..80120ea14f1ccd2784405c2bf2d54863bb52e8d3 100644 (file)
@@ -294,16 +294,27 @@ 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.
-
-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.
+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.
+For this mode of operation, 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. a 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, or 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 a usable content by setting the filter.<driver>.required configuration
+variable to `true`.
 
 For example, in .gitattributes, you would assign the `filter`
 attribute for paths.
@@ -335,6 +346,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:
index 12868ed7bda11648704ffe4e5d3415067764a6e2..c06309f5e102c36f5751fce8cb83229ff9acca42 100644 (file)
--- a/convert.c
+++ b/convert.c
@@ -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("%s: clean filter '%s' failed", path, 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("%s: smudge filter %s failed", path, ca.drv->name);
+
+       return ret | ret_filter;
 }
 
 int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
index f19e6510d04583866e39cbdea545a0d1323b7f76..e50f0f742fdc4dca766e3f236cd32e388f0c89aa 100755 (executable)
@@ -153,4 +153,41 @@ test_expect_success 'filter shell-escaped filenames' '
        :
 '
 
+test_expect_success 'required filter success' '
+       git config filter.required.smudge cat &&
+       git config filter.required.clean cat &&
+       git config filter.required.required true &&
+
+       echo "*.r filter=required" >.gitattributes &&
+
+       echo test >test.r &&
+       git add test.r &&
+       rm -f test.r &&
+       git checkout -- test.r
+'
+
+test_expect_success 'required filter smudge failure' '
+       git config filter.failsmudge.smudge false &&
+       git config filter.failsmudge.clean cat &&
+       git config filter.failsmudge.required true &&
+
+       echo "*.fs filter=failsmudge" >.gitattributes &&
+
+       echo test >test.fs &&
+       git add test.fs &&
+       rm -f test.fs &&
+       test_must_fail git checkout -- test.fs
+'
+
+test_expect_success 'required filter clean failure' '
+       git config filter.failclean.smudge cat &&
+       git config filter.failclean.clean false &&
+       git config filter.failclean.required true &&
+
+       echo "*.fc filter=failclean" >.gitattributes &&
+
+       echo test >test.fc &&
+       test_must_fail git add test.fc
+'
+
 test_done