1 #include "clar_libgit2.h"
5 #include "submodule_helpers.h"
6 #include "git2/sys/repository.h"
8 /* rewrite gitmodules -> .gitmodules
9 * rewrite the empty or relative urls inside each module
10 * rename the .gitted directory inside any submodule to .git
12 void rewrite_gitmodules(const char *workdir
)
14 git_str in_f
= GIT_STR_INIT
, out_f
= GIT_STR_INIT
, path
= GIT_STR_INIT
;
18 cl_git_pass(git_str_joinpath(&in_f
, workdir
, "gitmodules"));
19 cl_git_pass(git_str_joinpath(&out_f
, workdir
, ".gitmodules"));
21 cl_assert((in
= fopen(in_f
.ptr
, "rb")) != NULL
);
22 cl_assert((out
= fopen(out_f
.ptr
, "wb")) != NULL
);
24 while (fgets(line
, sizeof(line
), in
) != NULL
) {
27 while (*scan
== ' ' || *scan
== '\t') scan
++;
29 /* rename .gitted -> .git in submodule directories */
30 if (git__prefixcmp(scan
, "path =") == 0) {
31 scan
+= strlen("path =");
32 while (*scan
== ' ') scan
++;
34 git_str_joinpath(&path
, workdir
, scan
);
36 git_str_joinpath(&path
, path
.ptr
, ".gitted");
38 if (!git_str_oom(&path
) && p_access(path
.ptr
, F_OK
) == 0) {
39 git_str_joinpath(&out_f
, workdir
, scan
);
40 git_str_rtrim(&out_f
);
41 git_str_joinpath(&out_f
, out_f
.ptr
, ".git");
43 if (!git_str_oom(&out_f
))
44 p_rename(path
.ptr
, out_f
.ptr
);
48 /* copy non-"url =" lines verbatim */
49 if (git__prefixcmp(scan
, "url =") != 0) {
54 /* convert relative URLs in "url =" lines */
55 scan
+= strlen("url =");
56 while (*scan
== ' ') scan
++;
59 git_str_joinpath(&path
, workdir
, scan
);
61 } else if (!*scan
|| *scan
== '\n') {
62 git_str_joinpath(&path
, workdir
, "../testrepo.git");
68 git_fs_path_prettify(&path
, path
.ptr
, NULL
);
69 git_str_putc(&path
, '\n');
70 cl_assert(!git_str_oom(&path
));
72 fwrite(line
, scan
- line
, sizeof(char), out
);
79 cl_must_pass(p_unlink(in_f
.ptr
));
81 git_str_dispose(&in_f
);
82 git_str_dispose(&out_f
);
83 git_str_dispose(&path
);
86 static void cleanup_fixture_submodules(void *payload
)
88 cl_git_sandbox_cleanup(); /* either "submodules" or "submod2" */
91 cl_fixture_cleanup(payload
);
94 git_repository
*setup_fixture_submodules(void)
96 git_repository
*repo
= cl_git_sandbox_init("submodules");
98 cl_fixture_sandbox("testrepo.git");
100 rewrite_gitmodules(git_repository_workdir(repo
));
101 p_rename("submodules/testrepo/.gitted", "submodules/testrepo/.git");
103 cl_set_cleanup(cleanup_fixture_submodules
, "testrepo.git");
105 cl_git_pass(git_repository_reinit_filesystem(repo
, 1));
110 git_repository
*setup_fixture_submod2(void)
112 git_repository
*repo
= cl_git_sandbox_init("submod2");
114 cl_fixture_sandbox("submod2_target");
115 p_rename("submod2_target/.gitted", "submod2_target/.git");
117 rewrite_gitmodules(git_repository_workdir(repo
));
118 p_rename("submod2/not-submodule/.gitted", "submod2/not-submodule/.git");
119 p_rename("submod2/not/.gitted", "submod2/not/.git");
121 cl_set_cleanup(cleanup_fixture_submodules
, "submod2_target");
123 cl_git_pass(git_repository_reinit_filesystem(repo
, 1));
128 git_repository
*setup_fixture_submod3(void)
130 git_repository
*repo
= cl_git_sandbox_init("submod3");
132 cl_fixture_sandbox("submod2_target");
133 p_rename("submod2_target/.gitted", "submod2_target/.git");
135 rewrite_gitmodules(git_repository_workdir(repo
));
136 p_rename("submod3/One/.gitted", "submod3/One/.git");
137 p_rename("submod3/TWO/.gitted", "submod3/TWO/.git");
138 p_rename("submod3/three/.gitted", "submod3/three/.git");
139 p_rename("submod3/FoUr/.gitted", "submod3/FoUr/.git");
140 p_rename("submod3/Five/.gitted", "submod3/Five/.git");
141 p_rename("submod3/six/.gitted", "submod3/six/.git");
142 p_rename("submod3/sEvEn/.gitted", "submod3/sEvEn/.git");
143 p_rename("submod3/EIGHT/.gitted", "submod3/EIGHT/.git");
144 p_rename("submod3/nine/.gitted", "submod3/nine/.git");
145 p_rename("submod3/TEN/.gitted", "submod3/TEN/.git");
147 cl_set_cleanup(cleanup_fixture_submodules
, "submod2_target");
149 cl_git_pass(git_repository_reinit_filesystem(repo
, 1));
154 git_repository
*setup_fixture_super(void)
156 git_repository
*repo
= cl_git_sandbox_init("super");
158 cl_fixture_sandbox("sub.git");
159 p_mkdir("super/sub", 0777);
161 rewrite_gitmodules(git_repository_workdir(repo
));
163 cl_set_cleanup(cleanup_fixture_submodules
, "sub.git");
165 cl_git_pass(git_repository_reinit_filesystem(repo
, 1));
170 git_repository
*setup_fixture_submodule_simple(void)
172 git_repository
*repo
= cl_git_sandbox_init("submodule_simple");
174 cl_fixture_sandbox("testrepo.git");
175 p_mkdir("submodule_simple/testrepo", 0777);
177 cl_set_cleanup(cleanup_fixture_submodules
, "testrepo.git");
179 cl_git_pass(git_repository_reinit_filesystem(repo
, 1));
184 git_repository
*setup_fixture_submodule_with_path(void)
186 git_repository
*repo
= cl_git_sandbox_init("submodule_with_path");
188 cl_fixture_sandbox("testrepo.git");
189 p_mkdir("submodule_with_path/lib", 0777);
190 p_mkdir("submodule_with_path/lib/testrepo", 0777);
192 cl_set_cleanup(cleanup_fixture_submodules
, "testrepo.git");
194 cl_git_pass(git_repository_reinit_filesystem(repo
, 1));
199 void assert__submodule_exists(
200 git_repository
*repo
, const char *name
,
201 const char *msg
, const char *file
, const char *func
, int line
)
204 int error
= git_submodule_lookup(&sm
, repo
, name
);
206 cl_git_report_failure(error
, 0, file
, func
, line
, msg
);
207 cl_assert_at_line(sm
!= NULL
, file
, func
, line
);
208 git_submodule_free(sm
);
211 void refute__submodule_exists(
212 git_repository
*repo
, const char *name
, int expected_error
,
213 const char *msg
, const char *file
, const char *func
, int line
)
216 file
, func
, line
, msg
, 1, "%i",
217 expected_error
, (int)(git_submodule_lookup(NULL
, repo
, name
)));
220 unsigned int get_submodule_status(git_repository
*repo
, const char *name
)
222 unsigned int status
= 0;
224 assert(repo
&& name
);
226 cl_git_pass(git_submodule_status(&status
, repo
, name
, GIT_SUBMODULE_IGNORE_UNSPECIFIED
));
231 static int print_submodules(git_submodule
*sm
, const char *name
, void *p
)
233 unsigned int loc
= 0;
235 git_submodule_location(&loc
, sm
);
236 fprintf(stderr
, "# submodule %s (at %s) flags %x\n",
237 name
, git_submodule_path(sm
), loc
);
241 void dump_submodules(git_repository
*repo
)
243 git_submodule_foreach(repo
, print_submodules
, NULL
);