]> git.proxmox.com Git - libgit2.git/blob - tests/filter/crlf.c
Introduce core.safecrlf handling
[libgit2.git] / tests / filter / crlf.c
1 #include "clar_libgit2.h"
2 #include "git2/sys/filter.h"
3 #include "buffer.h"
4
5 static git_repository *g_repo = NULL;
6
7 void test_filter_crlf__initialize(void)
8 {
9 g_repo = cl_git_sandbox_init("crlf");
10
11 cl_git_mkfile("crlf/.gitattributes",
12 "*.txt text\n*.bin binary\n*.crlf text eol=crlf\n*.lf text eol=lf\n");
13
14 cl_repo_set_bool(g_repo, "core.autocrlf", true);
15 }
16
17 void test_filter_crlf__cleanup(void)
18 {
19 cl_git_sandbox_cleanup();
20 }
21
22 void test_filter_crlf__to_worktree(void)
23 {
24 git_filter_list *fl;
25 git_filter *crlf;
26 git_buf in = { 0 }, out = { 0 };
27
28 cl_git_pass(git_filter_list_new(&fl, g_repo, GIT_FILTER_TO_WORKTREE));
29
30 crlf = git_filter_lookup(GIT_FILTER_CRLF);
31 cl_assert(crlf != NULL);
32
33 cl_git_pass(git_filter_list_push(fl, crlf, NULL));
34
35 in.ptr = "Some text\nRight here\n";
36 in.size = strlen(in.ptr);
37
38 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
39
40 #ifdef GIT_WIN32
41 cl_assert_equal_s("Some text\r\nRight here\r\n", out.ptr);
42 #else
43 cl_assert_equal_s("Some text\nRight here\n", out.ptr);
44 #endif
45
46 git_filter_list_free(fl);
47 git_buf_free(&out);
48 }
49
50 void test_filter_crlf__to_odb(void)
51 {
52 git_filter_list *fl;
53 git_filter *crlf;
54 git_buf in = { 0 }, out = { 0 };
55
56 cl_git_pass(git_filter_list_new(&fl, g_repo, GIT_FILTER_TO_ODB));
57
58 crlf = git_filter_lookup(GIT_FILTER_CRLF);
59 cl_assert(crlf != NULL);
60
61 cl_git_pass(git_filter_list_push(fl, crlf, NULL));
62
63 in.ptr = "Some text\r\nRight here\r\n";
64 in.size = strlen(in.ptr);
65
66 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
67
68 cl_assert_equal_s("Some text\nRight here\n", out.ptr);
69
70 git_filter_list_free(fl);
71 git_buf_free(&out);
72 }
73
74 void test_filter_crlf__with_safecrlf(void)
75 {
76 git_filter_list *fl;
77 git_filter *crlf;
78 git_buf in = {0}, out = GIT_BUF_INIT;
79
80 cl_repo_set_bool(g_repo, "core.safecrlf", true);
81
82 cl_git_pass(git_filter_list_new(&fl, g_repo, GIT_FILTER_TO_ODB));
83
84 crlf = git_filter_lookup(GIT_FILTER_CRLF);
85 cl_assert(crlf != NULL);
86
87 cl_git_pass(git_filter_list_push(fl, crlf, NULL));
88
89 /* Normalized \r\n succeeds with safecrlf */
90 in.ptr = "Normal\r\nCRLF\r\nline-endings.\r\n";
91 in.size = strlen(in.ptr);
92
93 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
94 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr);
95
96 /* Mix of line endings fails with safecrlf */
97 in.ptr = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
98 in.size = strlen(in.ptr);
99
100 cl_git_fail(git_filter_list_apply_to_data(&out, fl, &in));
101 cl_assert_equal_i(giterr_last()->klass, GITERR_FILTER);
102
103 /* Normalized \n fails with safecrlf */
104 in.ptr = "Normal\nLF\nonly\nline-endings.\n";
105 in.size = strlen(in.ptr);
106
107 cl_git_fail(git_filter_list_apply_to_data(&out, fl, &in));
108 cl_assert_equal_i(giterr_last()->klass, GITERR_FILTER);
109
110 git_filter_list_free(fl);
111 git_buf_free(&out);
112 }
113
114 void test_filter_crlf__no_safecrlf(void)
115 {
116 git_filter_list *fl;
117 git_filter *crlf;
118 git_buf in = {0}, out = GIT_BUF_INIT;
119
120 cl_git_pass(git_filter_list_new(&fl, g_repo, GIT_FILTER_TO_ODB));
121
122 crlf = git_filter_lookup(GIT_FILTER_CRLF);
123 cl_assert(crlf != NULL);
124
125 cl_git_pass(git_filter_list_push(fl, crlf, NULL));
126
127 /* Normalized \r\n succeeds with safecrlf */
128 in.ptr = "Normal\r\nCRLF\r\nline-endings.\r\n";
129 in.size = strlen(in.ptr);
130
131 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
132 cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr);
133
134 /* Mix of line endings fails with safecrlf */
135 in.ptr = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
136 in.size = strlen(in.ptr);
137
138 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
139 cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr);
140
141 /* Normalized \n fails with safecrlf */
142 in.ptr = "Normal\nLF\nonly\nline-endings.\n";
143 in.size = strlen(in.ptr);
144
145 cl_git_pass(git_filter_list_apply_to_data(&out, fl, &in));
146 cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out.ptr);
147
148 git_filter_list_free(fl);
149 git_buf_free(&out);
150 }
151