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