blob: 1ebda30c5069fd803971954e282cb0c551d05493 [file] [log] [blame]
Brandon Williamse7241972017-12-12 11:53:52 -08001
Johannes Schindelin752c0c22009-10-19 14:38:32 +02002#include "cache.h"
Brandon Williams69aba532017-06-22 11:43:45 -07003#include "repository.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07004#include "config.h"
Heiko Voigt851e18c2015-08-17 17:21:59 -07005#include "submodule-config.h"
Johannes Schindelin752c0c22009-10-19 14:38:32 +02006#include "submodule.h"
7#include "dir.h"
8#include "diff.h"
9#include "commit.h"
10#include "revision.h"
Jens Lehmannee6fc512010-01-16 18:42:24 +010011#include "run-command.h"
Jens Lehmannc7e1a732010-03-04 22:20:33 +010012#include "diffcore.h"
Heiko Voigt68d03e42010-07-07 15:39:13 +020013#include "refs.h"
Jens Lehmannaee9c7d2010-08-06 00:39:25 +020014#include "string-list.h"
Jeff Kingfe299ec2020-03-30 10:03:46 -040015#include "oid-array.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040016#include "strvec.h"
Jens Lehmann5fee9952013-07-30 21:50:34 +020017#include "blob.h"
Stefan Bellerfe85ee62015-12-15 16:04:11 -080018#include "thread-utils.h"
Jeff King46387282016-04-28 09:38:20 -040019#include "quote.h"
Brandon Williams06bf4ad2017-04-05 10:47:19 -070020#include "remote.h"
Stefan Bellerf6f85862016-12-12 11:04:35 -080021#include "worktree.h"
Stefan Beller046b4822017-05-31 17:30:47 -070022#include "parse-options.h"
Stefan Beller0d4a1322018-03-23 18:20:56 +010023#include "object-store.h"
Derrick Stolee64043552018-07-20 16:33:04 +000024#include "commit-reach.h"
Jonathan Tan2a69ff02022-03-17 11:24:47 -070025#include "shallow.h"
Jens Lehmannaee9c7d2010-08-06 00:39:25 +020026
Stefan Bellerd7a38032017-05-26 12:10:12 -070027static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
Jeff King6859de42011-09-12 15:56:52 -040028static int initialized_fetch_ref_tips;
brian m. carlson910650d2017-03-31 01:40:00 +000029static struct oid_array ref_tips_before_fetch;
30static struct oid_array ref_tips_after_fetch;
Jeff King6859de42011-09-12 15:56:52 -040031
Jens Lehmannd4e98b52011-05-14 18:26:58 +020032/*
Brandon Williams34e2ba02017-08-02 12:49:21 -070033 * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file
34 * will be disabled because we can't guess what might be configured in
35 * .gitmodules unless the user resolves the conflict.
Jens Lehmannd4e98b52011-05-14 18:26:58 +020036 */
Derrick Stolee847a9e52021-04-01 01:49:39 +000037int is_gitmodules_unmerged(struct index_state *istate)
Brandon Williams34e2ba02017-08-02 12:49:21 -070038{
39 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
40 if (pos < 0) { /* .gitmodules not found or isn't merged */
41 pos = -1 - pos;
42 if (istate->cache_nr > pos) { /* there is a .gitmodules */
43 const struct cache_entry *ce = istate->cache[pos];
44 if (ce_namelen(ce) == strlen(GITMODULES_FILE) &&
45 !strcmp(ce->name, GITMODULES_FILE))
46 return 1;
47 }
48 }
49
50 return 0;
51}
Johannes Schindelin752c0c22009-10-19 14:38:32 +020052
Jens Lehmann5fee9952013-07-30 21:50:34 +020053/*
Antonio Ospiteb5c259f2018-10-05 15:05:59 +020054 * Check if the .gitmodules file is safe to write.
55 *
56 * Writing to the .gitmodules file requires that the file exists in the
57 * working tree or, if it doesn't, that a brand new .gitmodules file is going
58 * to be created (i.e. it's neither in the index nor in the current branch).
59 *
60 * It is not safe to write to .gitmodules if it's not in the working tree but
61 * it is in the index or in the current branch, because writing new values
62 * (and staging them) would blindly overwrite ALL the old content.
63 */
64int is_writing_gitmodules_ok(void)
65{
66 struct object_id oid;
67 return file_exists(GITMODULES_FILE) ||
68 (get_oid(GITMODULES_INDEX, &oid) < 0 && get_oid(GITMODULES_HEAD, &oid) < 0);
69}
70
71/*
Brandon Williams91b83482017-08-02 12:49:20 -070072 * Check if the .gitmodules file has unstaged modifications. This must be
73 * checked before allowing modifications to the .gitmodules file with the
74 * intention to stage them later, because when continuing we would stage the
75 * modifications the user didn't stage herself too. That might change in a
76 * future version when we learn to stage the changes we do ourselves without
77 * staging any previous modifications.
Jens Lehmann5fee9952013-07-30 21:50:34 +020078 */
Brandon Williams7da9aba2017-12-12 11:53:51 -080079int is_staging_gitmodules_ok(struct index_state *istate)
Jens Lehmann5fee9952013-07-30 21:50:34 +020080{
Brandon Williams91b83482017-08-02 12:49:20 -070081 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
82
83 if ((pos >= 0) && (pos < istate->cache_nr)) {
84 struct stat st;
85 if (lstat(GITMODULES_FILE, &st) == 0 &&
David Turner7edee322020-01-27 13:58:56 -050086 ie_modified(istate, istate->cache[pos], &st, 0) & DATA_CHANGED)
Brandon Williams91b83482017-08-02 12:49:20 -070087 return 0;
88 }
89
90 return 1;
Jens Lehmann5fee9952013-07-30 21:50:34 +020091}
92
Nguyễn Thái Ngọc Duy2e2d4042017-08-23 19:36:57 +070093static int for_each_remote_ref_submodule(const char *submodule,
94 each_ref_fn fn, void *cb_data)
95{
96 return refs_for_each_remote_ref(get_submodule_ref_store(submodule),
97 fn, cb_data);
98}
99
Jens Lehmann06567812013-08-06 21:15:11 +0200100/*
101 * Try to update the "path" entry in the "submodule.<name>" section of the
102 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
103 * with the correct path=<oldpath> setting was found and we could update it.
104 */
105int update_path_in_gitmodules(const char *oldpath, const char *newpath)
106{
107 struct strbuf entry = STRBUF_INIT;
Heiko Voigt851e18c2015-08-17 17:21:59 -0700108 const struct submodule *submodule;
Antonio Ospite45f5ef32018-10-05 15:05:53 +0200109 int ret;
Jens Lehmann06567812013-08-06 21:15:11 +0200110
Brandon Williams4c0eeaf2017-08-02 12:49:16 -0700111 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
Jens Lehmann06567812013-08-06 21:15:11 +0200112 return -1;
113
Nguyễn Thái Ngọc Duy68f08b42018-08-13 18:14:31 +0200114 if (is_gitmodules_unmerged(the_repository->index))
Jens Lehmann06567812013-08-06 21:15:11 +0200115 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
116
brian m. carlson14228442021-04-26 01:02:56 +0000117 submodule = submodule_from_path(the_repository, null_oid(), oldpath);
Heiko Voigt851e18c2015-08-17 17:21:59 -0700118 if (!submodule || !submodule->name) {
Jens Lehmann06567812013-08-06 21:15:11 +0200119 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
120 return -1;
121 }
122 strbuf_addstr(&entry, "submodule.");
Heiko Voigt851e18c2015-08-17 17:21:59 -0700123 strbuf_addstr(&entry, submodule->name);
Jens Lehmann06567812013-08-06 21:15:11 +0200124 strbuf_addstr(&entry, ".path");
Antonio Ospite45f5ef32018-10-05 15:05:53 +0200125 ret = config_set_in_gitmodules_file_gently(entry.buf, newpath);
Jens Lehmann06567812013-08-06 21:15:11 +0200126 strbuf_release(&entry);
Antonio Ospite45f5ef32018-10-05 15:05:53 +0200127 return ret;
Jens Lehmann06567812013-08-06 21:15:11 +0200128}
129
Jens Lehmann95c16412013-08-06 21:15:25 +0200130/*
131 * Try to remove the "submodule.<name>" section from .gitmodules where the given
132 * path is configured. Return 0 only if a .gitmodules file was found, a section
133 * with the correct path=<path> setting was found and we could remove it.
134 */
135int remove_path_from_gitmodules(const char *path)
136{
137 struct strbuf sect = STRBUF_INIT;
Heiko Voigt851e18c2015-08-17 17:21:59 -0700138 const struct submodule *submodule;
Jens Lehmann95c16412013-08-06 21:15:25 +0200139
Brandon Williams4c0eeaf2017-08-02 12:49:16 -0700140 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
Jens Lehmann95c16412013-08-06 21:15:25 +0200141 return -1;
142
Nguyễn Thái Ngọc Duy68f08b42018-08-13 18:14:31 +0200143 if (is_gitmodules_unmerged(the_repository->index))
Jens Lehmann95c16412013-08-06 21:15:25 +0200144 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
145
brian m. carlson14228442021-04-26 01:02:56 +0000146 submodule = submodule_from_path(the_repository, null_oid(), path);
Heiko Voigt851e18c2015-08-17 17:21:59 -0700147 if (!submodule || !submodule->name) {
Jens Lehmann95c16412013-08-06 21:15:25 +0200148 warning(_("Could not find section in .gitmodules where path=%s"), path);
149 return -1;
150 }
151 strbuf_addstr(&sect, "submodule.");
Heiko Voigt851e18c2015-08-17 17:21:59 -0700152 strbuf_addstr(&sect, submodule->name);
Brandon Williams4c0eeaf2017-08-02 12:49:16 -0700153 if (git_config_rename_section_in_file(GITMODULES_FILE, sect.buf, NULL) < 0) {
Jens Lehmann95c16412013-08-06 21:15:25 +0200154 /* Maybe the user already did that, don't error out here */
155 warning(_("Could not remove .gitmodules entry for %s"), path);
156 strbuf_release(&sect);
157 return -1;
158 }
159 strbuf_release(&sect);
160 return 0;
161}
162
Brandon Williams3b8317a2017-12-12 11:53:50 -0800163void stage_updated_gitmodules(struct index_state *istate)
Jens Lehmann5fee9952013-07-30 21:50:34 +0200164{
Brandon Williams3b8317a2017-12-12 11:53:50 -0800165 if (add_file_to_index(istate, GITMODULES_FILE, 0))
Jens Lehmann5fee9952013-07-30 21:50:34 +0200166 die(_("staging updated .gitmodules failed"));
167}
168
Jonathan Tana35e03d2021-08-16 14:09:51 -0700169static struct string_list added_submodule_odb_paths = STRING_LIST_INIT_NODUP;
170
Jonathan Tan8d33c3a2021-08-16 14:09:52 -0700171void add_submodule_odb_by_path(const char *path)
172{
173 string_list_insert(&added_submodule_odb_paths, xstrdup(path));
174}
175
Jonathan Tana35e03d2021-08-16 14:09:51 -0700176int register_all_submodule_odb_as_alternates(void)
177{
178 int i;
179 int ret = added_submodule_odb_paths.nr;
180
181 for (i = 0; i < added_submodule_odb_paths.nr; i++)
182 add_to_alternates_memory(added_submodule_odb_paths.items[i].string);
183 if (ret) {
184 string_list_clear(&added_submodule_odb_paths, 0);
Jonathan Tan71ef66d2021-10-08 14:08:20 -0700185 trace2_data_intmax("submodule", the_repository,
186 "register_all_submodule_odb_as_alternates/registered", ret);
Jonathan Tana35e03d2021-08-16 14:09:51 -0700187 if (git_env_bool("GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB", 0))
188 BUG("register_all_submodule_odb_as_alternates() called");
189 }
190 return ret;
191}
192
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200193void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
194 const char *path)
195{
Stefan Beller3b8fb392018-03-28 15:35:29 -0700196 const struct submodule *submodule = submodule_from_path(the_repository,
brian m. carlson14228442021-04-26 01:02:56 +0000197 null_oid(),
198 path);
Heiko Voigt851e18c2015-08-17 17:21:59 -0700199 if (submodule) {
Brandon Williamsfdfa9e92017-08-03 11:19:52 -0700200 const char *ignore;
201 char *key;
202
203 key = xstrfmt("submodule.%s.ignore", submodule->name);
Jeff Kingf1de9812020-08-14 12:17:36 -0400204 if (repo_config_get_string_tmp(the_repository, key, &ignore))
Brandon Williamsfdfa9e92017-08-03 11:19:52 -0700205 ignore = submodule->ignore;
206 free(key);
207
208 if (ignore)
209 handle_ignore_submodules_arg(diffopt, ignore);
Nguyễn Thái Ngọc Duy68f08b42018-08-13 18:14:31 +0200210 else if (is_gitmodules_unmerged(the_repository->index))
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700211 diffopt->flags.ignore_submodules = 1;
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200212 }
213}
214
Stefan Beller046b4822017-05-31 17:30:47 -0700215/* Cheap function that only determines if we're interested in submodules at all */
216int git_default_submodule_config(const char *var, const char *value, void *cb)
217{
218 if (!strcmp(var, "submodule.recurse")) {
219 int v = git_config_bool(var, value) ?
220 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
221 config_update_recurse_submodules = v;
222 }
223 return 0;
Stefan Beller1d789d02017-05-26 12:10:13 -0700224}
225
Stefan Bellerd7a38032017-05-26 12:10:12 -0700226int option_parse_recurse_submodules_worktree_updater(const struct option *opt,
227 const char *arg, int unset)
228{
229 if (unset) {
230 config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
231 return 0;
232 }
233 if (arg)
234 config_update_recurse_submodules =
235 parse_update_recurse_submodules_arg(opt->long_name,
236 arg);
237 else
238 config_update_recurse_submodules = RECURSE_SUBMODULES_ON;
239
240 return 0;
241}
242
Brandon Williams5688c282016-12-16 11:03:16 -0800243/*
Brandon Williamsf9f42562016-12-16 11:03:17 -0800244 * Determine if a submodule has been initialized at a given 'path'
245 */
Atharva Raykara4521282021-08-06 19:34:31 +0530246/*
247 * NEEDSWORK: Emit a warning if submodule.active exists, but is valueless,
248 * ie, the config looks like: "[submodule] active\n".
249 * Since that is an invalid pathspec, we should inform the user.
250 */
Glen Choo961b1302022-01-28 16:04:45 -0800251int is_tree_submodule_active(struct repository *repo,
252 const struct object_id *treeish_name,
253 const char *path)
Brandon Williamsf9f42562016-12-16 11:03:17 -0800254{
255 int ret = 0;
Brandon Williamsa086f922017-03-17 15:38:01 -0700256 char *key = NULL;
257 char *value = NULL;
258 const struct string_list *sl;
Brandon Williams627d9342017-06-22 11:43:46 -0700259 const struct submodule *module;
260
Glen Choo961b1302022-01-28 16:04:45 -0800261 module = submodule_from_path(repo, treeish_name, path);
Brandon Williamsf9f42562016-12-16 11:03:17 -0800262
Brandon Williamsa086f922017-03-17 15:38:01 -0700263 /* early return if there isn't a path->module mapping */
264 if (!module)
265 return 0;
Brandon Williamsf9f42562016-12-16 11:03:17 -0800266
Brandon Williamsa086f922017-03-17 15:38:01 -0700267 /* submodule.<name>.active is set */
268 key = xstrfmt("submodule.%s.active", module->name);
Brandon Williams627d9342017-06-22 11:43:46 -0700269 if (!repo_config_get_bool(repo, key, &ret)) {
Brandon Williamsf9f42562016-12-16 11:03:17 -0800270 free(key);
Brandon Williamsa086f922017-03-17 15:38:01 -0700271 return ret;
272 }
273 free(key);
274
275 /* submodule.active is set */
Brandon Williams627d9342017-06-22 11:43:46 -0700276 sl = repo_config_get_value_multi(repo, "submodule.active");
Brandon Williamsa086f922017-03-17 15:38:01 -0700277 if (sl) {
278 struct pathspec ps;
Jeff Kingc972bf42020-07-28 16:25:12 -0400279 struct strvec args = STRVEC_INIT;
Brandon Williamsa086f922017-03-17 15:38:01 -0700280 const struct string_list_item *item;
281
282 for_each_string_list_item(item, sl) {
Jeff Kingc972bf42020-07-28 16:25:12 -0400283 strvec_push(&args, item->string);
Brandon Williamsa086f922017-03-17 15:38:01 -0700284 }
285
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400286 parse_pathspec(&ps, 0, 0, NULL, args.v);
Nguyễn Thái Ngọc Duy68f08b42018-08-13 18:14:31 +0200287 ret = match_pathspec(repo->index, &ps, path, strlen(path), 0, NULL, 1);
Brandon Williamsa086f922017-03-17 15:38:01 -0700288
Jeff Kingc972bf42020-07-28 16:25:12 -0400289 strvec_clear(&args);
Brandon Williamsa086f922017-03-17 15:38:01 -0700290 clear_pathspec(&ps);
291 return ret;
Brandon Williamsf9f42562016-12-16 11:03:17 -0800292 }
293
Brandon Williamsa086f922017-03-17 15:38:01 -0700294 /* fallback to checking if the URL is set */
295 key = xstrfmt("submodule.%s.url", module->name);
Brandon Williams627d9342017-06-22 11:43:46 -0700296 ret = !repo_config_get_string(repo, key, &value);
Brandon Williamsa086f922017-03-17 15:38:01 -0700297
298 free(value);
299 free(key);
Brandon Williamsf9f42562016-12-16 11:03:17 -0800300 return ret;
301}
302
Glen Choo961b1302022-01-28 16:04:45 -0800303int is_submodule_active(struct repository *repo, const char *path)
304{
305 return is_tree_submodule_active(repo, null_oid(), path);
306}
307
Stefan Beller15cdc642017-03-14 14:46:31 -0700308int is_submodule_populated_gently(const char *path, int *return_error_code)
Brandon Williams5688c282016-12-16 11:03:16 -0800309{
310 int ret = 0;
311 char *gitdir = xstrfmt("%s/.git", path);
312
Stefan Beller15cdc642017-03-14 14:46:31 -0700313 if (resolve_gitdir_gently(gitdir, return_error_code))
Brandon Williams5688c282016-12-16 11:03:16 -0800314 ret = 1;
315
316 free(gitdir);
317 return ret;
318}
319
Brandon Williamsbdab9722017-05-09 12:17:59 -0700320/*
321 * Dies if the provided 'prefix' corresponds to an unpopulated submodule
322 */
Derrick Stolee847a9e52021-04-01 01:49:39 +0000323void die_in_unpopulated_submodule(struct index_state *istate,
Brandon Williamsbdab9722017-05-09 12:17:59 -0700324 const char *prefix)
325{
326 int i, prefixlen;
327
328 if (!prefix)
329 return;
330
331 prefixlen = strlen(prefix);
332
333 for (i = 0; i < istate->cache_nr; i++) {
334 struct cache_entry *ce = istate->cache[i];
335 int ce_len = ce_namelen(ce);
336
337 if (!S_ISGITLINK(ce->ce_mode))
338 continue;
339 if (prefixlen <= ce_len)
340 continue;
341 if (strncmp(ce->name, prefix, ce_len))
342 continue;
343 if (prefix[ce_len] != '/')
344 continue;
345
346 die(_("in unpopulated submodule '%s'"), ce->name);
347 }
348}
349
Brandon Williamsc08397e2017-05-11 15:04:24 -0700350/*
351 * Dies if any paths in the provided pathspec descends into a submodule
352 */
Derrick Stolee847a9e52021-04-01 01:49:39 +0000353void die_path_inside_submodule(struct index_state *istate,
Brandon Williamsc08397e2017-05-11 15:04:24 -0700354 const struct pathspec *ps)
355{
356 int i, j;
357
358 for (i = 0; i < istate->cache_nr; i++) {
359 struct cache_entry *ce = istate->cache[i];
360 int ce_len = ce_namelen(ce);
361
362 if (!S_ISGITLINK(ce->ce_mode))
363 continue;
364
365 for (j = 0; j < ps->nr ; j++) {
366 const struct pathspec_item *item = &ps->items[j];
367
368 if (item->len <= ce_len)
369 continue;
370 if (item->match[ce_len] != '/')
371 continue;
372 if (strncmp(ce->name, item->match, ce_len))
373 continue;
374 if (item->len == ce_len + 1)
375 continue;
376
377 die(_("Pathspec '%s' is in submodule '%.*s'"),
378 item->original, ce_len, ce->name);
379 }
380 }
381}
382
Brandon Williamsec6141a2017-08-03 11:19:50 -0700383enum submodule_update_type parse_submodule_update_type(const char *value)
384{
385 if (!strcmp(value, "none"))
386 return SM_UPDATE_NONE;
387 else if (!strcmp(value, "checkout"))
388 return SM_UPDATE_CHECKOUT;
389 else if (!strcmp(value, "rebase"))
390 return SM_UPDATE_REBASE;
391 else if (!strcmp(value, "merge"))
392 return SM_UPDATE_MERGE;
393 else if (*value == '!')
394 return SM_UPDATE_COMMAND;
395 else
396 return SM_UPDATE_UNSPECIFIED;
397}
398
Stefan Bellerea2fa5a2016-02-29 18:07:11 -0800399int parse_submodule_update_strategy(const char *value,
400 struct submodule_update_strategy *dst)
401{
Brandon Williamsec6141a2017-08-03 11:19:50 -0700402 enum submodule_update_type type;
403
Stefan Bellerea2fa5a2016-02-29 18:07:11 -0800404 free((void*)dst->command);
405 dst->command = NULL;
Brandon Williamsec6141a2017-08-03 11:19:50 -0700406
407 type = parse_submodule_update_type(value);
408 if (type == SM_UPDATE_UNSPECIFIED)
Stefan Bellerea2fa5a2016-02-29 18:07:11 -0800409 return -1;
Brandon Williamsec6141a2017-08-03 11:19:50 -0700410
411 dst->type = type;
412 if (type == SM_UPDATE_COMMAND)
413 dst->command = xstrdup(value + 1);
414
Stefan Bellerea2fa5a2016-02-29 18:07:11 -0800415 return 0;
416}
417
Ævar Arnfjörð Bjarmasonb9dd63f2022-09-01 01:18:05 +0200418const char *submodule_update_type_to_string(enum submodule_update_type type)
Stefan Beller36042422016-04-15 17:50:13 -0700419{
Ævar Arnfjörð Bjarmasonb9dd63f2022-09-01 01:18:05 +0200420 switch (type) {
Stefan Beller36042422016-04-15 17:50:13 -0700421 case SM_UPDATE_CHECKOUT:
422 return "checkout";
423 case SM_UPDATE_MERGE:
424 return "merge";
425 case SM_UPDATE_REBASE:
426 return "rebase";
427 case SM_UPDATE_NONE:
428 return "none";
429 case SM_UPDATE_UNSPECIFIED:
Stefan Beller36042422016-04-15 17:50:13 -0700430 case SM_UPDATE_COMMAND:
Ævar Arnfjörð Bjarmasonb9dd63f2022-09-01 01:18:05 +0200431 BUG("init_submodule() should handle type %d", type);
432 default:
433 BUG("unexpected update strategy type: %d", type);
Stefan Beller36042422016-04-15 17:50:13 -0700434 }
Stefan Beller36042422016-04-15 17:50:13 -0700435}
436
Jens Lehmann46a958b2010-06-25 16:56:47 +0200437void handle_ignore_submodules_arg(struct diff_options *diffopt,
438 const char *arg)
439{
Sangeeta Jain8ef93122020-11-10 14:09:00 +0530440 diffopt->flags.ignore_submodule_set = 1;
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700441 diffopt->flags.ignore_submodules = 0;
442 diffopt->flags.ignore_untracked_in_submodules = 0;
443 diffopt->flags.ignore_dirty_submodules = 0;
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +0200444
Jens Lehmann46a958b2010-06-25 16:56:47 +0200445 if (!strcmp(arg, "all"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700446 diffopt->flags.ignore_submodules = 1;
Jens Lehmann46a958b2010-06-25 16:56:47 +0200447 else if (!strcmp(arg, "untracked"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700448 diffopt->flags.ignore_untracked_in_submodules = 1;
Jens Lehmann46a958b2010-06-25 16:56:47 +0200449 else if (!strcmp(arg, "dirty"))
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700450 diffopt->flags.ignore_dirty_submodules = 1;
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200451 else if (strcmp(arg, "none"))
Ralf Thielowa4ffbbb2020-01-15 19:07:01 +0100452 die(_("bad --ignore-submodules argument: %s"), arg);
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700453 /*
454 * Please update _git_status() in git-completion.bash when you
455 * add new options
456 */
Jens Lehmann46a958b2010-06-25 16:56:47 +0200457}
458
Junio C Hamano4831c232020-09-18 17:58:05 -0700459static int prepare_submodule_diff_summary(struct repository *r, struct rev_info *rev,
460 const char *path,
461 struct commit *left, struct commit *right,
462 struct commit_list *merge_bases)
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500463{
Jacob Keller8e6df652016-08-31 16:27:24 -0700464 struct commit_list *list;
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500465
Michael Forney85a1ec22020-06-23 13:56:59 -0700466 repo_init_revisions(r, rev, NULL);
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500467 setup_revisions(0, NULL, rev, NULL);
468 rev->left_right = 1;
469 rev->first_parent_only = 1;
470 left->object.flags |= SYMMETRIC_LEFT;
471 add_pending_object(rev, &left->object, path);
472 add_pending_object(rev, &right->object, path);
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500473 for (list = merge_bases; list; list = list->next) {
474 list->item->object.flags |= UNINTERESTING;
475 add_pending_object(rev, &list->item->object,
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000476 oid_to_hex(&list->item->object.oid));
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500477 }
478 return prepare_revision_walk(rev);
479}
480
Shourya Shukla180b1542020-08-13 01:14:02 +0530481static void print_submodule_diff_summary(struct repository *r, struct rev_info *rev, struct diff_options *o)
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500482{
483 static const char format[] = " %m %s";
484 struct strbuf sb = STRBUF_INIT;
485 struct commit *commit;
486
487 while ((commit = get_revision(rev))) {
488 struct pretty_print_context ctx = {0};
489 ctx.date_mode = rev->date_mode;
Alexey Shumkinecaee802013-06-26 14:19:50 +0400490 ctx.output_encoding = get_log_output_encoding();
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500491 strbuf_setlen(&sb, 0);
Stefan Beller605f0ec2018-12-14 16:09:37 -0800492 repo_format_commit_message(r, commit, format, &sb,
493 &ctx);
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500494 strbuf_addch(&sb, '\n');
Stefan Bellerf3597132017-06-29 17:07:00 -0700495 if (commit->object.flags & SYMMETRIC_LEFT)
496 diff_emit_submodule_del(o, sb.buf);
497 else
498 diff_emit_submodule_add(o, sb.buf);
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500499 }
500 strbuf_release(&sb);
501}
502
Jeff Kingc972bf42020-07-28 16:25:12 -0400503void prepare_submodule_repo_env(struct strvec *out)
Stefan Beller6cd57572017-03-14 14:46:35 -0700504{
Jonathan Tand1fa9432021-06-17 10:13:25 -0700505 prepare_other_repo_env(out, DEFAULT_GIT_DIR_ENVIRONMENT);
Stefan Beller6cd57572017-03-14 14:46:35 -0700506}
507
Jeff Kingc972bf42020-07-28 16:25:12 -0400508static void prepare_submodule_repo_env_in_gitdir(struct strvec *out)
Stefan Bellera62387b2018-11-28 16:27:55 -0800509{
Jonathan Tand1fa9432021-06-17 10:13:25 -0700510 prepare_other_repo_env(out, ".");
Stefan Bellera62387b2018-11-28 16:27:55 -0800511}
512
Stefan Beller605f0ec2018-12-14 16:09:37 -0800513/*
514 * Initialize a repository struct for a submodule based on the provided 'path'.
515 *
Stefan Beller605f0ec2018-12-14 16:09:37 -0800516 * Returns the repository struct on success,
517 * NULL when the submodule is not present.
Jacob Keller8e6df652016-08-31 16:27:24 -0700518 */
Stefan Beller605f0ec2018-12-14 16:09:37 -0800519static struct repository *open_submodule(const char *path)
520{
521 struct strbuf sb = STRBUF_INIT;
522 struct repository *out = xmalloc(sizeof(*out));
523
524 if (submodule_to_gitdir(&sb, path) || repo_init(out, sb.buf, NULL)) {
525 strbuf_release(&sb);
526 free(out);
527 return NULL;
528 }
529
530 /* Mark it as a submodule */
531 out->submodule_prefix = xstrdup(path);
532
533 strbuf_release(&sb);
534 return out;
535}
536
537/*
538 * Helper function to display the submodule header line prior to the full
539 * summary output.
540 *
541 * If it can locate the submodule git directory it will create a repository
542 * handle for the submodule and lookup both the left and right commits and
543 * put them into the left and right pointers.
544 */
545static void show_submodule_header(struct diff_options *o,
546 const char *path,
Jacob Keller602a2832016-08-31 16:27:23 -0700547 struct object_id *one, struct object_id *two,
Stefan Bellerf3597132017-06-29 17:07:00 -0700548 unsigned dirty_submodule,
Stefan Beller605f0ec2018-12-14 16:09:37 -0800549 struct repository *sub,
Jacob Keller8e6df652016-08-31 16:27:24 -0700550 struct commit **left, struct commit **right,
551 struct commit_list **merge_bases)
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200552{
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200553 const char *message = NULL;
554 struct strbuf sb = STRBUF_INIT;
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200555 int fast_forward = 0, fast_backward = 0;
556
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100557 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
Stefan Bellerf3597132017-06-29 17:07:00 -0700558 diff_emit_submodule_untracked(o, path);
559
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100560 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
Stefan Bellerf3597132017-06-29 17:07:00 -0700561 diff_emit_submodule_modified(o, path);
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100562
Jacob Keller8e6df652016-08-31 16:27:24 -0700563 if (is_null_oid(one))
564 message = "(new submodule)";
565 else if (is_null_oid(two))
566 message = "(submodule deleted)";
567
Stefan Beller605f0ec2018-12-14 16:09:37 -0800568 if (!sub) {
Jacob Keller8e6df652016-08-31 16:27:24 -0700569 if (!message)
Stefan Beller2d94dd22017-09-26 11:27:56 -0700570 message = "(commits not present)";
Jacob Keller8e6df652016-08-31 16:27:24 -0700571 goto output_header;
572 }
573
574 /*
575 * Attempt to lookup the commit references, and determine if this is
576 * a fast forward or fast backwards update.
577 */
Stefan Beller605f0ec2018-12-14 16:09:37 -0800578 *left = lookup_commit_reference(sub, one);
579 *right = lookup_commit_reference(sub, two);
Jacob Keller8e6df652016-08-31 16:27:24 -0700580
581 /*
582 * Warn about missing commits in the submodule project, but only if
583 * they aren't null.
584 */
585 if ((!is_null_oid(one) && !*left) ||
586 (!is_null_oid(two) && !*right))
587 message = "(commits not present)";
588
Stefan Beller605f0ec2018-12-14 16:09:37 -0800589 *merge_bases = repo_get_merge_bases(sub, *left, *right);
Jacob Keller8e6df652016-08-31 16:27:24 -0700590 if (*merge_bases) {
591 if ((*merge_bases)->item == *left)
592 fast_forward = 1;
593 else if ((*merge_bases)->item == *right)
594 fast_backward = 1;
595 }
596
Jeff King4a7e27e2018-08-28 17:22:40 -0400597 if (oideq(one, two)) {
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100598 strbuf_release(&sb);
599 return;
600 }
601
Jacob Keller8e6df652016-08-31 16:27:24 -0700602output_header:
Stefan Bellerf3597132017-06-29 17:07:00 -0700603 strbuf_addf(&sb, "Submodule %s ", path);
brian m. carlson30e677e2018-03-12 02:27:28 +0000604 strbuf_add_unique_abbrev(&sb, one, DEFAULT_ABBREV);
René Scharfea94bb682016-10-08 17:38:47 +0200605 strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
brian m. carlson30e677e2018-03-12 02:27:28 +0000606 strbuf_add_unique_abbrev(&sb, two, DEFAULT_ABBREV);
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200607 if (message)
Stefan Bellerf3597132017-06-29 17:07:00 -0700608 strbuf_addf(&sb, " %s\n", message);
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200609 else
Stefan Bellerf3597132017-06-29 17:07:00 -0700610 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
611 diff_emit_submodule_header(o, sb.buf);
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200612
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200613 strbuf_release(&sb);
614}
Jens Lehmannee6fc512010-01-16 18:42:24 +0100615
Shourya Shukla180b1542020-08-13 01:14:02 +0530616void show_submodule_diff_summary(struct diff_options *o, const char *path,
Jacob Keller8e6df652016-08-31 16:27:24 -0700617 struct object_id *one, struct object_id *two,
Stefan Bellerf3597132017-06-29 17:07:00 -0700618 unsigned dirty_submodule)
Jacob Keller8e6df652016-08-31 16:27:24 -0700619{
Ævar Arnfjörð Bjarmasonf196c1e2022-04-13 22:01:38 +0200620 struct rev_info rev = REV_INFO_INIT;
Jacob Keller8e6df652016-08-31 16:27:24 -0700621 struct commit *left = NULL, *right = NULL;
622 struct commit_list *merge_bases = NULL;
Stefan Beller605f0ec2018-12-14 16:09:37 -0800623 struct repository *sub;
Jacob Keller8e6df652016-08-31 16:27:24 -0700624
Stefan Beller605f0ec2018-12-14 16:09:37 -0800625 sub = open_submodule(path);
Stefan Bellerf3597132017-06-29 17:07:00 -0700626 show_submodule_header(o, path, one, two, dirty_submodule,
Stefan Beller605f0ec2018-12-14 16:09:37 -0800627 sub, &left, &right, &merge_bases);
Jacob Keller8e6df652016-08-31 16:27:24 -0700628
629 /*
630 * If we don't have both a left and a right pointer, there is no
631 * reason to try and display a summary. The header line should contain
632 * all the information the user needs.
633 */
Stefan Beller605f0ec2018-12-14 16:09:37 -0800634 if (!left || !right || !sub)
Jacob Keller8e6df652016-08-31 16:27:24 -0700635 goto out;
636
637 /* Treat revision walker failure the same as missing commits */
Junio C Hamano4831c232020-09-18 17:58:05 -0700638 if (prepare_submodule_diff_summary(sub, &rev, path, left, right, merge_bases)) {
Stefan Bellerf3597132017-06-29 17:07:00 -0700639 diff_emit_submodule_error(o, "(revision walker failed)\n");
Jacob Keller8e6df652016-08-31 16:27:24 -0700640 goto out;
641 }
642
Shourya Shukla180b1542020-08-13 01:14:02 +0530643 print_submodule_diff_summary(sub, &rev, o);
Jacob Keller8e6df652016-08-31 16:27:24 -0700644
645out:
Ævar Arnfjörð Bjarmasonbf20fe42022-04-13 22:01:34 +0200646 free_commit_list(merge_bases);
Ævar Arnfjörð Bjarmasonf196c1e2022-04-13 22:01:38 +0200647 release_revisions(&rev);
Jacob Keller8e6df652016-08-31 16:27:24 -0700648 clear_commit_marks(left, ~0);
649 clear_commit_marks(right, ~0);
Stefan Beller605f0ec2018-12-14 16:09:37 -0800650 if (sub) {
651 repo_clear(sub);
652 free(sub);
653 }
Jacob Keller8e6df652016-08-31 16:27:24 -0700654}
655
Stefan Bellerf3597132017-06-29 17:07:00 -0700656void show_submodule_inline_diff(struct diff_options *o, const char *path,
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700657 struct object_id *one, struct object_id *two,
Stefan Bellerf3597132017-06-29 17:07:00 -0700658 unsigned dirty_submodule)
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700659{
Brandon Williamsbc099912018-02-14 10:59:49 -0800660 const struct object_id *old_oid = the_hash_algo->empty_tree, *new_oid = the_hash_algo->empty_tree;
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700661 struct commit *left = NULL, *right = NULL;
662 struct commit_list *merge_bases = NULL;
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700663 struct child_process cp = CHILD_PROCESS_INIT;
Stefan Bellerf3597132017-06-29 17:07:00 -0700664 struct strbuf sb = STRBUF_INIT;
Stefan Beller605f0ec2018-12-14 16:09:37 -0800665 struct repository *sub;
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700666
Stefan Beller605f0ec2018-12-14 16:09:37 -0800667 sub = open_submodule(path);
Stefan Bellerf3597132017-06-29 17:07:00 -0700668 show_submodule_header(o, path, one, two, dirty_submodule,
Stefan Beller605f0ec2018-12-14 16:09:37 -0800669 sub, &left, &right, &merge_bases);
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700670
671 /* We need a valid left and right commit to display a difference */
672 if (!(left || is_null_oid(one)) ||
673 !(right || is_null_oid(two)))
674 goto done;
675
676 if (left)
Brandon Williamsbc099912018-02-14 10:59:49 -0800677 old_oid = one;
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700678 if (right)
Brandon Williamsbc099912018-02-14 10:59:49 -0800679 new_oid = two;
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700680
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700681 cp.git_cmd = 1;
682 cp.dir = path;
Stefan Bellerf3597132017-06-29 17:07:00 -0700683 cp.out = -1;
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700684 cp.no_stdin = 1;
685
686 /* TODO: other options may need to be passed here. */
Jeff Kingc972bf42020-07-28 16:25:12 -0400687 strvec_pushl(&cp.args, "diff", "--submodule=diff", NULL);
688 strvec_pushf(&cp.args, "--color=%s", want_color(o->use_color) ?
Stefan Bellerf3597132017-06-29 17:07:00 -0700689 "always" : "never");
Stefan Beller5a522142017-05-04 14:43:55 -0700690
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700691 if (o->flags.reverse_diff) {
Jeff Kingc972bf42020-07-28 16:25:12 -0400692 strvec_pushf(&cp.args, "--src-prefix=%s%s/",
Jeff Kingf6d89422020-07-28 16:26:31 -0400693 o->b_prefix, path);
Jeff Kingc972bf42020-07-28 16:25:12 -0400694 strvec_pushf(&cp.args, "--dst-prefix=%s%s/",
Jeff Kingf6d89422020-07-28 16:26:31 -0400695 o->a_prefix, path);
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700696 } else {
Jeff Kingc972bf42020-07-28 16:25:12 -0400697 strvec_pushf(&cp.args, "--src-prefix=%s%s/",
Jeff Kingf6d89422020-07-28 16:26:31 -0400698 o->a_prefix, path);
Jeff Kingc972bf42020-07-28 16:25:12 -0400699 strvec_pushf(&cp.args, "--dst-prefix=%s%s/",
Jeff Kingf6d89422020-07-28 16:26:31 -0400700 o->b_prefix, path);
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700701 }
Jeff Kingc972bf42020-07-28 16:25:12 -0400702 strvec_push(&cp.args, oid_to_hex(old_oid));
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700703 /*
704 * If the submodule has modified content, we will diff against the
705 * work tree, under the assumption that the user has asked for the
706 * diff format and wishes to actually see all differences even if they
707 * haven't yet been committed to the submodule yet.
708 */
709 if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED))
Jeff Kingc972bf42020-07-28 16:25:12 -0400710 strvec_push(&cp.args, oid_to_hex(new_oid));
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700711
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +0200712 prepare_submodule_repo_env(&cp.env);
David Turnerf1c03682021-08-31 09:12:56 -0400713
714 if (!is_directory(path)) {
715 /* fall back to absorbed git dir, if any */
716 if (!sub)
717 goto done;
718 cp.dir = sub->gitdir;
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +0200719 strvec_push(&cp.env, GIT_DIR_ENVIRONMENT "=.");
720 strvec_push(&cp.env, GIT_WORK_TREE_ENVIRONMENT "=.");
David Turnerf1c03682021-08-31 09:12:56 -0400721 }
722
David Turner67f61ef2021-08-31 09:12:57 -0400723 if (start_command(&cp)) {
Stefan Bellerf3597132017-06-29 17:07:00 -0700724 diff_emit_submodule_error(o, "(diff failed)\n");
David Turner67f61ef2021-08-31 09:12:57 -0400725 goto done;
726 }
Stefan Bellerf3597132017-06-29 17:07:00 -0700727
728 while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF)
729 diff_emit_submodule_pipethrough(o, sb.buf, sb.len);
730
731 if (finish_command(&cp))
732 diff_emit_submodule_error(o, "(diff failed)\n");
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700733
734done:
Stefan Bellerf3597132017-06-29 17:07:00 -0700735 strbuf_release(&sb);
Ævar Arnfjörð Bjarmasonbf20fe42022-04-13 22:01:34 +0200736 free_commit_list(merge_bases);
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700737 if (left)
738 clear_commit_marks(left, ~0);
739 if (right)
740 clear_commit_marks(right, ~0);
Stefan Beller605f0ec2018-12-14 16:09:37 -0800741 if (sub) {
742 repo_clear(sub);
743 free(sub);
744 }
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700745}
746
Stefan Beller84f89252017-03-14 14:46:34 -0700747int should_update_submodules(void)
748{
749 return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
750}
751
752const struct submodule *submodule_from_ce(const struct cache_entry *ce)
753{
754 if (!S_ISGITLINK(ce->ce_mode))
755 return NULL;
756
757 if (!should_update_submodules())
758 return NULL;
759
brian m. carlson14228442021-04-26 01:02:56 +0000760 return submodule_from_path(the_repository, null_oid(), ce->name);
Stefan Beller84f89252017-03-14 14:46:34 -0700761}
762
Brandon Williamsaacc5c12017-05-01 18:02:39 -0700763
Heiko Voigtc68f8372017-10-16 15:58:27 +0200764struct collect_changed_submodules_cb_data {
Nguyễn Thái Ngọc Duy6245b982018-10-19 19:34:43 +0200765 struct repository *repo;
Heiko Voigtc68f8372017-10-16 15:58:27 +0200766 struct string_list *changed;
767 const struct object_id *commit_oid;
768};
769
770/*
771 * this would normally be two functions: default_name_from_path() and
772 * path_from_default_name(). Since the default name is the same as
773 * the submodule path we can get away with just one function which only
774 * checks whether there is a submodule in the working directory at that
775 * location.
776 */
777static const char *default_name_or_path(const char *path_or_name)
778{
779 int error_code;
780
781 if (!is_submodule_populated_gently(path_or_name, &error_code))
782 return NULL;
783
784 return path_or_name;
785}
786
Glen Choo6e1e0c92022-03-07 16:14:29 -0800787/*
788 * Holds relevant information for a changed submodule. Used as the .util
Glen Choob90d9f72022-03-07 16:14:32 -0800789 * member of the changed submodule name string_list_item.
790 *
791 * (super_oid, path) allows the submodule config to be read from _some_
792 * .gitmodules file. We store this information the first time we find a
793 * superproject commit that points to the submodule, but this is
794 * arbitrary - we can choose any (super_oid, path) that matches the
795 * submodule's name.
796 *
797 * NEEDSWORK: Storing an arbitrary commit is undesirable because we can't
798 * guarantee that we're reading the commit that the user would expect. A better
799 * scheme would be to just fetch a submodule by its name. This requires two
800 * steps:
801 * - Create a function that behaves like repo_submodule_init(), but accepts a
802 * submodule name instead of treeish_name and path. This should be easy
803 * because repo_submodule_init() internally uses the submodule's name.
804 *
805 * - Replace most instances of 'struct submodule' (which is the .gitmodules
806 * config) with just the submodule name. This is OK because we expect
807 * submodule settings to be stored in .git/config (via "git submodule init"),
808 * not .gitmodules. This also lets us delete get_non_gitmodules_submodule(),
809 * which constructs a bogus 'struct submodule' for the sake of giving a
810 * placeholder name to a gitlink.
Glen Choo6e1e0c92022-03-07 16:14:29 -0800811 */
812struct changed_submodule_data {
Glen Choob90d9f72022-03-07 16:14:32 -0800813 /*
814 * The first superproject commit in the rev walk that points to
815 * the submodule.
816 */
817 const struct object_id *super_oid;
818 /*
819 * Path to the submodule in the superproject commit referenced
820 * by 'super_oid'.
821 */
822 char *path;
Glen Choo6e1e0c92022-03-07 16:14:29 -0800823 /* The submodule commits that have changed in the rev walk. */
824 struct oid_array new_commits;
825};
826
827static void changed_submodule_data_clear(struct changed_submodule_data *cs_data)
828{
829 oid_array_clear(&cs_data->new_commits);
Glen Choob90d9f72022-03-07 16:14:32 -0800830 free(cs_data->path);
Glen Choo6e1e0c92022-03-07 16:14:29 -0800831}
832
Brandon Williamsaacc5c12017-05-01 18:02:39 -0700833static void collect_changed_submodules_cb(struct diff_queue_struct *q,
834 struct diff_options *options,
835 void *data)
836{
Heiko Voigtc68f8372017-10-16 15:58:27 +0200837 struct collect_changed_submodules_cb_data *me = data;
838 struct string_list *changed = me->changed;
839 const struct object_id *commit_oid = me->commit_oid;
Brandon Williamsaacc5c12017-05-01 18:02:39 -0700840 int i;
Brandon Williamsaacc5c12017-05-01 18:02:39 -0700841
842 for (i = 0; i < q->nr; i++) {
843 struct diff_filepair *p = q->queue[i];
Heiko Voigtc68f8372017-10-16 15:58:27 +0200844 const struct submodule *submodule;
845 const char *name;
Glen Choo1e5dd3a2022-03-07 16:14:28 -0800846 struct string_list_item *item;
Glen Choo6e1e0c92022-03-07 16:14:29 -0800847 struct changed_submodule_data *cs_data;
Heiko Voigtc68f8372017-10-16 15:58:27 +0200848
Brandon Williamsaacc5c12017-05-01 18:02:39 -0700849 if (!S_ISGITLINK(p->two->mode))
850 continue;
851
Nguyễn Thái Ngọc Duy6245b982018-10-19 19:34:43 +0200852 submodule = submodule_from_path(me->repo,
Stefan Beller3b8fb392018-03-28 15:35:29 -0700853 commit_oid, p->two->path);
Heiko Voigtc68f8372017-10-16 15:58:27 +0200854 if (submodule)
855 name = submodule->name;
856 else {
857 name = default_name_or_path(p->two->path);
858 /* make sure name does not collide with existing one */
Stefan Beller5fc84752018-06-14 10:31:07 -0700859 if (name)
Nguyễn Thái Ngọc Duy6245b982018-10-19 19:34:43 +0200860 submodule = submodule_from_name(me->repo,
Stefan Beller5fc84752018-06-14 10:31:07 -0700861 commit_oid, name);
Heiko Voigtc68f8372017-10-16 15:58:27 +0200862 if (submodule) {
Ralf Thielowa4ffbbb2020-01-15 19:07:01 +0100863 warning(_("Submodule in commit %s at path: "
Heiko Voigtc68f8372017-10-16 15:58:27 +0200864 "'%s' collides with a submodule named "
Ralf Thielowa4ffbbb2020-01-15 19:07:01 +0100865 "the same. Skipping it."),
Stefan Beller5fc84752018-06-14 10:31:07 -0700866 oid_to_hex(commit_oid), p->two->path);
Heiko Voigtc68f8372017-10-16 15:58:27 +0200867 name = NULL;
868 }
Brandon Williamsaacc5c12017-05-01 18:02:39 -0700869 }
Heiko Voigtc68f8372017-10-16 15:58:27 +0200870
871 if (!name)
872 continue;
873
Glen Choo1e5dd3a2022-03-07 16:14:28 -0800874 item = string_list_insert(changed, name);
Glen Choob90d9f72022-03-07 16:14:32 -0800875 if (item->util)
876 cs_data = item->util;
877 else {
Glen Choo6e1e0c92022-03-07 16:14:29 -0800878 item->util = xcalloc(1, sizeof(struct changed_submodule_data));
Glen Choob90d9f72022-03-07 16:14:32 -0800879 cs_data = item->util;
880 cs_data->super_oid = commit_oid;
881 cs_data->path = xstrdup(p->two->path);
882 }
Glen Choo6e1e0c92022-03-07 16:14:29 -0800883 oid_array_append(&cs_data->new_commits, &p->two->oid);
Brandon Williamsaacc5c12017-05-01 18:02:39 -0700884 }
885}
886
887/*
888 * Collect the paths of submodules in 'changed' which have changed based on
889 * the revisions as specified in 'argv'. Each entry in 'changed' will also
890 * have a corresponding 'struct oid_array' (in the 'util' field) which lists
891 * what the submodule pointers were updated to during the change.
892 */
Nguyễn Thái Ngọc Duy6245b982018-10-19 19:34:43 +0200893static void collect_changed_submodules(struct repository *r,
Nguyễn Thái Ngọc Duy174d1312018-09-21 17:57:35 +0200894 struct string_list *changed,
Jeff Kingc972bf42020-07-28 16:25:12 -0400895 struct strvec *argv)
Brandon Williamsaacc5c12017-05-01 18:02:39 -0700896{
897 struct rev_info rev;
898 const struct commit *commit;
Orgad Shaneha462bee2020-09-06 20:53:55 +0000899 int save_warning;
900 struct setup_revision_opt s_r_opt = {
901 .assume_dashdash = 1,
902 };
Brandon Williamsaacc5c12017-05-01 18:02:39 -0700903
Orgad Shaneha462bee2020-09-06 20:53:55 +0000904 save_warning = warn_on_object_refname_ambiguity;
905 warn_on_object_refname_ambiguity = 0;
Nguyễn Thái Ngọc Duy6245b982018-10-19 19:34:43 +0200906 repo_init_revisions(r, &rev, NULL);
Orgad Shaneha462bee2020-09-06 20:53:55 +0000907 setup_revisions(argv->nr, argv->v, &rev, &s_r_opt);
908 warn_on_object_refname_ambiguity = save_warning;
Brandon Williamsaacc5c12017-05-01 18:02:39 -0700909 if (prepare_revision_walk(&rev))
Ralf Thielowa4ffbbb2020-01-15 19:07:01 +0100910 die(_("revision walk setup failed"));
Brandon Williamsaacc5c12017-05-01 18:02:39 -0700911
912 while ((commit = get_revision(&rev))) {
913 struct rev_info diff_rev;
Heiko Voigtc68f8372017-10-16 15:58:27 +0200914 struct collect_changed_submodules_cb_data data;
Nguyễn Thái Ngọc Duy6245b982018-10-19 19:34:43 +0200915 data.repo = r;
Heiko Voigtc68f8372017-10-16 15:58:27 +0200916 data.changed = changed;
917 data.commit_oid = &commit->object.oid;
Brandon Williamsaacc5c12017-05-01 18:02:39 -0700918
Nguyễn Thái Ngọc Duy6245b982018-10-19 19:34:43 +0200919 repo_init_revisions(r, &diff_rev, NULL);
Brandon Williamsaacc5c12017-05-01 18:02:39 -0700920 diff_rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
921 diff_rev.diffopt.format_callback = collect_changed_submodules_cb;
Heiko Voigtc68f8372017-10-16 15:58:27 +0200922 diff_rev.diffopt.format_callback_data = &data;
Sergey Organovd01141d2020-09-29 14:31:22 +0300923 diff_rev.dense_combined_merges = 1;
924 diff_tree_combined_merge(commit, &diff_rev);
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +0200925 release_revisions(&diff_rev);
Brandon Williamsaacc5c12017-05-01 18:02:39 -0700926 }
927
928 reset_revision_walk();
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +0200929 release_revisions(&rev);
Brandon Williamsaacc5c12017-05-01 18:02:39 -0700930}
931
Glen Choo6e1e0c92022-03-07 16:14:29 -0800932static void free_submodules_data(struct string_list *submodules)
Brandon Williamsaacc5c12017-05-01 18:02:39 -0700933{
934 struct string_list_item *item;
935 for_each_string_list_item(item, submodules)
Glen Choo6e1e0c92022-03-07 16:14:29 -0800936 changed_submodule_data_clear(item->util);
937
Brandon Williamsaacc5c12017-05-01 18:02:39 -0700938 string_list_clear(submodules, 1);
939}
940
Michael Haggerty7290ef52015-05-25 18:39:07 +0000941static int has_remote(const char *refname, const struct object_id *oid,
942 int flags, void *cb_data)
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200943{
944 return 1;
945}
946
brian m. carlson1b7ba792017-03-31 01:39:59 +0000947static int append_oid_to_argv(const struct object_id *oid, void *data)
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200948{
Jeff Kingc972bf42020-07-28 16:25:12 -0400949 struct strvec *argv = data;
950 strvec_push(argv, oid_to_hex(oid));
Heiko Voigt9cfa1c22016-11-16 16:11:05 +0100951 return 0;
952}
953
Stefan Beller3c96aa92017-09-12 10:30:27 -0700954struct has_commit_data {
Nguyễn Thái Ngọc Duy6245b982018-10-19 19:34:43 +0200955 struct repository *repo;
Stefan Beller3c96aa92017-09-12 10:30:27 -0700956 int result;
957 const char *path;
Glen Choo7c2f8cc2022-03-07 16:14:27 -0800958 const struct object_id *super_oid;
Stefan Beller3c96aa92017-09-12 10:30:27 -0700959};
960
brian m. carlson1b7ba792017-03-31 01:39:59 +0000961static int check_has_commit(const struct object_id *oid, void *data)
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200962{
Stefan Beller3c96aa92017-09-12 10:30:27 -0700963 struct has_commit_data *cb = data;
Jonathan Tan13a2f622021-10-08 14:08:19 -0700964 struct repository subrepo;
965 enum object_type type;
Heiko Voigt5b6607d2016-11-16 16:11:06 +0100966
Glen Choo7c2f8cc2022-03-07 16:14:27 -0800967 if (repo_submodule_init(&subrepo, cb->repo, cb->path, cb->super_oid)) {
Jonathan Tan13a2f622021-10-08 14:08:19 -0700968 cb->result = 0;
Glen Choo5fff35d2022-03-07 16:14:33 -0800969 /* subrepo failed to init, so don't clean it up. */
970 return 0;
Jonathan Tan13a2f622021-10-08 14:08:19 -0700971 }
972
973 type = oid_object_info(&subrepo, oid, NULL);
Heiko Voigt5b6607d2016-11-16 16:11:06 +0100974
Stefan Beller3c96aa92017-09-12 10:30:27 -0700975 switch (type) {
976 case OBJ_COMMIT:
Jonathan Tan13a2f622021-10-08 14:08:19 -0700977 goto cleanup;
Stefan Beller3c96aa92017-09-12 10:30:27 -0700978 case OBJ_BAD:
979 /*
980 * Object is missing or invalid. If invalid, an error message
981 * has already been printed.
982 */
983 cb->result = 0;
Jonathan Tan13a2f622021-10-08 14:08:19 -0700984 goto cleanup;
Stefan Beller3c96aa92017-09-12 10:30:27 -0700985 default:
986 die(_("submodule entry '%s' (%s) is a %s, not a commit"),
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800987 cb->path, oid_to_hex(oid), type_name(type));
Stefan Beller3c96aa92017-09-12 10:30:27 -0700988 }
Jonathan Tan13a2f622021-10-08 14:08:19 -0700989cleanup:
990 repo_clear(&subrepo);
991 return 0;
Heiko Voigt5b6607d2016-11-16 16:11:06 +0100992}
993
Nguyễn Thái Ngọc Duy6245b982018-10-19 19:34:43 +0200994static int submodule_has_commits(struct repository *r,
995 const char *path,
Glen Choo7c2f8cc2022-03-07 16:14:27 -0800996 const struct object_id *super_oid,
Nguyễn Thái Ngọc Duy6245b982018-10-19 19:34:43 +0200997 struct oid_array *commits)
Heiko Voigt5b6607d2016-11-16 16:11:06 +0100998{
Glen Choo7c2f8cc2022-03-07 16:14:27 -0800999 struct has_commit_data has_commit = {
1000 .repo = r,
1001 .result = 1,
1002 .path = path,
1003 .super_oid = super_oid
1004 };
Heiko Voigt5b6607d2016-11-16 16:11:06 +01001005
brian m. carlson910650d2017-03-31 01:40:00 +00001006 oid_array_for_each_unique(commits, check_has_commit, &has_commit);
Brandon Williams7c8d2b02017-05-01 18:02:38 -07001007
Stefan Beller3c96aa92017-09-12 10:30:27 -07001008 if (has_commit.result) {
Brandon Williams7c8d2b02017-05-01 18:02:38 -07001009 /*
1010 * Even if the submodule is checked out and the commit is
1011 * present, make sure it exists in the submodule's object store
1012 * and that it is reachable from a ref.
1013 */
1014 struct child_process cp = CHILD_PROCESS_INIT;
1015 struct strbuf out = STRBUF_INIT;
1016
Jeff Kingc972bf42020-07-28 16:25:12 -04001017 strvec_pushl(&cp.args, "rev-list", "-n", "1", NULL);
Brandon Williams7c8d2b02017-05-01 18:02:38 -07001018 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
Jeff Kingc972bf42020-07-28 16:25:12 -04001019 strvec_pushl(&cp.args, "--not", "--all", NULL);
Brandon Williams7c8d2b02017-05-01 18:02:38 -07001020
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02001021 prepare_submodule_repo_env(&cp.env);
Brandon Williams7c8d2b02017-05-01 18:02:38 -07001022 cp.git_cmd = 1;
1023 cp.no_stdin = 1;
1024 cp.dir = path;
1025
1026 if (capture_command(&cp, &out, GIT_MAX_HEXSZ + 1) || out.len)
Stefan Beller3c96aa92017-09-12 10:30:27 -07001027 has_commit.result = 0;
Brandon Williams7c8d2b02017-05-01 18:02:38 -07001028
1029 strbuf_release(&out);
1030 }
1031
Stefan Beller3c96aa92017-09-12 10:30:27 -07001032 return has_commit.result;
Heiko Voigt5b6607d2016-11-16 16:11:06 +01001033}
1034
Nguyễn Thái Ngọc Duy6245b982018-10-19 19:34:43 +02001035static int submodule_needs_pushing(struct repository *r,
1036 const char *path,
1037 struct oid_array *commits)
Heiko Voigt5b6607d2016-11-16 16:11:06 +01001038{
Glen Choo7c2f8cc2022-03-07 16:14:27 -08001039 if (!submodule_has_commits(r, path, null_oid(), commits))
Heiko Voigt250ab242016-11-16 16:11:07 +01001040 /*
1041 * NOTE: We do consider it safe to return "no" here. The
1042 * correct answer would be "We do not know" instead of
1043 * "No push needed", but it is quite hard to change
1044 * the submodule pointer without having the submodule
1045 * around. If a user did however change the submodules
1046 * without having the submodule around, this indicates
1047 * an expert who knows what they are doing or a
1048 * maintainer integrating work from other people. In
1049 * both cases it should be safe to skip this check.
1050 */
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +02001051 return 0;
1052
1053 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
René Scharfed3180272014-08-19 21:09:35 +02001054 struct child_process cp = CHILD_PROCESS_INIT;
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +02001055 struct strbuf buf = STRBUF_INIT;
1056 int needs_pushing = 0;
1057
Jeff Kingc972bf42020-07-28 16:25:12 -04001058 strvec_push(&cp.args, "rev-list");
brian m. carlson910650d2017-03-31 01:40:00 +00001059 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
Jeff Kingc972bf42020-07-28 16:25:12 -04001060 strvec_pushl(&cp.args, "--not", "--remotes", "-n", "1" , NULL);
Heiko Voigt5b6607d2016-11-16 16:11:06 +01001061
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02001062 prepare_submodule_repo_env(&cp.env);
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +02001063 cp.git_cmd = 1;
1064 cp.no_stdin = 1;
1065 cp.out = -1;
1066 cp.dir = path;
1067 if (start_command(&cp))
Ralf Thielowa4ffbbb2020-01-15 19:07:01 +01001068 die(_("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s"),
Heiko Voigt5b6607d2016-11-16 16:11:06 +01001069 path);
brian m. carlsondb1ba2a2019-02-19 00:04:59 +00001070 if (strbuf_read(&buf, cp.out, the_hash_algo->hexsz + 1))
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +02001071 needs_pushing = 1;
1072 finish_command(&cp);
1073 close(cp.out);
1074 strbuf_release(&buf);
1075 return needs_pushing;
1076 }
1077
1078 return 0;
1079}
1080
Nguyễn Thái Ngọc Duy6245b982018-10-19 19:34:43 +02001081int find_unpushed_submodules(struct repository *r,
Nguyễn Thái Ngọc Duy174d1312018-09-21 17:57:35 +02001082 struct oid_array *commits,
1083 const char *remotes_name,
1084 struct string_list *needs_pushing)
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +02001085{
Heiko Voigt14739442016-11-16 16:11:04 +01001086 struct string_list submodules = STRING_LIST_INIT_DUP;
Heiko Voigtc68f8372017-10-16 15:58:27 +02001087 struct string_list_item *name;
Jeff Kingc972bf42020-07-28 16:25:12 -04001088 struct strvec argv = STRVEC_INIT;
Heiko Voigta762e512012-03-29 09:21:23 +02001089
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001090 /* argv.v[0] will be ignored by setup_revisions */
Jeff Kingc972bf42020-07-28 16:25:12 -04001091 strvec_push(&argv, "find_unpushed_submodules");
brian m. carlson910650d2017-03-31 01:40:00 +00001092 oid_array_for_each_unique(commits, append_oid_to_argv, &argv);
Jeff Kingc972bf42020-07-28 16:25:12 -04001093 strvec_push(&argv, "--not");
1094 strvec_pushf(&argv, "--remotes=%s", remotes_name);
Heiko Voigt9cfa1c22016-11-16 16:11:05 +01001095
Nguyễn Thái Ngọc Duy6245b982018-10-19 19:34:43 +02001096 collect_changed_submodules(r, &submodules, &argv);
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +02001097
Heiko Voigtc68f8372017-10-16 15:58:27 +02001098 for_each_string_list_item(name, &submodules) {
Glen Choo6e1e0c92022-03-07 16:14:29 -08001099 struct changed_submodule_data *cs_data = name->util;
Heiko Voigtc68f8372017-10-16 15:58:27 +02001100 const struct submodule *submodule;
1101 const char *path = NULL;
1102
brian m. carlson14228442021-04-26 01:02:56 +00001103 submodule = submodule_from_name(r, null_oid(), name->string);
Heiko Voigtc68f8372017-10-16 15:58:27 +02001104 if (submodule)
1105 path = submodule->path;
1106 else
1107 path = default_name_or_path(name->string);
1108
1109 if (!path)
1110 continue;
Heiko Voigt5b6607d2016-11-16 16:11:06 +01001111
Glen Choo6e1e0c92022-03-07 16:14:29 -08001112 if (submodule_needs_pushing(r, path, &cs_data->new_commits))
Brandon Williamsaacc5c12017-05-01 18:02:39 -07001113 string_list_insert(needs_pushing, path);
Heiko Voigt14739442016-11-16 16:11:04 +01001114 }
Brandon Williams610b2332017-04-28 16:53:58 -07001115
Glen Choo6e1e0c92022-03-07 16:14:29 -08001116 free_submodules_data(&submodules);
Jeff Kingc972bf42020-07-28 16:25:12 -04001117 strvec_clear(&argv);
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +02001118
Heiko Voigta762e512012-03-29 09:21:23 +02001119 return needs_pushing->nr;
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +02001120}
1121
Brandon Williams2a905562017-04-05 10:47:16 -07001122static int push_submodule(const char *path,
Brandon Williams06bf4ad2017-04-05 10:47:19 -07001123 const struct remote *remote,
Brandon Williams60fba4b2018-05-16 15:58:23 -07001124 const struct refspec *rs,
Brandon Williams2a905562017-04-05 10:47:16 -07001125 const struct string_list *push_options,
1126 int dry_run)
Heiko Voigteb21c732012-03-29 09:21:24 +02001127{
Heiko Voigteb21c732012-03-29 09:21:24 +02001128 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
René Scharfed3180272014-08-19 21:09:35 +02001129 struct child_process cp = CHILD_PROCESS_INIT;
Jeff Kingc972bf42020-07-28 16:25:12 -04001130 strvec_push(&cp.args, "push");
Brandon Williams0301c822016-11-17 10:46:04 -08001131 if (dry_run)
Jeff Kingc972bf42020-07-28 16:25:12 -04001132 strvec_push(&cp.args, "--dry-run");
Heiko Voigteb21c732012-03-29 09:21:24 +02001133
Brandon Williams2a905562017-04-05 10:47:16 -07001134 if (push_options && push_options->nr) {
1135 const struct string_list_item *item;
1136 for_each_string_list_item(item, push_options)
Jeff Kingc972bf42020-07-28 16:25:12 -04001137 strvec_pushf(&cp.args, "--push-option=%s",
Jeff Kingf6d89422020-07-28 16:26:31 -04001138 item->string);
Brandon Williams2a905562017-04-05 10:47:16 -07001139 }
Brandon Williams06bf4ad2017-04-05 10:47:19 -07001140
1141 if (remote->origin != REMOTE_UNCONFIGURED) {
1142 int i;
Jeff Kingc972bf42020-07-28 16:25:12 -04001143 strvec_push(&cp.args, remote->name);
Brandon Williams60fba4b2018-05-16 15:58:23 -07001144 for (i = 0; i < rs->raw_nr; i++)
Jeff Kingc972bf42020-07-28 16:25:12 -04001145 strvec_push(&cp.args, rs->raw[i]);
Brandon Williams06bf4ad2017-04-05 10:47:19 -07001146 }
1147
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02001148 prepare_submodule_repo_env(&cp.env);
Heiko Voigteb21c732012-03-29 09:21:24 +02001149 cp.git_cmd = 1;
1150 cp.no_stdin = 1;
1151 cp.dir = path;
1152 if (run_command(&cp))
1153 return 0;
1154 close(cp.out);
1155 }
1156
1157 return 1;
1158}
1159
Brandon Williams06bf4ad2017-04-05 10:47:19 -07001160/*
1161 * Perform a check in the submodule to see if the remote and refspec work.
1162 * Die if the submodule can't be pushed.
1163 */
Brandon Williamsc7be7202017-07-20 10:40:37 -07001164static void submodule_push_check(const char *path, const char *head,
1165 const struct remote *remote,
Brandon Williams60fba4b2018-05-16 15:58:23 -07001166 const struct refspec *rs)
Brandon Williams06bf4ad2017-04-05 10:47:19 -07001167{
1168 struct child_process cp = CHILD_PROCESS_INIT;
1169 int i;
1170
Jeff Kingc972bf42020-07-28 16:25:12 -04001171 strvec_push(&cp.args, "submodule--helper");
1172 strvec_push(&cp.args, "push-check");
1173 strvec_push(&cp.args, head);
1174 strvec_push(&cp.args, remote->name);
Brandon Williams06bf4ad2017-04-05 10:47:19 -07001175
Brandon Williams60fba4b2018-05-16 15:58:23 -07001176 for (i = 0; i < rs->raw_nr; i++)
Jeff Kingc972bf42020-07-28 16:25:12 -04001177 strvec_push(&cp.args, rs->raw[i]);
Brandon Williams06bf4ad2017-04-05 10:47:19 -07001178
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02001179 prepare_submodule_repo_env(&cp.env);
Brandon Williams06bf4ad2017-04-05 10:47:19 -07001180 cp.git_cmd = 1;
1181 cp.no_stdin = 1;
1182 cp.no_stdout = 1;
1183 cp.dir = path;
1184
1185 /*
1186 * Simply indicate if 'submodule--helper push-check' failed.
1187 * More detailed error information will be provided by the
1188 * child process.
1189 */
1190 if (run_command(&cp))
Ralf Thielowa4ffbbb2020-01-15 19:07:01 +01001191 die(_("process for submodule '%s' failed"), path);
Brandon Williams06bf4ad2017-04-05 10:47:19 -07001192}
1193
Nguyễn Thái Ngọc Duy6245b982018-10-19 19:34:43 +02001194int push_unpushed_submodules(struct repository *r,
Nguyễn Thái Ngọc Duy174d1312018-09-21 17:57:35 +02001195 struct oid_array *commits,
Brandon Williams06bf4ad2017-04-05 10:47:19 -07001196 const struct remote *remote,
Brandon Williams60fba4b2018-05-16 15:58:23 -07001197 const struct refspec *rs,
Brandon Williams2a905562017-04-05 10:47:16 -07001198 const struct string_list *push_options,
Brandon Williams0301c822016-11-17 10:46:04 -08001199 int dry_run)
Heiko Voigteb21c732012-03-29 09:21:24 +02001200{
1201 int i, ret = 1;
Tanay Abhraf93d7c62014-07-18 02:19:00 -07001202 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
Heiko Voigteb21c732012-03-29 09:21:24 +02001203
Nguyễn Thái Ngọc Duy6245b982018-10-19 19:34:43 +02001204 if (!find_unpushed_submodules(r, commits,
Nguyễn Thái Ngọc Duy174d1312018-09-21 17:57:35 +02001205 remote->name, &needs_pushing))
Heiko Voigteb21c732012-03-29 09:21:24 +02001206 return 1;
1207
Brandon Williams06bf4ad2017-04-05 10:47:19 -07001208 /*
1209 * Verify that the remote and refspec can be propagated to all
1210 * submodules. This check can be skipped if the remote and refspec
1211 * won't be propagated due to the remote being unconfigured (e.g. a URL
1212 * instead of a remote name).
1213 */
Brandon Williamsc7be7202017-07-20 10:40:37 -07001214 if (remote->origin != REMOTE_UNCONFIGURED) {
1215 char *head;
1216 struct object_id head_oid;
1217
brian m. carlson0f2dc722017-10-15 22:06:55 +00001218 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
Brandon Williamsc7be7202017-07-20 10:40:37 -07001219 if (!head)
1220 die(_("Failed to resolve HEAD as a valid ref."));
1221
Brandon Williams06bf4ad2017-04-05 10:47:19 -07001222 for (i = 0; i < needs_pushing.nr; i++)
1223 submodule_push_check(needs_pushing.items[i].string,
Brandon Williams60fba4b2018-05-16 15:58:23 -07001224 head, remote, rs);
Brandon Williamsc7be7202017-07-20 10:40:37 -07001225 free(head);
1226 }
Brandon Williams06bf4ad2017-04-05 10:47:19 -07001227
1228 /* Actually push the submodules */
Heiko Voigteb21c732012-03-29 09:21:24 +02001229 for (i = 0; i < needs_pushing.nr; i++) {
1230 const char *path = needs_pushing.items[i].string;
Ralf Thielowa4ffbbb2020-01-15 19:07:01 +01001231 fprintf(stderr, _("Pushing submodule '%s'\n"), path);
Brandon Williams60fba4b2018-05-16 15:58:23 -07001232 if (!push_submodule(path, remote, rs,
Brandon Williams06bf4ad2017-04-05 10:47:19 -07001233 push_options, dry_run)) {
Ralf Thielowa4ffbbb2020-01-15 19:07:01 +01001234 fprintf(stderr, _("Unable to push submodule '%s'\n"), path);
Heiko Voigteb21c732012-03-29 09:21:24 +02001235 ret = 0;
1236 }
1237 }
1238
1239 string_list_clear(&needs_pushing, 0);
1240
1241 return ret;
1242}
1243
Brandon Williams419fd782017-04-28 16:53:57 -07001244static int append_oid_to_array(const char *ref, const struct object_id *oid,
1245 int flags, void *data)
Jeff King6859de42011-09-12 15:56:52 -04001246{
Brandon Williams419fd782017-04-28 16:53:57 -07001247 struct oid_array *array = data;
1248 oid_array_append(array, oid);
Jeff King6859de42011-09-12 15:56:52 -04001249 return 0;
1250}
1251
brian m. carlson2eb80bc2017-03-26 16:01:35 +00001252void check_for_new_submodule_commits(struct object_id *oid)
Jens Lehmann88a21972011-03-06 23:10:46 +01001253{
Jeff King6859de42011-09-12 15:56:52 -04001254 if (!initialized_fetch_ref_tips) {
Brandon Williams419fd782017-04-28 16:53:57 -07001255 for_each_ref(append_oid_to_array, &ref_tips_before_fetch);
Jeff King6859de42011-09-12 15:56:52 -04001256 initialized_fetch_ref_tips = 1;
1257 }
1258
brian m. carlson910650d2017-03-31 01:40:00 +00001259 oid_array_append(&ref_tips_after_fetch, oid);
Jeff King6859de42011-09-12 15:56:52 -04001260}
1261
Glen Choob90d9f72022-03-07 16:14:32 -08001262/*
1263 * Returns 1 if there is at least one submodule gitdir in
1264 * $GIT_DIR/modules and 0 otherwise. This follows
1265 * submodule_name_to_gitdir(), which looks for submodules in
1266 * $GIT_DIR/modules, not $GIT_COMMON_DIR.
1267 *
1268 * A submodule can be moved to $GIT_DIR/modules manually by running "git
1269 * submodule absorbgitdirs", or it may be initialized there by "git
1270 * submodule update".
1271 */
1272static int repo_has_absorbed_submodules(struct repository *r)
1273{
1274 int ret;
1275 struct strbuf buf = STRBUF_INIT;
1276
1277 strbuf_repo_git_path(&buf, r, "modules/");
1278 ret = file_exists(buf.buf) && !is_empty_dir(buf.buf);
1279 strbuf_release(&buf);
1280 return ret;
1281}
1282
Stefan Beller16dd6fe2018-11-28 16:27:51 -08001283static void calculate_changed_submodule_paths(struct repository *r,
1284 struct string_list *changed_submodule_names)
Jeff King6859de42011-09-12 15:56:52 -04001285{
Jeff Kingc972bf42020-07-28 16:25:12 -04001286 struct strvec argv = STRVEC_INIT;
Stefan Bellerbcd73372018-11-28 16:27:52 -08001287 struct string_list_item *name;
Jens Lehmann88a21972011-03-06 23:10:46 +01001288
Glen Choob90d9f72022-03-07 16:14:32 -08001289 /* No need to check if no submodules would be fetched */
1290 if (!submodule_from_path(r, NULL, NULL) &&
1291 !repo_has_absorbed_submodules(r))
Jens Lehmann18322ba2011-09-09 20:22:03 +02001292 return;
1293
Jeff Kingc972bf42020-07-28 16:25:12 -04001294 strvec_push(&argv, "--"); /* argv[0] program name */
brian m. carlson910650d2017-03-31 01:40:00 +00001295 oid_array_for_each_unique(&ref_tips_after_fetch,
Brandon Williamsd1a84602017-04-28 16:53:59 -07001296 append_oid_to_argv, &argv);
Jeff Kingc972bf42020-07-28 16:25:12 -04001297 strvec_push(&argv, "--not");
brian m. carlson910650d2017-03-31 01:40:00 +00001298 oid_array_for_each_unique(&ref_tips_before_fetch,
Brandon Williamsd1a84602017-04-28 16:53:59 -07001299 append_oid_to_argv, &argv);
Jens Lehmann88a21972011-03-06 23:10:46 +01001300
1301 /*
1302 * Collect all submodules (whether checked out or not) for which new
Heiko Voigtc68f8372017-10-16 15:58:27 +02001303 * commits have been recorded upstream in "changed_submodule_names".
Jens Lehmann88a21972011-03-06 23:10:46 +01001304 */
Stefan Bellerbcd73372018-11-28 16:27:52 -08001305 collect_changed_submodules(r, changed_submodule_names, &argv);
Brandon Williamsaacc5c12017-05-01 18:02:39 -07001306
Stefan Bellerbcd73372018-11-28 16:27:52 -08001307 for_each_string_list_item(name, changed_submodule_names) {
Glen Choo6e1e0c92022-03-07 16:14:29 -08001308 struct changed_submodule_data *cs_data = name->util;
Heiko Voigtc68f8372017-10-16 15:58:27 +02001309 const struct submodule *submodule;
1310 const char *path = NULL;
1311
brian m. carlson14228442021-04-26 01:02:56 +00001312 submodule = submodule_from_name(r, null_oid(), name->string);
Heiko Voigtc68f8372017-10-16 15:58:27 +02001313 if (submodule)
1314 path = submodule->path;
1315 else
1316 path = default_name_or_path(name->string);
1317
1318 if (!path)
1319 continue;
Brandon Williamsaacc5c12017-05-01 18:02:39 -07001320
Glen Choo6e1e0c92022-03-07 16:14:29 -08001321 if (submodule_has_commits(r, path, null_oid(), &cs_data->new_commits)) {
1322 changed_submodule_data_clear(cs_data);
Stefan Bellerbcd73372018-11-28 16:27:52 -08001323 *name->string = '\0';
1324 }
Jens Lehmann88a21972011-03-06 23:10:46 +01001325 }
Jeff King6859de42011-09-12 15:56:52 -04001326
Stefan Bellerbcd73372018-11-28 16:27:52 -08001327 string_list_remove_empty_items(changed_submodule_names, 1);
1328
Jeff Kingc972bf42020-07-28 16:25:12 -04001329 strvec_clear(&argv);
brian m. carlson910650d2017-03-31 01:40:00 +00001330 oid_array_clear(&ref_tips_before_fetch);
1331 oid_array_clear(&ref_tips_after_fetch);
Jeff King6859de42011-09-12 15:56:52 -04001332 initialized_fetch_ref_tips = 0;
Jens Lehmann88a21972011-03-06 23:10:46 +01001333}
1334
Nguyễn Thái Ngọc Duy6245b982018-10-19 19:34:43 +02001335int submodule_touches_in_range(struct repository *r,
Nguyễn Thái Ngọc Duy174d1312018-09-21 17:57:35 +02001336 struct object_id *excl_oid,
Stefan Bellera6d7eb22017-06-23 12:13:02 -07001337 struct object_id *incl_oid)
1338{
1339 struct string_list subs = STRING_LIST_INIT_DUP;
Jeff Kingc972bf42020-07-28 16:25:12 -04001340 struct strvec args = STRVEC_INIT;
Stefan Bellera6d7eb22017-06-23 12:13:02 -07001341 int ret;
1342
Stefan Bellera6d7eb22017-06-23 12:13:02 -07001343 /* No need to check if there are no submodules configured */
Nguyễn Thái Ngọc Duy6245b982018-10-19 19:34:43 +02001344 if (!submodule_from_path(r, NULL, NULL))
Stefan Bellera6d7eb22017-06-23 12:13:02 -07001345 return 0;
1346
Jeff Kingc972bf42020-07-28 16:25:12 -04001347 strvec_push(&args, "--"); /* args[0] program name */
1348 strvec_push(&args, oid_to_hex(incl_oid));
Jonathan Tan4d36f882018-05-24 13:47:29 -07001349 if (!is_null_oid(excl_oid)) {
Jeff Kingc972bf42020-07-28 16:25:12 -04001350 strvec_push(&args, "--not");
1351 strvec_push(&args, oid_to_hex(excl_oid));
Jonathan Tan4d36f882018-05-24 13:47:29 -07001352 }
Stefan Bellera6d7eb22017-06-23 12:13:02 -07001353
Nguyễn Thái Ngọc Duy6245b982018-10-19 19:34:43 +02001354 collect_changed_submodules(r, &subs, &args);
Stefan Bellera6d7eb22017-06-23 12:13:02 -07001355 ret = subs.nr;
1356
Jeff Kingc972bf42020-07-28 16:25:12 -04001357 strvec_clear(&args);
Stefan Bellera6d7eb22017-06-23 12:13:02 -07001358
Glen Choo6e1e0c92022-03-07 16:14:29 -08001359 free_submodules_data(&subs);
Stefan Bellera6d7eb22017-06-23 12:13:02 -07001360 return ret;
1361}
1362
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001363struct submodule_parallel_fetch {
Glen Choob90d9f72022-03-07 16:14:32 -08001364 /*
1365 * The index of the last index entry processed by
1366 * get_fetch_task_from_index().
1367 */
1368 int index_count;
1369 /*
1370 * The index of the last string_list entry processed by
1371 * get_fetch_task_from_changed().
1372 */
1373 int changed_count;
Jeff Kingc972bf42020-07-28 16:25:12 -04001374 struct strvec args;
Brandon Williamse7241972017-12-12 11:53:52 -08001375 struct repository *r;
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001376 const char *prefix;
1377 int command_line_option;
Brandon Williams8fa29152017-08-02 12:49:19 -07001378 int default_option;
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001379 int quiet;
1380 int result;
Stefan Beller16dd6fe2018-11-28 16:27:51 -08001381
Glen Choob90d9f72022-03-07 16:14:32 -08001382 /*
1383 * Names of submodules that have new commits. Generated by
1384 * walking the newly fetched superproject commits.
1385 */
Stefan Beller16dd6fe2018-11-28 16:27:51 -08001386 struct string_list changed_submodule_names;
Glen Choob90d9f72022-03-07 16:14:32 -08001387 /*
1388 * Names of submodules that have already been processed. Lets us
1389 * avoid fetching the same submodule more than once.
1390 */
1391 struct string_list seen_submodule_names;
Stefan Bellerbe76c212018-12-06 13:26:55 -08001392
1393 /* Pending fetches by OIDs */
1394 struct fetch_task **oid_fetch_tasks;
1395 int oid_fetch_tasks_nr, oid_fetch_tasks_alloc;
Emily Shaffer02225402020-01-16 14:20:12 -08001396
1397 struct strbuf submodules_with_errors;
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001398};
Ævar Arnfjörð Bjarmasonf69a6e42021-09-27 14:54:27 +02001399#define SPF_INIT { \
1400 .args = STRVEC_INIT, \
1401 .changed_submodule_names = STRING_LIST_INIT_DUP, \
Glen Choob90d9f72022-03-07 16:14:32 -08001402 .seen_submodule_names = STRING_LIST_INIT_DUP, \
Ævar Arnfjörð Bjarmasonf69a6e42021-09-27 14:54:27 +02001403 .submodules_with_errors = STRBUF_INIT, \
1404}
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001405
Heiko Voigt4b4aced2017-10-16 15:59:05 +02001406static int get_fetch_recurse_config(const struct submodule *submodule,
1407 struct submodule_parallel_fetch *spf)
1408{
1409 if (spf->command_line_option != RECURSE_SUBMODULES_DEFAULT)
1410 return spf->command_line_option;
1411
1412 if (submodule) {
1413 char *key;
1414 const char *value;
1415
1416 int fetch_recurse = submodule->fetch_recurse;
1417 key = xstrfmt("submodule.%s.fetchRecurseSubmodules", submodule->name);
Jeff Kingf1de9812020-08-14 12:17:36 -04001418 if (!repo_config_get_string_tmp(spf->r, key, &value)) {
Heiko Voigt4b4aced2017-10-16 15:59:05 +02001419 fetch_recurse = parse_fetch_recurse_submodules_arg(key, value);
1420 }
1421 free(key);
1422
1423 if (fetch_recurse != RECURSE_SUBMODULES_NONE)
1424 /* local config overrules everything except commandline */
1425 return fetch_recurse;
1426 }
1427
1428 return spf->default_option;
1429}
1430
Stefan Bellerbe76c212018-12-06 13:26:55 -08001431/*
1432 * Fetch in progress (if callback data) or
1433 * pending (if in oid_fetch_tasks in struct submodule_parallel_fetch)
1434 */
1435struct fetch_task {
1436 struct repository *repo;
1437 const struct submodule *sub;
1438 unsigned free_sub : 1; /* Do we need to free the submodule? */
Glen Choo73bc90d2022-03-07 16:14:30 -08001439 const char *default_argv; /* The default fetch mode. */
Glen Choob90d9f72022-03-07 16:14:32 -08001440 struct strvec git_args; /* Args for the child git process. */
Stefan Bellerbe76c212018-12-06 13:26:55 -08001441
1442 struct oid_array *commits; /* Ensure these commits are fetched */
1443};
1444
1445/**
1446 * When a submodule is not defined in .gitmodules, we cannot access it
1447 * via the regular submodule-config. Create a fake submodule, which we can
1448 * work on.
1449 */
1450static const struct submodule *get_non_gitmodules_submodule(const char *path)
1451{
1452 struct submodule *ret = NULL;
1453 const char *name = default_name_or_path(path);
1454
1455 if (!name)
1456 return NULL;
1457
1458 ret = xmalloc(sizeof(*ret));
1459 memset(ret, 0, sizeof(*ret));
1460 ret->path = name;
1461 ret->name = name;
1462
1463 return (const struct submodule *) ret;
1464}
1465
Stefan Bellerbe76c212018-12-06 13:26:55 -08001466static void fetch_task_release(struct fetch_task *p)
1467{
1468 if (p->free_sub)
1469 free((void*)p->sub);
1470 p->free_sub = 0;
1471 p->sub = NULL;
1472
1473 if (p->repo)
1474 repo_clear(p->repo);
1475 FREE_AND_NULL(p->repo);
Glen Choob90d9f72022-03-07 16:14:32 -08001476
1477 strvec_clear(&p->git_args);
Stefan Bellerbe76c212018-12-06 13:26:55 -08001478}
1479
Stefan Beller26f80cc2018-11-28 16:27:54 -08001480static struct repository *get_submodule_repo_for(struct repository *r,
Glen Choo7c2f8cc2022-03-07 16:14:27 -08001481 const char *path,
1482 const struct object_id *treeish_name)
Stefan Beller26f80cc2018-11-28 16:27:54 -08001483{
1484 struct repository *ret = xmalloc(sizeof(*ret));
1485
Glen Choo7c2f8cc2022-03-07 16:14:27 -08001486 if (repo_submodule_init(ret, r, path, treeish_name)) {
Jonathan Tan5df51062021-09-09 11:47:27 -07001487 free(ret);
1488 return NULL;
Stefan Beller26f80cc2018-11-28 16:27:54 -08001489 }
1490
1491 return ret;
1492}
1493
Glen Choo5370b912022-03-07 16:14:31 -08001494static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf,
1495 const char *path,
1496 const struct object_id *treeish_name)
1497{
1498 struct fetch_task *task = xmalloc(sizeof(*task));
1499 memset(task, 0, sizeof(*task));
1500
1501 task->sub = submodule_from_path(spf->r, treeish_name, path);
1502
1503 if (!task->sub) {
1504 /*
1505 * No entry in .gitmodules? Technically not a submodule,
1506 * but historically we supported repositories that happen to be
1507 * in-place where a gitlink is. Keep supporting them.
1508 */
1509 task->sub = get_non_gitmodules_submodule(path);
1510 if (!task->sub)
1511 goto cleanup;
1512
1513 task->free_sub = 1;
1514 }
1515
Glen Choob90d9f72022-03-07 16:14:32 -08001516 if (string_list_lookup(&spf->seen_submodule_names, task->sub->name))
1517 goto cleanup;
1518
Glen Choo5370b912022-03-07 16:14:31 -08001519 switch (get_fetch_recurse_config(task->sub, spf))
1520 {
1521 default:
1522 case RECURSE_SUBMODULES_DEFAULT:
1523 case RECURSE_SUBMODULES_ON_DEMAND:
1524 if (!task->sub ||
1525 !string_list_lookup(
1526 &spf->changed_submodule_names,
1527 task->sub->name))
1528 goto cleanup;
1529 task->default_argv = "on-demand";
1530 break;
1531 case RECURSE_SUBMODULES_ON:
1532 task->default_argv = "yes";
1533 break;
1534 case RECURSE_SUBMODULES_OFF:
1535 goto cleanup;
1536 }
1537
1538 task->repo = get_submodule_repo_for(spf->r, path, treeish_name);
1539
1540 return task;
1541
1542 cleanup:
1543 fetch_task_release(task);
1544 free(task);
1545 return NULL;
1546}
1547
Glen Choo73bc90d2022-03-07 16:14:30 -08001548static struct fetch_task *
Glen Choob90d9f72022-03-07 16:14:32 -08001549get_fetch_task_from_index(struct submodule_parallel_fetch *spf,
1550 struct strbuf *err)
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001551{
Glen Choob90d9f72022-03-07 16:14:32 -08001552 for (; spf->index_count < spf->r->index->cache_nr; spf->index_count++) {
1553 const struct cache_entry *ce =
1554 spf->r->index->cache[spf->index_count];
Stefan Bellerbe76c212018-12-06 13:26:55 -08001555 struct fetch_task *task;
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001556
1557 if (!S_ISGITLINK(ce->ce_mode))
1558 continue;
1559
Glen Choo5370b912022-03-07 16:14:31 -08001560 task = fetch_task_create(spf, ce->name, null_oid());
Stefan Bellerbe76c212018-12-06 13:26:55 -08001561 if (!task)
1562 continue;
Brandon Williams492c6c42017-08-03 11:19:51 -07001563
Stefan Bellerbe76c212018-12-06 13:26:55 -08001564 if (task->repo) {
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001565 if (!spf->quiet)
Ralf Thielowa4ffbbb2020-01-15 19:07:01 +01001566 strbuf_addf(err, _("Fetching submodule %s%s\n"),
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001567 spf->prefix, ce->name);
Stefan Beller26f80cc2018-11-28 16:27:54 -08001568
Glen Choob90d9f72022-03-07 16:14:32 -08001569 spf->index_count++;
Glen Choo73bc90d2022-03-07 16:14:30 -08001570 return task;
Stefan Beller26f80cc2018-11-28 16:27:54 -08001571 } else {
Peter Kaestle505a2762020-12-09 11:58:44 +01001572 struct strbuf empty_submodule_path = STRBUF_INIT;
Stefan Bellerbe76c212018-12-06 13:26:55 -08001573
1574 fetch_task_release(task);
1575 free(task);
1576
Stefan Beller26f80cc2018-11-28 16:27:54 -08001577 /*
1578 * An empty directory is normal,
1579 * the submodule is not initialized
1580 */
Peter Kaestle505a2762020-12-09 11:58:44 +01001581 strbuf_addf(&empty_submodule_path, "%s/%s/",
1582 spf->r->worktree,
1583 ce->name);
Stefan Beller26f80cc2018-11-28 16:27:54 -08001584 if (S_ISGITLINK(ce->ce_mode) &&
Peter Kaestle505a2762020-12-09 11:58:44 +01001585 !is_empty_dir(empty_submodule_path.buf)) {
Stefan Beller26f80cc2018-11-28 16:27:54 -08001586 spf->result = 1;
1587 strbuf_addf(err,
Emily Shaffer303b3c12020-02-06 16:48:33 -08001588 _("Could not access submodule '%s'\n"),
Stefan Beller26f80cc2018-11-28 16:27:54 -08001589 ce->name);
1590 }
Peter Kaestle505a2762020-12-09 11:58:44 +01001591 strbuf_release(&empty_submodule_path);
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001592 }
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001593 }
Glen Choo73bc90d2022-03-07 16:14:30 -08001594 return NULL;
1595}
1596
Glen Choob90d9f72022-03-07 16:14:32 -08001597static struct fetch_task *
1598get_fetch_task_from_changed(struct submodule_parallel_fetch *spf,
1599 struct strbuf *err)
1600{
1601 for (; spf->changed_count < spf->changed_submodule_names.nr;
1602 spf->changed_count++) {
1603 struct string_list_item item =
1604 spf->changed_submodule_names.items[spf->changed_count];
1605 struct changed_submodule_data *cs_data = item.util;
1606 struct fetch_task *task;
1607
1608 if (!is_tree_submodule_active(spf->r, cs_data->super_oid,cs_data->path))
1609 continue;
1610
1611 task = fetch_task_create(spf, cs_data->path,
1612 cs_data->super_oid);
1613 if (!task)
1614 continue;
1615
1616 if (!task->repo) {
1617 strbuf_addf(err, _("Could not access submodule '%s' at commit %s\n"),
1618 cs_data->path,
1619 find_unique_abbrev(cs_data->super_oid, DEFAULT_ABBREV));
1620
1621 fetch_task_release(task);
1622 free(task);
1623 continue;
1624 }
1625
1626 if (!spf->quiet)
1627 strbuf_addf(err,
1628 _("Fetching submodule %s%s at commit %s\n"),
1629 spf->prefix, task->sub->path,
1630 find_unique_abbrev(cs_data->super_oid,
1631 DEFAULT_ABBREV));
1632
1633 spf->changed_count++;
1634 /*
1635 * NEEDSWORK: Submodules set/unset a value for
1636 * core.worktree when they are populated/unpopulated by
1637 * "git checkout" (and similar commands, see
1638 * submodule_move_head() and
1639 * connect_work_tree_and_git_dir()), but if the
1640 * submodule is unpopulated in another way (e.g. "git
1641 * rm", "rm -r"), core.worktree will still be set even
1642 * though the directory doesn't exist, and the child
1643 * process will crash while trying to chdir into the
1644 * nonexistent directory.
1645 *
1646 * In this case, we know that the submodule has no
1647 * working tree, so we can work around this by
1648 * setting "--work-tree=." (--bare does not work because
1649 * worktree settings take precedence over bare-ness).
1650 * However, this is not necessarily true in other cases,
1651 * so a generalized solution is still necessary.
1652 *
1653 * Possible solutions:
1654 * - teach "git [add|rm]" to unset core.worktree and
1655 * discourage users from removing submodules without
1656 * using a Git command.
1657 * - teach submodule child processes to ignore stale
1658 * core.worktree values.
1659 */
1660 strvec_push(&task->git_args, "--work-tree=.");
1661 return task;
1662 }
1663 return NULL;
1664}
1665
Glen Choo73bc90d2022-03-07 16:14:30 -08001666static int get_next_submodule(struct child_process *cp, struct strbuf *err,
1667 void *data, void **task_cb)
1668{
1669 struct submodule_parallel_fetch *spf = data;
Glen Choob90d9f72022-03-07 16:14:32 -08001670 struct fetch_task *task =
1671 get_fetch_task_from_index(spf, err);
1672 if (!task)
1673 task = get_fetch_task_from_changed(spf, err);
Glen Choo73bc90d2022-03-07 16:14:30 -08001674
1675 if (task) {
1676 struct strbuf submodule_prefix = STRBUF_INIT;
1677
1678 child_process_init(cp);
1679 cp->dir = task->repo->gitdir;
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02001680 prepare_submodule_repo_env_in_gitdir(&cp->env);
Glen Choo73bc90d2022-03-07 16:14:30 -08001681 cp->git_cmd = 1;
1682 strvec_init(&cp->args);
Glen Choob90d9f72022-03-07 16:14:32 -08001683 if (task->git_args.nr)
1684 strvec_pushv(&cp->args, task->git_args.v);
Glen Choo73bc90d2022-03-07 16:14:30 -08001685 strvec_pushv(&cp->args, spf->args.v);
1686 strvec_push(&cp->args, task->default_argv);
1687 strvec_push(&cp->args, "--submodule-prefix");
1688
1689 strbuf_addf(&submodule_prefix, "%s%s/",
1690 spf->prefix,
1691 task->sub->path);
1692 strvec_push(&cp->args, submodule_prefix.buf);
1693 *task_cb = task;
1694
1695 strbuf_release(&submodule_prefix);
Glen Choob90d9f72022-03-07 16:14:32 -08001696 string_list_insert(&spf->seen_submodule_names, task->sub->name);
Glen Choo73bc90d2022-03-07 16:14:30 -08001697 return 1;
1698 }
Stefan Bellerbe76c212018-12-06 13:26:55 -08001699
1700 if (spf->oid_fetch_tasks_nr) {
1701 struct fetch_task *task =
1702 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr - 1];
1703 struct strbuf submodule_prefix = STRBUF_INIT;
1704 spf->oid_fetch_tasks_nr--;
1705
1706 strbuf_addf(&submodule_prefix, "%s%s/",
1707 spf->prefix, task->sub->path);
1708
1709 child_process_init(cp);
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02001710 prepare_submodule_repo_env_in_gitdir(&cp->env);
Stefan Bellerbe76c212018-12-06 13:26:55 -08001711 cp->git_cmd = 1;
1712 cp->dir = task->repo->gitdir;
1713
Jeff Kingc972bf42020-07-28 16:25:12 -04001714 strvec_init(&cp->args);
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001715 strvec_pushv(&cp->args, spf->args.v);
Jeff Kingc972bf42020-07-28 16:25:12 -04001716 strvec_push(&cp->args, "on-demand");
1717 strvec_push(&cp->args, "--submodule-prefix");
1718 strvec_push(&cp->args, submodule_prefix.buf);
Stefan Bellerbe76c212018-12-06 13:26:55 -08001719
1720 /* NEEDSWORK: have get_default_remote from submodule--helper */
Jeff Kingc972bf42020-07-28 16:25:12 -04001721 strvec_push(&cp->args, "origin");
Stefan Bellerbe76c212018-12-06 13:26:55 -08001722 oid_array_for_each_unique(task->commits,
1723 append_oid_to_argv, &cp->args);
1724
1725 *task_cb = task;
1726 strbuf_release(&submodule_prefix);
1727 return 1;
1728 }
1729
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001730 return 0;
1731}
1732
Stefan Beller2a73b3d2016-02-29 13:57:06 -08001733static int fetch_start_failure(struct strbuf *err,
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001734 void *cb, void *task_cb)
1735{
1736 struct submodule_parallel_fetch *spf = cb;
Stefan Bellerbe76c212018-12-06 13:26:55 -08001737 struct fetch_task *task = task_cb;
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001738
1739 spf->result = 1;
1740
Stefan Bellerbe76c212018-12-06 13:26:55 -08001741 fetch_task_release(task);
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001742 return 0;
1743}
1744
Stefan Bellerbe76c212018-12-06 13:26:55 -08001745static int commit_missing_in_sub(const struct object_id *oid, void *data)
1746{
1747 struct repository *subrepo = data;
1748
1749 enum object_type type = oid_object_info(subrepo, oid, NULL);
1750
1751 return type != OBJ_COMMIT;
1752}
1753
Stefan Beller2a73b3d2016-02-29 13:57:06 -08001754static int fetch_finish(int retvalue, struct strbuf *err,
1755 void *cb, void *task_cb)
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001756{
1757 struct submodule_parallel_fetch *spf = cb;
Stefan Bellerbe76c212018-12-06 13:26:55 -08001758 struct fetch_task *task = task_cb;
1759
1760 struct string_list_item *it;
Glen Choo6e1e0c92022-03-07 16:14:29 -08001761 struct changed_submodule_data *cs_data;
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001762
Emily Shaffer02225402020-01-16 14:20:12 -08001763 if (!task || !task->sub)
1764 BUG("callback cookie bogus");
1765
1766 if (retvalue) {
Jonathan Tanbd5e5672019-03-13 10:57:38 -07001767 /*
1768 * NEEDSWORK: This indicates that the overall fetch
1769 * failed, even though there may be a subsequent fetch
1770 * by commit hash that might work. It may be a good
1771 * idea to not indicate failure in this case, and only
1772 * indicate failure if the subsequent fetch fails.
1773 */
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001774 spf->result = 1;
1775
Emily Shaffer02225402020-01-16 14:20:12 -08001776 strbuf_addf(&spf->submodules_with_errors, "\t%s\n",
1777 task->sub->name);
1778 }
Stefan Bellerbe76c212018-12-06 13:26:55 -08001779
1780 /* Is this the second time we process this submodule? */
1781 if (task->commits)
1782 goto out;
1783
1784 it = string_list_lookup(&spf->changed_submodule_names, task->sub->name);
1785 if (!it)
1786 /* Could be an unchanged submodule, not contained in the list */
1787 goto out;
1788
Glen Choo6e1e0c92022-03-07 16:14:29 -08001789 cs_data = it->util;
1790 oid_array_filter(&cs_data->new_commits,
Stefan Bellerbe76c212018-12-06 13:26:55 -08001791 commit_missing_in_sub,
1792 task->repo);
1793
1794 /* Are there commits we want, but do not exist? */
Glen Choo6e1e0c92022-03-07 16:14:29 -08001795 if (cs_data->new_commits.nr) {
1796 task->commits = &cs_data->new_commits;
Stefan Bellerbe76c212018-12-06 13:26:55 -08001797 ALLOC_GROW(spf->oid_fetch_tasks,
1798 spf->oid_fetch_tasks_nr + 1,
1799 spf->oid_fetch_tasks_alloc);
1800 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr] = task;
1801 spf->oid_fetch_tasks_nr++;
1802 return 0;
1803 }
1804
1805out:
1806 fetch_task_release(task);
1807
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001808 return 0;
1809}
1810
Glen Choob90d9f72022-03-07 16:14:32 -08001811int fetch_submodules(struct repository *r,
1812 const struct strvec *options,
1813 const char *prefix, int command_line_option,
1814 int default_option,
1815 int quiet, int max_parallel_jobs)
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001816{
1817 int i;
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001818 struct submodule_parallel_fetch spf = SPF_INIT;
1819
Brandon Williamse7241972017-12-12 11:53:52 -08001820 spf.r = r;
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001821 spf.command_line_option = command_line_option;
Brandon Williams8fa29152017-08-02 12:49:19 -07001822 spf.default_option = default_option;
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001823 spf.quiet = quiet;
1824 spf.prefix = prefix;
1825
Brandon Williamse7241972017-12-12 11:53:52 -08001826 if (!r->worktree)
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001827 goto out;
1828
Brandon Williamse7241972017-12-12 11:53:52 -08001829 if (repo_read_index(r) < 0)
Ralf Thielowa4ffbbb2020-01-15 19:07:01 +01001830 die(_("index file corrupt"));
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001831
Jeff Kingc972bf42020-07-28 16:25:12 -04001832 strvec_push(&spf.args, "fetch");
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001833 for (i = 0; i < options->nr; i++)
1834 strvec_push(&spf.args, options->v[i]);
Jeff Kingc972bf42020-07-28 16:25:12 -04001835 strvec_push(&spf.args, "--recurse-submodules-default");
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001836 /* default value, "--submodule-prefix" and its value are added later */
1837
Stefan Beller16dd6fe2018-11-28 16:27:51 -08001838 calculate_changed_submodule_paths(r, &spf.changed_submodule_names);
1839 string_list_sort(&spf.changed_submodule_names);
Jeff Hostetleree4512e2019-02-22 14:25:01 -08001840 run_processes_parallel_tr2(max_parallel_jobs,
1841 get_next_submodule,
1842 fetch_start_failure,
1843 fetch_finish,
1844 &spf,
1845 "submodule", "parallel/fetch");
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001846
Emily Shaffer02225402020-01-16 14:20:12 -08001847 if (spf.submodules_with_errors.len > 0)
1848 fprintf(stderr, _("Errors during submodule fetch:\n%s"),
1849 spf.submodules_with_errors.buf);
1850
1851
Jeff Kingc972bf42020-07-28 16:25:12 -04001852 strvec_clear(&spf.args);
Jens Lehmann88a21972011-03-06 23:10:46 +01001853out:
Glen Choo6e1e0c92022-03-07 16:14:29 -08001854 free_submodules_data(&spf.changed_submodule_names);
Stefan Bellerfe85ee62015-12-15 16:04:11 -08001855 return spf.result;
Jens Lehmann7dce19d2010-11-12 13:54:52 +01001856}
1857
Jens Lehmann3bfc4502010-03-13 23:00:27 +01001858unsigned is_submodule_modified(const char *path, int ignore_untracked)
Jens Lehmannee6fc512010-01-16 18:42:24 +01001859{
René Scharfed3180272014-08-19 21:09:35 +02001860 struct child_process cp = CHILD_PROCESS_INIT;
Jens Lehmannee6fc512010-01-16 18:42:24 +01001861 struct strbuf buf = STRBUF_INIT;
Stefan Belleraf6865a2017-03-24 17:36:06 -07001862 FILE *fp;
Jens Lehmannc7e1a732010-03-04 22:20:33 +01001863 unsigned dirty_submodule = 0;
Jens Lehmanneee49b62010-04-10 19:01:12 +02001864 const char *git_dir;
Stefan Belleraf6865a2017-03-24 17:36:06 -07001865 int ignore_cp_exit_code = 0;
Jens Lehmannee6fc512010-01-16 18:42:24 +01001866
Jens Lehmanneee49b62010-04-10 19:01:12 +02001867 strbuf_addf(&buf, "%s/.git", path);
Junio C Hamano13d6ec92011-08-22 14:04:56 -07001868 git_dir = read_gitfile(buf.buf);
Jens Lehmanneee49b62010-04-10 19:01:12 +02001869 if (!git_dir)
1870 git_dir = buf.buf;
Stefan Beller5c896f72017-03-24 17:36:08 -07001871 if (!is_git_directory(git_dir)) {
1872 if (is_directory(git_dir))
1873 die(_("'%s' not recognized as a git repository"), git_dir);
Jens Lehmannee6fc512010-01-16 18:42:24 +01001874 strbuf_release(&buf);
1875 /* The submodule is not checked out, so it is not modified */
1876 return 0;
Jens Lehmannee6fc512010-01-16 18:42:24 +01001877 }
1878 strbuf_reset(&buf);
1879
Jeff Kingc972bf42020-07-28 16:25:12 -04001880 strvec_pushl(&cp.args, "status", "--porcelain=2", NULL);
Jens Lehmann3bfc4502010-03-13 23:00:27 +01001881 if (ignore_untracked)
Jeff Kingc972bf42020-07-28 16:25:12 -04001882 strvec_push(&cp.args, "-uno");
Jens Lehmann3bfc4502010-03-13 23:00:27 +01001883
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02001884 prepare_submodule_repo_env(&cp.env);
Jens Lehmannee6fc512010-01-16 18:42:24 +01001885 cp.git_cmd = 1;
1886 cp.no_stdin = 1;
1887 cp.out = -1;
Jens Lehmanneee49b62010-04-10 19:01:12 +02001888 cp.dir = path;
Jens Lehmannee6fc512010-01-16 18:42:24 +01001889 if (start_command(&cp))
Ralf Thielowa4ffbbb2020-01-15 19:07:01 +01001890 die(_("Could not run 'git status --porcelain=2' in submodule %s"), path);
Jens Lehmannee6fc512010-01-16 18:42:24 +01001891
Stefan Belleraf6865a2017-03-24 17:36:06 -07001892 fp = xfdopen(cp.out, "r");
1893 while (strbuf_getwholeline(&buf, fp, '\n') != EOF) {
Stefan Bellerfcecf0b2017-03-24 17:36:07 -07001894 /* regular untracked files */
1895 if (buf.buf[0] == '?')
Jens Lehmannc7e1a732010-03-04 22:20:33 +01001896 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
Jens Lehmannee6fc512010-01-16 18:42:24 +01001897
Stefan Beller40069d62017-03-29 15:26:16 -07001898 if (buf.buf[0] == 'u' ||
1899 buf.buf[0] == '1' ||
1900 buf.buf[0] == '2') {
1901 /* T = line type, XY = status, SSSS = submodule state */
1902 if (buf.len < strlen("T XY SSSS"))
Johannes Schindelin033abf92018-05-02 11:38:39 +02001903 BUG("invalid status --porcelain=2 line %s",
Stefan Beller40069d62017-03-29 15:26:16 -07001904 buf.buf);
1905
1906 if (buf.buf[5] == 'S' && buf.buf[8] == 'U')
1907 /* nested untracked file */
1908 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1909
1910 if (buf.buf[0] == 'u' ||
1911 buf.buf[0] == '2' ||
1912 memcmp(buf.buf + 5, "S..U", 4))
1913 /* other change */
1914 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
1915 }
Stefan Beller64f9a942017-03-24 17:36:05 -07001916
1917 if ((dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
1918 ((dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
Stefan Belleraf6865a2017-03-24 17:36:06 -07001919 ignore_untracked)) {
1920 /*
1921 * We're not interested in any further information from
1922 * the child any more, neither output nor its exit code.
1923 */
1924 ignore_cp_exit_code = 1;
Stefan Beller64f9a942017-03-24 17:36:05 -07001925 break;
Stefan Belleraf6865a2017-03-24 17:36:06 -07001926 }
Jens Lehmannee6fc512010-01-16 18:42:24 +01001927 }
Stefan Belleraf6865a2017-03-24 17:36:06 -07001928 fclose(fp);
Jens Lehmannee6fc512010-01-16 18:42:24 +01001929
Stefan Belleraf6865a2017-03-24 17:36:06 -07001930 if (finish_command(&cp) && !ignore_cp_exit_code)
Ralf Thielowa4ffbbb2020-01-15 19:07:01 +01001931 die(_("'git status --porcelain=2' failed in submodule %s"), path);
Jens Lehmannee6fc512010-01-16 18:42:24 +01001932
Jens Lehmannee6fc512010-01-16 18:42:24 +01001933 strbuf_release(&buf);
Jens Lehmannc7e1a732010-03-04 22:20:33 +01001934 return dirty_submodule;
Jens Lehmannee6fc512010-01-16 18:42:24 +01001935}
Heiko Voigt68d03e42010-07-07 15:39:13 +02001936
Jens Lehmann293ab152012-09-26 20:21:13 +02001937int submodule_uses_gitfile(const char *path)
1938{
René Scharfed3180272014-08-19 21:09:35 +02001939 struct child_process cp = CHILD_PROCESS_INIT;
Jens Lehmann293ab152012-09-26 20:21:13 +02001940 struct strbuf buf = STRBUF_INIT;
1941 const char *git_dir;
1942
1943 strbuf_addf(&buf, "%s/.git", path);
1944 git_dir = read_gitfile(buf.buf);
1945 if (!git_dir) {
1946 strbuf_release(&buf);
1947 return 0;
1948 }
1949 strbuf_release(&buf);
1950
1951 /* Now test that all nested submodules use a gitfile too */
Junio C Hamanoafbdba32020-08-26 15:25:03 -07001952 strvec_pushl(&cp.args,
1953 "submodule", "foreach", "--quiet", "--recursive",
1954 "test -f .git", NULL);
1955
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02001956 prepare_submodule_repo_env(&cp.env);
Jens Lehmann293ab152012-09-26 20:21:13 +02001957 cp.git_cmd = 1;
1958 cp.no_stdin = 1;
1959 cp.no_stderr = 1;
1960 cp.no_stdout = 1;
1961 cp.dir = path;
1962 if (run_command(&cp))
1963 return 0;
1964
1965 return 1;
1966}
1967
Stefan Beller83b76962016-12-20 15:20:11 -08001968/*
1969 * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
1970 * when doing so.
1971 *
1972 * Return 1 if we'd lose data, return 0 if the removal is fine,
1973 * and negative values for errors.
1974 */
1975int bad_to_remove_submodule(const char *path, unsigned flags)
Jens Lehmann293ab152012-09-26 20:21:13 +02001976{
Jens Lehmann293ab152012-09-26 20:21:13 +02001977 ssize_t len;
René Scharfed3180272014-08-19 21:09:35 +02001978 struct child_process cp = CHILD_PROCESS_INIT;
Jens Lehmann293ab152012-09-26 20:21:13 +02001979 struct strbuf buf = STRBUF_INIT;
Stefan Beller83b76962016-12-20 15:20:11 -08001980 int ret = 0;
Jens Lehmann293ab152012-09-26 20:21:13 +02001981
René Scharfedbe44fa2015-05-19 23:44:23 +02001982 if (!file_exists(path) || is_empty_dir(path))
Jens Lehmann293ab152012-09-26 20:21:13 +02001983 return 0;
1984
Stefan Beller83b76962016-12-20 15:20:11 -08001985 if (!submodule_uses_gitfile(path))
1986 return 1;
1987
Jeff Kingc972bf42020-07-28 16:25:12 -04001988 strvec_pushl(&cp.args, "status", "--porcelain",
Jeff Kingf6d89422020-07-28 16:26:31 -04001989 "--ignore-submodules=none", NULL);
Stefan Beller83b76962016-12-20 15:20:11 -08001990
1991 if (flags & SUBMODULE_REMOVAL_IGNORE_UNTRACKED)
Jeff Kingc972bf42020-07-28 16:25:12 -04001992 strvec_push(&cp.args, "-uno");
Stefan Beller83b76962016-12-20 15:20:11 -08001993 else
Jeff Kingc972bf42020-07-28 16:25:12 -04001994 strvec_push(&cp.args, "-uall");
Stefan Beller83b76962016-12-20 15:20:11 -08001995
1996 if (!(flags & SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))
Jeff Kingc972bf42020-07-28 16:25:12 -04001997 strvec_push(&cp.args, "--ignored");
Stefan Beller83b76962016-12-20 15:20:11 -08001998
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02001999 prepare_submodule_repo_env(&cp.env);
Jens Lehmann293ab152012-09-26 20:21:13 +02002000 cp.git_cmd = 1;
2001 cp.no_stdin = 1;
2002 cp.out = -1;
2003 cp.dir = path;
Stefan Beller83b76962016-12-20 15:20:11 -08002004 if (start_command(&cp)) {
2005 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
Ralf Thielow35ad44c2017-04-13 18:40:45 +02002006 die(_("could not start 'git status' in submodule '%s'"),
Stefan Beller83b76962016-12-20 15:20:11 -08002007 path);
2008 ret = -1;
2009 goto out;
2010 }
Jens Lehmann293ab152012-09-26 20:21:13 +02002011
2012 len = strbuf_read(&buf, cp.out, 1024);
2013 if (len > 2)
Stefan Beller83b76962016-12-20 15:20:11 -08002014 ret = 1;
Jens Lehmann293ab152012-09-26 20:21:13 +02002015 close(cp.out);
2016
Stefan Beller83b76962016-12-20 15:20:11 -08002017 if (finish_command(&cp)) {
2018 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
Ralf Thielow35ad44c2017-04-13 18:40:45 +02002019 die(_("could not run 'git status' in submodule '%s'"),
Stefan Beller83b76962016-12-20 15:20:11 -08002020 path);
2021 ret = -1;
2022 }
2023out:
Jens Lehmann293ab152012-09-26 20:21:13 +02002024 strbuf_release(&buf);
Stefan Beller83b76962016-12-20 15:20:11 -08002025 return ret;
Jens Lehmann293ab152012-09-26 20:21:13 +02002026}
2027
Stefan Beller898c2e62018-12-14 15:59:43 -08002028void submodule_unset_core_worktree(const struct submodule *sub)
2029{
Jonathan Tance125d42021-09-15 11:59:19 -07002030 struct strbuf config_path = STRBUF_INIT;
Stefan Beller898c2e62018-12-14 15:59:43 -08002031
Jonathan Tance125d42021-09-15 11:59:19 -07002032 submodule_name_to_gitdir(&config_path, the_repository, sub->name);
2033 strbuf_addstr(&config_path, "/config");
2034
2035 if (git_config_set_in_file_gently(config_path.buf, "core.worktree", NULL))
Stefan Beller898c2e62018-12-14 15:59:43 -08002036 warning(_("Could not unset core.worktree setting in submodule '%s'"),
2037 sub->path);
2038
Jonathan Tance125d42021-09-15 11:59:19 -07002039 strbuf_release(&config_path);
Stefan Beller898c2e62018-12-14 15:59:43 -08002040}
2041
Stefan Beller202275b2017-03-14 14:46:36 -07002042static const char *get_super_prefix_or_empty(void)
2043{
2044 const char *s = get_super_prefix();
2045 if (!s)
2046 s = "";
2047 return s;
2048}
2049
Stefan Beller6e3c1592017-03-14 14:46:37 -07002050static int submodule_has_dirty_index(const struct submodule *sub)
2051{
2052 struct child_process cp = CHILD_PROCESS_INIT;
2053
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02002054 prepare_submodule_repo_env(&cp.env);
Stefan Beller6e3c1592017-03-14 14:46:37 -07002055
2056 cp.git_cmd = 1;
Jeff Kingc972bf42020-07-28 16:25:12 -04002057 strvec_pushl(&cp.args, "diff-index", "--quiet",
Jeff Kingf6d89422020-07-28 16:26:31 -04002058 "--cached", "HEAD", NULL);
Stefan Beller6e3c1592017-03-14 14:46:37 -07002059 cp.no_stdin = 1;
2060 cp.no_stdout = 1;
2061 cp.dir = sub->path;
2062 if (start_command(&cp))
Ralf Thielowa4ffbbb2020-01-15 19:07:01 +01002063 die(_("could not recurse into submodule '%s'"), sub->path);
Stefan Beller6e3c1592017-03-14 14:46:37 -07002064
2065 return finish_command(&cp);
2066}
2067
2068static void submodule_reset_index(const char *path)
2069{
2070 struct child_process cp = CHILD_PROCESS_INIT;
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02002071 prepare_submodule_repo_env(&cp.env);
Stefan Beller6e3c1592017-03-14 14:46:37 -07002072
2073 cp.git_cmd = 1;
2074 cp.no_stdin = 1;
2075 cp.dir = path;
2076
Jeff Kingc972bf42020-07-28 16:25:12 -04002077 strvec_pushf(&cp.args, "--super-prefix=%s%s/",
Jeff Kingf6d89422020-07-28 16:26:31 -04002078 get_super_prefix_or_empty(), path);
Elijah Newren94b7f152021-09-27 16:33:47 +00002079 /* TODO: determine if this might overwright untracked files */
Jeff Kingc972bf42020-07-28 16:25:12 -04002080 strvec_pushl(&cp.args, "read-tree", "-u", "--reset", NULL);
Stefan Beller6e3c1592017-03-14 14:46:37 -07002081
Jeff Kingc972bf42020-07-28 16:25:12 -04002082 strvec_push(&cp.args, empty_tree_oid_hex());
Stefan Beller6e3c1592017-03-14 14:46:37 -07002083
2084 if (run_command(&cp))
Ralf Thielowa4ffbbb2020-01-15 19:07:01 +01002085 die(_("could not reset submodule index"));
Stefan Beller6e3c1592017-03-14 14:46:37 -07002086}
2087
2088/**
2089 * Moves a submodule at a given path from a given head to another new head.
2090 * For edge cases (a submodule coming into existence or removing a submodule)
2091 * pass NULL for old or new respectively.
2092 */
2093int submodule_move_head(const char *path,
Brandon Williamsbc099912018-02-14 10:59:49 -08002094 const char *old_head,
2095 const char *new_head,
Stefan Beller6e3c1592017-03-14 14:46:37 -07002096 unsigned flags)
2097{
2098 int ret = 0;
2099 struct child_process cp = CHILD_PROCESS_INIT;
2100 const struct submodule *sub;
Stefan Bellerf2d48992017-04-18 14:37:24 -07002101 int *error_code_ptr, error_code;
Stefan Beller6e3c1592017-03-14 14:46:37 -07002102
Brandon Williams627d9342017-06-22 11:43:46 -07002103 if (!is_submodule_active(the_repository, path))
Stefan Beller823bab02017-04-18 14:37:23 -07002104 return 0;
2105
Stefan Bellerf2d48992017-04-18 14:37:24 -07002106 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
2107 /*
2108 * Pass non NULL pointer to is_submodule_populated_gently
2109 * to prevent die()-ing. We'll use connect_work_tree_and_git_dir
2110 * to fixup the submodule in the force case later.
2111 */
2112 error_code_ptr = &error_code;
2113 else
2114 error_code_ptr = NULL;
2115
Brandon Williamsbc099912018-02-14 10:59:49 -08002116 if (old_head && !is_submodule_populated_gently(path, error_code_ptr))
Stefan Bellerf2d48992017-04-18 14:37:24 -07002117 return 0;
Stefan Beller6e3c1592017-03-14 14:46:37 -07002118
brian m. carlson14228442021-04-26 01:02:56 +00002119 sub = submodule_from_path(the_repository, null_oid(), path);
Stefan Beller6e3c1592017-03-14 14:46:37 -07002120
2121 if (!sub)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002122 BUG("could not get submodule information for '%s'", path);
Stefan Beller6e3c1592017-03-14 14:46:37 -07002123
Brandon Williamsbc099912018-02-14 10:59:49 -08002124 if (old_head && !(flags & SUBMODULE_MOVE_HEAD_FORCE)) {
Stefan Beller6e3c1592017-03-14 14:46:37 -07002125 /* Check if the submodule has a dirty index. */
2126 if (submodule_has_dirty_index(sub))
2127 return error(_("submodule '%s' has dirty index"), path);
2128 }
2129
2130 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
Brandon Williamsbc099912018-02-14 10:59:49 -08002131 if (old_head) {
Stefan Beller6e3c1592017-03-14 14:46:37 -07002132 if (!submodule_uses_gitfile(path))
Jeff Kingcf7a9012019-05-09 17:27:31 -04002133 absorb_git_dir_into_superproject(path,
Stefan Beller6e3c1592017-03-14 14:46:37 -07002134 ABSORB_GITDIR_RECURSE_SUBMODULES);
2135 } else {
Jonathan Tance125d42021-09-15 11:59:19 -07002136 struct strbuf gitdir = STRBUF_INIT;
2137 submodule_name_to_gitdir(&gitdir, the_repository,
2138 sub->name);
2139 connect_work_tree_and_git_dir(path, gitdir.buf, 0);
2140 strbuf_release(&gitdir);
Stefan Beller6e3c1592017-03-14 14:46:37 -07002141
2142 /* make sure the index is clean as well */
2143 submodule_reset_index(path);
2144 }
Stefan Bellerf2d48992017-04-18 14:37:24 -07002145
Brandon Williamsbc099912018-02-14 10:59:49 -08002146 if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
Jonathan Tance125d42021-09-15 11:59:19 -07002147 struct strbuf gitdir = STRBUF_INIT;
2148 submodule_name_to_gitdir(&gitdir, the_repository,
2149 sub->name);
2150 connect_work_tree_and_git_dir(path, gitdir.buf, 1);
2151 strbuf_release(&gitdir);
Stefan Bellerf2d48992017-04-18 14:37:24 -07002152 }
Stefan Beller6e3c1592017-03-14 14:46:37 -07002153 }
2154
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02002155 prepare_submodule_repo_env(&cp.env);
Stefan Beller6e3c1592017-03-14 14:46:37 -07002156
2157 cp.git_cmd = 1;
2158 cp.no_stdin = 1;
2159 cp.dir = path;
2160
Jeff Kingc972bf42020-07-28 16:25:12 -04002161 strvec_pushf(&cp.args, "--super-prefix=%s%s/",
Jeff Kingf6d89422020-07-28 16:26:31 -04002162 get_super_prefix_or_empty(), path);
Jeff Kingc972bf42020-07-28 16:25:12 -04002163 strvec_pushl(&cp.args, "read-tree", "--recurse-submodules", NULL);
Stefan Beller6e3c1592017-03-14 14:46:37 -07002164
2165 if (flags & SUBMODULE_MOVE_HEAD_DRY_RUN)
Jeff Kingc972bf42020-07-28 16:25:12 -04002166 strvec_push(&cp.args, "-n");
Stefan Beller6e3c1592017-03-14 14:46:37 -07002167 else
Jeff Kingc972bf42020-07-28 16:25:12 -04002168 strvec_push(&cp.args, "-u");
Stefan Beller6e3c1592017-03-14 14:46:37 -07002169
2170 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
Jeff Kingc972bf42020-07-28 16:25:12 -04002171 strvec_push(&cp.args, "--reset");
Stefan Beller6e3c1592017-03-14 14:46:37 -07002172 else
Jeff Kingc972bf42020-07-28 16:25:12 -04002173 strvec_push(&cp.args, "-m");
Stefan Beller6e3c1592017-03-14 14:46:37 -07002174
Stefan Beller7dcc1f42018-01-05 12:03:04 -08002175 if (!(flags & SUBMODULE_MOVE_HEAD_FORCE))
Jeff Kingc972bf42020-07-28 16:25:12 -04002176 strvec_push(&cp.args, old_head ? old_head : empty_tree_oid_hex());
Stefan Beller7dcc1f42018-01-05 12:03:04 -08002177
Jeff Kingc972bf42020-07-28 16:25:12 -04002178 strvec_push(&cp.args, new_head ? new_head : empty_tree_oid_hex());
Stefan Beller6e3c1592017-03-14 14:46:37 -07002179
2180 if (run_command(&cp)) {
Stefan Bellerba95d4e2018-06-20 15:32:53 -07002181 ret = error(_("Submodule '%s' could not be updated."), path);
Stefan Beller6e3c1592017-03-14 14:46:37 -07002182 goto out;
2183 }
2184
2185 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
Brandon Williamsbc099912018-02-14 10:59:49 -08002186 if (new_head) {
Stefan Bellera17062c2017-05-02 12:32:12 -07002187 child_process_init(&cp);
Stefan Beller6e3c1592017-03-14 14:46:37 -07002188 /* also set the HEAD accordingly */
Stefan Bellera17062c2017-05-02 12:32:12 -07002189 cp.git_cmd = 1;
2190 cp.no_stdin = 1;
2191 cp.dir = path;
Stefan Beller6e3c1592017-03-14 14:46:37 -07002192
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02002193 prepare_submodule_repo_env(&cp.env);
Jeff Kingc972bf42020-07-28 16:25:12 -04002194 strvec_pushl(&cp.args, "update-ref", "HEAD",
Jeff Kingf6d89422020-07-28 16:26:31 -04002195 "--no-deref", new_head, NULL);
Stefan Beller6e3c1592017-03-14 14:46:37 -07002196
Stefan Bellera17062c2017-05-02 12:32:12 -07002197 if (run_command(&cp)) {
Stefan Beller6e3c1592017-03-14 14:46:37 -07002198 ret = -1;
2199 goto out;
2200 }
2201 } else {
2202 struct strbuf sb = STRBUF_INIT;
2203
2204 strbuf_addf(&sb, "%s/.git", path);
2205 unlink_or_warn(sb.buf);
2206 strbuf_release(&sb);
2207
2208 if (is_empty_dir(path))
2209 rmdir_or_warn(path);
Stefan Beller898c2e62018-12-14 15:59:43 -08002210
2211 submodule_unset_core_worktree(sub);
Stefan Beller6e3c1592017-03-14 14:46:37 -07002212 }
2213 }
2214out:
2215 return ret;
2216}
2217
Johannes Schindelina8dee3c2019-10-01 23:27:18 +02002218int validate_submodule_git_dir(char *git_dir, const char *submodule_name)
2219{
2220 size_t len = strlen(git_dir), suffix_len = strlen(submodule_name);
2221 char *p;
2222 int ret = 0;
2223
2224 if (len <= suffix_len || (p = git_dir + len - suffix_len)[-1] != '/' ||
2225 strcmp(p, submodule_name))
2226 BUG("submodule name '%s' not a suffix of git dir '%s'",
2227 submodule_name, git_dir);
2228
2229 /*
2230 * We prevent the contents of sibling submodules' git directories to
2231 * clash.
2232 *
2233 * Example: having a submodule named `hippo` and another one named
2234 * `hippo/hooks` would result in the git directories
2235 * `.git/modules/hippo/` and `.git/modules/hippo/hooks/`, respectively,
2236 * but the latter directory is already designated to contain the hooks
2237 * of the former.
2238 */
2239 for (; *p; p++) {
2240 if (is_dir_sep(*p)) {
2241 char c = *p;
2242
2243 *p = '\0';
2244 if (is_git_directory(git_dir))
2245 ret = -1;
2246 *p = c;
2247
2248 if (ret < 0)
2249 return error(_("submodule git dir '%s' is "
2250 "inside git dir '%.*s'"),
2251 git_dir,
2252 (int)(p - git_dir), git_dir);
2253 }
2254 }
2255
2256 return 0;
2257}
2258
Stefan Bellerf6f85862016-12-12 11:04:35 -08002259/*
2260 * Embeds a single submodules git directory into the superprojects git dir,
2261 * non recursively.
2262 */
Jeff Kingcf7a9012019-05-09 17:27:31 -04002263static void relocate_single_git_dir_into_superproject(const char *path)
Stefan Bellerf6f85862016-12-12 11:04:35 -08002264{
2265 char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
Jonathan Tance125d42021-09-15 11:59:19 -07002266 struct strbuf new_gitdir = STRBUF_INIT;
Stefan Bellerf6f85862016-12-12 11:04:35 -08002267 const struct submodule *sub;
2268
2269 if (submodule_uses_worktrees(path))
2270 die(_("relocate_gitdir for submodule '%s' with "
2271 "more than one worktree not supported"), path);
2272
2273 old_git_dir = xstrfmt("%s/.git", path);
2274 if (read_gitfile(old_git_dir))
2275 /* If it is an actual gitfile, it doesn't need migration. */
2276 return;
2277
Johannes Schindelince83ead2017-03-08 16:43:40 +01002278 real_old_git_dir = real_pathdup(old_git_dir, 1);
Stefan Bellerf6f85862016-12-12 11:04:35 -08002279
brian m. carlson14228442021-04-26 01:02:56 +00002280 sub = submodule_from_path(the_repository, null_oid(), path);
Stefan Bellerf6f85862016-12-12 11:04:35 -08002281 if (!sub)
2282 die(_("could not lookup name for submodule '%s'"), path);
2283
Jonathan Tance125d42021-09-15 11:59:19 -07002284 submodule_name_to_gitdir(&new_gitdir, the_repository, sub->name);
2285 if (validate_submodule_git_dir(new_gitdir.buf, sub->name) < 0)
Johannes Schindelina8dee3c2019-10-01 23:27:18 +02002286 die(_("refusing to move '%s' into an existing git dir"),
2287 real_old_git_dir);
Jonathan Tance125d42021-09-15 11:59:19 -07002288 if (safe_create_leading_directories_const(new_gitdir.buf) < 0)
2289 die(_("could not create directory '%s'"), new_gitdir.buf);
2290 real_new_git_dir = real_pathdup(new_gitdir.buf, 1);
Stefan Bellerf6f85862016-12-12 11:04:35 -08002291
Stefan Bellerf6f85862016-12-12 11:04:35 -08002292 fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
Stefan Beller202275b2017-03-14 14:46:36 -07002293 get_super_prefix_or_empty(), path,
Stefan Bellerf6f85862016-12-12 11:04:35 -08002294 real_old_git_dir, real_new_git_dir);
2295
2296 relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
2297
2298 free(old_git_dir);
2299 free(real_old_git_dir);
2300 free(real_new_git_dir);
Jonathan Tance125d42021-09-15 11:59:19 -07002301 strbuf_release(&new_gitdir);
Stefan Bellerf6f85862016-12-12 11:04:35 -08002302}
2303
2304/*
2305 * Migrate the git directory of the submodule given by path from
2306 * having its git directory within the working tree to the git dir nested
2307 * in its superprojects git dir under modules/.
2308 */
Jeff Kingcf7a9012019-05-09 17:27:31 -04002309void absorb_git_dir_into_superproject(const char *path,
Stefan Bellerf6f85862016-12-12 11:04:35 -08002310 unsigned flags)
2311{
Stefan Bellerec9629b2017-01-25 15:04:50 -08002312 int err_code;
2313 const char *sub_git_dir;
Stefan Bellerf6f85862016-12-12 11:04:35 -08002314 struct strbuf gitdir = STRBUF_INIT;
Stefan Bellerf6f85862016-12-12 11:04:35 -08002315 strbuf_addf(&gitdir, "%s/.git", path);
Stefan Bellerec9629b2017-01-25 15:04:50 -08002316 sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code);
Stefan Bellerf6f85862016-12-12 11:04:35 -08002317
2318 /* Not populated? */
Stefan Bellerec9629b2017-01-25 15:04:50 -08002319 if (!sub_git_dir) {
Stefan Bellerec9629b2017-01-25 15:04:50 -08002320 const struct submodule *sub;
Jonathan Tance125d42021-09-15 11:59:19 -07002321 struct strbuf sub_gitdir = STRBUF_INIT;
Stefan Bellerf6f85862016-12-12 11:04:35 -08002322
Stefan Bellerec9629b2017-01-25 15:04:50 -08002323 if (err_code == READ_GITFILE_ERR_STAT_FAILED) {
2324 /* unpopulated as expected */
2325 strbuf_release(&gitdir);
2326 return;
2327 }
2328
2329 if (err_code != READ_GITFILE_ERR_NOT_A_REPO)
2330 /* We don't know what broke here. */
2331 read_gitfile_error_die(err_code, path, NULL);
2332
2333 /*
2334 * Maybe populated, but no git directory was found?
2335 * This can happen if the superproject is a submodule
2336 * itself and was just absorbed. The absorption of the
2337 * superproject did not rewrite the git file links yet,
2338 * fix it now.
2339 */
brian m. carlson14228442021-04-26 01:02:56 +00002340 sub = submodule_from_path(the_repository, null_oid(), path);
Stefan Bellerec9629b2017-01-25 15:04:50 -08002341 if (!sub)
2342 die(_("could not lookup name for submodule '%s'"), path);
Jonathan Tance125d42021-09-15 11:59:19 -07002343 submodule_name_to_gitdir(&sub_gitdir, the_repository, sub->name);
2344 connect_work_tree_and_git_dir(path, sub_gitdir.buf, 0);
2345 strbuf_release(&sub_gitdir);
Stefan Bellerec9629b2017-01-25 15:04:50 -08002346 } else {
2347 /* Is it already absorbed into the superprojects git dir? */
Johannes Schindelince83ead2017-03-08 16:43:40 +01002348 char *real_sub_git_dir = real_pathdup(sub_git_dir, 1);
2349 char *real_common_git_dir = real_pathdup(get_git_common_dir(), 1);
Stefan Bellerec9629b2017-01-25 15:04:50 -08002350
2351 if (!starts_with(real_sub_git_dir, real_common_git_dir))
Jeff Kingcf7a9012019-05-09 17:27:31 -04002352 relocate_single_git_dir_into_superproject(path);
Stefan Bellerec9629b2017-01-25 15:04:50 -08002353
2354 free(real_sub_git_dir);
2355 free(real_common_git_dir);
2356 }
2357 strbuf_release(&gitdir);
Stefan Bellerf6f85862016-12-12 11:04:35 -08002358
2359 if (flags & ABSORB_GITDIR_RECURSE_SUBMODULES) {
2360 struct child_process cp = CHILD_PROCESS_INIT;
2361 struct strbuf sb = STRBUF_INIT;
2362
2363 if (flags & ~ABSORB_GITDIR_RECURSE_SUBMODULES)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002364 BUG("we don't know how to pass the flags down?");
Stefan Bellerf6f85862016-12-12 11:04:35 -08002365
Stefan Beller202275b2017-03-14 14:46:36 -07002366 strbuf_addstr(&sb, get_super_prefix_or_empty());
Stefan Bellerf6f85862016-12-12 11:04:35 -08002367 strbuf_addstr(&sb, path);
2368 strbuf_addch(&sb, '/');
2369
2370 cp.dir = path;
2371 cp.git_cmd = 1;
2372 cp.no_stdin = 1;
Jeff Kingc972bf42020-07-28 16:25:12 -04002373 strvec_pushl(&cp.args, "--super-prefix", sb.buf,
Jeff Kingf6d89422020-07-28 16:26:31 -04002374 "submodule--helper",
Ævar Arnfjörð Bjarmason6e556c42022-06-28 12:05:29 +02002375 "absorbgitdirs", NULL);
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02002376 prepare_submodule_repo_env(&cp.env);
Stefan Bellerf6f85862016-12-12 11:04:35 -08002377 if (run_command(&cp))
2378 die(_("could not recurse into submodule '%s'"), path);
2379
2380 strbuf_release(&sb);
2381 }
Stefan Bellerf6f85862016-12-12 11:04:35 -08002382}
Stefan Bellerbf0231c2017-03-08 15:07:42 -08002383
Alexandr Miloslavskiy49d3c4b2020-03-10 13:11:24 +00002384int get_superproject_working_tree(struct strbuf *buf)
Stefan Bellerbf0231c2017-03-08 15:07:42 -08002385{
2386 struct child_process cp = CHILD_PROCESS_INIT;
2387 struct strbuf sb = STRBUF_INIT;
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +00002388 struct strbuf one_up = STRBUF_INIT;
Ævar Arnfjörð Bjarmasonbc57ba12022-07-01 12:42:52 +02002389 char *cwd = xgetcwd();
Alexandr Miloslavskiy49d3c4b2020-03-10 13:11:24 +00002390 int ret = 0;
Stefan Bellerbf0231c2017-03-08 15:07:42 -08002391 const char *subpath;
2392 int code;
2393 ssize_t len;
2394
2395 if (!is_inside_work_tree())
2396 /*
2397 * FIXME:
2398 * We might have a superproject, but it is harder
2399 * to determine.
2400 */
Alexandr Miloslavskiy49d3c4b2020-03-10 13:11:24 +00002401 return 0;
Stefan Bellerbf0231c2017-03-08 15:07:42 -08002402
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +00002403 if (!strbuf_realpath(&one_up, "../", 0))
Alexandr Miloslavskiy49d3c4b2020-03-10 13:11:24 +00002404 return 0;
Stefan Bellerbf0231c2017-03-08 15:07:42 -08002405
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +00002406 subpath = relative_path(cwd, one_up.buf, &sb);
2407 strbuf_release(&one_up);
Stefan Bellerbf0231c2017-03-08 15:07:42 -08002408
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +02002409 prepare_submodule_repo_env(&cp.env);
2410 strvec_pop(&cp.env);
Stefan Bellerbf0231c2017-03-08 15:07:42 -08002411
Jeff Kingc972bf42020-07-28 16:25:12 -04002412 strvec_pushl(&cp.args, "--literal-pathspecs", "-C", "..",
Jeff Kingf6d89422020-07-28 16:26:31 -04002413 "ls-files", "-z", "--stage", "--full-name", "--",
2414 subpath, NULL);
Stefan Bellerbf0231c2017-03-08 15:07:42 -08002415 strbuf_reset(&sb);
2416
2417 cp.no_stdin = 1;
2418 cp.no_stderr = 1;
2419 cp.out = -1;
2420 cp.git_cmd = 1;
2421
2422 if (start_command(&cp))
2423 die(_("could not start ls-files in .."));
2424
2425 len = strbuf_read(&sb, cp.out, PATH_MAX);
2426 close(cp.out);
2427
2428 if (starts_with(sb.buf, "160000")) {
2429 int super_sub_len;
2430 int cwd_len = strlen(cwd);
2431 char *super_sub, *super_wt;
2432
2433 /*
2434 * There is a superproject having this repo as a submodule.
2435 * The format is <mode> SP <hash> SP <stage> TAB <full name> \0,
2436 * We're only interested in the name after the tab.
2437 */
2438 super_sub = strchr(sb.buf, '\t') + 1;
Sam McKelviec5cbb272018-09-27 11:10:54 -07002439 super_sub_len = strlen(super_sub);
Stefan Bellerbf0231c2017-03-08 15:07:42 -08002440
2441 if (super_sub_len > cwd_len ||
2442 strcmp(&cwd[cwd_len - super_sub_len], super_sub))
Johannes Schindelinc3c34862018-05-02 11:38:41 +02002443 BUG("returned path string doesn't match cwd?");
Stefan Bellerbf0231c2017-03-08 15:07:42 -08002444
2445 super_wt = xstrdup(cwd);
2446 super_wt[cwd_len - super_sub_len] = '\0';
2447
Alexandr Miloslavskiy49d3c4b2020-03-10 13:11:24 +00002448 strbuf_realpath(buf, super_wt, 1);
2449 ret = 1;
Stefan Bellerbf0231c2017-03-08 15:07:42 -08002450 free(super_wt);
2451 }
Ævar Arnfjörð Bjarmasonbc57ba12022-07-01 12:42:52 +02002452 free(cwd);
Stefan Bellerbf0231c2017-03-08 15:07:42 -08002453 strbuf_release(&sb);
2454
2455 code = finish_command(&cp);
2456
2457 if (code == 128)
2458 /* '../' is not a git repository */
Alexandr Miloslavskiy49d3c4b2020-03-10 13:11:24 +00002459 return 0;
Stefan Bellerbf0231c2017-03-08 15:07:42 -08002460 if (code == 0 && len == 0)
2461 /* There is an unrelated git repository at '../' */
Alexandr Miloslavskiy49d3c4b2020-03-10 13:11:24 +00002462 return 0;
Stefan Bellerbf0231c2017-03-08 15:07:42 -08002463 if (code)
2464 die(_("ls-tree returned unexpected return code %d"), code);
2465
2466 return ret;
2467}
Nguyễn Thái Ngọc Duybbbb7de2017-03-26 09:42:30 +07002468
Han-Wen Nienhuys3ce08542017-09-25 17:59:26 +02002469/*
2470 * Put the gitdir for a submodule (given relative to the main
2471 * repository worktree) into `buf`, or return -1 on error.
2472 */
Nguyễn Thái Ngọc Duybbbb7de2017-03-26 09:42:30 +07002473int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
2474{
2475 const struct submodule *sub;
2476 const char *git_dir;
2477 int ret = 0;
2478
2479 strbuf_reset(buf);
2480 strbuf_addstr(buf, submodule);
2481 strbuf_complete(buf, '/');
2482 strbuf_addstr(buf, ".git");
2483
2484 git_dir = read_gitfile(buf->buf);
2485 if (git_dir) {
2486 strbuf_reset(buf);
2487 strbuf_addstr(buf, git_dir);
2488 }
2489 if (!is_git_directory(buf->buf)) {
brian m. carlson14228442021-04-26 01:02:56 +00002490 sub = submodule_from_path(the_repository, null_oid(),
2491 submodule);
Nguyễn Thái Ngọc Duybbbb7de2017-03-26 09:42:30 +07002492 if (!sub) {
2493 ret = -1;
2494 goto cleanup;
2495 }
2496 strbuf_reset(buf);
Jonathan Tance125d42021-09-15 11:59:19 -07002497 submodule_name_to_gitdir(buf, the_repository, sub->name);
Nguyễn Thái Ngọc Duybbbb7de2017-03-26 09:42:30 +07002498 }
2499
2500cleanup:
2501 return ret;
2502}
Jonathan Tance125d42021-09-15 11:59:19 -07002503
2504void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
2505 const char *submodule_name)
2506{
2507 /*
2508 * NEEDSWORK: The current way of mapping a submodule's name to
2509 * its location in .git/modules/ has problems with some naming
2510 * schemes. For example, if a submodule is named "foo" and
2511 * another is named "foo/bar" (whether present in the same
2512 * superproject commit or not - the problem will arise if both
2513 * superproject commits have been checked out at any point in
2514 * time), or if two submodule names only have different cases in
2515 * a case-insensitive filesystem.
2516 *
2517 * There are several solutions, including encoding the path in
2518 * some way, introducing a submodule.<name>.gitdir config in
2519 * .git/config (not .gitmodules) that allows overriding what the
2520 * gitdir of a submodule would be (and teach Git, upon noticing
2521 * a clash, to automatically determine a non-clashing name and
2522 * to write such a config), or introducing a
2523 * submodule.<name>.gitdir config in .gitmodules that repo
2524 * administrators can explicitly set. Nothing has been decided,
2525 * so for now, just append the name at the end of the path.
2526 */
2527 strbuf_repo_git_path(buf, r, "modules/");
2528 strbuf_addstr(buf, submodule_name);
2529}