]> git.ipfire.org Git - thirdparty/git.git/commitdiff
convert: fail gracefully upon missing clean cmd on required filter
authorMatheus Tavares <matheus.bernardino@usp.br>
Fri, 26 Feb 2021 16:23:54 +0000 (13:23 -0300)
committerJunio C Hamano <gitster@pobox.com>
Fri, 26 Feb 2021 19:20:02 +0000 (11:20 -0800)
The gitattributes documentation mentions that either the clean cmd or
the smudge cmd can be left unspecified in a filter definition. However,
when the filter is marked as 'required', the absence of any one of these
two should be treated as an error. Git already fails under these
circumstances, but not always in a pleasant way: omitting a clean cmd in
a required filter triggers an assertion error which leaves the user with
a quite verbose message:

git: convert.c:1459: convert_to_git_filter_fd: Assertion "ca.drv->clean || ca.drv->process" failed.

This assertion is not really necessary, as the apply_filter() call below
it already performs the same check. And when this condition is not met,
the function returns 0, making the caller die() with a much nicer
message. (Also note that die()-ing here is the right behavior as
`would_convert_to_git_filter_fd() == true` is a precondition to use
convert_to_git_filter_fd(), and the former is only true when the filter
is required.) So remove the assertion and add two regression tests to
make sure that git fails nicely when either the smudge or clean command
is missing on a required filter.

Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
convert.c
t/t0021-conversion.sh

index ee360c2f07ced0daa8f3f7b1b651fae7c245cb26..2c2c2adf614c9fc077cc10af1680a578b59fc34c 100644 (file)
--- a/convert.c
+++ b/convert.c
@@ -1456,7 +1456,6 @@ void convert_to_git_filter_fd(const struct index_state *istate,
        convert_attrs(istate, &ca, path);
 
        assert(ca.drv);
-       assert(ca.drv->clean || ca.drv->process);
 
        if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN, NULL, NULL))
                die(_("%s: clean filter '%s' failed"), path, ca.drv->name);
index e828ee964c1b86f307d7522ce280ee2483348111..4f8415d419bc635d60314ee18448fc7040f66940 100755 (executable)
@@ -257,6 +257,30 @@ test_expect_success 'required filter clean failure' '
        test_must_fail git add test.fc
 '
 
+test_expect_success 'required filter with absent clean field' '
+       test_config filter.absentclean.smudge cat &&
+       test_config filter.absentclean.required true &&
+
+       echo "*.ac filter=absentclean" >.gitattributes &&
+
+       echo test >test.ac &&
+       test_must_fail git add test.ac 2>stderr &&
+       test_i18ngrep "fatal: test.ac: clean filter .absentclean. failed" stderr
+'
+
+test_expect_success 'required filter with absent smudge field' '
+       test_config filter.absentsmudge.clean cat &&
+       test_config filter.absentsmudge.required true &&
+
+       echo "*.as filter=absentsmudge" >.gitattributes &&
+
+       echo test >test.as &&
+       git add test.as &&
+       rm -f test.as &&
+       test_must_fail git checkout -- test.as 2>stderr &&
+       test_i18ngrep "fatal: test.as: smudge filter absentsmudge failed" stderr
+'
+
 test_expect_success 'filtering large input to small output should use little memory' '
        test_config filter.devnull.clean "cat >/dev/null" &&
        test_config filter.devnull.required true &&