]> git.proxmox.com Git - libgit2.git/blob - tests/filter/file.c
Merge pull request #2970 from ethomson/inmemory_bare
[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_OBJ_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_free(&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 return 0;
68 }
69
70 void buf_writestream_free(git_writestream *s)
71 {
72 struct buf_writestream *stream = (struct buf_writestream *)s;
73 git_buf_free(&stream->buf);
74 }
75
76 void test_filter_file__apply_stream(void)
77 {
78 git_filter_list *fl;
79 git_filter *crlf;
80 struct buf_writestream write_target = { {
81 buf_writestream_write,
82 buf_writestream_close,
83 buf_writestream_free } };
84
85 cl_git_pass(git_filter_list_new(
86 &fl, g_repo, GIT_FILTER_TO_ODB, 0));
87
88 crlf = git_filter_lookup(GIT_FILTER_CRLF);
89 cl_assert(crlf != NULL);
90
91 cl_git_pass(git_filter_list_push(fl, crlf, NULL));
92
93 cl_git_pass(git_filter_list_stream_file(fl, g_repo, "all-crlf", (git_writestream *)&write_target));
94 cl_assert_equal_s("crlf\ncrlf\ncrlf\ncrlf\n", write_target.buf.ptr);
95
96 git_filter_list_free(fl);
97 }