@@ -256,3 +256,64 @@ void test_index_filemodes__invalid(void)
256256
257257 git_index_free (index );
258258}
259+
260+ void test_index_filemodes__frombuffer_requires_files (void )
261+ {
262+ git_index * index ;
263+ git_index_entry new_entry ;
264+ const git_index_entry * ret_entry ;
265+ const char * content = "hey there\n" ;
266+
267+ cl_git_pass (git_repository_index (& index , g_repo ));
268+
269+ /* regular blob */
270+ new_entry .path = "dummy-file.txt" ;
271+ new_entry .mode = GIT_FILEMODE_BLOB ;
272+
273+ cl_git_pass (git_index_add_frombuffer (index ,
274+ & new_entry , content , strlen (content )));
275+
276+ cl_assert ((ret_entry = git_index_get_bypath (index , "dummy-file.txt" , 0 )));
277+ cl_assert_equal_s ("dummy-file.txt" , ret_entry -> path );
278+ cl_assert_equal_i (GIT_FILEMODE_BLOB , ret_entry -> mode );
279+
280+ /* executable blob */
281+ new_entry .path = "dummy-file.txt" ;
282+ new_entry .mode = GIT_FILEMODE_BLOB_EXECUTABLE ;
283+
284+ cl_git_pass (git_index_add_frombuffer (index ,
285+ & new_entry , content , strlen (content )));
286+
287+ cl_assert ((ret_entry = git_index_get_bypath (index , "dummy-file.txt" , 0 )));
288+ cl_assert_equal_s ("dummy-file.txt" , ret_entry -> path );
289+ cl_assert_equal_i (GIT_FILEMODE_BLOB_EXECUTABLE , ret_entry -> mode );
290+
291+ /* links are also acceptable */
292+ new_entry .path = "dummy-link.txt" ;
293+ new_entry .mode = GIT_FILEMODE_LINK ;
294+
295+ cl_git_pass (git_index_add_frombuffer (index ,
296+ & new_entry , content , strlen (content )));
297+
298+ cl_assert ((ret_entry = git_index_get_bypath (index , "dummy-link.txt" , 0 )));
299+ cl_assert_equal_s ("dummy-link.txt" , ret_entry -> path );
300+ cl_assert_equal_i (GIT_FILEMODE_LINK , ret_entry -> mode );
301+
302+ /* trees are rejected */
303+ new_entry .path = "invalid_mode.txt" ;
304+ new_entry .mode = GIT_FILEMODE_TREE ;
305+
306+ cl_git_fail (git_index_add_frombuffer (index ,
307+ & new_entry , content , strlen (content )));
308+ cl_assert_equal_p (NULL , git_index_get_bypath (index , "invalid_mode.txt" , 0 ));
309+
310+ /* submodules are rejected */
311+ new_entry .path = "invalid_mode.txt" ;
312+ new_entry .mode = GIT_FILEMODE_COMMIT ;
313+
314+ cl_git_fail (git_index_add_frombuffer (index ,
315+ & new_entry , content , strlen (content )));
316+ cl_assert_equal_p (NULL , git_index_get_bypath (index , "invalid_mode.txt" , 0 ));
317+
318+ git_index_free (index );
319+ }
0 commit comments