]>
git.proxmox.com Git - libgit2.git/blob - tests/libgit2/diff/drivers.c
1 #include "clar_libgit2.h"
2 #include "diff_helpers.h"
3 #include "repository.h"
4 #include "diff_driver.h"
6 static git_repository
* g_repo
= NULL
;
8 void test_diff_drivers__initialize ( void )
12 void test_diff_drivers__cleanup ( void )
14 cl_git_sandbox_cleanup ();
18 static void overwrite_filemode ( const char * expected
, git_buf
* actual
)
23 found
= strstr ( expected
, "100644" );
27 offset
= (( const char *) found
) - expected
;
28 if ( actual
-> size
< offset
+ 6 )
31 if ( memcmp (& actual
-> ptr
[ offset
], "100644" , 6 ) != 0 )
32 memcpy (& actual
-> ptr
[ offset
], "100644" , 6 );
35 void test_diff_drivers__patterns ( void )
38 const char * one_sha
= "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13" ;
42 git_buf actual
= GIT_BUF_INIT
;
43 const char * expected0
= "diff --git a/untimely.txt b/untimely.txt \n index 9a69d96..57fd0cf 100644 \n --- a/untimely.txt \n +++ b/untimely.txt \n @@ -22,3 +22,5 @@ Comes through the blood of the vanguards who \n dreamed--too soon--it had sounded. \r\n \r\n -- Rudyard Kipling \r\n + \r\n +Some new stuff \r\n " ;
44 const char * expected1
= "diff --git a/untimely.txt b/untimely.txt \n index 9a69d96..57fd0cf 100644 \n Binary files a/untimely.txt and b/untimely.txt differ \n " ;
45 const char * expected2
= "diff --git a/untimely.txt b/untimely.txt \n index 9a69d96..57fd0cf 100644 \n --- a/untimely.txt \n +++ b/untimely.txt \n @@ -22,3 +22,5 @@ Heaven delivers on earth the Hour that cannot be \n dreamed--too soon--it had sounded. \r\n \r\n -- Rudyard Kipling \r\n + \r\n +Some new stuff \r\n " ;
47 g_repo
= cl_git_sandbox_init ( "renames" );
49 one
= resolve_commit_oid_to_tree ( g_repo
, one_sha
);
53 cl_git_pass ( git_diff_tree_to_workdir (& diff
, g_repo
, one
, NULL
));
54 cl_assert_equal_i ( 0 , ( int ) git_diff_num_deltas ( diff
));
59 cl_git_append2file ( "renames/untimely.txt" , " \r\n Some new stuff \r\n " );
61 cl_git_pass ( git_diff_tree_to_workdir (& diff
, g_repo
, one
, NULL
));
62 cl_assert_equal_i ( 1 , ( int ) git_diff_num_deltas ( diff
));
64 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
65 cl_git_pass ( git_patch_to_buf (& actual
, patch
));
66 cl_assert_equal_s ( expected0
, actual
. ptr
);
68 git_buf_dispose (& actual
);
69 git_patch_free ( patch
);
72 /* attribute diff set to false */
74 cl_git_rewritefile ( "renames/.gitattributes" , "untimely.txt -diff \n " );
76 cl_git_pass ( git_diff_tree_to_workdir (& diff
, g_repo
, one
, NULL
));
77 cl_assert_equal_i ( 1 , ( int ) git_diff_num_deltas ( diff
));
79 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
80 cl_git_pass ( git_patch_to_buf (& actual
, patch
));
81 cl_assert_equal_s ( expected1
, actual
. ptr
);
83 git_buf_dispose (& actual
);
84 git_patch_free ( patch
);
87 /* attribute diff set to unconfigured value (should use default) */
89 cl_git_rewritefile ( "renames/.gitattributes" , "untimely.txt diff=kipling0 \n " );
91 cl_git_pass ( git_diff_tree_to_workdir (& diff
, g_repo
, one
, NULL
));
92 cl_assert_equal_i ( 1 , ( int ) git_diff_num_deltas ( diff
));
94 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
95 cl_git_pass ( git_patch_to_buf (& actual
, patch
));
96 cl_assert_equal_s ( expected0
, actual
. ptr
);
98 git_buf_dispose (& actual
);
99 git_patch_free ( patch
);
102 /* let's define that driver */
104 cl_git_pass ( git_repository_config (& cfg
, g_repo
));
105 cl_git_pass ( git_config_set_bool ( cfg
, "diff.kipling0.binary" , 1 ));
106 git_config_free ( cfg
);
108 cl_git_pass ( git_diff_tree_to_workdir (& diff
, g_repo
, one
, NULL
));
109 cl_assert_equal_i ( 1 , ( int ) git_diff_num_deltas ( diff
));
111 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
112 cl_git_pass ( git_patch_to_buf (& actual
, patch
));
113 cl_assert_equal_s ( expected1
, actual
. ptr
);
115 git_buf_dispose (& actual
);
116 git_patch_free ( patch
);
119 /* let's use a real driver with some regular expressions */
121 git_diff_driver_registry_free ( g_repo
-> diff_drivers
);
122 g_repo
-> diff_drivers
= NULL
;
124 cl_git_pass ( git_repository_config (& cfg
, g_repo
));
125 cl_git_pass ( git_config_set_bool ( cfg
, "diff.kipling0.binary" , 0 ));
126 cl_git_pass ( git_config_set_string ( cfg
, "diff.kipling0.xfuncname" , "^H.*$" ));
127 git_config_free ( cfg
);
129 cl_git_pass ( git_diff_tree_to_workdir (& diff
, g_repo
, one
, NULL
));
130 cl_assert_equal_i ( 1 , ( int ) git_diff_num_deltas ( diff
));
132 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
133 cl_git_pass ( git_patch_to_buf (& actual
, patch
));
134 cl_assert_equal_s ( expected2
, actual
. ptr
);
136 git_buf_dispose (& actual
);
137 git_patch_free ( patch
);
143 void test_diff_drivers__long_lines ( void )
145 const char * base
= "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non nisi ligula. Ut viverra enim sed lobortis suscipit. \n Phasellus eget erat odio. Praesent at est iaculis, ultricies augue vel, dignissim risus. Suspendisse at nisi quis turpis fringilla rutrum id sit amet nulla. \n Nam eget dolor fermentum, aliquet nisl at, convallis tellus. Pellentesque rhoncus erat enim, id porttitor elit euismod quis. \n Mauris sollicitudin magna odio, non egestas libero vehicula ut. Etiam et quam velit. Fusce eget libero rhoncus, ultricies felis sit amet, egestas purus. \n Aliquam in semper tellus. Pellentesque adipiscing rutrum velit, quis malesuada lacus consequat eget. \n " ;
149 git_buf actual
= GIT_BUF_INIT
;
150 const char * expected
= "diff --git a/longlines.txt b/longlines.txt \n index c1ce6ef..0134431 100644 \n --- a/longlines.txt \n +++ b/longlines.txt \n @@ -3,3 +3,5 @@ Phasellus eget erat odio. Praesent at est iaculis, ultricies augue vel, dignissi \n Nam eget dolor fermentum, aliquet nisl at, convallis tellus. Pellentesque rhoncus erat enim, id porttitor elit euismod quis. \n Mauris sollicitudin magna odio, non egestas libero vehicula ut. Etiam et quam velit. Fusce eget libero rhoncus, ultricies felis sit amet, egestas purus. \n Aliquam in semper tellus. Pellentesque adipiscing rutrum velit, quis malesuada lacus consequat eget. \n +newline \n +newline \n " ;
152 g_repo
= cl_git_sandbox_init ( "empty_standard_repo" );
154 cl_git_mkfile ( "empty_standard_repo/longlines.txt" , base
);
155 cl_git_pass ( git_repository_index (& idx
, g_repo
));
156 cl_git_pass ( git_index_add_bypath ( idx
, "longlines.txt" ));
157 cl_git_pass ( git_index_write ( idx
));
160 cl_git_append2file ( "empty_standard_repo/longlines.txt" , "newline \n newline \n " );
162 cl_git_pass ( git_diff_index_to_workdir (& diff
, g_repo
, NULL
, NULL
));
163 cl_assert_equal_sz ( 1 , git_diff_num_deltas ( diff
));
164 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
165 cl_git_pass ( git_patch_to_buf (& actual
, patch
));
167 /* if chmod not supported, overwrite mode bits since anything is possible */
168 overwrite_filemode ( expected
, & actual
);
170 cl_assert_equal_s ( expected
, actual
. ptr
);
172 git_buf_dispose (& actual
);
173 git_patch_free ( patch
);
177 void test_diff_drivers__builtins ( void )
181 git_str file
= GIT_STR_INIT
, expected
= GIT_STR_INIT
;
182 git_buf actual
= GIT_BUF_INIT
;
183 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
184 git_vector files
= GIT_VECTOR_INIT
;
186 char * path
, * extension
;
188 g_repo
= cl_git_sandbox_init ( "userdiff" );
190 cl_git_pass ( git_fs_path_dirload (& files
, "userdiff/files" , 9 , 0 ));
192 opts
. interhunk_lines
= 1 ;
193 opts
. context_lines
= 1 ;
194 opts
. pathspec
. count
= 1 ;
196 git_vector_foreach (& files
, i
, path
) {
197 if ( git__prefixcmp ( path
, "files/file." ))
199 extension
= path
+ strlen ( "files/file." );
200 opts
. pathspec
. strings
= & path
;
202 /* do diff with no special driver */
204 cl_git_pass ( git_diff_index_to_workdir (& diff
, g_repo
, NULL
, & opts
));
205 cl_assert_equal_sz ( 1 , git_diff_num_deltas ( diff
));
206 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
207 cl_git_pass ( git_patch_to_buf (& actual
, patch
));
209 git_str_sets (& expected
, "userdiff/expected/nodriver/diff." );
210 git_str_puts (& expected
, extension
);
211 cl_git_pass ( git_futils_readbuffer (& expected
, expected
. ptr
));
213 overwrite_filemode ( expected
. ptr
, & actual
);
215 cl_assert_equal_s ( expected
. ptr
, actual
. ptr
);
217 git_buf_dispose (& actual
);
218 git_patch_free ( patch
);
221 /* do diff with driver */
224 FILE * fp
= fopen ( "userdiff/.gitattributes" , "w" );
225 fprintf ( fp
, "*.%s diff=%s \n " , extension
, extension
);
229 cl_git_pass ( git_diff_index_to_workdir (& diff
, g_repo
, NULL
, & opts
));
230 cl_assert_equal_sz ( 1 , git_diff_num_deltas ( diff
));
231 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
232 cl_git_pass ( git_patch_to_buf (& actual
, patch
));
234 git_str_sets (& expected
, "userdiff/expected/driver/diff." );
235 git_str_puts (& expected
, extension
);
236 cl_git_pass ( git_futils_readbuffer (& expected
, expected
. ptr
));
238 overwrite_filemode ( expected
. ptr
, & actual
);
240 cl_assert_equal_s ( expected
. ptr
, actual
. ptr
);
242 git_buf_dispose (& actual
);
243 git_patch_free ( patch
);
249 git_buf_dispose (& actual
);
250 git_str_dispose (& file
);
251 git_str_dispose (& expected
);
252 git_vector_free (& files
);
255 void test_diff_drivers__invalid_pattern ( void )
261 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
263 g_repo
= cl_git_sandbox_init ( "userdiff" );
264 cl_git_mkfile ( "userdiff/.gitattributes" , "*.storyboard diff=storyboard \n " );
266 cl_git_pass ( git_repository_config__weakptr (& cfg
, g_repo
));
267 cl_git_pass ( git_config_set_string ( cfg
, "diff.storyboard.xfuncname" , "<!--(.*?)-->" ));
269 cl_git_mkfile ( "userdiff/dummy.storyboard" , "" );
270 cl_git_pass ( git_repository_index__weakptr (& idx
, g_repo
));
271 cl_git_pass ( git_index_add_bypath ( idx
, "dummy.storyboard" ));
272 cl_git_mkfile ( "userdiff/dummy.storyboard" , "some content \n " );
274 cl_git_pass ( git_diff_index_to_workdir (& diff
, g_repo
, NULL
, & opts
));
275 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
277 git_patch_free ( patch
);