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