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