diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2021-05-06 15:37:31 +0100 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2021-05-06 16:31:54 +0100 |
commit | 68b9605acc31d7cba3da322820b484c8db2e0a1e (patch) | |
tree | 5be2d6e1201422a4392e65fa3c39ce43424f9273 | |
parent | 5309b4654e5a387d18fe5181e376305332188eca (diff) | |
download | libgit2-68b9605acc31d7cba3da322820b484c8db2e0a1e.tar.gz |
filter: deprecate git_filter_list_apply_to_data
Deprecate `git_filter_list_apply_to_data` as it takes user input as a
`git_buf`. Users should use `git_filter_list_apply_to_buffer` instead.
-rw-r--r-- | include/git2/deprecated.h | 10 | ||||
-rw-r--r-- | include/git2/filter.h | 13 | ||||
-rw-r--r-- | src/filter.c | 22 | ||||
-rw-r--r-- | tests/filter/crlf.c | 116 | ||||
-rw-r--r-- | tests/filter/custom.c | 29 | ||||
-rw-r--r-- | tests/filter/wildcard.c | 18 |
6 files changed, 111 insertions, 97 deletions
diff --git a/include/git2/deprecated.h b/include/git2/deprecated.h index a51950906..ac60488ac 100644 --- a/include/git2/deprecated.h +++ b/include/git2/deprecated.h @@ -141,6 +141,16 @@ GIT_EXTERN(int) git_filter_list_stream_data( git_buf *data, git_writestream *target); +/** Deprecated in favor of `git_filter_list_apply_to_buffer`. + * + * @deprecated Use git_filter_list_apply_to_buffer + * @see Use git_filter_list_apply_to_buffer + */ +GIT_EXTERN(int) git_filter_list_apply_to_data( + git_buf *out, + git_filter_list *filters, + git_buf *in); + /**@}*/ /** @name Deprecated Tree Functions diff --git a/include/git2/filter.h b/include/git2/filter.h index 3e84877df..a0185ee88 100644 --- a/include/git2/filter.h +++ b/include/git2/filter.h @@ -135,19 +135,6 @@ GIT_EXTERN(int) git_filter_list_apply_to_buffer( size_t in_len); /** - * Apply filter list to a data buffer. - * - * @param out Buffer to store the result of the filtering - * @param filters A loaded git_filter_list (or NULL) - * @param in Buffer containing the data to filter - * @return 0 on success, an error code otherwise - */ -GIT_EXTERN(int) git_filter_list_apply_to_data( - git_buf *out, - git_filter_list *filters, - git_buf *in); - -/** * Apply a filter list to the contents of a file on disk * * @param out buffer into which to store the filtered file diff --git a/src/filter.c b/src/filter.c index 2ba0f1e76..200d70517 100644 --- a/src/filter.c +++ b/src/filter.c @@ -742,17 +742,6 @@ int git_filter_list_apply_to_buffer( return error; } -int git_filter_list_apply_to_data( - git_buf *tgt, git_filter_list *filters, git_buf *src) -{ - int error; - - if ((error = git_buf_sanitize(src)) < 0) - return error; - - return git_filter_list_apply_to_buffer(tgt, filters, src->ptr, src->size); -} - int git_filter_list_apply_to_file( git_buf *out, git_filter_list *filters, @@ -1072,4 +1061,15 @@ int git_filter_list_stream_data( return git_filter_list_stream_buffer(filters, data->ptr, data->size, target); } +int git_filter_list_apply_to_data( + git_buf *tgt, git_filter_list *filters, git_buf *src) +{ + int error; + + if ((error = git_buf_sanitize(src)) < 0) + return error; + + return git_filter_list_apply_to_buffer(tgt, filters, src->ptr, src->size); +} + #endif diff --git a/tests/filter/crlf.c b/tests/filter/crlf.c index a266005d4..bc9c7f289 100644 --- a/tests/filter/crlf.c +++ b/tests/filter/crlf.c @@ -23,7 +23,9 @@ void test_filter_crlf__to_worktree(void) { git_filter_list *fl; git_filter *crlf; - git_buf in = { 0 }, out = { 0 }; + git_buf out = GIT_BUF_INIT; + const char *in; + size_t in_len; cl_git_pass(git_filter_list_new( &fl, g_repo, GIT_FILTER_TO_WORKTREE, 0)); @@ -33,10 +35,10 @@ void test_filter_crlf__to_worktree(void) cl_git_pass(git_filter_list_push(fl, crlf, NULL)); - in.ptr = "Some text\nRight here\n"; - in.size = strlen(in.ptr); + in = "Some text\nRight here\n"; + in_len = strlen(in); - cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); + cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); cl_assert_equal_s("Some text\r\nRight here\r\n", out.ptr); @@ -48,7 +50,9 @@ void test_filter_crlf__to_odb(void) { git_filter_list *fl; git_filter *crlf; - git_buf in = { 0 }, out = { 0 }; + git_buf out = GIT_BUF_INIT; + const char *in; + size_t in_len; cl_git_pass(git_filter_list_new( &fl, g_repo, GIT_FILTER_TO_ODB, 0)); @@ -58,10 +62,10 @@ void test_filter_crlf__to_odb(void) cl_git_pass(git_filter_list_push(fl, crlf, NULL)); - in.ptr = "Some text\r\nRight here\r\n"; - in.size = strlen(in.ptr); + in = "Some text\r\nRight here\r\n"; + in_len = strlen(in); - cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); + cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); cl_assert_equal_s("Some text\nRight here\n", out.ptr); @@ -73,7 +77,9 @@ void test_filter_crlf__with_safecrlf(void) { git_filter_list *fl; git_filter *crlf; - git_buf in = {0}, out = GIT_BUF_INIT; + git_buf out = GIT_BUF_INIT; + const char *in; + size_t in_len; cl_repo_set_bool(g_repo, "core.safecrlf", true); @@ -86,31 +92,31 @@ void test_filter_crlf__with_safecrlf(void) cl_git_pass(git_filter_list_push(fl, crlf, NULL)); /* Normalized \r\n succeeds with safecrlf */ - in.ptr = "Normal\r\nCRLF\r\nline-endings.\r\n"; - in.size = strlen(in.ptr); + in = "Normal\r\nCRLF\r\nline-endings.\r\n"; + in_len = strlen(in); - cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); + cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr); /* Mix of line endings fails with safecrlf */ - in.ptr = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n"; - in.size = strlen(in.ptr); + in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n"; + in_len = strlen(in); - cl_git_fail(git_filter_list_apply_to_data(&out, fl, &in)); + cl_git_fail(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); cl_assert_equal_i(git_error_last()->klass, GIT_ERROR_FILTER); /* Normalized \n fails for autocrlf=true when safecrlf=true */ - in.ptr = "Normal\nLF\nonly\nline-endings.\n"; - in.size = strlen(in.ptr); + in = "Normal\nLF\nonly\nline-endings.\n"; + in_len = strlen(in); - cl_git_fail(git_filter_list_apply_to_data(&out, fl, &in)); + cl_git_fail(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); cl_assert_equal_i(git_error_last()->klass, GIT_ERROR_FILTER); /* String with \r but without \r\n does not fail with safecrlf */ - in.ptr = "Normal\nCR only\rand some more\nline-endings.\n"; - in.size = strlen(in.ptr); + in = "Normal\nCR only\rand some more\nline-endings.\n"; + in_len = strlen(in); - cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); + cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); cl_assert_equal_s("Normal\nCR only\rand some more\nline-endings.\n", out.ptr); git_filter_list_free(fl); @@ -121,7 +127,9 @@ void test_filter_crlf__with_safecrlf_and_unsafe_allowed(void) { git_filter_list *fl; git_filter *crlf; - git_buf in = {0}, out = GIT_BUF_INIT; + git_buf out = GIT_BUF_INIT; + const char *in; + size_t in_len; cl_repo_set_bool(g_repo, "core.safecrlf", true); @@ -134,25 +142,25 @@ void test_filter_crlf__with_safecrlf_and_unsafe_allowed(void) cl_git_pass(git_filter_list_push(fl, crlf, NULL)); /* Normalized \r\n succeeds with safecrlf */ - in.ptr = "Normal\r\nCRLF\r\nline-endings.\r\n"; - in.size = strlen(in.ptr); + in = "Normal\r\nCRLF\r\nline-endings.\r\n"; + in_len = strlen(in); - cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); + cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr); /* Mix of line endings fails with safecrlf, but allowed to pass */ - in.ptr = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n"; - in.size = strlen(in.ptr); + in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n"; + in_len = strlen(in); - cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); + cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); /* TODO: check for warning */ cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr); /* Normalized \n fails with safecrlf, but allowed to pass */ - in.ptr = "Normal\nLF\nonly\nline-endings.\n"; - in.size = strlen(in.ptr); + in = "Normal\nLF\nonly\nline-endings.\n"; + in_len = strlen(in); - cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); + cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); /* TODO: check for warning */ cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out.ptr); @@ -164,7 +172,9 @@ void test_filter_crlf__no_safecrlf(void) { git_filter_list *fl; git_filter *crlf; - git_buf in = {0}, out = GIT_BUF_INIT; + git_buf out = GIT_BUF_INIT; + const char *in; + size_t in_len; cl_git_pass(git_filter_list_new( &fl, g_repo, GIT_FILTER_TO_ODB, 0)); @@ -175,24 +185,24 @@ void test_filter_crlf__no_safecrlf(void) cl_git_pass(git_filter_list_push(fl, crlf, NULL)); /* Normalized \r\n succeeds with safecrlf */ - in.ptr = "Normal\r\nCRLF\r\nline-endings.\r\n"; - in.size = strlen(in.ptr); + in = "Normal\r\nCRLF\r\nline-endings.\r\n"; + in_len = strlen(in); - cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); + cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr); /* Mix of line endings fails with safecrlf */ - in.ptr = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n"; - in.size = strlen(in.ptr); + in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n"; + in_len = strlen(in); - cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); + cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr); /* Normalized \n fails with safecrlf */ - in.ptr = "Normal\nLF\nonly\nline-endings.\n"; - in.size = strlen(in.ptr); + in = "Normal\nLF\nonly\nline-endings.\n"; + in_len = strlen(in); - cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); + cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out.ptr); git_filter_list_free(fl); @@ -203,7 +213,9 @@ void test_filter_crlf__safecrlf_warn(void) { git_filter_list *fl; git_filter *crlf; - git_buf in = {0}, out = GIT_BUF_INIT; + git_buf out = GIT_BUF_INIT; + const char *in; + size_t in_len; cl_repo_set_string(g_repo, "core.safecrlf", "warn"); @@ -216,26 +228,26 @@ void test_filter_crlf__safecrlf_warn(void) cl_git_pass(git_filter_list_push(fl, crlf, NULL)); /* Normalized \r\n succeeds with safecrlf=warn */ - in.ptr = "Normal\r\nCRLF\r\nline-endings.\r\n"; - in.size = strlen(in.ptr); + in = "Normal\r\nCRLF\r\nline-endings.\r\n"; + in_len = strlen(in); - cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); + cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr); /* Mix of line endings succeeds with safecrlf=warn */ - in.ptr = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n"; - in.size = strlen(in.ptr); + in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n"; + in_len = strlen(in); - cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); + cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); /* TODO: check for warning */ cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr); /* Normalized \n is reversible, so does not fail with safecrlf=warn */ - in.ptr = "Normal\nLF\nonly\nline-endings.\n"; - in.size = strlen(in.ptr); + in = "Normal\nLF\nonly\nline-endings.\n"; + in_len = strlen(in); - cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); - cl_assert_equal_s(in.ptr, out.ptr); + cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); + cl_assert_equal_s(in, out.ptr); git_filter_list_free(fl); git_buf_dispose(&out); diff --git a/tests/filter/custom.c b/tests/filter/custom.c index fe2025fc2..3554cb52c 100644 --- a/tests/filter/custom.c +++ b/tests/filter/custom.c @@ -95,13 +95,17 @@ static void register_custom_filters(void) void test_filter_custom__to_odb(void) { git_filter_list *fl; - git_buf out = { 0 }; - git_buf in = GIT_BUF_INIT_CONST(workdir_data, strlen(workdir_data)); + git_buf out = GIT_BUF_INIT; + const char *in; + size_t in_len; cl_git_pass(git_filter_list_load( &fl, g_repo, NULL, "herofile", GIT_FILTER_TO_ODB, 0)); - cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); + in = workdir_data; + in_len = strlen(workdir_data); + + cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); cl_assert_equal_i(BITFLIPPED_AND_REVERSED_DATA_LEN, out.size); @@ -115,14 +119,17 @@ void test_filter_custom__to_odb(void) void test_filter_custom__to_workdir(void) { git_filter_list *fl; - git_buf out = { 0 }; - git_buf in = GIT_BUF_INIT_CONST( - bitflipped_and_reversed_data, BITFLIPPED_AND_REVERSED_DATA_LEN); + git_buf out = GIT_BUF_INIT; + const char *in; + size_t in_len; cl_git_pass(git_filter_list_load( &fl, g_repo, NULL, "herofile", GIT_FILTER_TO_WORKTREE, 0)); - cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); + in = (char *)bitflipped_and_reversed_data; + in_len = BITFLIPPED_AND_REVERSED_DATA_LEN; + + cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len)); cl_assert_equal_i(strlen(workdir_data), out.size); @@ -246,12 +253,16 @@ void test_filter_custom__erroneous_filter_fails(void) { git_filter_list *filters; git_buf out = GIT_BUF_INIT; - git_buf in = GIT_BUF_INIT_CONST(workdir_data, strlen(workdir_data)); + const char *in; + size_t in_len; cl_git_pass(git_filter_list_load( &filters, g_repo, NULL, "villain", GIT_FILTER_TO_WORKTREE, 0)); - cl_git_fail(git_filter_list_apply_to_data(&out, filters, &in)); + in = workdir_data; + in_len = strlen(workdir_data); + + cl_git_fail(git_filter_list_apply_to_buffer(&out, filters, in, in_len)); git_filter_list_free(filters); git_buf_dispose(&out); diff --git a/tests/filter/wildcard.c b/tests/filter/wildcard.c index 3c25ea266..62a84ead5 100644 --- a/tests/filter/wildcard.c +++ b/tests/filter/wildcard.c @@ -123,13 +123,12 @@ static git_filter *create_wildcard_filter(void) void test_filter_wildcard__reverse(void) { git_filter_list *fl; - git_buf in = GIT_BUF_INIT, out = GIT_BUF_INIT; + git_buf out = GIT_BUF_INIT; cl_git_pass(git_filter_list_load( &fl, g_repo, NULL, "hero-reverse-foo", GIT_FILTER_TO_ODB, 0)); - cl_git_pass(git_buf_put(&in, (char *)input, DATA_LEN)); - cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); + cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, (char *)input, DATA_LEN)); cl_assert_equal_i(DATA_LEN, out.size); @@ -138,19 +137,17 @@ void test_filter_wildcard__reverse(void) git_filter_list_free(fl); git_buf_dispose(&out); - git_buf_dispose(&in); } void test_filter_wildcard__flip(void) { git_filter_list *fl; - git_buf in = GIT_BUF_INIT, out = GIT_BUF_INIT; + git_buf out = GIT_BUF_INIT; cl_git_pass(git_filter_list_load( &fl, g_repo, NULL, "hero-flip-foo", GIT_FILTER_TO_ODB, 0)); - cl_git_pass(git_buf_put(&in, (char *)input, DATA_LEN)); - cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); + cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, (char *)input, DATA_LEN)); cl_assert_equal_i(DATA_LEN, out.size); @@ -159,19 +156,17 @@ void test_filter_wildcard__flip(void) git_filter_list_free(fl); git_buf_dispose(&out); - git_buf_dispose(&in); } void test_filter_wildcard__none(void) { git_filter_list *fl; - git_buf in = GIT_BUF_INIT, out = GIT_BUF_INIT; + git_buf out = GIT_BUF_INIT; cl_git_pass(git_filter_list_load( &fl, g_repo, NULL, "none-foo", GIT_FILTER_TO_ODB, 0)); - cl_git_pass(git_buf_put(&in, (char *)input, DATA_LEN)); - cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in)); + cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, (char *)input, DATA_LEN)); cl_assert_equal_i(DATA_LEN, out.size); @@ -180,5 +175,4 @@ void test_filter_wildcard__none(void) git_filter_list_free(fl); git_buf_dispose(&out); - git_buf_dispose(&in); } |