1 #include "clar_libgit2.h"
2 #include "git2/sys/filter.h"
6 static git_repository
*g_repo
= NULL
;
8 void test_filter_file__initialize(void)
10 git_reference
*head_ref
;
13 g_repo
= cl_git_sandbox_init("crlf");
15 cl_git_mkfile("crlf/.gitattributes",
16 "*.txt text\n*.bin binary\n*.crlf text eol=crlf\n*.lf text eol=lf\n");
18 cl_repo_set_bool(g_repo
, "core.autocrlf", true);
20 cl_git_pass(git_repository_head(&head_ref
, g_repo
));
21 cl_git_pass(git_reference_peel((git_object
**)&head
, head_ref
, GIT_OBJECT_COMMIT
));
22 cl_git_pass(git_reset(g_repo
, (git_object
*)head
, GIT_RESET_HARD
, NULL
));
24 git_commit_free(head
);
25 git_reference_free(head_ref
);
28 void test_filter_file__cleanup(void)
30 cl_git_sandbox_cleanup();
33 void test_filter_file__apply(void)
37 git_buf buf
= GIT_BUF_INIT
;
39 cl_git_pass(git_filter_list_new(
40 &fl
, g_repo
, GIT_FILTER_TO_ODB
, 0));
42 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
43 cl_assert(crlf
!= NULL
);
45 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
47 cl_git_pass(git_filter_list_apply_to_file(&buf
, fl
, g_repo
, "all-crlf"));
48 cl_assert_equal_s("crlf\ncrlf\ncrlf\ncrlf\n", buf
.ptr
);
50 git_buf_dispose(&buf
);
51 git_filter_list_free(fl
);
54 struct buf_writestream
{
59 int buf_writestream_write(git_writestream
*s
, const char *buf
, size_t len
)
61 struct buf_writestream
*stream
= (struct buf_writestream
*)s
;
62 return git_buf_put(&stream
->buf
, buf
, len
);
65 int buf_writestream_close(git_writestream
*s
)
71 void buf_writestream_free(git_writestream
*s
)
73 struct buf_writestream
*stream
= (struct buf_writestream
*)s
;
74 git_buf_dispose(&stream
->buf
);
77 void test_filter_file__apply_stream(void)
81 struct buf_writestream write_target
= { {
82 buf_writestream_write
,
83 buf_writestream_close
,
84 buf_writestream_free
} };
86 cl_git_pass(git_filter_list_new(
87 &fl
, g_repo
, GIT_FILTER_TO_ODB
, 0));
89 crlf
= git_filter_lookup(GIT_FILTER_CRLF
);
90 cl_assert(crlf
!= NULL
);
92 cl_git_pass(git_filter_list_push(fl
, crlf
, NULL
));
94 cl_git_pass(git_filter_list_stream_file(fl
, g_repo
, "all-crlf", &write_target
.base
));
95 cl_assert_equal_s("crlf\ncrlf\ncrlf\ncrlf\n", write_target
.buf
.ptr
);
97 git_filter_list_free(fl
);
98 write_target
.base
.free(&write_target
.base
);