]> git.proxmox.com Git - libgit2.git/blob - tests/submodule/submodule_helpers.c
b8fc9f60deb8d8ddfe7e2c334e1ef61d4c1810cf
[libgit2.git] / tests / submodule / submodule_helpers.c
1 #include "clar_libgit2.h"
2 #include "path.h"
3 #include "util.h"
4 #include "posix.h"
5 #include "submodule_helpers.h"
6 #include "git2/sys/repository.h"
7
8 /* rewrite gitmodules -> .gitmodules
9 * rewrite the empty or relative urls inside each module
10 * rename the .gitted directory inside any submodule to .git
11 */
12 void rewrite_gitmodules(const char *workdir)
13 {
14 git_str in_f = GIT_STR_INIT, out_f = GIT_STR_INIT, path = GIT_STR_INIT;
15 FILE *in, *out;
16 char line[256];
17
18 cl_git_pass(git_str_joinpath(&in_f, workdir, "gitmodules"));
19 cl_git_pass(git_str_joinpath(&out_f, workdir, ".gitmodules"));
20
21 cl_assert((in = fopen(in_f.ptr, "rb")) != NULL);
22 cl_assert((out = fopen(out_f.ptr, "wb")) != NULL);
23
24 while (fgets(line, sizeof(line), in) != NULL) {
25 char *scan = line;
26
27 while (*scan == ' ' || *scan == '\t') scan++;
28
29 /* rename .gitted -> .git in submodule directories */
30 if (git__prefixcmp(scan, "path =") == 0) {
31 scan += strlen("path =");
32 while (*scan == ' ') scan++;
33
34 git_str_joinpath(&path, workdir, scan);
35 git_str_rtrim(&path);
36 git_str_joinpath(&path, path.ptr, ".gitted");
37
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");
42
43 if (!git_str_oom(&out_f))
44 p_rename(path.ptr, out_f.ptr);
45 }
46 }
47
48 /* copy non-"url =" lines verbatim */
49 if (git__prefixcmp(scan, "url =") != 0) {
50 fputs(line, out);
51 continue;
52 }
53
54 /* convert relative URLs in "url =" lines */
55 scan += strlen("url =");
56 while (*scan == ' ') scan++;
57
58 if (*scan == '.') {
59 git_str_joinpath(&path, workdir, scan);
60 git_str_rtrim(&path);
61 } else if (!*scan || *scan == '\n') {
62 git_str_joinpath(&path, workdir, "../testrepo.git");
63 } else {
64 fputs(line, out);
65 continue;
66 }
67
68 git_fs_path_prettify(&path, path.ptr, NULL);
69 git_str_putc(&path, '\n');
70 cl_assert(!git_str_oom(&path));
71
72 fwrite(line, scan - line, sizeof(char), out);
73 fputs(path.ptr, out);
74 }
75
76 fclose(in);
77 fclose(out);
78
79 cl_must_pass(p_unlink(in_f.ptr));
80
81 git_str_dispose(&in_f);
82 git_str_dispose(&out_f);
83 git_str_dispose(&path);
84 }
85
86 static void cleanup_fixture_submodules(void *payload)
87 {
88 cl_git_sandbox_cleanup(); /* either "submodules" or "submod2" */
89
90 if (payload)
91 cl_fixture_cleanup(payload);
92 }
93
94 git_repository *setup_fixture_submodules(void)
95 {
96 git_repository *repo = cl_git_sandbox_init("submodules");
97
98 cl_fixture_sandbox("testrepo.git");
99
100 rewrite_gitmodules(git_repository_workdir(repo));
101 p_rename("submodules/testrepo/.gitted", "submodules/testrepo/.git");
102
103 cl_set_cleanup(cleanup_fixture_submodules, "testrepo.git");
104
105 cl_git_pass(git_repository_reinit_filesystem(repo, 1));
106
107 return repo;
108 }
109
110 git_repository *setup_fixture_submod2(void)
111 {
112 git_repository *repo = cl_git_sandbox_init("submod2");
113
114 cl_fixture_sandbox("submod2_target");
115 p_rename("submod2_target/.gitted", "submod2_target/.git");
116
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");
120
121 cl_set_cleanup(cleanup_fixture_submodules, "submod2_target");
122
123 cl_git_pass(git_repository_reinit_filesystem(repo, 1));
124
125 return repo;
126 }
127
128 git_repository *setup_fixture_submod3(void)
129 {
130 git_repository *repo = cl_git_sandbox_init("submod3");
131
132 cl_fixture_sandbox("submod2_target");
133 p_rename("submod2_target/.gitted", "submod2_target/.git");
134
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");
146
147 cl_set_cleanup(cleanup_fixture_submodules, "submod2_target");
148
149 cl_git_pass(git_repository_reinit_filesystem(repo, 1));
150
151 return repo;
152 }
153
154 git_repository *setup_fixture_super(void)
155 {
156 git_repository *repo = cl_git_sandbox_init("super");
157
158 cl_fixture_sandbox("sub.git");
159 p_mkdir("super/sub", 0777);
160
161 rewrite_gitmodules(git_repository_workdir(repo));
162
163 cl_set_cleanup(cleanup_fixture_submodules, "sub.git");
164
165 cl_git_pass(git_repository_reinit_filesystem(repo, 1));
166
167 return repo;
168 }
169
170 git_repository *setup_fixture_submodule_simple(void)
171 {
172 git_repository *repo = cl_git_sandbox_init("submodule_simple");
173
174 cl_fixture_sandbox("testrepo.git");
175 p_mkdir("submodule_simple/testrepo", 0777);
176
177 cl_set_cleanup(cleanup_fixture_submodules, "testrepo.git");
178
179 cl_git_pass(git_repository_reinit_filesystem(repo, 1));
180
181 return repo;
182 }
183
184 git_repository *setup_fixture_submodule_with_path(void)
185 {
186 git_repository *repo = cl_git_sandbox_init("submodule_with_path");
187
188 cl_fixture_sandbox("testrepo.git");
189 p_mkdir("submodule_with_path/lib", 0777);
190 p_mkdir("submodule_with_path/lib/testrepo", 0777);
191
192 cl_set_cleanup(cleanup_fixture_submodules, "testrepo.git");
193
194 cl_git_pass(git_repository_reinit_filesystem(repo, 1));
195
196 return repo;
197 }
198
199 void assert__submodule_exists(
200 git_repository *repo, const char *name,
201 const char *msg, const char *file, const char *func, int line)
202 {
203 git_submodule *sm;
204 int error = git_submodule_lookup(&sm, repo, name);
205 if (error)
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);
209 }
210
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)
214 {
215 clar__assert_equal(
216 file, func, line, msg, 1, "%i",
217 expected_error, (int)(git_submodule_lookup(NULL, repo, name)));
218 }
219
220 unsigned int get_submodule_status(git_repository *repo, const char *name)
221 {
222 unsigned int status = 0;
223
224 assert(repo && name);
225
226 cl_git_pass(git_submodule_status(&status, repo, name, GIT_SUBMODULE_IGNORE_UNSPECIFIED));
227
228 return status;
229 }
230
231 static int print_submodules(git_submodule *sm, const char *name, void *p)
232 {
233 unsigned int loc = 0;
234 GIT_UNUSED(p);
235 git_submodule_location(&loc, sm);
236 fprintf(stderr, "# submodule %s (at %s) flags %x\n",
237 name, git_submodule_path(sm), loc);
238 return 0;
239 }
240
241 void dump_submodules(git_repository *repo)
242 {
243 git_submodule_foreach(repo, print_submodules, NULL);
244 }
245