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