1 #include "clar_libgit2.h"
6 #include "submodule_helpers.h"
7 #include "git2/sys/repository.h"
9 /* rewrite gitmodules -> .gitmodules
10 * rewrite the empty or relative urls inside each module
11 * rename the .gitted directory inside any submodule to .git
13 void rewrite_gitmodules(const char *workdir
)
15 git_buf in_f
= GIT_BUF_INIT
, out_f
= GIT_BUF_INIT
, path
= GIT_BUF_INIT
;
19 cl_git_pass(git_buf_joinpath(&in_f
, workdir
, "gitmodules"));
20 cl_git_pass(git_buf_joinpath(&out_f
, workdir
, ".gitmodules"));
22 cl_assert((in
= fopen(in_f
.ptr
, "rb")) != NULL
);
23 cl_assert((out
= fopen(out_f
.ptr
, "wb")) != NULL
);
25 while (fgets(line
, sizeof(line
), in
) != NULL
) {
28 while (*scan
== ' ' || *scan
== '\t') scan
++;
30 /* rename .gitted -> .git in submodule directories */
31 if (git__prefixcmp(scan
, "path =") == 0) {
32 scan
+= strlen("path =");
33 while (*scan
== ' ') scan
++;
35 git_buf_joinpath(&path
, workdir
, scan
);
37 git_buf_joinpath(&path
, path
.ptr
, ".gitted");
39 if (!git_buf_oom(&path
) && p_access(path
.ptr
, F_OK
) == 0) {
40 git_buf_joinpath(&out_f
, workdir
, scan
);
41 git_buf_rtrim(&out_f
);
42 git_buf_joinpath(&out_f
, out_f
.ptr
, ".git");
44 if (!git_buf_oom(&out_f
))
45 p_rename(path
.ptr
, out_f
.ptr
);
49 /* copy non-"url =" lines verbatim */
50 if (git__prefixcmp(scan
, "url =") != 0) {
55 /* convert relative URLs in "url =" lines */
56 scan
+= strlen("url =");
57 while (*scan
== ' ') scan
++;
60 git_buf_joinpath(&path
, workdir
, scan
);
62 } else if (!*scan
|| *scan
== '\n') {
63 git_buf_joinpath(&path
, workdir
, "../testrepo.git");
69 git_path_prettify(&path
, path
.ptr
, NULL
);
70 git_buf_putc(&path
, '\n');
71 cl_assert(!git_buf_oom(&path
));
73 fwrite(line
, scan
- line
, sizeof(char), out
);
80 cl_must_pass(p_unlink(in_f
.ptr
));
87 static void cleanup_fixture_submodules(void *payload
)
89 cl_git_sandbox_cleanup(); /* either "submodules" or "submod2" */
92 cl_fixture_cleanup(payload
);
95 git_repository
*setup_fixture_submodules(void)
97 git_repository
*repo
= cl_git_sandbox_init("submodules");
99 cl_fixture_sandbox("testrepo.git");
101 rewrite_gitmodules(git_repository_workdir(repo
));
102 p_rename("submodules/testrepo/.gitted", "submodules/testrepo/.git");
104 cl_set_cleanup(cleanup_fixture_submodules
, "testrepo.git");
106 cl_git_pass(git_repository_reinit_filesystem(repo
, 1));
111 git_repository
*setup_fixture_submod2(void)
113 git_repository
*repo
= cl_git_sandbox_init("submod2");
115 cl_fixture_sandbox("submod2_target");
116 p_rename("submod2_target/.gitted", "submod2_target/.git");
118 rewrite_gitmodules(git_repository_workdir(repo
));
119 p_rename("submod2/not-submodule/.gitted", "submod2/not-submodule/.git");
120 p_rename("submod2/not/.gitted", "submod2/not/.git");
122 cl_set_cleanup(cleanup_fixture_submodules
, "submod2_target");
124 cl_git_pass(git_repository_reinit_filesystem(repo
, 1));
129 git_repository
*setup_fixture_super(void)
131 git_repository
*repo
= cl_git_sandbox_init("super");
133 cl_fixture_sandbox("sub.git");
134 p_mkdir("super/sub", 0777);
136 rewrite_gitmodules(git_repository_workdir(repo
));
138 cl_set_cleanup(cleanup_fixture_submodules
, "sub.git");
140 cl_git_pass(git_repository_reinit_filesystem(repo
, 1));
145 git_repository
*setup_fixture_submodule_simple(void)
147 git_repository
*repo
= cl_git_sandbox_init("submodule_simple");
149 cl_fixture_sandbox("testrepo.git");
150 p_mkdir("submodule_simple/testrepo", 0777);
152 cl_set_cleanup(cleanup_fixture_submodules
, "testrepo.git");
154 cl_git_pass(git_repository_reinit_filesystem(repo
, 1));
159 git_repository
*setup_fixture_submodule_with_path(void)
161 git_repository
*repo
= cl_git_sandbox_init("submodule_with_path");
163 cl_fixture_sandbox("testrepo.git");
164 p_mkdir("submodule_with_path/lib", 0777);
165 p_mkdir("submodule_with_path/lib/testrepo", 0777);
167 cl_set_cleanup(cleanup_fixture_submodules
, "testrepo.git");
169 cl_git_pass(git_repository_reinit_filesystem(repo
, 1));
174 void assert__submodule_exists(
175 git_repository
*repo
, const char *name
,
176 const char *msg
, const char *file
, int line
)
179 int error
= git_submodule_lookup(&sm
, repo
, name
);
181 cl_git_report_failure(error
, file
, line
, msg
);
182 cl_assert_at_line(sm
!= NULL
, file
, line
);
183 git_submodule_free(sm
);
186 void refute__submodule_exists(
187 git_repository
*repo
, const char *name
, int expected_error
,
188 const char *msg
, const char *file
, int line
)
191 file
, line
, msg
, 1, "%i",
192 expected_error
, (int)(git_submodule_lookup(NULL
, repo
, name
)));
195 unsigned int get_submodule_status(git_repository
*repo
, const char *name
)
197 unsigned int status
= 0;
199 assert(repo
&& name
);
201 cl_git_pass(git_submodule_status(&status
, repo
, name
, GIT_SUBMODULE_IGNORE_UNSPECIFIED
));
206 static int print_submodules(git_submodule
*sm
, const char *name
, void *p
)
208 unsigned int loc
= 0;
210 git_submodule_location(&loc
, sm
);
211 fprintf(stderr
, "# submodule %s (at %s) flags %x\n",
212 name
, git_submodule_path(sm
), loc
);
216 void dump_submodules(git_repository
*repo
)
218 git_submodule_foreach(repo
, print_submodules
, NULL
);