Line | Count | Source |
1 | | #define USE_THE_REPOSITORY_VARIABLE |
2 | | #define DISABLE_SIGN_COMPARE_WARNINGS |
3 | | |
4 | | #include "git-compat-util.h" |
5 | | #include "abspath.h" |
6 | | #include "repository.h" |
7 | | #include "config.h" |
8 | | #include "submodule-config.h" |
9 | | #include "submodule.h" |
10 | | #include "dir.h" |
11 | | #include "diff.h" |
12 | | #include "commit.h" |
13 | | #include "environment.h" |
14 | | #include "gettext.h" |
15 | | #include "hex.h" |
16 | | #include "revision.h" |
17 | | #include "run-command.h" |
18 | | #include "diffcore.h" |
19 | | #include "refs.h" |
20 | | #include "string-list.h" |
21 | | #include "oid-array.h" |
22 | | #include "strvec.h" |
23 | | #include "thread-utils.h" |
24 | | #include "path.h" |
25 | | #include "remote.h" |
26 | | #include "worktree.h" |
27 | | #include "parse-options.h" |
28 | | #include "object-file.h" |
29 | | #include "object-name.h" |
30 | | #include "odb.h" |
31 | | #include "commit-reach.h" |
32 | | #include "read-cache-ll.h" |
33 | | #include "setup.h" |
34 | | #include "advice.h" |
35 | | #include "url.h" |
36 | | |
37 | | static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF; |
38 | | static int initialized_fetch_ref_tips; |
39 | | static struct oid_array ref_tips_before_fetch; |
40 | | static struct oid_array ref_tips_after_fetch; |
41 | | |
42 | | /* |
43 | | * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file |
44 | | * will be disabled because we can't guess what might be configured in |
45 | | * .gitmodules unless the user resolves the conflict. |
46 | | */ |
47 | | int is_gitmodules_unmerged(struct index_state *istate) |
48 | 0 | { |
49 | 0 | int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE)); |
50 | 0 | if (pos < 0) { /* .gitmodules not found or isn't merged */ |
51 | 0 | pos = -1 - pos; |
52 | 0 | if (istate->cache_nr > pos) { /* there is a .gitmodules */ |
53 | 0 | const struct cache_entry *ce = istate->cache[pos]; |
54 | 0 | if (ce_namelen(ce) == strlen(GITMODULES_FILE) && |
55 | 0 | !strcmp(ce->name, GITMODULES_FILE)) |
56 | 0 | return 1; |
57 | 0 | } |
58 | 0 | } |
59 | | |
60 | 0 | return 0; |
61 | 0 | } |
62 | | |
63 | | /* |
64 | | * Check if the .gitmodules file is safe to write. |
65 | | * |
66 | | * Writing to the .gitmodules file requires that the file exists in the |
67 | | * working tree or, if it doesn't, that a brand new .gitmodules file is going |
68 | | * to be created (i.e. it's neither in the index nor in the current branch). |
69 | | * |
70 | | * It is not safe to write to .gitmodules if it's not in the working tree but |
71 | | * it is in the index or in the current branch, because writing new values |
72 | | * (and staging them) would blindly overwrite ALL the old content. |
73 | | */ |
74 | | int is_writing_gitmodules_ok(void) |
75 | 0 | { |
76 | 0 | struct object_id oid; |
77 | 0 | return file_exists(GITMODULES_FILE) || |
78 | 0 | (repo_get_oid(the_repository, GITMODULES_INDEX, &oid) < 0 && repo_get_oid(the_repository, GITMODULES_HEAD, &oid) < 0); |
79 | 0 | } |
80 | | |
81 | | /* |
82 | | * Check if the .gitmodules file has unstaged modifications. This must be |
83 | | * checked before allowing modifications to the .gitmodules file with the |
84 | | * intention to stage them later, because when continuing we would stage the |
85 | | * modifications the user didn't stage herself too. That might change in a |
86 | | * future version when we learn to stage the changes we do ourselves without |
87 | | * staging any previous modifications. |
88 | | */ |
89 | | int is_staging_gitmodules_ok(struct index_state *istate) |
90 | 0 | { |
91 | 0 | int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE)); |
92 | |
|
93 | 0 | if ((pos >= 0) && (pos < istate->cache_nr)) { |
94 | 0 | struct stat st; |
95 | 0 | if (lstat(GITMODULES_FILE, &st) == 0 && |
96 | 0 | ie_modified(istate, istate->cache[pos], &st, 0) & DATA_CHANGED) |
97 | 0 | return 0; |
98 | 0 | } |
99 | | |
100 | 0 | return 1; |
101 | 0 | } |
102 | | |
103 | | static int for_each_remote_ref_submodule(const char *submodule, |
104 | | each_ref_fn fn, void *cb_data) |
105 | 0 | { |
106 | 0 | return refs_for_each_remote_ref(repo_get_submodule_ref_store(the_repository, |
107 | 0 | submodule), |
108 | 0 | fn, cb_data); |
109 | 0 | } |
110 | | |
111 | | /* |
112 | | * Try to update the "path" entry in the "submodule.<name>" section of the |
113 | | * .gitmodules file. Return 0 only if a .gitmodules file was found, a section |
114 | | * with the correct path=<oldpath> setting was found and we could update it. |
115 | | */ |
116 | | int update_path_in_gitmodules(const char *oldpath, const char *newpath) |
117 | 0 | { |
118 | 0 | struct strbuf entry = STRBUF_INIT; |
119 | 0 | const struct submodule *submodule; |
120 | 0 | int ret; |
121 | |
|
122 | 0 | if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */ |
123 | 0 | return -1; |
124 | | |
125 | 0 | if (is_gitmodules_unmerged(the_repository->index)) |
126 | 0 | die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first")); |
127 | | |
128 | 0 | submodule = submodule_from_path(the_repository, null_oid(the_hash_algo), oldpath); |
129 | 0 | if (!submodule || !submodule->name) { |
130 | 0 | warning(_("Could not find section in .gitmodules where path=%s"), oldpath); |
131 | 0 | return -1; |
132 | 0 | } |
133 | 0 | strbuf_addstr(&entry, "submodule."); |
134 | 0 | strbuf_addstr(&entry, submodule->name); |
135 | 0 | strbuf_addstr(&entry, ".path"); |
136 | 0 | ret = config_set_in_gitmodules_file_gently(entry.buf, newpath); |
137 | 0 | strbuf_release(&entry); |
138 | 0 | return ret; |
139 | 0 | } |
140 | | |
141 | | /* |
142 | | * Try to remove the "submodule.<name>" section from .gitmodules where the given |
143 | | * path is configured. Return 0 only if a .gitmodules file was found, a section |
144 | | * with the correct path=<path> setting was found and we could remove it. |
145 | | */ |
146 | | int remove_path_from_gitmodules(const char *path) |
147 | 0 | { |
148 | 0 | struct strbuf sect = STRBUF_INIT; |
149 | 0 | const struct submodule *submodule; |
150 | |
|
151 | 0 | if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */ |
152 | 0 | return -1; |
153 | | |
154 | 0 | if (is_gitmodules_unmerged(the_repository->index)) |
155 | 0 | die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first")); |
156 | | |
157 | 0 | submodule = submodule_from_path(the_repository, null_oid(the_hash_algo), path); |
158 | 0 | if (!submodule || !submodule->name) { |
159 | 0 | warning(_("Could not find section in .gitmodules where path=%s"), path); |
160 | 0 | return -1; |
161 | 0 | } |
162 | 0 | strbuf_addstr(§, "submodule."); |
163 | 0 | strbuf_addstr(§, submodule->name); |
164 | 0 | if (repo_config_rename_section_in_file(the_repository, GITMODULES_FILE, sect.buf, NULL) < 0) { |
165 | | /* Maybe the user already did that, don't error out here */ |
166 | 0 | warning(_("Could not remove .gitmodules entry for %s"), path); |
167 | 0 | strbuf_release(§); |
168 | 0 | return -1; |
169 | 0 | } |
170 | 0 | strbuf_release(§); |
171 | 0 | return 0; |
172 | 0 | } |
173 | | |
174 | | void stage_updated_gitmodules(struct index_state *istate) |
175 | 0 | { |
176 | 0 | if (add_file_to_index(istate, GITMODULES_FILE, 0)) |
177 | 0 | die(_("staging updated .gitmodules failed")); |
178 | 0 | } |
179 | | |
180 | | void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt, |
181 | | const char *path) |
182 | 0 | { |
183 | 0 | const struct submodule *submodule = submodule_from_path(the_repository, |
184 | 0 | null_oid(the_hash_algo), |
185 | 0 | path); |
186 | 0 | if (submodule) { |
187 | 0 | const char *ignore; |
188 | 0 | char *key; |
189 | |
|
190 | 0 | key = xstrfmt("submodule.%s.ignore", submodule->name); |
191 | 0 | if (repo_config_get_string_tmp(the_repository, key, &ignore)) |
192 | 0 | ignore = submodule->ignore; |
193 | 0 | free(key); |
194 | |
|
195 | 0 | if (ignore) |
196 | 0 | handle_ignore_submodules_arg(diffopt, ignore); |
197 | 0 | else if (is_gitmodules_unmerged(the_repository->index)) |
198 | 0 | diffopt->flags.ignore_submodules = 1; |
199 | 0 | } |
200 | 0 | } |
201 | | |
202 | | /* Cheap function that only determines if we're interested in submodules at all */ |
203 | | int git_default_submodule_config(const char *var, const char *value, |
204 | | void *cb UNUSED) |
205 | 0 | { |
206 | 0 | if (!strcmp(var, "submodule.recurse")) { |
207 | 0 | int v = git_config_bool(var, value) ? |
208 | 0 | RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF; |
209 | 0 | config_update_recurse_submodules = v; |
210 | 0 | } |
211 | 0 | return 0; |
212 | 0 | } |
213 | | |
214 | | int option_parse_recurse_submodules_worktree_updater(const struct option *opt, |
215 | | const char *arg, int unset) |
216 | 0 | { |
217 | 0 | if (unset) { |
218 | 0 | config_update_recurse_submodules = RECURSE_SUBMODULES_OFF; |
219 | 0 | return 0; |
220 | 0 | } |
221 | 0 | if (arg) |
222 | 0 | config_update_recurse_submodules = |
223 | 0 | parse_update_recurse_submodules_arg(opt->long_name, |
224 | 0 | arg); |
225 | 0 | else |
226 | 0 | config_update_recurse_submodules = RECURSE_SUBMODULES_ON; |
227 | |
|
228 | 0 | return 0; |
229 | 0 | } |
230 | | |
231 | | /* |
232 | | * Determine if a submodule has been initialized at a given 'path' |
233 | | */ |
234 | | /* |
235 | | * NEEDSWORK: Emit a warning if submodule.active exists, but is valueless, |
236 | | * ie, the config looks like: "[submodule] active\n". |
237 | | * Since that is an invalid pathspec, we should inform the user. |
238 | | */ |
239 | | int is_tree_submodule_active(struct repository *repo, |
240 | | const struct object_id *treeish_name, |
241 | | const char *path) |
242 | 0 | { |
243 | 0 | int ret = 0; |
244 | 0 | char *key = NULL; |
245 | 0 | char *value = NULL; |
246 | 0 | const struct string_list *sl; |
247 | 0 | const struct submodule *module; |
248 | |
|
249 | 0 | module = submodule_from_path(repo, treeish_name, path); |
250 | | |
251 | | /* early return if there isn't a path->module mapping */ |
252 | 0 | if (!module) |
253 | 0 | return 0; |
254 | | |
255 | | /* submodule.<name>.active is set */ |
256 | 0 | key = xstrfmt("submodule.%s.active", module->name); |
257 | 0 | if (!repo_config_get_bool(repo, key, &ret)) { |
258 | 0 | free(key); |
259 | 0 | return ret; |
260 | 0 | } |
261 | 0 | free(key); |
262 | | |
263 | | /* submodule.active is set */ |
264 | 0 | if (!repo_config_get_string_multi(repo, "submodule.active", &sl)) { |
265 | 0 | struct pathspec ps; |
266 | 0 | struct strvec args = STRVEC_INIT; |
267 | 0 | const struct string_list_item *item; |
268 | |
|
269 | 0 | for_each_string_list_item(item, sl) { |
270 | 0 | strvec_push(&args, item->string); |
271 | 0 | } |
272 | |
|
273 | 0 | parse_pathspec(&ps, 0, 0, NULL, args.v); |
274 | 0 | ret = match_pathspec(repo->index, &ps, path, strlen(path), 0, NULL, 1); |
275 | |
|
276 | 0 | strvec_clear(&args); |
277 | 0 | clear_pathspec(&ps); |
278 | 0 | return ret; |
279 | 0 | } |
280 | | |
281 | | /* fallback to checking if the URL is set */ |
282 | 0 | key = xstrfmt("submodule.%s.url", module->name); |
283 | 0 | ret = !repo_config_get_string(repo, key, &value); |
284 | |
|
285 | 0 | free(value); |
286 | 0 | free(key); |
287 | 0 | return ret; |
288 | 0 | } |
289 | | |
290 | | int is_submodule_active(struct repository *repo, const char *path) |
291 | 0 | { |
292 | 0 | return is_tree_submodule_active(repo, null_oid(the_hash_algo), path); |
293 | 0 | } |
294 | | |
295 | | int is_submodule_populated_gently(const char *path, int *return_error_code) |
296 | 0 | { |
297 | 0 | int ret = 0; |
298 | 0 | char *gitdir = xstrfmt("%s/.git", path); |
299 | |
|
300 | 0 | if (resolve_gitdir_gently(gitdir, return_error_code)) |
301 | 0 | ret = 1; |
302 | |
|
303 | 0 | free(gitdir); |
304 | 0 | return ret; |
305 | 0 | } |
306 | | |
307 | | /* |
308 | | * Dies if the provided 'prefix' corresponds to an unpopulated submodule |
309 | | */ |
310 | | void die_in_unpopulated_submodule(struct index_state *istate, |
311 | | const char *prefix) |
312 | 0 | { |
313 | 0 | int i, prefixlen; |
314 | |
|
315 | 0 | if (!prefix) |
316 | 0 | return; |
317 | | |
318 | 0 | prefixlen = strlen(prefix); |
319 | |
|
320 | 0 | for (i = 0; i < istate->cache_nr; i++) { |
321 | 0 | struct cache_entry *ce = istate->cache[i]; |
322 | 0 | int ce_len = ce_namelen(ce); |
323 | |
|
324 | 0 | if (!S_ISGITLINK(ce->ce_mode)) |
325 | 0 | continue; |
326 | 0 | if (prefixlen <= ce_len) |
327 | 0 | continue; |
328 | 0 | if (strncmp(ce->name, prefix, ce_len)) |
329 | 0 | continue; |
330 | 0 | if (prefix[ce_len] != '/') |
331 | 0 | continue; |
332 | | |
333 | 0 | die(_("in unpopulated submodule '%s'"), ce->name); |
334 | 0 | } |
335 | 0 | } |
336 | | |
337 | | /* |
338 | | * Dies if any paths in the provided pathspec descends into a submodule |
339 | | */ |
340 | | void die_path_inside_submodule(struct index_state *istate, |
341 | | const struct pathspec *ps) |
342 | 0 | { |
343 | 0 | int i, j; |
344 | |
|
345 | 0 | for (i = 0; i < istate->cache_nr; i++) { |
346 | 0 | struct cache_entry *ce = istate->cache[i]; |
347 | 0 | int ce_len = ce_namelen(ce); |
348 | |
|
349 | 0 | if (!S_ISGITLINK(ce->ce_mode)) |
350 | 0 | continue; |
351 | | |
352 | 0 | for (j = 0; j < ps->nr ; j++) { |
353 | 0 | const struct pathspec_item *item = &ps->items[j]; |
354 | |
|
355 | 0 | if (item->len <= ce_len) |
356 | 0 | continue; |
357 | 0 | if (item->match[ce_len] != '/') |
358 | 0 | continue; |
359 | 0 | if (strncmp(ce->name, item->match, ce_len)) |
360 | 0 | continue; |
361 | 0 | if (item->len == ce_len + 1) |
362 | 0 | continue; |
363 | | |
364 | 0 | die(_("Pathspec '%s' is in submodule '%.*s'"), |
365 | 0 | item->original, ce_len, ce->name); |
366 | 0 | } |
367 | 0 | } |
368 | 0 | } |
369 | | |
370 | | enum submodule_update_type parse_submodule_update_type(const char *value) |
371 | 0 | { |
372 | 0 | if (!strcmp(value, "none")) |
373 | 0 | return SM_UPDATE_NONE; |
374 | 0 | else if (!strcmp(value, "checkout")) |
375 | 0 | return SM_UPDATE_CHECKOUT; |
376 | 0 | else if (!strcmp(value, "rebase")) |
377 | 0 | return SM_UPDATE_REBASE; |
378 | 0 | else if (!strcmp(value, "merge")) |
379 | 0 | return SM_UPDATE_MERGE; |
380 | 0 | else if (*value == '!') |
381 | 0 | return SM_UPDATE_COMMAND; |
382 | 0 | else |
383 | 0 | return SM_UPDATE_UNSPECIFIED; |
384 | 0 | } |
385 | | |
386 | | int parse_submodule_update_strategy(const char *value, |
387 | | struct submodule_update_strategy *dst) |
388 | 0 | { |
389 | 0 | enum submodule_update_type type; |
390 | |
|
391 | 0 | free((void*)dst->command); |
392 | 0 | dst->command = NULL; |
393 | |
|
394 | 0 | type = parse_submodule_update_type(value); |
395 | 0 | if (type == SM_UPDATE_UNSPECIFIED) |
396 | 0 | return -1; |
397 | | |
398 | 0 | dst->type = type; |
399 | 0 | if (type == SM_UPDATE_COMMAND) |
400 | 0 | dst->command = xstrdup(value + 1); |
401 | |
|
402 | 0 | return 0; |
403 | 0 | } |
404 | | |
405 | | void submodule_update_strategy_release(struct submodule_update_strategy *strategy) |
406 | 0 | { |
407 | 0 | free((char *) strategy->command); |
408 | 0 | } |
409 | | |
410 | | const char *submodule_update_type_to_string(enum submodule_update_type type) |
411 | 0 | { |
412 | 0 | switch (type) { |
413 | 0 | case SM_UPDATE_CHECKOUT: |
414 | 0 | return "checkout"; |
415 | 0 | case SM_UPDATE_MERGE: |
416 | 0 | return "merge"; |
417 | 0 | case SM_UPDATE_REBASE: |
418 | 0 | return "rebase"; |
419 | 0 | case SM_UPDATE_NONE: |
420 | 0 | return "none"; |
421 | 0 | case SM_UPDATE_UNSPECIFIED: |
422 | 0 | case SM_UPDATE_COMMAND: |
423 | 0 | BUG("init_submodule() should handle type %d", type); |
424 | 0 | default: |
425 | 0 | BUG("unexpected update strategy type: %d", type); |
426 | 0 | } |
427 | 0 | } |
428 | | |
429 | | void handle_ignore_submodules_arg(struct diff_options *diffopt, |
430 | | const char *arg) |
431 | 0 | { |
432 | 0 | diffopt->flags.ignore_submodule_set = 1; |
433 | 0 | diffopt->flags.ignore_submodules = 0; |
434 | 0 | diffopt->flags.ignore_untracked_in_submodules = 0; |
435 | 0 | diffopt->flags.ignore_dirty_submodules = 0; |
436 | |
|
437 | 0 | if (!strcmp(arg, "all")) |
438 | 0 | diffopt->flags.ignore_submodules = 1; |
439 | 0 | else if (!strcmp(arg, "untracked")) |
440 | 0 | diffopt->flags.ignore_untracked_in_submodules = 1; |
441 | 0 | else if (!strcmp(arg, "dirty")) |
442 | 0 | diffopt->flags.ignore_dirty_submodules = 1; |
443 | 0 | else if (strcmp(arg, "none")) |
444 | 0 | die(_("bad --ignore-submodules argument: %s"), arg); |
445 | | /* |
446 | | * Please update _git_status() in git-completion.bash when you |
447 | | * add new options |
448 | | */ |
449 | 0 | } |
450 | | |
451 | | static int prepare_submodule_diff_summary(struct repository *r, struct rev_info *rev, |
452 | | const char *path, |
453 | | struct commit *left, struct commit *right, |
454 | | struct commit_list *merge_bases) |
455 | 0 | { |
456 | 0 | struct commit_list *list; |
457 | |
|
458 | 0 | repo_init_revisions(r, rev, NULL); |
459 | 0 | setup_revisions(0, NULL, rev, NULL); |
460 | 0 | rev->left_right = 1; |
461 | 0 | rev->first_parent_only = 1; |
462 | 0 | left->object.flags |= SYMMETRIC_LEFT; |
463 | 0 | add_pending_object(rev, &left->object, path); |
464 | 0 | add_pending_object(rev, &right->object, path); |
465 | 0 | for (list = merge_bases; list; list = list->next) { |
466 | 0 | list->item->object.flags |= UNINTERESTING; |
467 | 0 | add_pending_object(rev, &list->item->object, |
468 | 0 | oid_to_hex(&list->item->object.oid)); |
469 | 0 | } |
470 | 0 | return prepare_revision_walk(rev); |
471 | 0 | } |
472 | | |
473 | | static void print_submodule_diff_summary(struct repository *r, struct rev_info *rev, struct diff_options *o) |
474 | 0 | { |
475 | 0 | static const char format[] = " %m %s"; |
476 | 0 | struct strbuf sb = STRBUF_INIT; |
477 | 0 | struct commit *commit; |
478 | |
|
479 | 0 | while ((commit = get_revision(rev))) { |
480 | 0 | struct pretty_print_context ctx = {0}; |
481 | 0 | ctx.date_mode = rev->date_mode; |
482 | 0 | ctx.output_encoding = get_log_output_encoding(); |
483 | 0 | strbuf_setlen(&sb, 0); |
484 | 0 | repo_format_commit_message(r, commit, format, &sb, |
485 | 0 | &ctx); |
486 | 0 | strbuf_addch(&sb, '\n'); |
487 | 0 | if (commit->object.flags & SYMMETRIC_LEFT) |
488 | 0 | diff_emit_submodule_del(o, sb.buf); |
489 | 0 | else |
490 | 0 | diff_emit_submodule_add(o, sb.buf); |
491 | 0 | } |
492 | 0 | strbuf_release(&sb); |
493 | 0 | } |
494 | | |
495 | | void prepare_submodule_repo_env(struct strvec *out) |
496 | 0 | { |
497 | 0 | prepare_other_repo_env(out, DEFAULT_GIT_DIR_ENVIRONMENT); |
498 | 0 | } |
499 | | |
500 | | static void prepare_submodule_repo_env_in_gitdir(struct strvec *out) |
501 | 0 | { |
502 | 0 | prepare_other_repo_env(out, "."); |
503 | 0 | } |
504 | | |
505 | | /* |
506 | | * Initialize a repository struct for a submodule based on the provided 'path'. |
507 | | * |
508 | | * Returns the repository struct on success, |
509 | | * NULL when the submodule is not present. |
510 | | */ |
511 | | static struct repository *open_submodule(const char *path) |
512 | 0 | { |
513 | 0 | struct strbuf sb = STRBUF_INIT; |
514 | 0 | struct repository *out = xmalloc(sizeof(*out)); |
515 | |
|
516 | 0 | if (submodule_to_gitdir(the_repository, &sb, path) || |
517 | 0 | repo_init(out, sb.buf, NULL)) { |
518 | 0 | strbuf_release(&sb); |
519 | 0 | free(out); |
520 | 0 | return NULL; |
521 | 0 | } |
522 | | |
523 | | /* Mark it as a submodule */ |
524 | 0 | out->submodule_prefix = xstrdup(path); |
525 | |
|
526 | 0 | strbuf_release(&sb); |
527 | 0 | return out; |
528 | 0 | } |
529 | | |
530 | | /* |
531 | | * Helper function to display the submodule header line prior to the full |
532 | | * summary output. |
533 | | * |
534 | | * If it can locate the submodule git directory it will create a repository |
535 | | * handle for the submodule and lookup both the left and right commits and |
536 | | * put them into the left and right pointers. |
537 | | */ |
538 | | static void show_submodule_header(struct diff_options *o, |
539 | | const char *path, |
540 | | struct object_id *one, struct object_id *two, |
541 | | unsigned dirty_submodule, |
542 | | struct repository *sub, |
543 | | struct commit **left, struct commit **right, |
544 | | struct commit_list **merge_bases) |
545 | 0 | { |
546 | 0 | const char *message = NULL; |
547 | 0 | struct strbuf sb = STRBUF_INIT; |
548 | 0 | int fast_forward = 0, fast_backward = 0; |
549 | |
|
550 | 0 | if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) |
551 | 0 | diff_emit_submodule_untracked(o, path); |
552 | |
|
553 | 0 | if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED) |
554 | 0 | diff_emit_submodule_modified(o, path); |
555 | |
|
556 | 0 | if (is_null_oid(one)) |
557 | 0 | message = "(new submodule)"; |
558 | 0 | else if (is_null_oid(two)) |
559 | 0 | message = "(submodule deleted)"; |
560 | |
|
561 | 0 | if (!sub) { |
562 | 0 | if (!message) |
563 | 0 | message = "(commits not present)"; |
564 | 0 | goto output_header; |
565 | 0 | } |
566 | | |
567 | | /* |
568 | | * Attempt to lookup the commit references, and determine if this is |
569 | | * a fast forward or fast backwards update. |
570 | | */ |
571 | 0 | *left = lookup_commit_reference(sub, one); |
572 | 0 | *right = lookup_commit_reference(sub, two); |
573 | | |
574 | | /* |
575 | | * Warn about missing commits in the submodule project, but only if |
576 | | * they aren't null. |
577 | | */ |
578 | 0 | if ((!is_null_oid(one) && !*left) || |
579 | 0 | (!is_null_oid(two) && !*right)) |
580 | 0 | message = "(commits not present)"; |
581 | |
|
582 | 0 | *merge_bases = NULL; |
583 | 0 | if (repo_get_merge_bases(sub, *left, *right, merge_bases) < 0) { |
584 | 0 | message = "(corrupt repository)"; |
585 | 0 | goto output_header; |
586 | 0 | } |
587 | | |
588 | 0 | if (*merge_bases) { |
589 | 0 | if ((*merge_bases)->item == *left) |
590 | 0 | fast_forward = 1; |
591 | 0 | else if ((*merge_bases)->item == *right) |
592 | 0 | fast_backward = 1; |
593 | 0 | } |
594 | |
|
595 | 0 | if (oideq(one, two)) { |
596 | 0 | strbuf_release(&sb); |
597 | 0 | return; |
598 | 0 | } |
599 | | |
600 | 0 | output_header: |
601 | 0 | strbuf_addf(&sb, "Submodule %s ", path); |
602 | 0 | strbuf_add_unique_abbrev(&sb, one, DEFAULT_ABBREV); |
603 | 0 | strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "..."); |
604 | 0 | strbuf_add_unique_abbrev(&sb, two, DEFAULT_ABBREV); |
605 | 0 | if (message) |
606 | 0 | strbuf_addf(&sb, " %s\n", message); |
607 | 0 | else |
608 | 0 | strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : ""); |
609 | 0 | diff_emit_submodule_header(o, sb.buf); |
610 | |
|
611 | 0 | strbuf_release(&sb); |
612 | 0 | } |
613 | | |
614 | | void show_submodule_diff_summary(struct diff_options *o, const char *path, |
615 | | struct object_id *one, struct object_id *two, |
616 | | unsigned dirty_submodule) |
617 | 0 | { |
618 | 0 | struct rev_info rev = REV_INFO_INIT; |
619 | 0 | struct commit *left = NULL, *right = NULL; |
620 | 0 | struct commit_list *merge_bases = NULL; |
621 | 0 | struct repository *sub; |
622 | |
|
623 | 0 | sub = open_submodule(path); |
624 | 0 | show_submodule_header(o, path, one, two, dirty_submodule, |
625 | 0 | sub, &left, &right, &merge_bases); |
626 | | |
627 | | /* |
628 | | * If we don't have both a left and a right pointer, there is no |
629 | | * reason to try and display a summary. The header line should contain |
630 | | * all the information the user needs. |
631 | | */ |
632 | 0 | if (!left || !right || !sub) |
633 | 0 | goto out; |
634 | | |
635 | | /* Treat revision walker failure the same as missing commits */ |
636 | 0 | if (prepare_submodule_diff_summary(sub, &rev, path, left, right, merge_bases)) { |
637 | 0 | diff_emit_submodule_error(o, "(revision walker failed)\n"); |
638 | 0 | goto out; |
639 | 0 | } |
640 | | |
641 | 0 | print_submodule_diff_summary(sub, &rev, o); |
642 | |
|
643 | 0 | out: |
644 | 0 | commit_list_free(merge_bases); |
645 | 0 | release_revisions(&rev); |
646 | 0 | clear_commit_marks(left, ~0); |
647 | 0 | clear_commit_marks(right, ~0); |
648 | 0 | if (sub) { |
649 | 0 | repo_clear(sub); |
650 | 0 | free(sub); |
651 | 0 | } |
652 | 0 | } |
653 | | |
654 | | void show_submodule_inline_diff(struct diff_options *o, const char *path, |
655 | | struct object_id *one, struct object_id *two, |
656 | | unsigned dirty_submodule) |
657 | 0 | { |
658 | 0 | const struct object_id *old_oid = the_hash_algo->empty_tree, *new_oid = the_hash_algo->empty_tree; |
659 | 0 | struct commit *left = NULL, *right = NULL; |
660 | 0 | struct commit_list *merge_bases = NULL; |
661 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
662 | 0 | struct strbuf sb = STRBUF_INIT; |
663 | 0 | struct repository *sub; |
664 | |
|
665 | 0 | sub = open_submodule(path); |
666 | 0 | show_submodule_header(o, path, one, two, dirty_submodule, |
667 | 0 | sub, &left, &right, &merge_bases); |
668 | | |
669 | | /* We need a valid left and right commit to display a difference */ |
670 | 0 | if (!(left || is_null_oid(one)) || |
671 | 0 | !(right || is_null_oid(two))) |
672 | 0 | goto done; |
673 | | |
674 | 0 | if (left) |
675 | 0 | old_oid = one; |
676 | 0 | if (right) |
677 | 0 | new_oid = two; |
678 | |
|
679 | 0 | cp.git_cmd = 1; |
680 | 0 | cp.dir = path; |
681 | 0 | cp.out = -1; |
682 | 0 | cp.no_stdin = 1; |
683 | | |
684 | | /* TODO: other options may need to be passed here. */ |
685 | 0 | strvec_pushl(&cp.args, "diff", "--submodule=diff", NULL); |
686 | 0 | strvec_pushf(&cp.args, "--color=%s", want_color(o->use_color) ? |
687 | 0 | "always" : "never"); |
688 | |
|
689 | 0 | if (o->flags.reverse_diff) { |
690 | 0 | strvec_pushf(&cp.args, "--src-prefix=%s%s/", |
691 | 0 | o->b_prefix, path); |
692 | 0 | strvec_pushf(&cp.args, "--dst-prefix=%s%s/", |
693 | 0 | o->a_prefix, path); |
694 | 0 | } else { |
695 | 0 | strvec_pushf(&cp.args, "--src-prefix=%s%s/", |
696 | 0 | o->a_prefix, path); |
697 | 0 | strvec_pushf(&cp.args, "--dst-prefix=%s%s/", |
698 | 0 | o->b_prefix, path); |
699 | 0 | } |
700 | 0 | strvec_push(&cp.args, oid_to_hex(old_oid)); |
701 | | /* |
702 | | * If the submodule has modified content, we will diff against the |
703 | | * work tree, under the assumption that the user has asked for the |
704 | | * diff format and wishes to actually see all differences even if they |
705 | | * haven't yet been committed to the submodule yet. |
706 | | */ |
707 | 0 | if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED)) |
708 | 0 | strvec_push(&cp.args, oid_to_hex(new_oid)); |
709 | |
|
710 | 0 | prepare_submodule_repo_env(&cp.env); |
711 | |
|
712 | 0 | if (!is_directory(path)) { |
713 | | /* fall back to absorbed git dir, if any */ |
714 | 0 | if (!sub) |
715 | 0 | goto done; |
716 | 0 | cp.dir = sub->gitdir; |
717 | 0 | strvec_push(&cp.env, GIT_DIR_ENVIRONMENT "=."); |
718 | 0 | strvec_push(&cp.env, GIT_WORK_TREE_ENVIRONMENT "=."); |
719 | 0 | } |
720 | | |
721 | 0 | if (start_command(&cp)) { |
722 | 0 | diff_emit_submodule_error(o, "(diff failed)\n"); |
723 | 0 | goto done; |
724 | 0 | } |
725 | | |
726 | 0 | while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF) |
727 | 0 | diff_emit_submodule_pipethrough(o, sb.buf, sb.len); |
728 | |
|
729 | 0 | if (finish_command(&cp)) |
730 | 0 | diff_emit_submodule_error(o, "(diff failed)\n"); |
731 | |
|
732 | 0 | done: |
733 | 0 | strbuf_release(&sb); |
734 | 0 | commit_list_free(merge_bases); |
735 | 0 | if (left) |
736 | 0 | clear_commit_marks(left, ~0); |
737 | 0 | if (right) |
738 | 0 | clear_commit_marks(right, ~0); |
739 | 0 | if (sub) { |
740 | 0 | repo_clear(sub); |
741 | 0 | free(sub); |
742 | 0 | } |
743 | 0 | } |
744 | | |
745 | | int should_update_submodules(void) |
746 | 0 | { |
747 | 0 | return config_update_recurse_submodules == RECURSE_SUBMODULES_ON; |
748 | 0 | } |
749 | | |
750 | | const struct submodule *submodule_from_ce(const struct cache_entry *ce) |
751 | 0 | { |
752 | 0 | if (!S_ISGITLINK(ce->ce_mode)) |
753 | 0 | return NULL; |
754 | | |
755 | 0 | if (!should_update_submodules()) |
756 | 0 | return NULL; |
757 | | |
758 | 0 | return submodule_from_path(the_repository, null_oid(the_hash_algo), ce->name); |
759 | 0 | } |
760 | | |
761 | | |
762 | | struct collect_changed_submodules_cb_data { |
763 | | struct repository *repo; |
764 | | struct string_list *changed; |
765 | | const struct object_id *commit_oid; |
766 | | }; |
767 | | |
768 | | /* |
769 | | * this would normally be two functions: default_name_from_path() and |
770 | | * path_from_default_name(). Since the default name is the same as |
771 | | * the submodule path we can get away with just one function which only |
772 | | * checks whether there is a submodule in the working directory at that |
773 | | * location. |
774 | | */ |
775 | | static const char *default_name_or_path(const char *path_or_name) |
776 | 0 | { |
777 | 0 | int error_code; |
778 | |
|
779 | 0 | if (!is_submodule_populated_gently(path_or_name, &error_code)) |
780 | 0 | return NULL; |
781 | | |
782 | 0 | return path_or_name; |
783 | 0 | } |
784 | | |
785 | | /* |
786 | | * Holds relevant information for a changed submodule. Used as the .util |
787 | | * member of the changed submodule name string_list_item. |
788 | | * |
789 | | * (super_oid, path) allows the submodule config to be read from _some_ |
790 | | * .gitmodules file. We store this information the first time we find a |
791 | | * superproject commit that points to the submodule, but this is |
792 | | * arbitrary - we can choose any (super_oid, path) that matches the |
793 | | * submodule's name. |
794 | | * |
795 | | * NEEDSWORK: Storing an arbitrary commit is undesirable because we can't |
796 | | * guarantee that we're reading the commit that the user would expect. A better |
797 | | * scheme would be to just fetch a submodule by its name. This requires two |
798 | | * steps: |
799 | | * - Create a function that behaves like repo_submodule_init(), but accepts a |
800 | | * submodule name instead of treeish_name and path. This should be easy |
801 | | * because repo_submodule_init() internally uses the submodule's name. |
802 | | * |
803 | | * - Replace most instances of 'struct submodule' (which is the .gitmodules |
804 | | * config) with just the submodule name. This is OK because we expect |
805 | | * submodule settings to be stored in .git/config (via "git submodule init"), |
806 | | * not .gitmodules. This also lets us delete get_non_gitmodules_submodule(), |
807 | | * which constructs a bogus 'struct submodule' for the sake of giving a |
808 | | * placeholder name to a gitlink. |
809 | | */ |
810 | | struct changed_submodule_data { |
811 | | /* |
812 | | * The first superproject commit in the rev walk that points to |
813 | | * the submodule. |
814 | | */ |
815 | | const struct object_id *super_oid; |
816 | | /* |
817 | | * Path to the submodule in the superproject commit referenced |
818 | | * by 'super_oid'. |
819 | | */ |
820 | | char *path; |
821 | | /* The submodule commits that have changed in the rev walk. */ |
822 | | struct oid_array new_commits; |
823 | | }; |
824 | | |
825 | | static void changed_submodule_data_clear(struct changed_submodule_data *cs_data) |
826 | 0 | { |
827 | 0 | oid_array_clear(&cs_data->new_commits); |
828 | 0 | free(cs_data->path); |
829 | 0 | } |
830 | | |
831 | | static void collect_changed_submodules_cb(struct diff_queue_struct *q, |
832 | | struct diff_options *options UNUSED, |
833 | | void *data) |
834 | 0 | { |
835 | 0 | struct collect_changed_submodules_cb_data *me = data; |
836 | 0 | struct string_list *changed = me->changed; |
837 | 0 | const struct object_id *commit_oid = me->commit_oid; |
838 | 0 | int i; |
839 | |
|
840 | 0 | for (i = 0; i < q->nr; i++) { |
841 | 0 | struct diff_filepair *p = q->queue[i]; |
842 | 0 | const struct submodule *submodule; |
843 | 0 | const char *name; |
844 | 0 | struct string_list_item *item; |
845 | 0 | struct changed_submodule_data *cs_data; |
846 | |
|
847 | 0 | if (!S_ISGITLINK(p->two->mode)) |
848 | 0 | continue; |
849 | | |
850 | 0 | submodule = submodule_from_path(me->repo, |
851 | 0 | commit_oid, p->two->path); |
852 | 0 | if (submodule) |
853 | 0 | name = submodule->name; |
854 | 0 | else { |
855 | 0 | name = default_name_or_path(p->two->path); |
856 | | /* make sure name does not collide with existing one */ |
857 | 0 | if (name) |
858 | 0 | submodule = submodule_from_name(me->repo, |
859 | 0 | commit_oid, name); |
860 | 0 | if (submodule) { |
861 | 0 | warning(_("Submodule in commit %s at path: " |
862 | 0 | "'%s' collides with a submodule named " |
863 | 0 | "the same. Skipping it."), |
864 | 0 | oid_to_hex(commit_oid), p->two->path); |
865 | 0 | name = NULL; |
866 | 0 | } |
867 | 0 | } |
868 | |
|
869 | 0 | if (!name) |
870 | 0 | continue; |
871 | | |
872 | 0 | item = string_list_insert(changed, name); |
873 | 0 | if (item->util) |
874 | 0 | cs_data = item->util; |
875 | 0 | else { |
876 | 0 | item->util = xcalloc(1, sizeof(struct changed_submodule_data)); |
877 | 0 | cs_data = item->util; |
878 | 0 | cs_data->super_oid = commit_oid; |
879 | 0 | cs_data->path = xstrdup(p->two->path); |
880 | 0 | } |
881 | 0 | oid_array_append(&cs_data->new_commits, &p->two->oid); |
882 | 0 | } |
883 | 0 | } |
884 | | |
885 | | /* |
886 | | * Collect the paths of submodules in 'changed' which have changed based on |
887 | | * the revisions as specified in 'argv'. Each entry in 'changed' will also |
888 | | * have a corresponding 'struct oid_array' (in the 'util' field) which lists |
889 | | * what the submodule pointers were updated to during the change. |
890 | | */ |
891 | | static void collect_changed_submodules(struct repository *r, |
892 | | struct string_list *changed, |
893 | | struct strvec *argv) |
894 | 0 | { |
895 | 0 | struct rev_info rev; |
896 | 0 | const struct commit *commit; |
897 | 0 | int save_warning; |
898 | 0 | struct setup_revision_opt s_r_opt = { |
899 | 0 | .assume_dashdash = 1, |
900 | 0 | }; |
901 | |
|
902 | 0 | save_warning = warn_on_object_refname_ambiguity; |
903 | 0 | warn_on_object_refname_ambiguity = 0; |
904 | 0 | repo_init_revisions(r, &rev, NULL); |
905 | 0 | setup_revisions_from_strvec(argv, &rev, &s_r_opt); |
906 | 0 | warn_on_object_refname_ambiguity = save_warning; |
907 | 0 | if (prepare_revision_walk(&rev)) |
908 | 0 | die(_("revision walk setup failed")); |
909 | | |
910 | 0 | while ((commit = get_revision(&rev))) { |
911 | 0 | struct rev_info diff_rev; |
912 | 0 | struct collect_changed_submodules_cb_data data; |
913 | 0 | data.repo = r; |
914 | 0 | data.changed = changed; |
915 | 0 | data.commit_oid = &commit->object.oid; |
916 | |
|
917 | 0 | repo_init_revisions(r, &diff_rev, NULL); |
918 | 0 | diff_rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; |
919 | 0 | diff_rev.diffopt.format_callback = collect_changed_submodules_cb; |
920 | 0 | diff_rev.diffopt.format_callback_data = &data; |
921 | 0 | diff_rev.dense_combined_merges = 1; |
922 | 0 | diff_tree_combined_merge(commit, &diff_rev); |
923 | 0 | release_revisions(&diff_rev); |
924 | 0 | } |
925 | |
|
926 | 0 | reset_revision_walk(); |
927 | 0 | release_revisions(&rev); |
928 | 0 | } |
929 | | |
930 | | static void free_submodules_data(struct string_list *submodules) |
931 | 0 | { |
932 | 0 | struct string_list_item *item; |
933 | 0 | for_each_string_list_item(item, submodules) |
934 | 0 | changed_submodule_data_clear(item->util); |
935 | |
|
936 | 0 | string_list_clear(submodules, 1); |
937 | 0 | } |
938 | | |
939 | | static int has_remote(const struct reference *ref UNUSED, void *cb_data UNUSED) |
940 | 0 | { |
941 | 0 | return 1; |
942 | 0 | } |
943 | | |
944 | | static int append_oid_to_argv(const struct object_id *oid, void *data) |
945 | 0 | { |
946 | 0 | struct strvec *argv = data; |
947 | 0 | strvec_push(argv, oid_to_hex(oid)); |
948 | 0 | return 0; |
949 | 0 | } |
950 | | |
951 | | struct has_commit_data { |
952 | | struct repository *repo; |
953 | | int result; |
954 | | const char *path; |
955 | | const struct object_id *super_oid; |
956 | | }; |
957 | | |
958 | | static int check_has_commit(const struct object_id *oid, void *data) |
959 | 0 | { |
960 | 0 | struct has_commit_data *cb = data; |
961 | 0 | struct repository subrepo; |
962 | 0 | enum object_type type; |
963 | |
|
964 | 0 | if (repo_submodule_init(&subrepo, cb->repo, cb->path, cb->super_oid)) { |
965 | 0 | cb->result = 0; |
966 | | /* subrepo failed to init, so don't clean it up. */ |
967 | 0 | return 0; |
968 | 0 | } |
969 | | |
970 | 0 | type = odb_read_object_info(subrepo.objects, oid, NULL); |
971 | |
|
972 | 0 | switch (type) { |
973 | 0 | case OBJ_COMMIT: |
974 | 0 | goto cleanup; |
975 | 0 | case OBJ_BAD: |
976 | | /* |
977 | | * Object is missing or invalid. If invalid, an error message |
978 | | * has already been printed. |
979 | | */ |
980 | 0 | cb->result = 0; |
981 | 0 | goto cleanup; |
982 | 0 | default: |
983 | 0 | die(_("submodule entry '%s' (%s) is a %s, not a commit"), |
984 | 0 | cb->path, oid_to_hex(oid), type_name(type)); |
985 | 0 | } |
986 | 0 | cleanup: |
987 | 0 | repo_clear(&subrepo); |
988 | 0 | return 0; |
989 | 0 | } |
990 | | |
991 | | static int submodule_has_commits(struct repository *r, |
992 | | const char *path, |
993 | | const struct object_id *super_oid, |
994 | | struct oid_array *commits) |
995 | 0 | { |
996 | 0 | struct has_commit_data has_commit = { |
997 | 0 | .repo = r, |
998 | 0 | .result = 1, |
999 | 0 | .path = path, |
1000 | 0 | .super_oid = super_oid |
1001 | 0 | }; |
1002 | |
|
1003 | 0 | if (validate_submodule_path(path) < 0) |
1004 | 0 | exit(128); |
1005 | | |
1006 | 0 | oid_array_for_each_unique(commits, check_has_commit, &has_commit); |
1007 | |
|
1008 | 0 | if (has_commit.result) { |
1009 | | /* |
1010 | | * Even if the submodule is checked out and the commit is |
1011 | | * present, make sure it exists in the submodule's object store |
1012 | | * and that it is reachable from a ref. |
1013 | | */ |
1014 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
1015 | 0 | struct strbuf out = STRBUF_INIT; |
1016 | |
|
1017 | 0 | strvec_pushl(&cp.args, "rev-list", "-n", "1", NULL); |
1018 | 0 | oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args); |
1019 | 0 | strvec_pushl(&cp.args, "--not", "--all", NULL); |
1020 | |
|
1021 | 0 | prepare_submodule_repo_env(&cp.env); |
1022 | 0 | cp.git_cmd = 1; |
1023 | 0 | cp.no_stdin = 1; |
1024 | 0 | cp.dir = path; |
1025 | |
|
1026 | 0 | if (capture_command(&cp, &out, GIT_MAX_HEXSZ + 1) || out.len) |
1027 | 0 | has_commit.result = 0; |
1028 | |
|
1029 | 0 | strbuf_release(&out); |
1030 | 0 | } |
1031 | |
|
1032 | 0 | return has_commit.result; |
1033 | 0 | } |
1034 | | |
1035 | | static int submodule_needs_pushing(struct repository *r, |
1036 | | const char *path, |
1037 | | struct oid_array *commits) |
1038 | 0 | { |
1039 | 0 | if (!submodule_has_commits(r, path, null_oid(the_hash_algo), commits)) |
1040 | | /* |
1041 | | * NOTE: We do consider it safe to return "no" here. The |
1042 | | * correct answer would be "We do not know" instead of |
1043 | | * "No push needed", but it is quite hard to change |
1044 | | * the submodule pointer without having the submodule |
1045 | | * around. If a user did however change the submodules |
1046 | | * without having the submodule around, this indicates |
1047 | | * an expert who knows what they are doing or a |
1048 | | * maintainer integrating work from other people. In |
1049 | | * both cases it should be safe to skip this check. |
1050 | | */ |
1051 | 0 | return 0; |
1052 | | |
1053 | 0 | if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) { |
1054 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
1055 | 0 | struct strbuf buf = STRBUF_INIT; |
1056 | 0 | int needs_pushing = 0; |
1057 | |
|
1058 | 0 | strvec_push(&cp.args, "rev-list"); |
1059 | 0 | oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args); |
1060 | 0 | strvec_pushl(&cp.args, "--not", "--remotes", "-n", "1" , NULL); |
1061 | |
|
1062 | 0 | prepare_submodule_repo_env(&cp.env); |
1063 | 0 | cp.git_cmd = 1; |
1064 | 0 | cp.no_stdin = 1; |
1065 | 0 | cp.out = -1; |
1066 | 0 | cp.dir = path; |
1067 | 0 | if (start_command(&cp)) |
1068 | 0 | die(_("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s"), |
1069 | 0 | path); |
1070 | 0 | if (strbuf_read(&buf, cp.out, the_hash_algo->hexsz + 1)) |
1071 | 0 | needs_pushing = 1; |
1072 | 0 | finish_command(&cp); |
1073 | 0 | close(cp.out); |
1074 | 0 | strbuf_release(&buf); |
1075 | 0 | return needs_pushing; |
1076 | 0 | } |
1077 | | |
1078 | 0 | return 0; |
1079 | 0 | } |
1080 | | |
1081 | | int find_unpushed_submodules(struct repository *r, |
1082 | | struct oid_array *commits, |
1083 | | const char *remotes_name, |
1084 | | struct string_list *needs_pushing) |
1085 | 0 | { |
1086 | 0 | struct string_list submodules = STRING_LIST_INIT_DUP; |
1087 | 0 | struct string_list_item *name; |
1088 | 0 | struct strvec argv = STRVEC_INIT; |
1089 | | |
1090 | | /* argv.v[0] will be ignored by setup_revisions */ |
1091 | 0 | strvec_push(&argv, "find_unpushed_submodules"); |
1092 | 0 | oid_array_for_each_unique(commits, append_oid_to_argv, &argv); |
1093 | 0 | strvec_push(&argv, "--not"); |
1094 | 0 | strvec_pushf(&argv, "--remotes=%s", remotes_name); |
1095 | |
|
1096 | 0 | collect_changed_submodules(r, &submodules, &argv); |
1097 | |
|
1098 | 0 | for_each_string_list_item(name, &submodules) { |
1099 | 0 | struct changed_submodule_data *cs_data = name->util; |
1100 | 0 | const struct submodule *submodule; |
1101 | 0 | const char *path = NULL; |
1102 | |
|
1103 | 0 | submodule = submodule_from_name(r, null_oid(the_hash_algo), name->string); |
1104 | 0 | if (submodule) |
1105 | 0 | path = submodule->path; |
1106 | 0 | else |
1107 | 0 | path = default_name_or_path(name->string); |
1108 | |
|
1109 | 0 | if (!path) |
1110 | 0 | continue; |
1111 | | |
1112 | 0 | if (submodule_needs_pushing(r, path, &cs_data->new_commits)) |
1113 | 0 | string_list_insert(needs_pushing, path); |
1114 | 0 | } |
1115 | |
|
1116 | 0 | free_submodules_data(&submodules); |
1117 | 0 | strvec_clear(&argv); |
1118 | |
|
1119 | 0 | return needs_pushing->nr; |
1120 | 0 | } |
1121 | | |
1122 | | static int push_submodule(const char *path, |
1123 | | const struct remote *remote, |
1124 | | const struct refspec *rs, |
1125 | | const struct string_list *push_options, |
1126 | | int dry_run) |
1127 | 0 | { |
1128 | 0 | if (validate_submodule_path(path) < 0) |
1129 | 0 | exit(128); |
1130 | | |
1131 | 0 | if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) { |
1132 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
1133 | 0 | strvec_push(&cp.args, "push"); |
1134 | | /* |
1135 | | * When recursing into a submodule, treat any "only" configurations as "on- |
1136 | | * demand", since "only" would not work (we need all submodules to be pushed |
1137 | | * in order to be able to push the superproject). |
1138 | | */ |
1139 | 0 | strvec_push(&cp.args, "--recurse-submodules=only-is-on-demand"); |
1140 | 0 | if (dry_run) |
1141 | 0 | strvec_push(&cp.args, "--dry-run"); |
1142 | |
|
1143 | 0 | if (push_options && push_options->nr) { |
1144 | 0 | const struct string_list_item *item; |
1145 | 0 | for_each_string_list_item(item, push_options) |
1146 | 0 | strvec_pushf(&cp.args, "--push-option=%s", |
1147 | 0 | item->string); |
1148 | 0 | } |
1149 | |
|
1150 | 0 | if (remote->origin != REMOTE_UNCONFIGURED) { |
1151 | 0 | int i; |
1152 | 0 | strvec_push(&cp.args, remote->name); |
1153 | 0 | for (i = 0; i < rs->nr; i++) |
1154 | 0 | strvec_push(&cp.args, rs->items[i].raw); |
1155 | 0 | } |
1156 | |
|
1157 | 0 | prepare_submodule_repo_env(&cp.env); |
1158 | 0 | cp.git_cmd = 1; |
1159 | 0 | cp.no_stdin = 1; |
1160 | 0 | cp.dir = path; |
1161 | 0 | if (run_command(&cp)) |
1162 | 0 | return 0; |
1163 | 0 | close(cp.out); |
1164 | 0 | } |
1165 | | |
1166 | 0 | return 1; |
1167 | 0 | } |
1168 | | |
1169 | | /* |
1170 | | * Perform a check in the submodule to see if the remote and refspec work. |
1171 | | * Die if the submodule can't be pushed. |
1172 | | */ |
1173 | | static void submodule_push_check(const char *path, const char *head, |
1174 | | const struct remote *remote, |
1175 | | const struct refspec *rs) |
1176 | 0 | { |
1177 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
1178 | 0 | int i; |
1179 | |
|
1180 | 0 | if (validate_submodule_path(path) < 0) |
1181 | 0 | exit(128); |
1182 | | |
1183 | 0 | strvec_push(&cp.args, "submodule--helper"); |
1184 | 0 | strvec_push(&cp.args, "push-check"); |
1185 | 0 | strvec_push(&cp.args, head); |
1186 | 0 | strvec_push(&cp.args, remote->name); |
1187 | |
|
1188 | 0 | for (i = 0; i < rs->nr; i++) |
1189 | 0 | strvec_push(&cp.args, rs->items[i].raw); |
1190 | |
|
1191 | 0 | prepare_submodule_repo_env(&cp.env); |
1192 | 0 | cp.git_cmd = 1; |
1193 | 0 | cp.no_stdin = 1; |
1194 | 0 | cp.no_stdout = 1; |
1195 | 0 | cp.dir = path; |
1196 | | |
1197 | | /* |
1198 | | * Simply indicate if 'submodule--helper push-check' failed. |
1199 | | * More detailed error information will be provided by the |
1200 | | * child process. |
1201 | | */ |
1202 | 0 | if (run_command(&cp)) |
1203 | 0 | die(_("process for submodule '%s' failed"), path); |
1204 | 0 | } |
1205 | | |
1206 | | int push_unpushed_submodules(struct repository *r, |
1207 | | struct oid_array *commits, |
1208 | | const struct remote *remote, |
1209 | | const struct refspec *rs, |
1210 | | const struct string_list *push_options, |
1211 | | int dry_run) |
1212 | 0 | { |
1213 | 0 | int i, ret = 1; |
1214 | 0 | struct string_list needs_pushing = STRING_LIST_INIT_DUP; |
1215 | |
|
1216 | 0 | if (!find_unpushed_submodules(r, commits, |
1217 | 0 | remote->name, &needs_pushing)) |
1218 | 0 | return 1; |
1219 | | |
1220 | | /* |
1221 | | * Verify that the remote and refspec can be propagated to all |
1222 | | * submodules. This check can be skipped if the remote and refspec |
1223 | | * won't be propagated due to the remote being unconfigured (e.g. a URL |
1224 | | * instead of a remote name). |
1225 | | */ |
1226 | 0 | if (remote->origin != REMOTE_UNCONFIGURED) { |
1227 | 0 | char *head; |
1228 | 0 | struct object_id head_oid; |
1229 | |
|
1230 | 0 | head = refs_resolve_refdup(get_main_ref_store(the_repository), |
1231 | 0 | "HEAD", 0, &head_oid, NULL); |
1232 | 0 | if (!head) |
1233 | 0 | die(_("Failed to resolve HEAD as a valid ref.")); |
1234 | | |
1235 | 0 | for (i = 0; i < needs_pushing.nr; i++) |
1236 | 0 | submodule_push_check(needs_pushing.items[i].string, |
1237 | 0 | head, remote, rs); |
1238 | 0 | free(head); |
1239 | 0 | } |
1240 | | |
1241 | | /* Actually push the submodules */ |
1242 | 0 | for (i = 0; i < needs_pushing.nr; i++) { |
1243 | 0 | const char *path = needs_pushing.items[i].string; |
1244 | 0 | fprintf(stderr, _("Pushing submodule '%s'\n"), path); |
1245 | 0 | if (!push_submodule(path, remote, rs, |
1246 | 0 | push_options, dry_run)) { |
1247 | 0 | fprintf(stderr, _("Unable to push submodule '%s'\n"), path); |
1248 | 0 | ret = 0; |
1249 | 0 | } |
1250 | 0 | } |
1251 | |
|
1252 | 0 | string_list_clear(&needs_pushing, 0); |
1253 | |
|
1254 | 0 | return ret; |
1255 | 0 | } |
1256 | | |
1257 | | static int append_oid_to_array(const struct reference *ref, void *data) |
1258 | 0 | { |
1259 | 0 | struct oid_array *array = data; |
1260 | 0 | oid_array_append(array, ref->oid); |
1261 | 0 | return 0; |
1262 | 0 | } |
1263 | | |
1264 | | void check_for_new_submodule_commits(struct object_id *oid) |
1265 | 0 | { |
1266 | 0 | if (!initialized_fetch_ref_tips) { |
1267 | 0 | refs_for_each_ref(get_main_ref_store(the_repository), |
1268 | 0 | append_oid_to_array, &ref_tips_before_fetch); |
1269 | 0 | initialized_fetch_ref_tips = 1; |
1270 | 0 | } |
1271 | |
|
1272 | 0 | oid_array_append(&ref_tips_after_fetch, oid); |
1273 | 0 | } |
1274 | | |
1275 | | /* |
1276 | | * Returns 1 if there is at least one submodule gitdir in |
1277 | | * $GIT_DIR/modules and 0 otherwise. This follows |
1278 | | * submodule_name_to_gitdir(), which looks for submodules in |
1279 | | * $GIT_DIR/modules, not $GIT_COMMON_DIR. |
1280 | | * |
1281 | | * A submodule can be moved to $GIT_DIR/modules manually by running "git |
1282 | | * submodule absorbgitdirs", or it may be initialized there by "git |
1283 | | * submodule update". |
1284 | | */ |
1285 | | static int repo_has_absorbed_submodules(struct repository *r) |
1286 | 0 | { |
1287 | 0 | int ret; |
1288 | 0 | struct strbuf buf = STRBUF_INIT; |
1289 | |
|
1290 | 0 | repo_git_path_append(r, &buf, "modules/"); |
1291 | 0 | ret = file_exists(buf.buf) && !is_empty_dir(buf.buf); |
1292 | 0 | strbuf_release(&buf); |
1293 | 0 | return ret; |
1294 | 0 | } |
1295 | | |
1296 | | static void calculate_changed_submodule_paths(struct repository *r, |
1297 | | struct string_list *changed_submodule_names) |
1298 | 0 | { |
1299 | 0 | struct strvec argv = STRVEC_INIT; |
1300 | 0 | struct string_list_item *name; |
1301 | | |
1302 | | /* No need to check if no submodules would be fetched */ |
1303 | 0 | if (!submodule_from_path(r, NULL, NULL) && |
1304 | 0 | !repo_has_absorbed_submodules(r)) |
1305 | 0 | return; |
1306 | | |
1307 | 0 | strvec_push(&argv, "--"); /* argv[0] program name */ |
1308 | 0 | oid_array_for_each_unique(&ref_tips_after_fetch, |
1309 | 0 | append_oid_to_argv, &argv); |
1310 | 0 | strvec_push(&argv, "--not"); |
1311 | 0 | oid_array_for_each_unique(&ref_tips_before_fetch, |
1312 | 0 | append_oid_to_argv, &argv); |
1313 | | |
1314 | | /* |
1315 | | * Collect all submodules (whether checked out or not) for which new |
1316 | | * commits have been recorded upstream in "changed_submodule_names". |
1317 | | */ |
1318 | 0 | collect_changed_submodules(r, changed_submodule_names, &argv); |
1319 | |
|
1320 | 0 | for_each_string_list_item(name, changed_submodule_names) { |
1321 | 0 | struct changed_submodule_data *cs_data = name->util; |
1322 | 0 | const struct submodule *submodule; |
1323 | 0 | const char *path = NULL; |
1324 | |
|
1325 | 0 | submodule = submodule_from_name(r, null_oid(the_hash_algo), name->string); |
1326 | 0 | if (submodule) |
1327 | 0 | path = submodule->path; |
1328 | 0 | else |
1329 | 0 | path = default_name_or_path(name->string); |
1330 | |
|
1331 | 0 | if (!path) |
1332 | 0 | continue; |
1333 | | |
1334 | 0 | if (submodule_has_commits(r, path, null_oid(the_hash_algo), &cs_data->new_commits)) { |
1335 | 0 | changed_submodule_data_clear(cs_data); |
1336 | 0 | *name->string = '\0'; |
1337 | 0 | } |
1338 | 0 | } |
1339 | |
|
1340 | 0 | string_list_remove_empty_items(changed_submodule_names, 1); |
1341 | |
|
1342 | 0 | strvec_clear(&argv); |
1343 | 0 | oid_array_clear(&ref_tips_before_fetch); |
1344 | 0 | oid_array_clear(&ref_tips_after_fetch); |
1345 | 0 | initialized_fetch_ref_tips = 0; |
1346 | 0 | } |
1347 | | |
1348 | | int submodule_touches_in_range(struct repository *r, |
1349 | | struct object_id *excl_oid, |
1350 | | struct object_id *incl_oid) |
1351 | 0 | { |
1352 | 0 | struct string_list subs = STRING_LIST_INIT_DUP; |
1353 | 0 | struct strvec args = STRVEC_INIT; |
1354 | 0 | int ret; |
1355 | | |
1356 | | /* No need to check if there are no submodules configured */ |
1357 | 0 | if (!submodule_from_path(r, NULL, NULL)) |
1358 | 0 | return 0; |
1359 | | |
1360 | 0 | strvec_push(&args, "--"); /* args[0] program name */ |
1361 | 0 | strvec_push(&args, oid_to_hex(incl_oid)); |
1362 | 0 | if (!is_null_oid(excl_oid)) { |
1363 | 0 | strvec_push(&args, "--not"); |
1364 | 0 | strvec_push(&args, oid_to_hex(excl_oid)); |
1365 | 0 | } |
1366 | |
|
1367 | 0 | collect_changed_submodules(r, &subs, &args); |
1368 | 0 | ret = subs.nr; |
1369 | |
|
1370 | 0 | strvec_clear(&args); |
1371 | |
|
1372 | 0 | free_submodules_data(&subs); |
1373 | 0 | return ret; |
1374 | 0 | } |
1375 | | |
1376 | | struct submodule_parallel_fetch { |
1377 | | /* |
1378 | | * The index of the last index entry processed by |
1379 | | * get_fetch_task_from_index(). |
1380 | | */ |
1381 | | int index_count; |
1382 | | /* |
1383 | | * The index of the last string_list entry processed by |
1384 | | * get_fetch_task_from_changed(). |
1385 | | */ |
1386 | | int changed_count; |
1387 | | struct strvec args; |
1388 | | struct repository *r; |
1389 | | const char *prefix; |
1390 | | int command_line_option; |
1391 | | int default_option; |
1392 | | int quiet; |
1393 | | int result; |
1394 | | |
1395 | | /* |
1396 | | * Names of submodules that have new commits. Generated by |
1397 | | * walking the newly fetched superproject commits. |
1398 | | */ |
1399 | | struct string_list changed_submodule_names; |
1400 | | /* |
1401 | | * Names of submodules that have already been processed. Lets us |
1402 | | * avoid fetching the same submodule more than once. |
1403 | | */ |
1404 | | struct string_list seen_submodule_names; |
1405 | | |
1406 | | /* Pending fetches by OIDs */ |
1407 | | struct fetch_task **oid_fetch_tasks; |
1408 | | int oid_fetch_tasks_nr, oid_fetch_tasks_alloc; |
1409 | | |
1410 | | struct strbuf submodules_with_errors; |
1411 | | }; |
1412 | 0 | #define SPF_INIT { \ |
1413 | 0 | .args = STRVEC_INIT, \ |
1414 | 0 | .changed_submodule_names = STRING_LIST_INIT_DUP, \ |
1415 | 0 | .seen_submodule_names = STRING_LIST_INIT_DUP, \ |
1416 | 0 | .submodules_with_errors = STRBUF_INIT, \ |
1417 | 0 | } |
1418 | | |
1419 | | static int get_fetch_recurse_config(const struct submodule *submodule, |
1420 | | struct submodule_parallel_fetch *spf) |
1421 | 0 | { |
1422 | 0 | if (spf->command_line_option != RECURSE_SUBMODULES_DEFAULT) |
1423 | 0 | return spf->command_line_option; |
1424 | | |
1425 | 0 | if (submodule) { |
1426 | 0 | char *key; |
1427 | 0 | const char *value; |
1428 | |
|
1429 | 0 | int fetch_recurse = submodule->fetch_recurse; |
1430 | 0 | key = xstrfmt("submodule.%s.fetchRecurseSubmodules", submodule->name); |
1431 | 0 | if (!repo_config_get_string_tmp(spf->r, key, &value)) { |
1432 | 0 | fetch_recurse = parse_fetch_recurse_submodules_arg(key, value); |
1433 | 0 | } |
1434 | 0 | free(key); |
1435 | |
|
1436 | 0 | if (fetch_recurse != RECURSE_SUBMODULES_NONE) |
1437 | | /* local config overrules everything except commandline */ |
1438 | 0 | return fetch_recurse; |
1439 | 0 | } |
1440 | | |
1441 | 0 | return spf->default_option; |
1442 | 0 | } |
1443 | | |
1444 | | /* |
1445 | | * Fetch in progress (if callback data) or |
1446 | | * pending (if in oid_fetch_tasks in struct submodule_parallel_fetch) |
1447 | | */ |
1448 | | struct fetch_task { |
1449 | | struct repository *repo; |
1450 | | const struct submodule *sub; |
1451 | | unsigned free_sub : 1; /* Do we need to free the submodule? */ |
1452 | | const char *default_argv; /* The default fetch mode. */ |
1453 | | struct strvec git_args; /* Args for the child git process. */ |
1454 | | |
1455 | | struct oid_array *commits; /* Ensure these commits are fetched */ |
1456 | | }; |
1457 | | |
1458 | | /** |
1459 | | * When a submodule is not defined in .gitmodules, we cannot access it |
1460 | | * via the regular submodule-config. Create a fake submodule, which we can |
1461 | | * work on. |
1462 | | */ |
1463 | | static const struct submodule *get_non_gitmodules_submodule(const char *path) |
1464 | 0 | { |
1465 | 0 | struct submodule *ret; |
1466 | 0 | const char *name = default_name_or_path(path); |
1467 | |
|
1468 | 0 | if (!name) |
1469 | 0 | return NULL; |
1470 | | |
1471 | 0 | CALLOC_ARRAY(ret, 1); |
1472 | 0 | ret->path = name; |
1473 | 0 | ret->name = name; |
1474 | |
|
1475 | 0 | return (const struct submodule *) ret; |
1476 | 0 | } |
1477 | | |
1478 | | static void fetch_task_free(struct fetch_task *p) |
1479 | 0 | { |
1480 | 0 | if (p->free_sub) |
1481 | 0 | free((void*)p->sub); |
1482 | 0 | p->free_sub = 0; |
1483 | 0 | p->sub = NULL; |
1484 | |
|
1485 | 0 | if (p->repo) |
1486 | 0 | repo_clear(p->repo); |
1487 | 0 | FREE_AND_NULL(p->repo); |
1488 | |
|
1489 | 0 | strvec_clear(&p->git_args); |
1490 | 0 | free(p); |
1491 | 0 | } |
1492 | | |
1493 | | static struct repository *get_submodule_repo_for(struct repository *r, |
1494 | | const char *path, |
1495 | | const struct object_id *treeish_name) |
1496 | 0 | { |
1497 | 0 | struct repository *ret = xmalloc(sizeof(*ret)); |
1498 | |
|
1499 | 0 | if (repo_submodule_init(ret, r, path, treeish_name)) { |
1500 | 0 | free(ret); |
1501 | 0 | return NULL; |
1502 | 0 | } |
1503 | | |
1504 | 0 | return ret; |
1505 | 0 | } |
1506 | | |
1507 | | static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf, |
1508 | | const char *path, |
1509 | | const struct object_id *treeish_name) |
1510 | 0 | { |
1511 | 0 | struct fetch_task *task; |
1512 | |
|
1513 | 0 | CALLOC_ARRAY(task, 1); |
1514 | |
|
1515 | 0 | if (validate_submodule_path(path) < 0) |
1516 | 0 | exit(128); |
1517 | | |
1518 | 0 | task->sub = submodule_from_path(spf->r, treeish_name, path); |
1519 | |
|
1520 | 0 | if (!task->sub) { |
1521 | | /* |
1522 | | * No entry in .gitmodules? Technically not a submodule, |
1523 | | * but historically we supported repositories that happen to be |
1524 | | * in-place where a gitlink is. Keep supporting them. |
1525 | | */ |
1526 | 0 | task->sub = get_non_gitmodules_submodule(path); |
1527 | 0 | if (!task->sub) |
1528 | 0 | goto cleanup; |
1529 | | |
1530 | 0 | task->free_sub = 1; |
1531 | 0 | } |
1532 | | |
1533 | 0 | if (string_list_lookup(&spf->seen_submodule_names, task->sub->name)) |
1534 | 0 | goto cleanup; |
1535 | | |
1536 | 0 | switch (get_fetch_recurse_config(task->sub, spf)) |
1537 | 0 | { |
1538 | 0 | default: |
1539 | 0 | case RECURSE_SUBMODULES_DEFAULT: |
1540 | 0 | case RECURSE_SUBMODULES_ON_DEMAND: |
1541 | 0 | if (!task->sub || |
1542 | 0 | !string_list_lookup( |
1543 | 0 | &spf->changed_submodule_names, |
1544 | 0 | task->sub->name)) |
1545 | 0 | goto cleanup; |
1546 | 0 | task->default_argv = "on-demand"; |
1547 | 0 | break; |
1548 | 0 | case RECURSE_SUBMODULES_ON: |
1549 | 0 | task->default_argv = "yes"; |
1550 | 0 | break; |
1551 | 0 | case RECURSE_SUBMODULES_OFF: |
1552 | 0 | goto cleanup; |
1553 | 0 | } |
1554 | | |
1555 | 0 | task->repo = get_submodule_repo_for(spf->r, path, treeish_name); |
1556 | |
|
1557 | 0 | return task; |
1558 | | |
1559 | 0 | cleanup: |
1560 | 0 | fetch_task_free(task); |
1561 | 0 | return NULL; |
1562 | 0 | } |
1563 | | |
1564 | | static struct fetch_task * |
1565 | | get_fetch_task_from_index(struct submodule_parallel_fetch *spf, |
1566 | | struct strbuf *err) |
1567 | 0 | { |
1568 | 0 | for (; spf->index_count < spf->r->index->cache_nr; spf->index_count++) { |
1569 | 0 | const struct cache_entry *ce = |
1570 | 0 | spf->r->index->cache[spf->index_count]; |
1571 | 0 | struct fetch_task *task; |
1572 | |
|
1573 | 0 | if (!S_ISGITLINK(ce->ce_mode)) |
1574 | 0 | continue; |
1575 | | |
1576 | 0 | task = fetch_task_create(spf, ce->name, null_oid(the_hash_algo)); |
1577 | 0 | if (!task) |
1578 | 0 | continue; |
1579 | | |
1580 | 0 | if (task->repo) { |
1581 | 0 | if (!spf->quiet) |
1582 | 0 | strbuf_addf(err, _("Fetching submodule %s%s\n"), |
1583 | 0 | spf->prefix, ce->name); |
1584 | |
|
1585 | 0 | spf->index_count++; |
1586 | 0 | return task; |
1587 | 0 | } else { |
1588 | 0 | struct strbuf empty_submodule_path = STRBUF_INIT; |
1589 | |
|
1590 | 0 | fetch_task_free(task); |
1591 | | |
1592 | | /* |
1593 | | * An empty directory is normal, |
1594 | | * the submodule is not initialized |
1595 | | */ |
1596 | 0 | strbuf_addf(&empty_submodule_path, "%s/%s/", |
1597 | 0 | spf->r->worktree, |
1598 | 0 | ce->name); |
1599 | 0 | if (S_ISGITLINK(ce->ce_mode) && |
1600 | 0 | !is_empty_dir(empty_submodule_path.buf)) { |
1601 | 0 | spf->result = 1; |
1602 | 0 | strbuf_addf(err, |
1603 | 0 | _("Could not access submodule '%s'\n"), |
1604 | 0 | ce->name); |
1605 | 0 | } |
1606 | 0 | strbuf_release(&empty_submodule_path); |
1607 | 0 | } |
1608 | 0 | } |
1609 | 0 | return NULL; |
1610 | 0 | } |
1611 | | |
1612 | | static struct fetch_task * |
1613 | | get_fetch_task_from_changed(struct submodule_parallel_fetch *spf, |
1614 | | struct strbuf *err) |
1615 | 0 | { |
1616 | 0 | for (; spf->changed_count < spf->changed_submodule_names.nr; |
1617 | 0 | spf->changed_count++) { |
1618 | 0 | struct string_list_item item = |
1619 | 0 | spf->changed_submodule_names.items[spf->changed_count]; |
1620 | 0 | struct changed_submodule_data *cs_data = item.util; |
1621 | 0 | struct fetch_task *task; |
1622 | |
|
1623 | 0 | if (!is_tree_submodule_active(spf->r, cs_data->super_oid,cs_data->path)) |
1624 | 0 | continue; |
1625 | | |
1626 | 0 | task = fetch_task_create(spf, cs_data->path, |
1627 | 0 | cs_data->super_oid); |
1628 | 0 | if (!task) |
1629 | 0 | continue; |
1630 | | |
1631 | 0 | if (!task->repo) { |
1632 | 0 | strbuf_addf(err, _("Could not access submodule '%s' at commit %s\n"), |
1633 | 0 | cs_data->path, |
1634 | 0 | repo_find_unique_abbrev(the_repository, cs_data->super_oid, DEFAULT_ABBREV)); |
1635 | |
|
1636 | 0 | fetch_task_free(task); |
1637 | 0 | continue; |
1638 | 0 | } |
1639 | | |
1640 | 0 | if (!spf->quiet) |
1641 | 0 | strbuf_addf(err, |
1642 | 0 | _("Fetching submodule %s%s at commit %s\n"), |
1643 | 0 | spf->prefix, task->sub->path, |
1644 | 0 | repo_find_unique_abbrev(the_repository, cs_data->super_oid, |
1645 | 0 | DEFAULT_ABBREV)); |
1646 | |
|
1647 | 0 | spf->changed_count++; |
1648 | | /* |
1649 | | * NEEDSWORK: Submodules set/unset a value for |
1650 | | * core.worktree when they are populated/unpopulated by |
1651 | | * "git checkout" (and similar commands, see |
1652 | | * submodule_move_head() and |
1653 | | * connect_work_tree_and_git_dir()), but if the |
1654 | | * submodule is unpopulated in another way (e.g. "git |
1655 | | * rm", "rm -r"), core.worktree will still be set even |
1656 | | * though the directory doesn't exist, and the child |
1657 | | * process will crash while trying to chdir into the |
1658 | | * nonexistent directory. |
1659 | | * |
1660 | | * In this case, we know that the submodule has no |
1661 | | * working tree, so we can work around this by |
1662 | | * setting "--work-tree=." (--bare does not work because |
1663 | | * worktree settings take precedence over bare-ness). |
1664 | | * However, this is not necessarily true in other cases, |
1665 | | * so a generalized solution is still necessary. |
1666 | | * |
1667 | | * Possible solutions: |
1668 | | * - teach "git [add|rm]" to unset core.worktree and |
1669 | | * discourage users from removing submodules without |
1670 | | * using a Git command. |
1671 | | * - teach submodule child processes to ignore stale |
1672 | | * core.worktree values. |
1673 | | */ |
1674 | 0 | strvec_push(&task->git_args, "--work-tree=."); |
1675 | 0 | return task; |
1676 | 0 | } |
1677 | 0 | return NULL; |
1678 | 0 | } |
1679 | | |
1680 | | static int get_next_submodule(struct child_process *cp, struct strbuf *err, |
1681 | | void *data, void **task_cb) |
1682 | 0 | { |
1683 | 0 | struct submodule_parallel_fetch *spf = data; |
1684 | 0 | struct fetch_task *task = |
1685 | 0 | get_fetch_task_from_index(spf, err); |
1686 | 0 | if (!task) |
1687 | 0 | task = get_fetch_task_from_changed(spf, err); |
1688 | |
|
1689 | 0 | if (task) { |
1690 | 0 | child_process_init(cp); |
1691 | 0 | cp->dir = task->repo->gitdir; |
1692 | 0 | prepare_submodule_repo_env_in_gitdir(&cp->env); |
1693 | 0 | cp->git_cmd = 1; |
1694 | 0 | strvec_init(&cp->args); |
1695 | 0 | if (task->git_args.nr) |
1696 | 0 | strvec_pushv(&cp->args, task->git_args.v); |
1697 | 0 | strvec_pushv(&cp->args, spf->args.v); |
1698 | 0 | strvec_push(&cp->args, task->default_argv); |
1699 | 0 | strvec_pushf(&cp->args, "--submodule-prefix=%s%s/", |
1700 | 0 | spf->prefix, task->sub->path); |
1701 | |
|
1702 | 0 | *task_cb = task; |
1703 | |
|
1704 | 0 | string_list_insert(&spf->seen_submodule_names, task->sub->name); |
1705 | 0 | return 1; |
1706 | 0 | } |
1707 | | |
1708 | 0 | if (spf->oid_fetch_tasks_nr) { |
1709 | 0 | struct fetch_task *task = |
1710 | 0 | spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr - 1]; |
1711 | 0 | spf->oid_fetch_tasks_nr--; |
1712 | |
|
1713 | 0 | child_process_init(cp); |
1714 | 0 | prepare_submodule_repo_env_in_gitdir(&cp->env); |
1715 | 0 | cp->git_cmd = 1; |
1716 | 0 | cp->dir = task->repo->gitdir; |
1717 | |
|
1718 | 0 | strvec_init(&cp->args); |
1719 | 0 | strvec_pushv(&cp->args, spf->args.v); |
1720 | 0 | strvec_push(&cp->args, "on-demand"); |
1721 | 0 | strvec_pushf(&cp->args, "--submodule-prefix=%s%s/", |
1722 | 0 | spf->prefix, task->sub->path); |
1723 | | |
1724 | | /* NEEDSWORK: have get_default_remote from submodule--helper */ |
1725 | 0 | strvec_push(&cp->args, "origin"); |
1726 | 0 | oid_array_for_each_unique(task->commits, |
1727 | 0 | append_oid_to_argv, &cp->args); |
1728 | |
|
1729 | 0 | *task_cb = task; |
1730 | 0 | return 1; |
1731 | 0 | } |
1732 | | |
1733 | 0 | return 0; |
1734 | 0 | } |
1735 | | |
1736 | | static int fetch_start_failure(struct strbuf *err UNUSED, |
1737 | | void *cb, void *task_cb) |
1738 | 0 | { |
1739 | 0 | struct submodule_parallel_fetch *spf = cb; |
1740 | 0 | struct fetch_task *task = task_cb; |
1741 | |
|
1742 | 0 | spf->result = 1; |
1743 | |
|
1744 | 0 | fetch_task_free(task); |
1745 | 0 | return 0; |
1746 | 0 | } |
1747 | | |
1748 | | static int commit_missing_in_sub(const struct object_id *oid, void *data) |
1749 | 0 | { |
1750 | 0 | struct repository *subrepo = data; |
1751 | 0 | enum object_type type = odb_read_object_info(subrepo->objects, oid, NULL); |
1752 | |
|
1753 | 0 | return type != OBJ_COMMIT; |
1754 | 0 | } |
1755 | | |
1756 | | static int fetch_finish(int retvalue, struct strbuf *err UNUSED, |
1757 | | void *cb, void *task_cb) |
1758 | 0 | { |
1759 | 0 | struct submodule_parallel_fetch *spf = cb; |
1760 | 0 | struct fetch_task *task = task_cb; |
1761 | |
|
1762 | 0 | struct string_list_item *it; |
1763 | 0 | struct changed_submodule_data *cs_data; |
1764 | |
|
1765 | 0 | if (!task || !task->sub) |
1766 | 0 | BUG("callback cookie bogus"); |
1767 | | |
1768 | 0 | if (retvalue) { |
1769 | | /* |
1770 | | * NEEDSWORK: This indicates that the overall fetch |
1771 | | * failed, even though there may be a subsequent fetch |
1772 | | * by commit hash that might work. It may be a good |
1773 | | * idea to not indicate failure in this case, and only |
1774 | | * indicate failure if the subsequent fetch fails. |
1775 | | */ |
1776 | 0 | spf->result = 1; |
1777 | |
|
1778 | 0 | strbuf_addf(&spf->submodules_with_errors, "\t%s\n", |
1779 | 0 | task->sub->name); |
1780 | 0 | } |
1781 | | |
1782 | | /* Is this the second time we process this submodule? */ |
1783 | 0 | if (task->commits) |
1784 | 0 | goto out; |
1785 | | |
1786 | 0 | it = string_list_lookup(&spf->changed_submodule_names, task->sub->name); |
1787 | 0 | if (!it) |
1788 | | /* Could be an unchanged submodule, not contained in the list */ |
1789 | 0 | goto out; |
1790 | | |
1791 | 0 | cs_data = it->util; |
1792 | 0 | oid_array_filter(&cs_data->new_commits, |
1793 | 0 | commit_missing_in_sub, |
1794 | 0 | task->repo); |
1795 | | |
1796 | | /* Are there commits we want, but do not exist? */ |
1797 | 0 | if (cs_data->new_commits.nr) { |
1798 | 0 | task->commits = &cs_data->new_commits; |
1799 | 0 | ALLOC_GROW(spf->oid_fetch_tasks, |
1800 | 0 | spf->oid_fetch_tasks_nr + 1, |
1801 | 0 | spf->oid_fetch_tasks_alloc); |
1802 | 0 | spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr] = task; |
1803 | 0 | spf->oid_fetch_tasks_nr++; |
1804 | 0 | return 0; |
1805 | 0 | } |
1806 | | |
1807 | 0 | out: |
1808 | 0 | fetch_task_free(task); |
1809 | 0 | return 0; |
1810 | 0 | } |
1811 | | |
1812 | | int fetch_submodules(struct repository *r, |
1813 | | const struct strvec *options, |
1814 | | const char *prefix, int command_line_option, |
1815 | | int default_option, |
1816 | | int quiet, int max_parallel_jobs) |
1817 | 0 | { |
1818 | 0 | int i; |
1819 | 0 | struct submodule_parallel_fetch spf = SPF_INIT; |
1820 | 0 | const struct run_process_parallel_opts opts = { |
1821 | 0 | .tr2_category = "submodule", |
1822 | 0 | .tr2_label = "parallel/fetch", |
1823 | |
|
1824 | 0 | .processes = max_parallel_jobs, |
1825 | |
|
1826 | 0 | .get_next_task = get_next_submodule, |
1827 | 0 | .start_failure = fetch_start_failure, |
1828 | 0 | .task_finished = fetch_finish, |
1829 | 0 | .data = &spf, |
1830 | 0 | }; |
1831 | |
|
1832 | 0 | spf.r = r; |
1833 | 0 | spf.command_line_option = command_line_option; |
1834 | 0 | spf.default_option = default_option; |
1835 | 0 | spf.quiet = quiet; |
1836 | 0 | spf.prefix = prefix; |
1837 | |
|
1838 | 0 | if (!r->worktree) |
1839 | 0 | goto out; |
1840 | | |
1841 | 0 | if (repo_read_index(r) < 0) |
1842 | 0 | die(_("index file corrupt")); |
1843 | | |
1844 | 0 | strvec_push(&spf.args, "fetch"); |
1845 | 0 | for (i = 0; i < options->nr; i++) |
1846 | 0 | strvec_push(&spf.args, options->v[i]); |
1847 | 0 | strvec_push(&spf.args, "--recurse-submodules-default"); |
1848 | | /* default value, "--submodule-prefix" and its value are added later */ |
1849 | |
|
1850 | 0 | calculate_changed_submodule_paths(r, &spf.changed_submodule_names); |
1851 | 0 | string_list_sort(&spf.changed_submodule_names); |
1852 | 0 | run_processes_parallel(&opts); |
1853 | |
|
1854 | 0 | if (spf.submodules_with_errors.len > 0) |
1855 | 0 | fprintf(stderr, _("Errors during submodule fetch:\n%s"), |
1856 | 0 | spf.submodules_with_errors.buf); |
1857 | | |
1858 | |
|
1859 | 0 | strvec_clear(&spf.args); |
1860 | 0 | out: |
1861 | 0 | free_submodules_data(&spf.changed_submodule_names); |
1862 | 0 | string_list_clear(&spf.seen_submodule_names, 0); |
1863 | 0 | strbuf_release(&spf.submodules_with_errors); |
1864 | 0 | free(spf.oid_fetch_tasks); |
1865 | 0 | return spf.result; |
1866 | 0 | } |
1867 | | |
1868 | | unsigned is_submodule_modified(const char *path, int ignore_untracked) |
1869 | 0 | { |
1870 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
1871 | 0 | struct strbuf buf = STRBUF_INIT; |
1872 | 0 | FILE *fp; |
1873 | 0 | unsigned dirty_submodule = 0; |
1874 | 0 | const char *git_dir; |
1875 | 0 | int ignore_cp_exit_code = 0; |
1876 | |
|
1877 | 0 | if (validate_submodule_path(path) < 0) |
1878 | 0 | exit(128); |
1879 | | |
1880 | 0 | strbuf_addf(&buf, "%s/.git", path); |
1881 | 0 | git_dir = read_gitfile(buf.buf); |
1882 | 0 | if (!git_dir) |
1883 | 0 | git_dir = buf.buf; |
1884 | 0 | if (!is_git_directory(git_dir)) { |
1885 | 0 | if (is_directory(git_dir)) |
1886 | 0 | die(_("'%s' not recognized as a git repository"), git_dir); |
1887 | 0 | strbuf_release(&buf); |
1888 | | /* The submodule is not checked out, so it is not modified */ |
1889 | 0 | return 0; |
1890 | 0 | } |
1891 | 0 | strbuf_reset(&buf); |
1892 | |
|
1893 | 0 | strvec_pushl(&cp.args, "status", "--porcelain=2", NULL); |
1894 | 0 | if (ignore_untracked) |
1895 | 0 | strvec_push(&cp.args, "-uno"); |
1896 | |
|
1897 | 0 | prepare_submodule_repo_env(&cp.env); |
1898 | 0 | cp.git_cmd = 1; |
1899 | 0 | cp.no_stdin = 1; |
1900 | 0 | cp.out = -1; |
1901 | 0 | cp.dir = path; |
1902 | 0 | if (start_command(&cp)) |
1903 | 0 | die(_("Could not run 'git status --porcelain=2' in submodule %s"), path); |
1904 | | |
1905 | 0 | fp = xfdopen(cp.out, "r"); |
1906 | 0 | while (strbuf_getwholeline(&buf, fp, '\n') != EOF) { |
1907 | | /* regular untracked files */ |
1908 | 0 | if (buf.buf[0] == '?') |
1909 | 0 | dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED; |
1910 | |
|
1911 | 0 | if (buf.buf[0] == 'u' || |
1912 | 0 | buf.buf[0] == '1' || |
1913 | 0 | buf.buf[0] == '2') { |
1914 | | /* T = line type, XY = status, SSSS = submodule state */ |
1915 | 0 | if (buf.len < strlen("T XY SSSS")) |
1916 | 0 | BUG("invalid status --porcelain=2 line %s", |
1917 | 0 | buf.buf); |
1918 | | |
1919 | 0 | if (buf.buf[5] == 'S' && buf.buf[8] == 'U') |
1920 | | /* nested untracked file */ |
1921 | 0 | dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED; |
1922 | |
|
1923 | 0 | if (buf.buf[0] == 'u' || |
1924 | 0 | buf.buf[0] == '2' || |
1925 | 0 | memcmp(buf.buf + 5, "S..U", 4)) |
1926 | | /* other change */ |
1927 | 0 | dirty_submodule |= DIRTY_SUBMODULE_MODIFIED; |
1928 | 0 | } |
1929 | | |
1930 | 0 | if ((dirty_submodule & DIRTY_SUBMODULE_MODIFIED) && |
1931 | 0 | ((dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) || |
1932 | 0 | ignore_untracked)) { |
1933 | | /* |
1934 | | * We're not interested in any further information from |
1935 | | * the child any more, neither output nor its exit code. |
1936 | | */ |
1937 | 0 | ignore_cp_exit_code = 1; |
1938 | 0 | break; |
1939 | 0 | } |
1940 | 0 | } |
1941 | 0 | fclose(fp); |
1942 | |
|
1943 | 0 | if (finish_command(&cp) && !ignore_cp_exit_code) |
1944 | 0 | die(_("'git status --porcelain=2' failed in submodule %s"), path); |
1945 | | |
1946 | 0 | strbuf_release(&buf); |
1947 | 0 | return dirty_submodule; |
1948 | 0 | } |
1949 | | |
1950 | | int submodule_uses_gitfile(const char *path) |
1951 | 0 | { |
1952 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
1953 | 0 | struct strbuf buf = STRBUF_INIT; |
1954 | 0 | const char *git_dir; |
1955 | |
|
1956 | 0 | if (validate_submodule_path(path) < 0) |
1957 | 0 | exit(128); |
1958 | | |
1959 | 0 | strbuf_addf(&buf, "%s/.git", path); |
1960 | 0 | git_dir = read_gitfile(buf.buf); |
1961 | 0 | if (!git_dir) { |
1962 | 0 | strbuf_release(&buf); |
1963 | 0 | return 0; |
1964 | 0 | } |
1965 | 0 | strbuf_release(&buf); |
1966 | | |
1967 | | /* Now test that all nested submodules use a gitfile too */ |
1968 | 0 | strvec_pushl(&cp.args, |
1969 | 0 | "submodule", "foreach", "--quiet", "--recursive", |
1970 | 0 | "test -f .git", NULL); |
1971 | |
|
1972 | 0 | prepare_submodule_repo_env(&cp.env); |
1973 | 0 | cp.git_cmd = 1; |
1974 | 0 | cp.no_stdin = 1; |
1975 | 0 | cp.no_stderr = 1; |
1976 | 0 | cp.no_stdout = 1; |
1977 | 0 | cp.dir = path; |
1978 | 0 | if (run_command(&cp)) |
1979 | 0 | return 0; |
1980 | | |
1981 | 0 | return 1; |
1982 | 0 | } |
1983 | | |
1984 | | /* |
1985 | | * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data |
1986 | | * when doing so. |
1987 | | * |
1988 | | * Return 1 if we'd lose data, return 0 if the removal is fine, |
1989 | | * and negative values for errors. |
1990 | | */ |
1991 | | int bad_to_remove_submodule(const char *path, unsigned flags) |
1992 | 0 | { |
1993 | 0 | ssize_t len; |
1994 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
1995 | 0 | struct strbuf buf = STRBUF_INIT; |
1996 | 0 | int ret = 0; |
1997 | |
|
1998 | 0 | if (validate_submodule_path(path) < 0) |
1999 | 0 | exit(128); |
2000 | | |
2001 | 0 | if (!file_exists(path) || is_empty_dir(path)) |
2002 | 0 | return 0; |
2003 | | |
2004 | 0 | if (!submodule_uses_gitfile(path)) |
2005 | 0 | return 1; |
2006 | | |
2007 | 0 | strvec_pushl(&cp.args, "status", "--porcelain", |
2008 | 0 | "--ignore-submodules=none", NULL); |
2009 | |
|
2010 | 0 | if (flags & SUBMODULE_REMOVAL_IGNORE_UNTRACKED) |
2011 | 0 | strvec_push(&cp.args, "-uno"); |
2012 | 0 | else |
2013 | 0 | strvec_push(&cp.args, "-uall"); |
2014 | |
|
2015 | 0 | if (!(flags & SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED)) |
2016 | 0 | strvec_push(&cp.args, "--ignored"); |
2017 | |
|
2018 | 0 | prepare_submodule_repo_env(&cp.env); |
2019 | 0 | cp.git_cmd = 1; |
2020 | 0 | cp.no_stdin = 1; |
2021 | 0 | cp.out = -1; |
2022 | 0 | cp.dir = path; |
2023 | 0 | if (start_command(&cp)) { |
2024 | 0 | if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR) |
2025 | 0 | die(_("could not start 'git status' in submodule '%s'"), |
2026 | 0 | path); |
2027 | 0 | ret = -1; |
2028 | 0 | goto out; |
2029 | 0 | } |
2030 | | |
2031 | 0 | len = strbuf_read(&buf, cp.out, 1024); |
2032 | 0 | if (len > 2) |
2033 | 0 | ret = 1; |
2034 | 0 | close(cp.out); |
2035 | |
|
2036 | 0 | if (finish_command(&cp)) { |
2037 | 0 | if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR) |
2038 | 0 | die(_("could not run 'git status' in submodule '%s'"), |
2039 | 0 | path); |
2040 | 0 | ret = -1; |
2041 | 0 | } |
2042 | 0 | out: |
2043 | 0 | strbuf_release(&buf); |
2044 | 0 | return ret; |
2045 | 0 | } |
2046 | | |
2047 | | void submodule_unset_core_worktree(const struct submodule *sub) |
2048 | 0 | { |
2049 | 0 | struct strbuf config_path = STRBUF_INIT; |
2050 | |
|
2051 | 0 | if (validate_submodule_path(sub->path) < 0) |
2052 | 0 | exit(128); |
2053 | | |
2054 | 0 | submodule_name_to_gitdir(&config_path, the_repository, sub->name); |
2055 | 0 | strbuf_addstr(&config_path, "/config"); |
2056 | |
|
2057 | 0 | if (repo_config_set_in_file_gently(the_repository, config_path.buf, "core.worktree", NULL, NULL)) |
2058 | 0 | warning(_("Could not unset core.worktree setting in submodule '%s'"), |
2059 | 0 | sub->path); |
2060 | |
|
2061 | 0 | strbuf_release(&config_path); |
2062 | 0 | } |
2063 | | |
2064 | | static int submodule_has_dirty_index(const struct submodule *sub) |
2065 | 0 | { |
2066 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
2067 | |
|
2068 | 0 | if (validate_submodule_path(sub->path) < 0) |
2069 | 0 | exit(128); |
2070 | | |
2071 | 0 | prepare_submodule_repo_env(&cp.env); |
2072 | |
|
2073 | 0 | cp.git_cmd = 1; |
2074 | 0 | strvec_pushl(&cp.args, "diff-index", "--quiet", |
2075 | 0 | "--cached", "HEAD", NULL); |
2076 | 0 | cp.no_stdin = 1; |
2077 | 0 | cp.no_stdout = 1; |
2078 | 0 | cp.dir = sub->path; |
2079 | 0 | if (start_command(&cp)) |
2080 | 0 | die(_("could not recurse into submodule '%s'"), sub->path); |
2081 | | |
2082 | 0 | return finish_command(&cp); |
2083 | 0 | } |
2084 | | |
2085 | | static void submodule_reset_index(const char *path, const char *super_prefix) |
2086 | 0 | { |
2087 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
2088 | |
|
2089 | 0 | if (validate_submodule_path(path) < 0) |
2090 | 0 | exit(128); |
2091 | | |
2092 | 0 | prepare_submodule_repo_env(&cp.env); |
2093 | |
|
2094 | 0 | cp.git_cmd = 1; |
2095 | 0 | cp.no_stdin = 1; |
2096 | 0 | cp.dir = path; |
2097 | | |
2098 | | /* TODO: determine if this might overwright untracked files */ |
2099 | 0 | strvec_pushl(&cp.args, "read-tree", "-u", "--reset", NULL); |
2100 | 0 | strvec_pushf(&cp.args, "--super-prefix=%s%s/", |
2101 | 0 | (super_prefix ? super_prefix : ""), path); |
2102 | |
|
2103 | 0 | strvec_push(&cp.args, empty_tree_oid_hex(the_repository->hash_algo)); |
2104 | |
|
2105 | 0 | if (run_command(&cp)) |
2106 | 0 | die(_("could not reset submodule index")); |
2107 | 0 | } |
2108 | | |
2109 | | /** |
2110 | | * Moves a submodule at a given path from a given head to another new head. |
2111 | | * For edge cases (a submodule coming into existence or removing a submodule) |
2112 | | * pass NULL for old or new respectively. |
2113 | | */ |
2114 | | int submodule_move_head(const char *path, const char *super_prefix, |
2115 | | const char *old_head, const char *new_head, |
2116 | | unsigned flags) |
2117 | 0 | { |
2118 | 0 | int ret = 0; |
2119 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
2120 | 0 | const struct submodule *sub; |
2121 | 0 | int *error_code_ptr, error_code; |
2122 | |
|
2123 | 0 | if (!is_submodule_active(the_repository, path)) |
2124 | 0 | return 0; |
2125 | | |
2126 | 0 | if (flags & SUBMODULE_MOVE_HEAD_FORCE) |
2127 | | /* |
2128 | | * Pass non NULL pointer to is_submodule_populated_gently |
2129 | | * to prevent die()-ing. We'll use connect_work_tree_and_git_dir |
2130 | | * to fixup the submodule in the force case later. |
2131 | | */ |
2132 | 0 | error_code_ptr = &error_code; |
2133 | 0 | else |
2134 | 0 | error_code_ptr = NULL; |
2135 | |
|
2136 | 0 | if (old_head && !is_submodule_populated_gently(path, error_code_ptr)) |
2137 | 0 | return 0; |
2138 | | |
2139 | 0 | sub = submodule_from_path(the_repository, null_oid(the_hash_algo), path); |
2140 | |
|
2141 | 0 | if (!sub) |
2142 | 0 | BUG("could not get submodule information for '%s'", path); |
2143 | | |
2144 | 0 | if (old_head && !(flags & SUBMODULE_MOVE_HEAD_FORCE)) { |
2145 | | /* Check if the submodule has a dirty index. */ |
2146 | 0 | if (submodule_has_dirty_index(sub)) |
2147 | 0 | return error(_("submodule '%s' has dirty index"), path); |
2148 | 0 | } |
2149 | | |
2150 | 0 | if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) { |
2151 | 0 | if (old_head) { |
2152 | 0 | if (!submodule_uses_gitfile(path)) |
2153 | 0 | absorb_git_dir_into_superproject(path, |
2154 | 0 | super_prefix); |
2155 | 0 | else { |
2156 | 0 | char *dotgit = xstrfmt("%s/.git", path); |
2157 | 0 | char *git_dir = xstrdup(read_gitfile(dotgit)); |
2158 | |
|
2159 | 0 | free(dotgit); |
2160 | 0 | if (validate_submodule_git_dir(git_dir, |
2161 | 0 | sub->name) < 0) |
2162 | 0 | die(_("refusing to create/use '%s' in " |
2163 | 0 | "another submodule's git dir. " |
2164 | 0 | "Enabling extensions.submodulePathConfig " |
2165 | 0 | "should fix this."), git_dir); |
2166 | 0 | free(git_dir); |
2167 | 0 | } |
2168 | 0 | } else { |
2169 | 0 | struct strbuf gitdir = STRBUF_INIT; |
2170 | 0 | submodule_name_to_gitdir(&gitdir, the_repository, |
2171 | 0 | sub->name); |
2172 | 0 | connect_work_tree_and_git_dir(path, gitdir.buf, 0); |
2173 | 0 | strbuf_release(&gitdir); |
2174 | | |
2175 | | /* make sure the index is clean as well */ |
2176 | 0 | submodule_reset_index(path, super_prefix); |
2177 | 0 | } |
2178 | | |
2179 | 0 | if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) { |
2180 | 0 | struct strbuf gitdir = STRBUF_INIT; |
2181 | 0 | submodule_name_to_gitdir(&gitdir, the_repository, |
2182 | 0 | sub->name); |
2183 | 0 | connect_work_tree_and_git_dir(path, gitdir.buf, 1); |
2184 | 0 | strbuf_release(&gitdir); |
2185 | 0 | } |
2186 | 0 | } |
2187 | | |
2188 | 0 | prepare_submodule_repo_env(&cp.env); |
2189 | |
|
2190 | 0 | cp.git_cmd = 1; |
2191 | 0 | cp.no_stdin = 1; |
2192 | 0 | cp.dir = path; |
2193 | |
|
2194 | 0 | strvec_pushl(&cp.args, "read-tree", "--recurse-submodules", NULL); |
2195 | 0 | strvec_pushf(&cp.args, "--super-prefix=%s%s/", |
2196 | 0 | (super_prefix ? super_prefix : ""), path); |
2197 | |
|
2198 | 0 | if (flags & SUBMODULE_MOVE_HEAD_DRY_RUN) |
2199 | 0 | strvec_push(&cp.args, "-n"); |
2200 | 0 | else |
2201 | 0 | strvec_push(&cp.args, "-u"); |
2202 | |
|
2203 | 0 | if (flags & SUBMODULE_MOVE_HEAD_FORCE) |
2204 | 0 | strvec_push(&cp.args, "--reset"); |
2205 | 0 | else |
2206 | 0 | strvec_push(&cp.args, "-m"); |
2207 | |
|
2208 | 0 | if (!(flags & SUBMODULE_MOVE_HEAD_FORCE)) |
2209 | 0 | strvec_push(&cp.args, old_head ? old_head : empty_tree_oid_hex(the_repository->hash_algo)); |
2210 | |
|
2211 | 0 | strvec_push(&cp.args, new_head ? new_head : empty_tree_oid_hex(the_repository->hash_algo)); |
2212 | |
|
2213 | 0 | if (run_command(&cp)) { |
2214 | 0 | ret = error(_("Submodule '%s' could not be updated."), path); |
2215 | 0 | goto out; |
2216 | 0 | } |
2217 | | |
2218 | 0 | if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) { |
2219 | 0 | if (new_head) { |
2220 | 0 | child_process_init(&cp); |
2221 | | /* also set the HEAD accordingly */ |
2222 | 0 | cp.git_cmd = 1; |
2223 | 0 | cp.no_stdin = 1; |
2224 | 0 | cp.dir = path; |
2225 | |
|
2226 | 0 | prepare_submodule_repo_env(&cp.env); |
2227 | 0 | strvec_pushl(&cp.args, "update-ref", "HEAD", |
2228 | 0 | "--no-deref", new_head, NULL); |
2229 | |
|
2230 | 0 | if (run_command(&cp)) { |
2231 | 0 | ret = -1; |
2232 | 0 | goto out; |
2233 | 0 | } |
2234 | 0 | } else { |
2235 | 0 | struct strbuf sb = STRBUF_INIT; |
2236 | |
|
2237 | 0 | strbuf_addf(&sb, "%s/.git", path); |
2238 | 0 | unlink_or_warn(sb.buf); |
2239 | 0 | strbuf_release(&sb); |
2240 | |
|
2241 | 0 | if (is_empty_dir(path)) |
2242 | 0 | rmdir_or_warn(path); |
2243 | |
|
2244 | 0 | submodule_unset_core_worktree(sub); |
2245 | 0 | } |
2246 | 0 | } |
2247 | 0 | out: |
2248 | 0 | return ret; |
2249 | 0 | } |
2250 | | |
2251 | | static int check_casefolding_conflict(const char *git_dir, |
2252 | | const char *submodule_name, |
2253 | | const bool suffixes_match) |
2254 | 0 | { |
2255 | 0 | char *p, *modules_dir = xstrdup(git_dir); |
2256 | 0 | struct dirent *de; |
2257 | 0 | DIR *dir = NULL; |
2258 | 0 | int ret = 0; |
2259 | |
|
2260 | 0 | if ((p = find_last_dir_sep(modules_dir))) |
2261 | 0 | *p = '\0'; |
2262 | | |
2263 | | /* No conflict is possible if modules_dir doesn't exist (first clone) */ |
2264 | 0 | if (!is_directory(modules_dir)) |
2265 | 0 | goto cleanup; |
2266 | | |
2267 | 0 | dir = opendir(modules_dir); |
2268 | 0 | if (!dir) { |
2269 | 0 | ret = -1; |
2270 | 0 | goto cleanup; |
2271 | 0 | } |
2272 | | |
2273 | | /* Check for another directory under .git/modules that differs only in case. */ |
2274 | 0 | while ((de = readdir(dir))) { |
2275 | 0 | if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) |
2276 | 0 | continue; |
2277 | | |
2278 | 0 | if ((suffixes_match || is_git_directory(git_dir)) && |
2279 | 0 | !strcasecmp(de->d_name, submodule_name) && |
2280 | 0 | strcmp(de->d_name, submodule_name)) { |
2281 | 0 | ret = -1; /* collision found */ |
2282 | 0 | break; |
2283 | 0 | } |
2284 | 0 | } |
2285 | |
|
2286 | 0 | cleanup: |
2287 | 0 | if (dir) |
2288 | 0 | closedir(dir); |
2289 | 0 | free(modules_dir); |
2290 | 0 | return ret; |
2291 | 0 | } |
2292 | | |
2293 | | struct submodule_from_gitdir_cb { |
2294 | | const char *gitdir; |
2295 | | const char *submodule_name; |
2296 | | bool conflict_found; |
2297 | | }; |
2298 | | |
2299 | | static int find_conflict_by_gitdir_cb(const char *var, const char *value, |
2300 | | const struct config_context *ctx UNUSED, void *data) |
2301 | 0 | { |
2302 | 0 | struct submodule_from_gitdir_cb *cb = data; |
2303 | 0 | const char *submodule_name_start; |
2304 | 0 | size_t submodule_name_len; |
2305 | 0 | const char *suffix = ".gitdir"; |
2306 | 0 | size_t suffix_len = strlen(suffix); |
2307 | |
|
2308 | 0 | if (!skip_prefix(var, "submodule.", &submodule_name_start)) |
2309 | 0 | return 0; |
2310 | | |
2311 | | /* Check if submodule_name_start ends with ".gitdir" */ |
2312 | 0 | submodule_name_len = strlen(submodule_name_start); |
2313 | 0 | if (submodule_name_len < suffix_len || |
2314 | 0 | strcmp(submodule_name_start + submodule_name_len - suffix_len, suffix) != 0) |
2315 | 0 | return 0; /* Does not end with ".gitdir" */ |
2316 | | |
2317 | 0 | submodule_name_len -= suffix_len; |
2318 | | |
2319 | | /* |
2320 | | * A conflict happens if: |
2321 | | * 1. The submodule names are different and |
2322 | | * 2. The gitdir paths resolve to the same absolute path |
2323 | | */ |
2324 | 0 | if (value && strncmp(cb->submodule_name, submodule_name_start, submodule_name_len)) { |
2325 | 0 | char *abs_path_cb = absolute_pathdup(cb->gitdir); |
2326 | 0 | char *abs_path_value = absolute_pathdup(value); |
2327 | |
|
2328 | 0 | cb->conflict_found = !strcmp(abs_path_cb, abs_path_value); |
2329 | |
|
2330 | 0 | free(abs_path_cb); |
2331 | 0 | free(abs_path_value); |
2332 | 0 | } |
2333 | |
|
2334 | 0 | return cb->conflict_found; |
2335 | 0 | } |
2336 | | |
2337 | | static bool submodule_conflicts_with_existing(const char *gitdir, const char *submodule_name) |
2338 | 0 | { |
2339 | 0 | struct submodule_from_gitdir_cb cb = { 0 }; |
2340 | 0 | cb.submodule_name = submodule_name; |
2341 | 0 | cb.gitdir = gitdir; |
2342 | | |
2343 | | /* Find conflicts with existing repo gitdir configs */ |
2344 | 0 | repo_config(the_repository, find_conflict_by_gitdir_cb, &cb); |
2345 | |
|
2346 | 0 | return cb.conflict_found; |
2347 | 0 | } |
2348 | | |
2349 | | /* |
2350 | | * Encoded gitdir validation, only used when extensions.submodulePathConfig is enabled. |
2351 | | * This does not print errors like the non-encoded version, because encoding is supposed |
2352 | | * to mitigate / fix all these. |
2353 | | */ |
2354 | | static int validate_submodule_encoded_git_dir(char *git_dir, const char *submodule_name) |
2355 | 0 | { |
2356 | 0 | const char *modules_marker = "/modules/"; |
2357 | 0 | char *p = git_dir, *last_submodule_name = NULL; |
2358 | 0 | int config_ignorecase = 0; |
2359 | |
|
2360 | 0 | if (!the_repository->repository_format_submodule_path_cfg) |
2361 | 0 | BUG("validate_submodule_encoded_git_dir() must be called with " |
2362 | 0 | "extensions.submodulePathConfig enabled."); |
2363 | | |
2364 | | /* Find the last submodule name in the gitdir path (modules can be nested). */ |
2365 | 0 | while ((p = strstr(p, modules_marker))) { |
2366 | 0 | last_submodule_name = p + strlen(modules_marker); |
2367 | 0 | p++; |
2368 | 0 | } |
2369 | | |
2370 | | /* Prevent the use of '/' in encoded names */ |
2371 | 0 | if (!last_submodule_name || strchr(last_submodule_name, '/')) |
2372 | 0 | return -1; |
2373 | | |
2374 | | /* Prevent conflicts with existing submodule gitdirs */ |
2375 | 0 | if (is_git_directory(git_dir) && |
2376 | 0 | submodule_conflicts_with_existing(git_dir, submodule_name)) |
2377 | 0 | return -1; |
2378 | | |
2379 | | /* Prevent conflicts on case-folding filesystems */ |
2380 | 0 | repo_config_get_bool(the_repository, "core.ignorecase", &config_ignorecase); |
2381 | 0 | if (ignore_case || config_ignorecase) { |
2382 | 0 | bool suffixes_match = !strcmp(last_submodule_name, submodule_name); |
2383 | 0 | return check_casefolding_conflict(git_dir, submodule_name, |
2384 | 0 | suffixes_match); |
2385 | 0 | } |
2386 | | |
2387 | 0 | return 0; |
2388 | 0 | } |
2389 | | |
2390 | | static int validate_submodule_legacy_git_dir(char *git_dir, const char *submodule_name) |
2391 | 0 | { |
2392 | 0 | size_t len = strlen(git_dir), suffix_len = strlen(submodule_name); |
2393 | 0 | char *p; |
2394 | 0 | int ret = 0; |
2395 | |
|
2396 | 0 | if (the_repository->repository_format_submodule_path_cfg) |
2397 | 0 | BUG("validate_submodule_git_dir() must be called with " |
2398 | 0 | "extensions.submodulePathConfig disabled."); |
2399 | | |
2400 | 0 | if (len <= suffix_len || (p = git_dir + len - suffix_len)[-1] != '/' || |
2401 | 0 | strcmp(p, submodule_name)) |
2402 | 0 | BUG("submodule name '%s' not a suffix of git dir '%s'", |
2403 | 0 | submodule_name, git_dir); |
2404 | | |
2405 | | /* |
2406 | | * We prevent the contents of sibling submodules' git directories to |
2407 | | * clash. |
2408 | | * |
2409 | | * Example: having a submodule named `hippo` and another one named |
2410 | | * `hippo/hooks` would result in the git directories |
2411 | | * `.git/modules/hippo/` and `.git/modules/hippo/hooks/`, respectively, |
2412 | | * but the latter directory is already designated to contain the hooks |
2413 | | * of the former. |
2414 | | */ |
2415 | 0 | for (; *p; p++) { |
2416 | 0 | if (is_dir_sep(*p)) { |
2417 | 0 | char c = *p; |
2418 | |
|
2419 | 0 | *p = '\0'; |
2420 | 0 | if (is_git_directory(git_dir)) |
2421 | 0 | ret = -1; |
2422 | 0 | *p = c; |
2423 | |
|
2424 | 0 | if (ret < 0) |
2425 | 0 | return error(_("submodule git dir '%s' is " |
2426 | 0 | "inside git dir '%.*s'"), |
2427 | 0 | git_dir, |
2428 | 0 | (int)(p - git_dir), git_dir); |
2429 | 0 | } |
2430 | 0 | } |
2431 | | |
2432 | 0 | return 0; |
2433 | 0 | } |
2434 | | |
2435 | | int validate_submodule_git_dir(char *git_dir, const char *submodule_name) |
2436 | 0 | { |
2437 | 0 | if (!the_repository->repository_format_submodule_path_cfg) |
2438 | 0 | return validate_submodule_legacy_git_dir(git_dir, submodule_name); |
2439 | | |
2440 | 0 | return validate_submodule_encoded_git_dir(git_dir, submodule_name); |
2441 | 0 | } |
2442 | | |
2443 | | int validate_submodule_path(const char *path) |
2444 | 0 | { |
2445 | 0 | char *p = xstrdup(path); |
2446 | 0 | struct stat st; |
2447 | 0 | int i, ret = 0; |
2448 | 0 | char sep; |
2449 | |
|
2450 | 0 | for (i = 0; !ret && p[i]; i++) { |
2451 | 0 | if (!is_dir_sep(p[i])) |
2452 | 0 | continue; |
2453 | | |
2454 | 0 | sep = p[i]; |
2455 | 0 | p[i] = '\0'; |
2456 | | /* allow missing components, but no symlinks */ |
2457 | 0 | ret = lstat(p, &st) || !S_ISLNK(st.st_mode) ? 0 : -1; |
2458 | 0 | p[i] = sep; |
2459 | 0 | if (ret) |
2460 | 0 | error(_("expected '%.*s' in submodule path '%s' not to " |
2461 | 0 | "be a symbolic link"), i, p, p); |
2462 | 0 | } |
2463 | 0 | if (!lstat(p, &st) && S_ISLNK(st.st_mode)) |
2464 | 0 | ret = error(_("expected submodule path '%s' not to be a " |
2465 | 0 | "symbolic link"), p); |
2466 | 0 | free(p); |
2467 | 0 | return ret; |
2468 | 0 | } |
2469 | | |
2470 | | |
2471 | | /* |
2472 | | * Embeds a single submodules git directory into the superprojects git dir, |
2473 | | * non recursively. |
2474 | | */ |
2475 | | static void relocate_single_git_dir_into_superproject(const char *path, |
2476 | | const char *super_prefix) |
2477 | 0 | { |
2478 | 0 | char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL; |
2479 | 0 | struct strbuf new_gitdir = STRBUF_INIT; |
2480 | 0 | const struct submodule *sub; |
2481 | |
|
2482 | 0 | if (validate_submodule_path(path) < 0) |
2483 | 0 | exit(128); |
2484 | | |
2485 | 0 | if (submodule_uses_worktrees(path)) |
2486 | 0 | die(_("relocate_gitdir for submodule '%s' with " |
2487 | 0 | "more than one worktree not supported"), path); |
2488 | | |
2489 | 0 | old_git_dir = xstrfmt("%s/.git", path); |
2490 | 0 | if (read_gitfile(old_git_dir)) |
2491 | | /* If it is an actual gitfile, it doesn't need migration. */ |
2492 | 0 | return; |
2493 | | |
2494 | 0 | real_old_git_dir = real_pathdup(old_git_dir, 1); |
2495 | |
|
2496 | 0 | sub = submodule_from_path(the_repository, null_oid(the_hash_algo), path); |
2497 | 0 | if (!sub) |
2498 | 0 | die(_("could not lookup name for submodule '%s'"), path); |
2499 | | |
2500 | 0 | submodule_name_to_gitdir(&new_gitdir, the_repository, sub->name); |
2501 | 0 | if (safe_create_leading_directories_const(the_repository, new_gitdir.buf) < 0) |
2502 | 0 | die(_("could not create directory '%s'"), new_gitdir.buf); |
2503 | 0 | real_new_git_dir = real_pathdup(new_gitdir.buf, 1); |
2504 | |
|
2505 | 0 | fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"), |
2506 | 0 | super_prefix ? super_prefix : "", path, |
2507 | 0 | real_old_git_dir, real_new_git_dir); |
2508 | |
|
2509 | 0 | relocate_gitdir(path, real_old_git_dir, real_new_git_dir); |
2510 | |
|
2511 | 0 | free(old_git_dir); |
2512 | 0 | free(real_old_git_dir); |
2513 | 0 | free(real_new_git_dir); |
2514 | 0 | strbuf_release(&new_gitdir); |
2515 | 0 | } |
2516 | | |
2517 | | static void absorb_git_dir_into_superproject_recurse(const char *path, |
2518 | | const char *super_prefix) |
2519 | 0 | { |
2520 | |
|
2521 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
2522 | |
|
2523 | 0 | if (validate_submodule_path(path) < 0) |
2524 | 0 | exit(128); |
2525 | | |
2526 | 0 | cp.dir = path; |
2527 | 0 | cp.git_cmd = 1; |
2528 | 0 | cp.no_stdin = 1; |
2529 | 0 | strvec_pushl(&cp.args, "submodule--helper", |
2530 | 0 | "absorbgitdirs", NULL); |
2531 | 0 | strvec_pushf(&cp.args, "--super-prefix=%s%s/", super_prefix ? |
2532 | 0 | super_prefix : "", path); |
2533 | |
|
2534 | 0 | prepare_submodule_repo_env(&cp.env); |
2535 | 0 | if (run_command(&cp)) |
2536 | 0 | die(_("could not recurse into submodule '%s'"), path); |
2537 | 0 | } |
2538 | | |
2539 | | /* |
2540 | | * Migrate the git directory of the submodule given by path from |
2541 | | * having its git directory within the working tree to the git dir nested |
2542 | | * in its superprojects git dir under modules/. |
2543 | | */ |
2544 | | void absorb_git_dir_into_superproject(const char *path, |
2545 | | const char *super_prefix) |
2546 | 0 | { |
2547 | 0 | int err_code; |
2548 | 0 | const char *sub_git_dir; |
2549 | 0 | struct strbuf gitdir = STRBUF_INIT; |
2550 | |
|
2551 | 0 | if (validate_submodule_path(path) < 0) |
2552 | 0 | exit(128); |
2553 | | |
2554 | 0 | strbuf_addf(&gitdir, "%s/.git", path); |
2555 | 0 | sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code); |
2556 | | |
2557 | | /* Not populated? */ |
2558 | 0 | if (!sub_git_dir) { |
2559 | 0 | const struct submodule *sub; |
2560 | 0 | struct strbuf sub_gitdir = STRBUF_INIT; |
2561 | |
|
2562 | 0 | if (err_code == READ_GITFILE_ERR_STAT_FAILED) { |
2563 | | /* unpopulated as expected */ |
2564 | 0 | strbuf_release(&gitdir); |
2565 | 0 | return; |
2566 | 0 | } |
2567 | | |
2568 | 0 | if (err_code != READ_GITFILE_ERR_NOT_A_REPO) |
2569 | | /* We don't know what broke here. */ |
2570 | 0 | read_gitfile_error_die(err_code, path, NULL); |
2571 | | |
2572 | | /* |
2573 | | * Maybe populated, but no git directory was found? |
2574 | | * This can happen if the superproject is a submodule |
2575 | | * itself and was just absorbed. The absorption of the |
2576 | | * superproject did not rewrite the git file links yet, |
2577 | | * fix it now. |
2578 | | */ |
2579 | 0 | sub = submodule_from_path(the_repository, null_oid(the_hash_algo), path); |
2580 | 0 | if (!sub) |
2581 | 0 | die(_("could not lookup name for submodule '%s'"), path); |
2582 | 0 | submodule_name_to_gitdir(&sub_gitdir, the_repository, sub->name); |
2583 | 0 | connect_work_tree_and_git_dir(path, sub_gitdir.buf, 0); |
2584 | 0 | strbuf_release(&sub_gitdir); |
2585 | 0 | } else { |
2586 | | /* Is it already absorbed into the superprojects git dir? */ |
2587 | 0 | char *real_sub_git_dir = real_pathdup(sub_git_dir, 1); |
2588 | 0 | char *real_common_git_dir = real_pathdup(repo_get_common_dir(the_repository), 1); |
2589 | |
|
2590 | 0 | if (!starts_with(real_sub_git_dir, real_common_git_dir)) |
2591 | 0 | relocate_single_git_dir_into_superproject(path, super_prefix); |
2592 | |
|
2593 | 0 | free(real_sub_git_dir); |
2594 | 0 | free(real_common_git_dir); |
2595 | 0 | } |
2596 | 0 | strbuf_release(&gitdir); |
2597 | |
|
2598 | 0 | absorb_git_dir_into_superproject_recurse(path, super_prefix); |
2599 | 0 | } |
2600 | | |
2601 | | int get_superproject_working_tree(struct strbuf *buf) |
2602 | 0 | { |
2603 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
2604 | 0 | struct strbuf sb = STRBUF_INIT; |
2605 | 0 | struct strbuf one_up = STRBUF_INIT; |
2606 | 0 | char *cwd = xgetcwd(); |
2607 | 0 | int ret = 0; |
2608 | 0 | const char *subpath; |
2609 | 0 | int code; |
2610 | 0 | ssize_t len; |
2611 | |
|
2612 | 0 | if (!is_inside_work_tree()) |
2613 | | /* |
2614 | | * FIXME: |
2615 | | * We might have a superproject, but it is harder |
2616 | | * to determine. |
2617 | | */ |
2618 | 0 | return 0; |
2619 | | |
2620 | 0 | if (!strbuf_realpath(&one_up, "../", 0)) |
2621 | 0 | return 0; |
2622 | | |
2623 | 0 | subpath = relative_path(cwd, one_up.buf, &sb); |
2624 | 0 | strbuf_release(&one_up); |
2625 | |
|
2626 | 0 | prepare_submodule_repo_env(&cp.env); |
2627 | 0 | strvec_pop(&cp.env); |
2628 | |
|
2629 | 0 | strvec_pushl(&cp.args, "--literal-pathspecs", "-C", "..", |
2630 | 0 | "ls-files", "-z", "--stage", "--full-name", "--", |
2631 | 0 | subpath, NULL); |
2632 | 0 | strbuf_reset(&sb); |
2633 | |
|
2634 | 0 | cp.no_stdin = 1; |
2635 | 0 | cp.no_stderr = 1; |
2636 | 0 | cp.out = -1; |
2637 | 0 | cp.git_cmd = 1; |
2638 | |
|
2639 | 0 | if (start_command(&cp)) |
2640 | 0 | die(_("could not start ls-files in ..")); |
2641 | | |
2642 | 0 | len = strbuf_read(&sb, cp.out, PATH_MAX); |
2643 | 0 | close(cp.out); |
2644 | |
|
2645 | 0 | if (starts_with(sb.buf, "160000")) { |
2646 | 0 | int super_sub_len; |
2647 | 0 | int cwd_len = strlen(cwd); |
2648 | 0 | char *super_sub, *super_wt; |
2649 | | |
2650 | | /* |
2651 | | * There is a superproject having this repo as a submodule. |
2652 | | * The format is <mode> SP <hash> SP <stage> TAB <full name> \0, |
2653 | | * We're only interested in the name after the tab. |
2654 | | */ |
2655 | 0 | super_sub = strchr(sb.buf, '\t') + 1; |
2656 | 0 | super_sub_len = strlen(super_sub); |
2657 | |
|
2658 | 0 | if (super_sub_len > cwd_len || |
2659 | 0 | strcmp(&cwd[cwd_len - super_sub_len], super_sub)) |
2660 | 0 | BUG("returned path string doesn't match cwd?"); |
2661 | | |
2662 | 0 | super_wt = xstrdup(cwd); |
2663 | 0 | super_wt[cwd_len - super_sub_len] = '\0'; |
2664 | |
|
2665 | 0 | strbuf_realpath(buf, super_wt, 1); |
2666 | 0 | ret = 1; |
2667 | 0 | free(super_wt); |
2668 | 0 | } |
2669 | 0 | free(cwd); |
2670 | 0 | strbuf_release(&sb); |
2671 | |
|
2672 | 0 | code = finish_command(&cp); |
2673 | |
|
2674 | 0 | if (code == 128) |
2675 | | /* '../' is not a git repository */ |
2676 | 0 | return 0; |
2677 | 0 | if (code == 0 && len == 0) |
2678 | | /* There is an unrelated git repository at '../' */ |
2679 | 0 | return 0; |
2680 | 0 | if (code) |
2681 | 0 | die(_("ls-tree returned unexpected return code %d"), code); |
2682 | | |
2683 | 0 | return ret; |
2684 | 0 | } |
2685 | | |
2686 | | /* |
2687 | | * Put the gitdir for a submodule (given relative to the main |
2688 | | * repository worktree) into `buf`, or return -1 on error. |
2689 | | */ |
2690 | | int submodule_to_gitdir(struct repository *repo, |
2691 | | struct strbuf *buf, const char *submodule) |
2692 | 0 | { |
2693 | 0 | const struct submodule *sub; |
2694 | 0 | const char *git_dir; |
2695 | 0 | int ret = 0; |
2696 | |
|
2697 | 0 | if (validate_submodule_path(submodule) < 0) |
2698 | 0 | exit(128); |
2699 | | |
2700 | 0 | strbuf_reset(buf); |
2701 | 0 | strbuf_addstr(buf, submodule); |
2702 | 0 | strbuf_complete(buf, '/'); |
2703 | 0 | strbuf_addstr(buf, ".git"); |
2704 | |
|
2705 | 0 | git_dir = read_gitfile(buf->buf); |
2706 | 0 | if (git_dir) { |
2707 | 0 | strbuf_reset(buf); |
2708 | 0 | strbuf_addstr(buf, git_dir); |
2709 | 0 | } |
2710 | 0 | if (!is_git_directory(buf->buf)) { |
2711 | 0 | sub = submodule_from_path(repo, null_oid(the_hash_algo), submodule); |
2712 | 0 | if (!sub) { |
2713 | 0 | ret = -1; |
2714 | 0 | goto cleanup; |
2715 | 0 | } |
2716 | 0 | strbuf_reset(buf); |
2717 | 0 | submodule_name_to_gitdir(buf, repo, sub->name); |
2718 | 0 | } |
2719 | | |
2720 | 0 | cleanup: |
2721 | 0 | return ret; |
2722 | 0 | } |
2723 | | |
2724 | | void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r, |
2725 | | const char *submodule_name) |
2726 | 0 | { |
2727 | 0 | if (!r->repository_format_submodule_path_cfg) { |
2728 | | /* |
2729 | | * If extensions.submodulePathConfig is disabled, |
2730 | | * continue to use the plain path. |
2731 | | */ |
2732 | 0 | repo_git_path_append(r, buf, "modules/%s", submodule_name); |
2733 | 0 | } else { |
2734 | 0 | const char *gitdir; |
2735 | 0 | char *key; |
2736 | 0 | int ret; |
2737 | | |
2738 | | /* Otherwise the extension is enabled, so use the gitdir config. */ |
2739 | 0 | key = xstrfmt("submodule.%s.gitdir", submodule_name); |
2740 | 0 | ret = repo_config_get_string_tmp(r, key, &gitdir); |
2741 | 0 | FREE_AND_NULL(key); |
2742 | |
|
2743 | 0 | if (ret) |
2744 | 0 | die(_("the 'submodule.%s.gitdir' config does not exist for module '%s'. " |
2745 | 0 | "Please ensure it is set, for example by running something like: " |
2746 | 0 | "'git config submodule.%s.gitdir .git/modules/%s'. For details " |
2747 | 0 | "see the extensions.submodulePathConfig documentation."), |
2748 | 0 | submodule_name, submodule_name, submodule_name, submodule_name); |
2749 | | |
2750 | 0 | strbuf_addstr(buf, gitdir); |
2751 | 0 | } |
2752 | | |
2753 | | /* validate because users might have modified the config */ |
2754 | 0 | if (validate_submodule_git_dir(buf->buf, submodule_name)) { |
2755 | 0 | advise(_("enabling extensions.submodulePathConfig might fix the " |
2756 | 0 | "following error, if it's not already enabled.")); |
2757 | 0 | die(_("refusing to create/use '%s' in another submodule's " |
2758 | 0 | " git dir."), buf->buf); |
2759 | 0 | } |
2760 | 0 | } |