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