]>
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_chmod_at_offset ( git_buf
* buf
, size_t offset
)
20 if ( cl_is_chmod_supported ())
23 if ( buf
-> size
> offset
+ 6 && memcmp (& buf
-> ptr
[ offset
], "100644" , 6 ) != 0 )
24 memcpy (& buf
-> ptr
[ offset
], "100644" , 6 );
27 void test_diff_drivers__patterns ( void )
30 const char * one_sha
= "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13" ;
34 git_buf actual
= GIT_BUF_INIT
;
35 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 " ;
36 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 " ;
37 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 " ;
39 g_repo
= cl_git_sandbox_init ( "renames" );
41 one
= resolve_commit_oid_to_tree ( g_repo
, one_sha
);
45 cl_git_pass ( git_diff_tree_to_workdir (& diff
, g_repo
, one
, NULL
));
46 cl_assert_equal_i ( 0 , ( int ) git_diff_num_deltas ( diff
));
51 cl_git_append2file ( "renames/untimely.txt" , " \r\n Some new stuff \r\n " );
53 cl_git_pass ( git_diff_tree_to_workdir (& diff
, g_repo
, one
, NULL
));
54 cl_assert_equal_i ( 1 , ( int ) git_diff_num_deltas ( diff
));
56 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
57 cl_git_pass ( git_patch_to_buf (& actual
, patch
));
58 cl_assert_equal_s ( expected0
, actual
. ptr
);
60 git_buf_free (& actual
);
61 git_patch_free ( patch
);
64 /* attribute diff set to false */
66 cl_git_rewritefile ( "renames/.gitattributes" , "untimely.txt -diff \n " );
68 cl_git_pass ( git_diff_tree_to_workdir (& diff
, g_repo
, one
, NULL
));
69 cl_assert_equal_i ( 1 , ( int ) git_diff_num_deltas ( diff
));
71 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
72 cl_git_pass ( git_patch_to_buf (& actual
, patch
));
73 cl_assert_equal_s ( expected1
, actual
. ptr
);
75 git_buf_free (& actual
);
76 git_patch_free ( patch
);
79 /* attribute diff set to unconfigured value (should use default) */
81 cl_git_rewritefile ( "renames/.gitattributes" , "untimely.txt diff=kipling0 \n " );
83 cl_git_pass ( git_diff_tree_to_workdir (& diff
, g_repo
, one
, NULL
));
84 cl_assert_equal_i ( 1 , ( int ) git_diff_num_deltas ( diff
));
86 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
87 cl_git_pass ( git_patch_to_buf (& actual
, patch
));
88 cl_assert_equal_s ( expected0
, actual
. ptr
);
90 git_buf_free (& actual
);
91 git_patch_free ( patch
);
94 /* let's define that driver */
96 cl_git_pass ( git_repository_config (& cfg
, g_repo
));
97 cl_git_pass ( git_config_set_bool ( cfg
, "diff.kipling0.binary" , 1 ));
100 cl_git_pass ( git_diff_tree_to_workdir (& diff
, g_repo
, one
, NULL
));
101 cl_assert_equal_i ( 1 , ( int ) git_diff_num_deltas ( diff
));
103 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
104 cl_git_pass ( git_patch_to_buf (& actual
, patch
));
105 cl_assert_equal_s ( expected1
, actual
. ptr
);
107 git_buf_free (& actual
);
108 git_patch_free ( patch
);
111 /* let's use a real driver with some regular expressions */
113 git_diff_driver_registry_free ( g_repo
-> diff_drivers
);
114 g_repo
-> diff_drivers
= NULL
;
116 cl_git_pass ( git_repository_config (& cfg
, g_repo
));
117 cl_git_pass ( git_config_set_bool ( cfg
, "diff.kipling0.binary" , 0 ));
118 cl_git_pass ( git_config_set_string ( cfg
, "diff.kipling0.xfuncname" , "^H" ));
119 git_config_free ( cfg
);
121 cl_git_pass ( git_diff_tree_to_workdir (& diff
, g_repo
, one
, NULL
));
122 cl_assert_equal_i ( 1 , ( int ) git_diff_num_deltas ( diff
));
124 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
125 cl_git_pass ( git_patch_to_buf (& actual
, patch
));
126 cl_assert_equal_s ( expected2
, actual
. ptr
);
128 git_buf_free (& actual
);
129 git_patch_free ( patch
);
135 void test_diff_drivers__long_lines ( void )
137 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 " ;
141 git_buf actual
= GIT_BUF_INIT
;
142 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 " ;
144 g_repo
= cl_git_sandbox_init ( "empty_standard_repo" );
146 cl_git_mkfile ( "empty_standard_repo/longlines.txt" , base
);
147 cl_git_pass ( git_repository_index (& idx
, g_repo
));
148 cl_git_pass ( git_index_add_bypath ( idx
, "longlines.txt" ));
149 cl_git_pass ( git_index_write ( idx
));
152 cl_git_append2file ( "empty_standard_repo/longlines.txt" , "newline \n newline \n " );
154 cl_git_pass ( git_diff_index_to_workdir (& diff
, g_repo
, NULL
, NULL
));
155 cl_assert_equal_sz ( 1 , git_diff_num_deltas ( diff
));
156 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
157 cl_git_pass ( git_patch_to_buf (& actual
, patch
));
159 /* if chmod not supported, overwrite mode bits since anything is possible */
160 overwrite_chmod_at_offset (& actual
, 66 );
162 cl_assert_equal_s ( expected
, actual
. ptr
);
164 git_buf_free (& actual
);
165 git_patch_free ( patch
);
169 void test_diff_drivers__builtins ( void )
174 git_buf actual
= GIT_BUF_INIT
;
175 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
178 " <h1 id= \" first section \" > \n <ol> \n <li>item 1.1</li> \n <li>item 1.2</li> \n <li>item 1.3</li> \n <li>item 1.4</li> \n <li>item 1.5</li> \n <li>item 1.6</li> \n <li>item 1.7</li> \n <li>item 1.8</li> \n <li>item 1.9</li> \n </ol> \n </h1> \n "
179 " <h1 id= \" second section \" > \n <ol> \n <li>item 2.1</li> \n <li>item 2.2</li> \n <li>item 2.3</li> \n <li>item 2.4</li> \n <li>item 2.5</li> \n <li>item 2.6</li> \n <li>item 2.7</li> \n <li>item 2.8</li> \n </ol> \n </h1> \n "
180 " <h1 id= \" third section \" > \n <ol> \n <li>item 3.1</li> \n <li>item 3.2</li> \n <li>item 3.3</li> \n <li>item 3.4</li> \n <li>item 3.5</li> \n <li>item 3.6</li> \n <li>item 3.7</li> \n <li>item 3.8</li> \n </ol> \n </h1> \n "
182 const char * modified
=
184 " <h1 id= \" first section \" > \n <ol> \n <li>item 1.1</li> \n <li>item 1.2 changed</li> \n <li>item 1.3 changed</li> \n <li>item 1.4</li> \n <li>item 1.5</li> \n <li>item 1.6</li> \n <li>item 1.7</li> \n <li>item 1.8</li> \n <li>item 1.9</li> \n <li>item 1.10 added</li> \n </ol> \n </h1> \n "
185 " <h1 id= \" second section \" > \n <ol> \n <li>item 2.1</li> \n <li>item 2.2</li> \n <li>item 2.3</li> \n <li>item 2.4</li> \n <li>item 2.5</li> \n <li>item 2.6</li> \n <li>item 2.7 changed</li> \n <li>item 2.7.1 added</li> \n <li>item 2.8</li> \n </ol> \n </h1> \n "
186 " <h1 id= \" third section \" > \n <ol> \n <li>item 3.1</li> \n <li>item 3.2</li> \n <li>item 3.3</li> \n <li>item 3.4</li> \n <li>item 3.5</li> \n <li>item 3.6</li> \n </ol> \n </h1> \n "
188 const char * expected_nodriver
=
189 "diff --git a/file.html b/file.html \n index 97b34db..c7dbed3 100644 \n --- a/file.html \n +++ b/file.html \n @@ -5,4 +5,4 @@ \n <li>item 1.1</li> \n - <li>item 1.2</li> \n - <li>item 1.3</li> \n + <li>item 1.2 changed</li> \n + <li>item 1.3 changed</li> \n <li>item 1.4</li> \n @@ -13,2 +13,3 @@ \n <li>item 1.9</li> \n + <li>item 1.10 added</li> \n </ol> \n @@ -23,3 +24,4 @@ \n <li>item 2.6</li> \n - <li>item 2.7</li> \n + <li>item 2.7 changed</li> \n + <li>item 2.7.1 added</li> \n <li>item 2.8</li> \n @@ -35,4 +37,2 @@ \n <li>item 3.6</li> \n - <li>item 3.7</li> \n - <li>item 3.8</li> \n </ol> \n " ;
190 const char * expected_driver
=
191 "diff --git a/file.html b/file.html \n index 97b34db..c7dbed3 100644 \n --- a/file.html \n +++ b/file.html \n @@ -5,4 +5,4 @@ <h1 id= \" first section \" > \n <li>item 1.1</li> \n - <li>item 1.2</li> \n - <li>item 1.3</li> \n + <li>item 1.2 changed</li> \n + <li>item 1.3 changed</li> \n <li>item 1.4</li> \n @@ -13,2 +13,3 @@ <h1 id= \" first section \" > \n <li>item 1.9</li> \n + <li>item 1.10 added</li> \n </ol> \n @@ -23,3 +24,4 @@ <h1 id= \" second section \" > \n <li>item 2.6</li> \n - <li>item 2.7</li> \n + <li>item 2.7 changed</li> \n + <li>item 2.7.1 added</li> \n <li>item 2.8</li> \n @@ -35,4 +37,2 @@ <h1 id= \" third section \" > \n <li>item 3.6</li> \n - <li>item 3.7</li> \n - <li>item 3.8</li> \n </ol> \n " ;
193 g_repo
= cl_git_sandbox_init ( "empty_standard_repo" );
195 cl_git_mkfile ( "empty_standard_repo/file.html" , base
);
196 cl_git_pass ( git_repository_index (& idx
, g_repo
));
197 cl_git_pass ( git_index_add_bypath ( idx
, "file.html" ));
198 cl_git_pass ( git_index_write ( idx
));
201 cl_git_rewritefile ( "empty_standard_repo/file.html" , modified
);
203 /* do diff with no special driver */
205 opts
. interhunk_lines
= 1 ;
206 opts
. context_lines
= 1 ;
208 cl_git_pass ( git_diff_index_to_workdir (& diff
, g_repo
, NULL
, & opts
));
209 cl_assert_equal_sz ( 1 , git_diff_num_deltas ( diff
));
210 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
211 cl_git_pass ( git_patch_to_buf (& actual
, patch
));
213 overwrite_chmod_at_offset (& actual
, 59 );
215 cl_assert_equal_s ( expected_nodriver
, actual
. ptr
);
217 git_buf_free (& actual
);
218 git_patch_free ( patch
);
221 /* do diff with HTML driver */
223 cl_git_mkfile ( "empty_standard_repo/.gitattributes" , "*.html diff=html \n " );
225 cl_git_pass ( git_diff_index_to_workdir (& diff
, g_repo
, NULL
, & opts
));
226 cl_assert_equal_sz ( 1 , git_diff_num_deltas ( diff
));
227 cl_git_pass ( git_patch_from_diff (& patch
, diff
, 0 ));
228 cl_git_pass ( git_patch_to_buf (& actual
, patch
));
230 overwrite_chmod_at_offset (& actual
, 59 );
232 cl_assert_equal_s ( expected_driver
, actual
. ptr
);
234 git_buf_free (& actual
);
235 git_patch_free ( patch
);