]>
git.proxmox.com Git - libgit2.git/blob - tests/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_free (& 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_free (& 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_free (& 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_free (& 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_free (& 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_free (& actual
);
173 git_patch_free ( patch
);
177 void test_diff_drivers__builtins ( void )
181 git_buf file
= GIT_BUF_INIT
, actual
= GIT_BUF_INIT
, expected
= GIT_BUF_INIT
;
182 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
183 git_vector files
= GIT_VECTOR_INIT
;
185 char * path
, * extension
;
187 g_repo
= cl_git_sandbox_init ( "userdiff" );
189 cl_git_pass ( git_path_dirload (& files
, "userdiff/files" , 9 , 0 ));
191 opts
. interhunk_lines
= 1 ;
192 opts
. context_lines
= 1 ;
193 opts
. pathspec
. count
= 1 ;
195 git_vector_foreach (& files
, i
, path
) {
196 if ( git__prefixcmp ( path
, "files/file." ))
198 extension
= path
+ strlen ( "files/file." );
199 opts
. pathspec
. strings
= & path
;
201 /* do diff with no special driver */
203 cl_git_pass ( git_diff_index_to_workdir (& diff
, g_repo
, NULL
, & opts
));
204 cl_assert_equal_sz ( 1 , git_diff_num_deltas ( diff
));
205 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
206 cl_git_pass ( git_patch_to_buf (& actual
, patch
));
208 git_buf_sets (& expected
, "userdiff/expected/nodriver/diff." );
209 git_buf_puts (& expected
, extension
);
210 cl_git_pass ( git_futils_readbuffer (& expected
, expected
. ptr
));
212 overwrite_filemode ( expected
. ptr
, & actual
);
214 cl_assert_equal_s ( expected
. ptr
, actual
. ptr
);
216 git_buf_clear (& actual
);
217 git_patch_free ( patch
);
220 /* do diff with driver */
223 FILE * fp
= fopen ( "userdiff/.gitattributes" , "w" );
224 fprintf ( fp
, "*.%s diff=%s \n " , extension
, extension
);
228 cl_git_pass ( git_diff_index_to_workdir (& diff
, g_repo
, NULL
, & opts
));
229 cl_assert_equal_sz ( 1 , git_diff_num_deltas ( diff
));
230 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
231 cl_git_pass ( git_patch_to_buf (& actual
, patch
));
233 git_buf_sets (& expected
, "userdiff/expected/driver/diff." );
234 git_buf_puts (& expected
, extension
);
235 cl_git_pass ( git_futils_readbuffer (& expected
, expected
. ptr
));
237 overwrite_filemode ( expected
. ptr
, & actual
);
239 cl_assert_equal_s ( expected
. ptr
, actual
. ptr
);
241 git_buf_clear (& actual
);
242 git_patch_free ( patch
);
249 git_buf_free (& actual
);
250 git_buf_free (& expected
);
251 git_vector_free (& files
);
254 void test_diff_drivers__invalid_pattern ( void )
260 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
262 g_repo
= cl_git_sandbox_init ( "userdiff" );
263 cl_git_mkfile ( "userdiff/.gitattributes" , "*.storyboard diff=storyboard \n " );
265 cl_git_pass ( git_repository_config__weakptr (& cfg
, g_repo
));
266 cl_git_pass ( git_config_set_string ( cfg
, "diff.storyboard.xfuncname" , "<!--(.*?)-->" ));
268 cl_git_mkfile ( "userdiff/dummy.storyboard" , "" );
269 cl_git_pass ( git_repository_index__weakptr (& idx
, g_repo
));
270 cl_git_pass ( git_index_add_bypath ( idx
, "dummy.storyboard" ));
271 cl_git_mkfile ( "userdiff/dummy.storyboard" , "some content \n " );
273 cl_git_pass ( git_diff_index_to_workdir (& diff
, g_repo
, NULL
, & opts
));
274 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
276 git_patch_free ( patch
);