]> git.proxmox.com Git - libgit2.git/blob - tests/filter/file.c
521c01c02e3ba86377a021474d4b9cd17944fffe
[libgit2.git] / tests / filter / file.c
1 #include "clar_libgit2.h"
2 #include "git2/sys/filter.h"
3 #include "crlf.h"
4 #include "buffer.h"
5
6 static git_repository *g_repo = NULL;
7
8 void test_filter_file__initialize(void)
9 {
10 git_reference *head_ref;
11 git_commit *head;
12
13 g_repo = cl_git_sandbox_init("crlf");
14
15 cl_git_mkfile("crlf/.gitattributes",
16 "*.txt text\n*.bin binary\n*.crlf text eol=crlf\n*.lf text eol=lf\n");
17
18 cl_repo_set_bool(g_repo, "core.autocrlf", true);
19
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));
23
24 git_commit_free(head);
25 git_reference_free(head_ref);
26 }
27
28 void test_filter_file__cleanup(void)
29 {
30 cl_git_sandbox_cleanup();
31 }
32
33 void test_filter_file__apply(void)
34 {
35 git_filter_list *fl;
36 git_filter *crlf;
37 git_buf buf = GIT_BUF_INIT;
38
39 cl_git_pass(git_filter_list_new(
40 &fl, g_repo, GIT_FILTER_TO_ODB, 0));
41
42 crlf = git_filter_lookup(GIT_FILTER_CRLF);
43 cl_assert(crlf != NULL);
44
45 cl_git_pass(git_filter_list_push(fl, crlf, NULL));
46
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);
49
50 git_buf_dispose(&buf);
51 git_filter_list_free(fl);
52 }
53
54 struct buf_writestream {
55 git_writestream base;
56 git_buf buf;
57 };
58
59 int buf_writestream_write(git_writestream *s, const char *buf, size_t len)
60 {
61 struct buf_writestream *stream = (struct buf_writestream *)s;
62 return git_buf_put(&stream->buf, buf, len);
63 }
64
65 int buf_writestream_close(git_writestream *s)
66 {
67 GIT_UNUSED(s);
68 return 0;
69 }
70
71 void buf_writestream_free(git_writestream *s)
72 {
73 struct buf_writestream *stream = (struct buf_writestream *)s;
74 git_buf_dispose(&stream->buf);
75 }
76
77 void test_filter_file__apply_stream(void)
78 {
79 git_filter_list *fl;
80 git_filter *crlf;
81 struct buf_writestream write_target = { {
82 buf_writestream_write,
83 buf_writestream_close,
84 buf_writestream_free } };
85
86 cl_git_pass(git_filter_list_new(
87 &fl, g_repo, GIT_FILTER_TO_ODB, 0));
88
89 crlf = git_filter_lookup(GIT_FILTER_CRLF);
90 cl_assert(crlf != NULL);
91
92 cl_git_pass(git_filter_list_push(fl, crlf, NULL));
93
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);
96
97 git_filter_list_free(fl);
98 write_target.base.free(&write_target.base);
99 }