]>
Commit | Line | Data |
---|---|---|
b2ab887e JM |
1 | #include "clar_libgit2.h" |
2 | #include "posix.h" | |
3 | #include "path.h" | |
4 | #include "submodule_helpers.h" | |
22a2d3d5 | 5 | #include "futils.h" |
b2ab887e JM |
6 | |
7 | static git_repository *g_repo = NULL; | |
8 | ||
9 | void test_submodule_init__cleanup(void) | |
10 | { | |
11 | cl_git_sandbox_cleanup(); | |
12 | } | |
13 | ||
14 | void test_submodule_init__absolute_url(void) | |
15 | { | |
16 | git_submodule *sm; | |
17 | git_config *cfg; | |
18 | git_buf absolute_url = GIT_BUF_INIT; | |
19 | const char *config_url; | |
20 | ||
21 | g_repo = setup_fixture_submodule_simple(); | |
22 | ||
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")); | |
25 | ||
b2ab887e | 26 | /* write the absolute url to the .gitmodules file*/ |
d6073b30 CMN |
27 | cl_git_pass(git_submodule_set_url(g_repo, "testrepo", absolute_url.ptr)); |
28 | ||
29 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo")); | |
b2ab887e JM |
30 | |
31 | /* verify that the .gitmodules is set with an absolute path*/ | |
32 | cl_assert_equal_s(absolute_url.ptr, git_submodule_url(sm)); | |
33 | ||
34 | /* init and verify that absolute path is written to .git/config */ | |
35 | cl_git_pass(git_submodule_init(sm, false)); | |
36 | ||
9a97f49e | 37 | cl_git_pass(git_repository_config_snapshot(&cfg, g_repo)); |
b2ab887e | 38 | |
9a97f49e | 39 | cl_git_pass(git_config_get_string(&config_url, cfg, "submodule.testrepo.url")); |
b2ab887e JM |
40 | cl_assert_equal_s(absolute_url.ptr, config_url); |
41 | ||
ac3d33df | 42 | git_buf_dispose(&absolute_url); |
b2ab887e | 43 | git_config_free(cfg); |
365d3672 | 44 | git_submodule_free(sm); |
b2ab887e JM |
45 | } |
46 | ||
47 | void test_submodule_init__relative_url(void) | |
48 | { | |
49 | git_submodule *sm; | |
50 | git_config *cfg; | |
51 | git_buf absolute_url = GIT_BUF_INIT; | |
52 | const char *config_url; | |
53 | ||
54 | g_repo = setup_fixture_submodule_simple(); | |
55 | ||
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")); | |
58 | ||
59 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo")); | |
60 | ||
61 | /* verify that the .gitmodules is set with an absolute path*/ | |
62 | cl_assert_equal_s("../testrepo.git", git_submodule_url(sm)); | |
63 | ||
64 | /* init and verify that absolute path is written to .git/config */ | |
65 | cl_git_pass(git_submodule_init(sm, false)); | |
66 | ||
9a97f49e | 67 | cl_git_pass(git_repository_config_snapshot(&cfg, g_repo)); |
b2ab887e | 68 | |
9a97f49e | 69 | cl_git_pass(git_config_get_string(&config_url, cfg, "submodule.testrepo.url")); |
b2ab887e JM |
70 | cl_assert_equal_s(absolute_url.ptr, config_url); |
71 | ||
ac3d33df | 72 | git_buf_dispose(&absolute_url); |
b2ab887e | 73 | git_config_free(cfg); |
365d3672 | 74 | git_submodule_free(sm); |
b2ab887e | 75 | } |
9d1f97df JM |
76 | |
77 | void test_submodule_init__relative_url_detached_head(void) | |
78 | { | |
79 | git_submodule *sm; | |
80 | git_config *cfg; | |
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; | |
85 | ||
86 | g_repo = setup_fixture_submodule_simple(); | |
87 | ||
88 | /* Put the parent repository into a detached head state. */ | |
89 | cl_git_pass(git_repository_head(&head_ref, g_repo)); | |
ac3d33df | 90 | cl_git_pass(git_reference_peel(&head_commit, head_ref, GIT_OBJECT_COMMIT)); |
9d1f97df | 91 | |
4e498646 | 92 | cl_git_pass(git_repository_set_head_detached(g_repo, git_commit_id((git_commit *)head_commit))); |
9d1f97df JM |
93 | |
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")); | |
96 | ||
97 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo")); | |
98 | ||
99 | /* verify that the .gitmodules is set with an absolute path*/ | |
100 | cl_assert_equal_s("../testrepo.git", git_submodule_url(sm)); | |
101 | ||
102 | /* init and verify that absolute path is written to .git/config */ | |
103 | cl_git_pass(git_submodule_init(sm, false)); | |
104 | ||
9a97f49e | 105 | cl_git_pass(git_repository_config_snapshot(&cfg, g_repo)); |
9d1f97df | 106 | |
9a97f49e | 107 | cl_git_pass(git_config_get_string(&config_url, cfg, "submodule.testrepo.url")); |
9d1f97df JM |
108 | cl_assert_equal_s(absolute_url.ptr, config_url); |
109 | ||
ac3d33df | 110 | git_buf_dispose(&absolute_url); |
9d1f97df | 111 | git_config_free(cfg); |
fe8399fe | 112 | git_object_free(head_commit); |
365d3672 ET |
113 | git_reference_free(head_ref); |
114 | git_submodule_free(sm); | |
9d1f97df | 115 | } |