]>
git.proxmox.com Git - libgit2.git/blob - tests/submodule/init.c
84143e18f2951f82fc76d543fab9f21cec27566a
1 #include "clar_libgit2.h"
4 #include "submodule_helpers.h"
7 static git_repository
*g_repo
= NULL
;
9 void test_submodule_init__cleanup(void)
11 cl_git_sandbox_cleanup();
14 void test_submodule_init__absolute_url(void)
18 git_buf absolute_url
= GIT_BUF_INIT
;
19 const char *config_url
;
21 g_repo
= setup_fixture_submodule_simple();
23 cl_assert(git_path_dirname_r(&absolute_url
, git_repository_workdir(g_repo
)) > 0);
24 cl_git_pass(git_buf_joinpath(&absolute_url
, absolute_url
.ptr
, "testrepo.git"));
26 /* write the absolute url to the .gitmodules file*/
27 cl_git_pass(git_submodule_set_url(g_repo
, "testrepo", absolute_url
.ptr
));
29 cl_git_pass(git_submodule_lookup(&sm
, g_repo
, "testrepo"));
31 /* verify that the .gitmodules is set with an absolute path*/
32 cl_assert_equal_s(absolute_url
.ptr
, git_submodule_url(sm
));
34 /* init and verify that absolute path is written to .git/config */
35 cl_git_pass(git_submodule_init(sm
, false));
37 cl_git_pass(git_repository_config_snapshot(&cfg
, g_repo
));
39 cl_git_pass(git_config_get_string(&config_url
, cfg
, "submodule.testrepo.url"));
40 cl_assert_equal_s(absolute_url
.ptr
, config_url
);
42 git_buf_dispose(&absolute_url
);
44 git_submodule_free(sm
);
47 void test_submodule_init__relative_url(void)
51 git_buf absolute_url
= GIT_BUF_INIT
;
52 const char *config_url
;
54 g_repo
= setup_fixture_submodule_simple();
56 cl_assert(git_path_dirname_r(&absolute_url
, git_repository_workdir(g_repo
)) > 0);
57 cl_git_pass(git_buf_joinpath(&absolute_url
, absolute_url
.ptr
, "testrepo.git"));
59 cl_git_pass(git_submodule_lookup(&sm
, g_repo
, "testrepo"));
61 /* verify that the .gitmodules is set with an absolute path*/
62 cl_assert_equal_s("../testrepo.git", git_submodule_url(sm
));
64 /* init and verify that absolute path is written to .git/config */
65 cl_git_pass(git_submodule_init(sm
, false));
67 cl_git_pass(git_repository_config_snapshot(&cfg
, g_repo
));
69 cl_git_pass(git_config_get_string(&config_url
, cfg
, "submodule.testrepo.url"));
70 cl_assert_equal_s(absolute_url
.ptr
, config_url
);
72 git_buf_dispose(&absolute_url
);
74 git_submodule_free(sm
);
77 void test_submodule_init__relative_url_detached_head(void)
81 git_buf absolute_url
= GIT_BUF_INIT
;
82 const char *config_url
;
83 git_reference
*head_ref
= NULL
;
84 git_object
*head_commit
= NULL
;
86 g_repo
= setup_fixture_submodule_simple();
88 /* Put the parent repository into a detached head state. */
89 cl_git_pass(git_repository_head(&head_ref
, g_repo
));
90 cl_git_pass(git_reference_peel(&head_commit
, head_ref
, GIT_OBJECT_COMMIT
));
92 cl_git_pass(git_repository_set_head_detached(g_repo
, git_commit_id((git_commit
*)head_commit
)));
94 cl_assert(git_path_dirname_r(&absolute_url
, git_repository_workdir(g_repo
)) > 0);
95 cl_git_pass(git_buf_joinpath(&absolute_url
, absolute_url
.ptr
, "testrepo.git"));
97 cl_git_pass(git_submodule_lookup(&sm
, g_repo
, "testrepo"));
99 /* verify that the .gitmodules is set with an absolute path*/
100 cl_assert_equal_s("../testrepo.git", git_submodule_url(sm
));
102 /* init and verify that absolute path is written to .git/config */
103 cl_git_pass(git_submodule_init(sm
, false));
105 cl_git_pass(git_repository_config_snapshot(&cfg
, g_repo
));
107 cl_git_pass(git_config_get_string(&config_url
, cfg
, "submodule.testrepo.url"));
108 cl_assert_equal_s(absolute_url
.ptr
, config_url
);
110 git_buf_dispose(&absolute_url
);
111 git_config_free(cfg
);
112 git_object_free(head_commit
);
113 git_reference_free(head_ref
);
114 git_submodule_free(sm
);