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