55#include "fileops.h"
66#include "repository.h"
77#include "remote.h"
8+ #include "repo/repo_helpers.h"
89
910static git_repository * g_repo ;
11+ static git_buf g_global_path = GIT_BUF_INIT ;
1012
1113void test_checkout_index__initialize (void )
1214{
@@ -22,21 +24,29 @@ void test_checkout_index__initialize(void)
2224 cl_git_rewritefile (
2325 "./testrepo/.gitattributes" ,
2426 "* text eol=lf\n" );
27+
28+ git_libgit2_opts (GIT_OPT_GET_SEARCH_PATH , GIT_CONFIG_LEVEL_GLOBAL ,
29+ & g_global_path );
2530}
2631
2732void test_checkout_index__cleanup (void )
2833{
34+ git_libgit2_opts (GIT_OPT_SET_SEARCH_PATH , GIT_CONFIG_LEVEL_GLOBAL ,
35+ g_global_path .ptr );
36+ git_buf_dispose (& g_global_path );
37+
2938 cl_git_sandbox_cleanup ();
3039
31- /* try to remove alternative dir */
32- if (git_path_isdir ("alternative" ))
33- git_futils_rmdir_r ("alternative" , NULL , GIT_RMDIR_REMOVE_FILES );
40+ /* try to remove directories created by tests */
41+ cl_fixture_cleanup ("alternative" );
42+ cl_fixture_cleanup ("symlink" );
43+ cl_fixture_cleanup ("symlink.git" );
44+ cl_fixture_cleanup ("tmp_global_path" );
3445}
3546
3647void test_checkout_index__cannot_checkout_a_bare_repository (void )
3748{
38- test_checkout_index__cleanup ();
39-
49+ cl_git_sandbox_cleanup ();
4050 g_repo = cl_git_sandbox_init ("testrepo.git" );
4151
4252 cl_git_fail (git_checkout_index (g_repo , NULL , NULL ));
@@ -136,23 +146,20 @@ void test_checkout_index__honor_coreautocrlf_setting_set_to_true(void)
136146#endif
137147}
138148
139- void test_checkout_index__honor_coresymlinks_default (void )
149+ static void populate_symlink_workdir (void )
140150{
141151 git_repository * repo ;
142152 git_remote * origin ;
143153 git_object * target ;
144- char cwd [GIT_PATH_MAX ];
145154
146155 const char * url = git_repository_path (g_repo );
147156
148- cl_assert (getcwd (cwd , sizeof (cwd )) != NULL );
149- cl_assert_equal_i (0 , p_mkdir ("readonly" , 0555 )); /* Read-only directory */
150- cl_assert_equal_i (0 , chdir ("readonly" ));
151157 cl_git_pass (git_repository_init (& repo , "../symlink.git" , true));
152- cl_assert_equal_i (0 , chdir (cwd ));
153- cl_assert_equal_i (0 , p_mkdir ("symlink" , 0777 ));
154158 cl_git_pass (git_repository_set_workdir (repo , "symlink" , 1 ));
155159
160+ /* Delete the `origin` repo (if it exists) so we can recreate it. */
161+ git_remote_delete (repo , GIT_REMOTE_ORIGIN );
162+
156163 cl_git_pass (git_remote_create (& origin , repo , GIT_REMOTE_ORIGIN , url ));
157164 cl_git_pass (git_remote_fetch (origin , NULL , NULL , NULL ));
158165 git_remote_free (origin );
@@ -161,23 +168,54 @@ void test_checkout_index__honor_coresymlinks_default(void)
161168 cl_git_pass (git_reset (repo , target , GIT_RESET_HARD , NULL ));
162169 git_object_free (target );
163170 git_repository_free (repo );
171+ }
164172
165- if (!filesystem_supports_symlinks ("symlink/test" )) {
166- check_file_contents ("./symlink/link_to_new.txt" , "new.txt" );
167- } else {
168- char link_data [1024 ];
169- int link_size = 1024 ;
173+ void test_checkout_index__honor_coresymlinks_default_true (void )
174+ {
175+ char link_data [GIT_PATH_MAX ];
176+ int link_size = GIT_PATH_MAX ;
170177
171- link_size = p_readlink ("./symlink/link_to_new.txt" , link_data , link_size );
172- cl_assert (link_size >= 0 );
178+ cl_must_pass (p_mkdir ("symlink" , 0777 ));
173179
174- link_data [link_size ] = '\0' ;
175- cl_assert_equal_i (link_size , strlen ("new.txt" ));
176- cl_assert_equal_s (link_data , "new.txt" );
177- check_file_contents ("./symlink/link_to_new.txt" , "my new file\n" );
178- }
180+ if (!filesystem_supports_symlinks ("symlink/test" ))
181+ cl_skip ();
179182
180- cl_fixture_cleanup ("symlink" );
183+ #ifdef GIT_WIN32
184+ /*
185+ * Windows explicitly requires the global configuration to have
186+ * core.symlinks=true in addition to actual filesystem support.
187+ */
188+ create_tmp_global_config ("tmp_global_path" , "core.symlinks" , "true" );
189+ #endif
190+
191+ populate_symlink_workdir ();
192+
193+ link_size = p_readlink ("./symlink/link_to_new.txt" , link_data , link_size );
194+ cl_assert (link_size >= 0 );
195+
196+ link_data [link_size ] = '\0' ;
197+ cl_assert_equal_i (link_size , strlen ("new.txt" ));
198+ cl_assert_equal_s (link_data , "new.txt" );
199+ check_file_contents ("./symlink/link_to_new.txt" , "my new file\n" );
200+ }
201+
202+ void test_checkout_index__honor_coresymlinks_default_false (void )
203+ {
204+ cl_must_pass (p_mkdir ("symlink" , 0777 ));
205+
206+ #ifndef GIT_WIN32
207+ /*
208+ * This test is largely for Windows platforms to ensure that
209+ * we respect an unset core.symlinks even when the platform
210+ * supports symlinks. Bail entirely on POSIX platforms that
211+ * do support symlinks.
212+ */
213+ if (filesystem_supports_symlinks ("symlink/test" ))
214+ cl_skip ();
215+ #endif
216+
217+ populate_symlink_workdir ();
218+ check_file_contents ("./symlink/link_to_new.txt" , "new.txt" );
181219}
182220
183221void test_checkout_index__coresymlinks_set_to_true_fails_when_unsupported (void )
@@ -558,9 +596,9 @@ void test_checkout_index__can_update_prefixed_files(void)
558596
559597void test_checkout_index__can_checkout_a_newly_initialized_repository (void )
560598{
561- test_checkout_index__cleanup ();
562-
599+ cl_git_sandbox_cleanup ();
563600 g_repo = cl_git_sandbox_init ("empty_standard_repo" );
601+
564602 cl_git_remove_placeholders (git_repository_path (g_repo ), "dummy-marker.txt" );
565603
566604 cl_git_pass (git_checkout_index (g_repo , NULL , NULL ));
@@ -570,8 +608,7 @@ void test_checkout_index__issue_1397(void)
570608{
571609 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT ;
572610
573- test_checkout_index__cleanup ();
574-
611+ cl_git_sandbox_cleanup ();
575612 g_repo = cl_git_sandbox_init ("issue_1397" );
576613
577614 cl_repo_set_bool (g_repo , "core.autocrlf" , true);
@@ -624,8 +661,7 @@ void test_checkout_index__target_directory_from_bare(void)
624661 checkout_counts cts ;
625662 memset (& cts , 0 , sizeof (cts ));
626663
627- test_checkout_index__cleanup ();
628-
664+ cl_git_sandbox_cleanup ();
629665 g_repo = cl_git_sandbox_init ("testrepo.git" );
630666 cl_assert (git_repository_is_bare (g_repo ));
631667
0 commit comments