Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 1 | #include "cache.h" |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 2 | #include "submodule-config.h" |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 3 | #include "submodule.h" |
| 4 | #include "dir.h" |
| 5 | #include "diff.h" |
| 6 | #include "commit.h" |
| 7 | #include "revision.h" |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 8 | #include "run-command.h" |
Jens Lehmann | c7e1a73 | 2010-03-04 22:20:33 +0100 | [diff] [blame] | 9 | #include "diffcore.h" |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 10 | #include "refs.h" |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 11 | #include "string-list.h" |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 12 | #include "sha1-array.h" |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 13 | #include "argv-array.h" |
Jens Lehmann | 5fee995 | 2013-07-30 21:50:34 +0200 | [diff] [blame] | 14 | #include "blob.h" |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 15 | #include "thread-utils.h" |
Jeff King | 4638728 | 2016-04-28 09:38:20 -0400 | [diff] [blame] | 16 | #include "quote.h" |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 17 | |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 18 | static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND; |
Stefan Beller | a028a19 | 2016-02-29 18:07:13 -0800 | [diff] [blame] | 19 | static int parallel_jobs = 1; |
Jeff King | 2721ce2 | 2016-06-13 06:04:20 -0400 | [diff] [blame] | 20 | static struct string_list changed_submodule_paths = STRING_LIST_INIT_NODUP; |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 21 | static int initialized_fetch_ref_tips; |
| 22 | static struct sha1_array ref_tips_before_fetch; |
| 23 | static struct sha1_array ref_tips_after_fetch; |
| 24 | |
Jens Lehmann | d4e98b5 | 2011-05-14 18:26:58 +0200 | [diff] [blame] | 25 | /* |
| 26 | * The following flag is set if the .gitmodules file is unmerged. We then |
| 27 | * disable recursion for all submodules where .git/config doesn't have a |
| 28 | * matching config entry because we can't guess what might be configured in |
| 29 | * .gitmodules unless the user resolves the conflict. When a command line |
| 30 | * option is given (which always overrides configuration) this flag will be |
| 31 | * ignored. |
| 32 | */ |
| 33 | static int gitmodules_is_unmerged; |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 34 | |
Jens Lehmann | 5fee995 | 2013-07-30 21:50:34 +0200 | [diff] [blame] | 35 | /* |
| 36 | * This flag is set if the .gitmodules file had unstaged modifications on |
| 37 | * startup. This must be checked before allowing modifications to the |
| 38 | * .gitmodules file with the intention to stage them later, because when |
| 39 | * continuing we would stage the modifications the user didn't stage herself |
| 40 | * too. That might change in a future version when we learn to stage the |
| 41 | * changes we do ourselves without staging any previous modifications. |
| 42 | */ |
| 43 | static int gitmodules_is_modified; |
| 44 | |
Jens Lehmann | 5fee995 | 2013-07-30 21:50:34 +0200 | [diff] [blame] | 45 | int is_staging_gitmodules_ok(void) |
| 46 | { |
| 47 | return !gitmodules_is_modified; |
| 48 | } |
| 49 | |
Jens Lehmann | 0656781 | 2013-08-06 21:15:11 +0200 | [diff] [blame] | 50 | /* |
| 51 | * Try to update the "path" entry in the "submodule.<name>" section of the |
| 52 | * .gitmodules file. Return 0 only if a .gitmodules file was found, a section |
| 53 | * with the correct path=<oldpath> setting was found and we could update it. |
| 54 | */ |
| 55 | int update_path_in_gitmodules(const char *oldpath, const char *newpath) |
| 56 | { |
| 57 | struct strbuf entry = STRBUF_INIT; |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 58 | const struct submodule *submodule; |
Jens Lehmann | 0656781 | 2013-08-06 21:15:11 +0200 | [diff] [blame] | 59 | |
| 60 | if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */ |
| 61 | return -1; |
| 62 | |
| 63 | if (gitmodules_is_unmerged) |
| 64 | die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first")); |
| 65 | |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 66 | submodule = submodule_from_path(null_sha1, oldpath); |
| 67 | if (!submodule || !submodule->name) { |
Jens Lehmann | 0656781 | 2013-08-06 21:15:11 +0200 | [diff] [blame] | 68 | warning(_("Could not find section in .gitmodules where path=%s"), oldpath); |
| 69 | return -1; |
| 70 | } |
| 71 | strbuf_addstr(&entry, "submodule."); |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 72 | strbuf_addstr(&entry, submodule->name); |
Jens Lehmann | 0656781 | 2013-08-06 21:15:11 +0200 | [diff] [blame] | 73 | strbuf_addstr(&entry, ".path"); |
Patrick Steinhardt | 30598ad | 2016-02-22 12:23:35 +0100 | [diff] [blame] | 74 | if (git_config_set_in_file_gently(".gitmodules", entry.buf, newpath) < 0) { |
Jens Lehmann | 0656781 | 2013-08-06 21:15:11 +0200 | [diff] [blame] | 75 | /* Maybe the user already did that, don't error out here */ |
| 76 | warning(_("Could not update .gitmodules entry %s"), entry.buf); |
| 77 | strbuf_release(&entry); |
| 78 | return -1; |
| 79 | } |
| 80 | strbuf_release(&entry); |
| 81 | return 0; |
| 82 | } |
| 83 | |
Jens Lehmann | 95c1641 | 2013-08-06 21:15:25 +0200 | [diff] [blame] | 84 | /* |
| 85 | * Try to remove the "submodule.<name>" section from .gitmodules where the given |
| 86 | * path is configured. Return 0 only if a .gitmodules file was found, a section |
| 87 | * with the correct path=<path> setting was found and we could remove it. |
| 88 | */ |
| 89 | int remove_path_from_gitmodules(const char *path) |
| 90 | { |
| 91 | struct strbuf sect = STRBUF_INIT; |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 92 | const struct submodule *submodule; |
Jens Lehmann | 95c1641 | 2013-08-06 21:15:25 +0200 | [diff] [blame] | 93 | |
| 94 | if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */ |
| 95 | return -1; |
| 96 | |
| 97 | if (gitmodules_is_unmerged) |
| 98 | die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first")); |
| 99 | |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 100 | submodule = submodule_from_path(null_sha1, path); |
| 101 | if (!submodule || !submodule->name) { |
Jens Lehmann | 95c1641 | 2013-08-06 21:15:25 +0200 | [diff] [blame] | 102 | warning(_("Could not find section in .gitmodules where path=%s"), path); |
| 103 | return -1; |
| 104 | } |
| 105 | strbuf_addstr(§, "submodule."); |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 106 | strbuf_addstr(§, submodule->name); |
Jens Lehmann | 95c1641 | 2013-08-06 21:15:25 +0200 | [diff] [blame] | 107 | if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) { |
| 108 | /* Maybe the user already did that, don't error out here */ |
| 109 | warning(_("Could not remove .gitmodules entry for %s"), path); |
| 110 | strbuf_release(§); |
| 111 | return -1; |
| 112 | } |
| 113 | strbuf_release(§); |
| 114 | return 0; |
| 115 | } |
| 116 | |
Jens Lehmann | 5fee995 | 2013-07-30 21:50:34 +0200 | [diff] [blame] | 117 | void stage_updated_gitmodules(void) |
| 118 | { |
Jens Lehmann | bc8d6b9 | 2013-11-07 15:33:43 +0100 | [diff] [blame] | 119 | if (add_file_to_cache(".gitmodules", 0)) |
Jens Lehmann | 5fee995 | 2013-07-30 21:50:34 +0200 | [diff] [blame] | 120 | die(_("staging updated .gitmodules failed")); |
| 121 | } |
| 122 | |
Junio C Hamano | cb58c93 | 2010-01-11 22:31:58 -0800 | [diff] [blame] | 123 | static int add_submodule_odb(const char *path) |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 124 | { |
| 125 | struct strbuf objects_directory = STRBUF_INIT; |
| 126 | struct alternate_object_database *alt_odb; |
Jens Lehmann | de7a796 | 2010-01-31 17:43:49 +0100 | [diff] [blame] | 127 | int ret = 0; |
Jeff King | 50a6c8e | 2016-02-22 17:44:35 -0500 | [diff] [blame] | 128 | size_t alloc; |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 129 | |
Jacob Keller | 99b43a6 | 2016-08-31 16:27:22 -0700 | [diff] [blame] | 130 | ret = strbuf_git_path_submodule(&objects_directory, path, "objects/"); |
| 131 | if (ret) |
| 132 | goto done; |
Jens Lehmann | de7a796 | 2010-01-31 17:43:49 +0100 | [diff] [blame] | 133 | if (!is_directory(objects_directory.buf)) { |
| 134 | ret = -1; |
| 135 | goto done; |
| 136 | } |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 137 | /* avoid adding it twice */ |
Jeff King | 9a6e4f0 | 2015-10-28 10:07:25 -0400 | [diff] [blame] | 138 | prepare_alt_odb(); |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 139 | for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next) |
| 140 | if (alt_odb->name - alt_odb->base == objects_directory.len && |
| 141 | !strncmp(alt_odb->base, objects_directory.buf, |
| 142 | objects_directory.len)) |
Jens Lehmann | de7a796 | 2010-01-31 17:43:49 +0100 | [diff] [blame] | 143 | goto done; |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 144 | |
Jeff King | 50a6c8e | 2016-02-22 17:44:35 -0500 | [diff] [blame] | 145 | alloc = st_add(objects_directory.len, 42); /* for "12/345..." sha1 */ |
| 146 | alt_odb = xmalloc(st_add(sizeof(*alt_odb), alloc)); |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 147 | alt_odb->next = alt_odb_list; |
Jeff King | c7ab0ba | 2015-09-24 17:08:12 -0400 | [diff] [blame] | 148 | xsnprintf(alt_odb->base, alloc, "%s", objects_directory.buf); |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 149 | alt_odb->name = alt_odb->base + objects_directory.len; |
| 150 | alt_odb->name[2] = '/'; |
| 151 | alt_odb->name[40] = '\0'; |
| 152 | alt_odb->name[41] = '\0'; |
| 153 | alt_odb_list = alt_odb; |
Heiko Voigt | 5e73633 | 2012-05-14 18:24:45 +0200 | [diff] [blame] | 154 | |
| 155 | /* add possible alternates from the submodule */ |
| 156 | read_info_alternates(objects_directory.buf, 0); |
Jens Lehmann | de7a796 | 2010-01-31 17:43:49 +0100 | [diff] [blame] | 157 | done: |
| 158 | strbuf_release(&objects_directory); |
| 159 | return ret; |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 162 | void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt, |
| 163 | const char *path) |
| 164 | { |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 165 | const struct submodule *submodule = submodule_from_path(null_sha1, path); |
| 166 | if (submodule) { |
| 167 | if (submodule->ignore) |
| 168 | handle_ignore_submodules_arg(diffopt, submodule->ignore); |
Jens Lehmann | d4e98b5 | 2011-05-14 18:26:58 +0200 | [diff] [blame] | 169 | else if (gitmodules_is_unmerged) |
| 170 | DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES); |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 174 | int submodule_config(const char *var, const char *value, void *cb) |
Jens Lehmann | 302ad7a | 2010-08-06 00:40:48 +0200 | [diff] [blame] | 175 | { |
Stefan Beller | a028a19 | 2016-02-29 18:07:13 -0800 | [diff] [blame] | 176 | if (!strcmp(var, "submodule.fetchjobs")) { |
| 177 | parallel_jobs = git_config_int(var, value); |
| 178 | if (parallel_jobs < 0) |
| 179 | die(_("negative values not allowed for submodule.fetchJobs")); |
| 180 | return 0; |
| 181 | } else if (starts_with(var, "submodule.")) |
Jens Lehmann | 302ad7a | 2010-08-06 00:40:48 +0200 | [diff] [blame] | 182 | return parse_submodule_config_option(var, value); |
Jens Lehmann | be254a0 | 2010-11-11 00:55:02 +0100 | [diff] [blame] | 183 | else if (!strcmp(var, "fetch.recursesubmodules")) { |
Jens Lehmann | 1fb2550 | 2011-03-06 23:11:48 +0100 | [diff] [blame] | 184 | config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value); |
Jens Lehmann | be254a0 | 2010-11-11 00:55:02 +0100 | [diff] [blame] | 185 | return 0; |
| 186 | } |
Jens Lehmann | 302ad7a | 2010-08-06 00:40:48 +0200 | [diff] [blame] | 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | void gitmodules_config(void) |
| 191 | { |
| 192 | const char *work_tree = get_git_work_tree(); |
| 193 | if (work_tree) { |
| 194 | struct strbuf gitmodules_path = STRBUF_INIT; |
Jens Lehmann | d4e98b5 | 2011-05-14 18:26:58 +0200 | [diff] [blame] | 195 | int pos; |
Jens Lehmann | 302ad7a | 2010-08-06 00:40:48 +0200 | [diff] [blame] | 196 | strbuf_addstr(&gitmodules_path, work_tree); |
| 197 | strbuf_addstr(&gitmodules_path, "/.gitmodules"); |
Jens Lehmann | d4e98b5 | 2011-05-14 18:26:58 +0200 | [diff] [blame] | 198 | if (read_cache() < 0) |
| 199 | die("index file corrupt"); |
| 200 | pos = cache_name_pos(".gitmodules", 11); |
| 201 | if (pos < 0) { /* .gitmodules not found or isn't merged */ |
| 202 | pos = -1 - pos; |
| 203 | if (active_nr > pos) { /* there is a .gitmodules */ |
| 204 | const struct cache_entry *ce = active_cache[pos]; |
| 205 | if (ce_namelen(ce) == 11 && |
| 206 | !memcmp(ce->name, ".gitmodules", 11)) |
| 207 | gitmodules_is_unmerged = 1; |
| 208 | } |
Jens Lehmann | 5fee995 | 2013-07-30 21:50:34 +0200 | [diff] [blame] | 209 | } else if (pos < active_nr) { |
| 210 | struct stat st; |
| 211 | if (lstat(".gitmodules", &st) == 0 && |
| 212 | ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED) |
| 213 | gitmodules_is_modified = 1; |
Jens Lehmann | d4e98b5 | 2011-05-14 18:26:58 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | if (!gitmodules_is_unmerged) |
| 217 | git_config_from_file(submodule_config, gitmodules_path.buf, NULL); |
Jens Lehmann | 302ad7a | 2010-08-06 00:40:48 +0200 | [diff] [blame] | 218 | strbuf_release(&gitmodules_path); |
| 219 | } |
| 220 | } |
| 221 | |
Stefan Beller | ea2fa5a | 2016-02-29 18:07:11 -0800 | [diff] [blame] | 222 | int parse_submodule_update_strategy(const char *value, |
| 223 | struct submodule_update_strategy *dst) |
| 224 | { |
| 225 | free((void*)dst->command); |
| 226 | dst->command = NULL; |
| 227 | if (!strcmp(value, "none")) |
| 228 | dst->type = SM_UPDATE_NONE; |
| 229 | else if (!strcmp(value, "checkout")) |
| 230 | dst->type = SM_UPDATE_CHECKOUT; |
| 231 | else if (!strcmp(value, "rebase")) |
| 232 | dst->type = SM_UPDATE_REBASE; |
| 233 | else if (!strcmp(value, "merge")) |
| 234 | dst->type = SM_UPDATE_MERGE; |
| 235 | else if (skip_prefix(value, "!", &value)) { |
| 236 | dst->type = SM_UPDATE_COMMAND; |
| 237 | dst->command = xstrdup(value); |
| 238 | } else |
| 239 | return -1; |
| 240 | return 0; |
| 241 | } |
| 242 | |
Stefan Beller | 3604242 | 2016-04-15 17:50:13 -0700 | [diff] [blame] | 243 | const char *submodule_strategy_to_string(const struct submodule_update_strategy *s) |
| 244 | { |
| 245 | struct strbuf sb = STRBUF_INIT; |
| 246 | switch (s->type) { |
| 247 | case SM_UPDATE_CHECKOUT: |
| 248 | return "checkout"; |
| 249 | case SM_UPDATE_MERGE: |
| 250 | return "merge"; |
| 251 | case SM_UPDATE_REBASE: |
| 252 | return "rebase"; |
| 253 | case SM_UPDATE_NONE: |
| 254 | return "none"; |
| 255 | case SM_UPDATE_UNSPECIFIED: |
| 256 | return NULL; |
| 257 | case SM_UPDATE_COMMAND: |
| 258 | strbuf_addf(&sb, "!%s", s->command); |
| 259 | return strbuf_detach(&sb, NULL); |
| 260 | } |
| 261 | return NULL; |
| 262 | } |
| 263 | |
Jens Lehmann | 46a958b | 2010-06-25 16:56:47 +0200 | [diff] [blame] | 264 | void handle_ignore_submodules_arg(struct diff_options *diffopt, |
| 265 | const char *arg) |
| 266 | { |
Johannes Schindelin | be4f2b4 | 2010-08-05 10:49:55 +0200 | [diff] [blame] | 267 | DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES); |
| 268 | DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES); |
| 269 | DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES); |
| 270 | |
Jens Lehmann | 46a958b | 2010-06-25 16:56:47 +0200 | [diff] [blame] | 271 | if (!strcmp(arg, "all")) |
| 272 | DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES); |
| 273 | else if (!strcmp(arg, "untracked")) |
| 274 | DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES); |
| 275 | else if (!strcmp(arg, "dirty")) |
| 276 | DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES); |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 277 | else if (strcmp(arg, "none")) |
Jens Lehmann | 46a958b | 2010-06-25 16:56:47 +0200 | [diff] [blame] | 278 | die("bad --ignore-submodules argument: %s", arg); |
| 279 | } |
| 280 | |
Jonathan Nieder | 808a95d | 2011-03-16 02:14:11 -0500 | [diff] [blame] | 281 | static int prepare_submodule_summary(struct rev_info *rev, const char *path, |
| 282 | struct commit *left, struct commit *right, |
Jacob Keller | 8e6df65 | 2016-08-31 16:27:24 -0700 | [diff] [blame] | 283 | struct commit_list *merge_bases) |
Jonathan Nieder | 808a95d | 2011-03-16 02:14:11 -0500 | [diff] [blame] | 284 | { |
Jacob Keller | 8e6df65 | 2016-08-31 16:27:24 -0700 | [diff] [blame] | 285 | struct commit_list *list; |
Jonathan Nieder | 808a95d | 2011-03-16 02:14:11 -0500 | [diff] [blame] | 286 | |
| 287 | init_revisions(rev, NULL); |
| 288 | setup_revisions(0, NULL, rev, NULL); |
| 289 | rev->left_right = 1; |
| 290 | rev->first_parent_only = 1; |
| 291 | left->object.flags |= SYMMETRIC_LEFT; |
| 292 | add_pending_object(rev, &left->object, path); |
| 293 | add_pending_object(rev, &right->object, path); |
Jonathan Nieder | 808a95d | 2011-03-16 02:14:11 -0500 | [diff] [blame] | 294 | for (list = merge_bases; list; list = list->next) { |
| 295 | list->item->object.flags |= UNINTERESTING; |
| 296 | add_pending_object(rev, &list->item->object, |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 297 | oid_to_hex(&list->item->object.oid)); |
Jonathan Nieder | 808a95d | 2011-03-16 02:14:11 -0500 | [diff] [blame] | 298 | } |
| 299 | return prepare_revision_walk(rev); |
| 300 | } |
| 301 | |
| 302 | static void print_submodule_summary(struct rev_info *rev, FILE *f, |
John Keeping | 0f33a06 | 2013-04-05 17:12:08 +0100 | [diff] [blame] | 303 | const char *line_prefix, |
Jonathan Nieder | 808a95d | 2011-03-16 02:14:11 -0500 | [diff] [blame] | 304 | const char *del, const char *add, const char *reset) |
| 305 | { |
| 306 | static const char format[] = " %m %s"; |
| 307 | struct strbuf sb = STRBUF_INIT; |
| 308 | struct commit *commit; |
| 309 | |
| 310 | while ((commit = get_revision(rev))) { |
| 311 | struct pretty_print_context ctx = {0}; |
| 312 | ctx.date_mode = rev->date_mode; |
Alexey Shumkin | ecaee80 | 2013-06-26 14:19:50 +0400 | [diff] [blame] | 313 | ctx.output_encoding = get_log_output_encoding(); |
Jonathan Nieder | 808a95d | 2011-03-16 02:14:11 -0500 | [diff] [blame] | 314 | strbuf_setlen(&sb, 0); |
John Keeping | 0f33a06 | 2013-04-05 17:12:08 +0100 | [diff] [blame] | 315 | strbuf_addstr(&sb, line_prefix); |
Jonathan Nieder | 808a95d | 2011-03-16 02:14:11 -0500 | [diff] [blame] | 316 | if (commit->object.flags & SYMMETRIC_LEFT) { |
| 317 | if (del) |
| 318 | strbuf_addstr(&sb, del); |
| 319 | } |
| 320 | else if (add) |
| 321 | strbuf_addstr(&sb, add); |
| 322 | format_commit_message(commit, format, &sb, &ctx); |
| 323 | if (reset) |
| 324 | strbuf_addstr(&sb, reset); |
| 325 | strbuf_addch(&sb, '\n'); |
| 326 | fprintf(f, "%s", sb.buf); |
| 327 | } |
| 328 | strbuf_release(&sb); |
| 329 | } |
| 330 | |
Jacob Keller | 8e6df65 | 2016-08-31 16:27:24 -0700 | [diff] [blame] | 331 | /* Helper function to display the submodule header line prior to the full |
| 332 | * summary output. If it can locate the submodule objects directory it will |
| 333 | * attempt to lookup both the left and right commits and put them into the |
| 334 | * left and right pointers. |
| 335 | */ |
| 336 | static void show_submodule_header(FILE *f, const char *path, |
John Keeping | 0f33a06 | 2013-04-05 17:12:08 +0100 | [diff] [blame] | 337 | const char *line_prefix, |
Jacob Keller | 602a283 | 2016-08-31 16:27:23 -0700 | [diff] [blame] | 338 | struct object_id *one, struct object_id *two, |
Ramkumar Ramachandra | 4e21513 | 2012-11-13 21:12:47 +0530 | [diff] [blame] | 339 | unsigned dirty_submodule, const char *meta, |
Jacob Keller | 8e6df65 | 2016-08-31 16:27:24 -0700 | [diff] [blame] | 340 | const char *reset, |
| 341 | struct commit **left, struct commit **right, |
| 342 | struct commit_list **merge_bases) |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 343 | { |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 344 | const char *message = NULL; |
| 345 | struct strbuf sb = STRBUF_INIT; |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 346 | int fast_forward = 0, fast_backward = 0; |
| 347 | |
Jens Lehmann | c7e1a73 | 2010-03-04 22:20:33 +0100 | [diff] [blame] | 348 | if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) |
John Keeping | 0f33a06 | 2013-04-05 17:12:08 +0100 | [diff] [blame] | 349 | fprintf(f, "%sSubmodule %s contains untracked content\n", |
| 350 | line_prefix, path); |
Jens Lehmann | c7e1a73 | 2010-03-04 22:20:33 +0100 | [diff] [blame] | 351 | if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED) |
John Keeping | 0f33a06 | 2013-04-05 17:12:08 +0100 | [diff] [blame] | 352 | fprintf(f, "%sSubmodule %s contains modified content\n", |
| 353 | line_prefix, path); |
Jens Lehmann | c7e1a73 | 2010-03-04 22:20:33 +0100 | [diff] [blame] | 354 | |
Jacob Keller | 8e6df65 | 2016-08-31 16:27:24 -0700 | [diff] [blame] | 355 | if (is_null_oid(one)) |
| 356 | message = "(new submodule)"; |
| 357 | else if (is_null_oid(two)) |
| 358 | message = "(submodule deleted)"; |
| 359 | |
| 360 | if (add_submodule_odb(path)) { |
| 361 | if (!message) |
| 362 | message = "(not initialized)"; |
| 363 | goto output_header; |
| 364 | } |
| 365 | |
| 366 | /* |
| 367 | * Attempt to lookup the commit references, and determine if this is |
| 368 | * a fast forward or fast backwards update. |
| 369 | */ |
| 370 | *left = lookup_commit_reference(one->hash); |
| 371 | *right = lookup_commit_reference(two->hash); |
| 372 | |
| 373 | /* |
| 374 | * Warn about missing commits in the submodule project, but only if |
| 375 | * they aren't null. |
| 376 | */ |
| 377 | if ((!is_null_oid(one) && !*left) || |
| 378 | (!is_null_oid(two) && !*right)) |
| 379 | message = "(commits not present)"; |
| 380 | |
| 381 | *merge_bases = get_merge_bases(*left, *right); |
| 382 | if (*merge_bases) { |
| 383 | if ((*merge_bases)->item == *left) |
| 384 | fast_forward = 1; |
| 385 | else if ((*merge_bases)->item == *right) |
| 386 | fast_backward = 1; |
| 387 | } |
| 388 | |
Jacob Keller | 602a283 | 2016-08-31 16:27:23 -0700 | [diff] [blame] | 389 | if (!oidcmp(one, two)) { |
Jens Lehmann | c7e1a73 | 2010-03-04 22:20:33 +0100 | [diff] [blame] | 390 | strbuf_release(&sb); |
| 391 | return; |
| 392 | } |
| 393 | |
Jacob Keller | 8e6df65 | 2016-08-31 16:27:24 -0700 | [diff] [blame] | 394 | output_header: |
John Keeping | 0f33a06 | 2013-04-05 17:12:08 +0100 | [diff] [blame] | 395 | strbuf_addf(&sb, "%s%sSubmodule %s %s..", line_prefix, meta, path, |
Jacob Keller | 602a283 | 2016-08-31 16:27:23 -0700 | [diff] [blame] | 396 | find_unique_abbrev(one->hash, DEFAULT_ABBREV)); |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 397 | if (!fast_backward && !fast_forward) |
| 398 | strbuf_addch(&sb, '.'); |
Jacob Keller | 602a283 | 2016-08-31 16:27:23 -0700 | [diff] [blame] | 399 | strbuf_addf(&sb, "%s", find_unique_abbrev(two->hash, DEFAULT_ABBREV)); |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 400 | if (message) |
Ramkumar Ramachandra | 4e21513 | 2012-11-13 21:12:47 +0530 | [diff] [blame] | 401 | strbuf_addf(&sb, " %s%s\n", message, reset); |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 402 | else |
Ramkumar Ramachandra | 4e21513 | 2012-11-13 21:12:47 +0530 | [diff] [blame] | 403 | strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset); |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 404 | fwrite(sb.buf, sb.len, 1, f); |
| 405 | |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 406 | strbuf_release(&sb); |
| 407 | } |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 408 | |
Jacob Keller | 8e6df65 | 2016-08-31 16:27:24 -0700 | [diff] [blame] | 409 | void show_submodule_summary(FILE *f, const char *path, |
| 410 | const char *line_prefix, |
| 411 | struct object_id *one, struct object_id *two, |
| 412 | unsigned dirty_submodule, const char *meta, |
| 413 | const char *del, const char *add, const char *reset) |
| 414 | { |
| 415 | struct rev_info rev; |
| 416 | struct commit *left = NULL, *right = NULL; |
| 417 | struct commit_list *merge_bases = NULL; |
| 418 | |
| 419 | show_submodule_header(f, path, line_prefix, one, two, dirty_submodule, |
| 420 | meta, reset, &left, &right, &merge_bases); |
| 421 | |
| 422 | /* |
| 423 | * If we don't have both a left and a right pointer, there is no |
| 424 | * reason to try and display a summary. The header line should contain |
| 425 | * all the information the user needs. |
| 426 | */ |
| 427 | if (!left || !right) |
| 428 | goto out; |
| 429 | |
| 430 | /* Treat revision walker failure the same as missing commits */ |
| 431 | if (prepare_submodule_summary(&rev, path, left, right, merge_bases)) { |
| 432 | fprintf(f, "%s(revision walker failed)\n", line_prefix); |
| 433 | goto out; |
| 434 | } |
| 435 | |
| 436 | print_submodule_summary(&rev, f, line_prefix, del, add, reset); |
| 437 | |
| 438 | out: |
| 439 | if (merge_bases) |
| 440 | free_commit_list(merge_bases); |
| 441 | clear_commit_marks(left, ~0); |
| 442 | clear_commit_marks(right, ~0); |
| 443 | } |
| 444 | |
Jacob Keller | fd47ae6 | 2016-08-31 16:27:25 -0700 | [diff] [blame] | 445 | void show_submodule_inline_diff(FILE *f, const char *path, |
| 446 | const char *line_prefix, |
| 447 | struct object_id *one, struct object_id *two, |
| 448 | unsigned dirty_submodule, const char *meta, |
| 449 | const char *del, const char *add, const char *reset, |
| 450 | const struct diff_options *o) |
| 451 | { |
| 452 | const struct object_id *old = &empty_tree_oid, *new = &empty_tree_oid; |
| 453 | struct commit *left = NULL, *right = NULL; |
| 454 | struct commit_list *merge_bases = NULL; |
| 455 | struct strbuf submodule_dir = STRBUF_INIT; |
| 456 | struct child_process cp = CHILD_PROCESS_INIT; |
| 457 | |
| 458 | show_submodule_header(f, path, line_prefix, one, two, dirty_submodule, |
| 459 | meta, reset, &left, &right, &merge_bases); |
| 460 | |
| 461 | /* We need a valid left and right commit to display a difference */ |
| 462 | if (!(left || is_null_oid(one)) || |
| 463 | !(right || is_null_oid(two))) |
| 464 | goto done; |
| 465 | |
| 466 | if (left) |
| 467 | old = one; |
| 468 | if (right) |
| 469 | new = two; |
| 470 | |
| 471 | fflush(f); |
| 472 | cp.git_cmd = 1; |
| 473 | cp.dir = path; |
| 474 | cp.out = dup(fileno(f)); |
| 475 | cp.no_stdin = 1; |
| 476 | |
| 477 | /* TODO: other options may need to be passed here. */ |
| 478 | argv_array_push(&cp.args, "diff"); |
| 479 | argv_array_pushf(&cp.args, "--line-prefix=%s", line_prefix); |
| 480 | if (DIFF_OPT_TST(o, REVERSE_DIFF)) { |
| 481 | argv_array_pushf(&cp.args, "--src-prefix=%s%s/", |
| 482 | o->b_prefix, path); |
| 483 | argv_array_pushf(&cp.args, "--dst-prefix=%s%s/", |
| 484 | o->a_prefix, path); |
| 485 | } else { |
| 486 | argv_array_pushf(&cp.args, "--src-prefix=%s%s/", |
| 487 | o->a_prefix, path); |
| 488 | argv_array_pushf(&cp.args, "--dst-prefix=%s%s/", |
| 489 | o->b_prefix, path); |
| 490 | } |
| 491 | argv_array_push(&cp.args, oid_to_hex(old)); |
| 492 | /* |
| 493 | * If the submodule has modified content, we will diff against the |
| 494 | * work tree, under the assumption that the user has asked for the |
| 495 | * diff format and wishes to actually see all differences even if they |
| 496 | * haven't yet been committed to the submodule yet. |
| 497 | */ |
| 498 | if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED)) |
| 499 | argv_array_push(&cp.args, oid_to_hex(new)); |
| 500 | |
| 501 | if (run_command(&cp)) |
| 502 | fprintf(f, "(diff failed)\n"); |
| 503 | |
| 504 | done: |
| 505 | strbuf_release(&submodule_dir); |
| 506 | if (merge_bases) |
| 507 | free_commit_list(merge_bases); |
| 508 | if (left) |
| 509 | clear_commit_marks(left, ~0); |
| 510 | if (right) |
| 511 | clear_commit_marks(right, ~0); |
| 512 | } |
| 513 | |
Jens Lehmann | be254a0 | 2010-11-11 00:55:02 +0100 | [diff] [blame] | 514 | void set_config_fetch_recurse_submodules(int value) |
| 515 | { |
| 516 | config_fetch_recurse_submodules = value; |
| 517 | } |
| 518 | |
Michael Haggerty | 7290ef5 | 2015-05-25 18:39:07 +0000 | [diff] [blame] | 519 | static int has_remote(const char *refname, const struct object_id *oid, |
| 520 | int flags, void *cb_data) |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 521 | { |
| 522 | return 1; |
| 523 | } |
| 524 | |
| 525 | static int submodule_needs_pushing(const char *path, const unsigned char sha1[20]) |
| 526 | { |
| 527 | if (add_submodule_odb(path) || !lookup_commit_reference(sha1)) |
| 528 | return 0; |
| 529 | |
| 530 | if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) { |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 531 | struct child_process cp = CHILD_PROCESS_INIT; |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 532 | const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL}; |
| 533 | struct strbuf buf = STRBUF_INIT; |
| 534 | int needs_pushing = 0; |
| 535 | |
| 536 | argv[1] = sha1_to_hex(sha1); |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 537 | cp.argv = argv; |
Jeff King | c12e865 | 2016-04-28 09:39:15 -0400 | [diff] [blame] | 538 | prepare_submodule_repo_env(&cp.env_array); |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 539 | cp.git_cmd = 1; |
| 540 | cp.no_stdin = 1; |
| 541 | cp.out = -1; |
| 542 | cp.dir = path; |
| 543 | if (start_command(&cp)) |
| 544 | die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s", |
| 545 | sha1_to_hex(sha1), path); |
| 546 | if (strbuf_read(&buf, cp.out, 41)) |
| 547 | needs_pushing = 1; |
| 548 | finish_command(&cp); |
| 549 | close(cp.out); |
| 550 | strbuf_release(&buf); |
| 551 | return needs_pushing; |
| 552 | } |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | static void collect_submodules_from_diff(struct diff_queue_struct *q, |
| 558 | struct diff_options *options, |
| 559 | void *data) |
| 560 | { |
| 561 | int i; |
Heiko Voigt | a762e51 | 2012-03-29 09:21:23 +0200 | [diff] [blame] | 562 | struct string_list *needs_pushing = data; |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 563 | |
| 564 | for (i = 0; i < q->nr; i++) { |
| 565 | struct diff_filepair *p = q->queue[i]; |
| 566 | if (!S_ISGITLINK(p->two->mode)) |
| 567 | continue; |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 568 | if (submodule_needs_pushing(p->two->path, p->two->oid.hash)) |
Heiko Voigt | a762e51 | 2012-03-29 09:21:23 +0200 | [diff] [blame] | 569 | string_list_insert(needs_pushing, p->two->path); |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
Heiko Voigt | a762e51 | 2012-03-29 09:21:23 +0200 | [diff] [blame] | 573 | static void find_unpushed_submodule_commits(struct commit *commit, |
| 574 | struct string_list *needs_pushing) |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 575 | { |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 576 | struct rev_info rev; |
| 577 | |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 578 | init_revisions(&rev, NULL); |
| 579 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; |
| 580 | rev.diffopt.format_callback = collect_submodules_from_diff; |
| 581 | rev.diffopt.format_callback_data = needs_pushing; |
René Scharfe | 78e98ea | 2011-12-17 11:27:19 +0100 | [diff] [blame] | 582 | diff_tree_combined_merge(commit, 1, &rev); |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 583 | } |
| 584 | |
Heiko Voigt | a762e51 | 2012-03-29 09:21:23 +0200 | [diff] [blame] | 585 | int find_unpushed_submodules(unsigned char new_sha1[20], |
| 586 | const char *remotes_name, struct string_list *needs_pushing) |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 587 | { |
| 588 | struct rev_info rev; |
| 589 | struct commit *commit; |
| 590 | const char *argv[] = {NULL, NULL, "--not", "NULL", NULL}; |
| 591 | int argc = ARRAY_SIZE(argv) - 1; |
| 592 | char *sha1_copy; |
Heiko Voigt | a762e51 | 2012-03-29 09:21:23 +0200 | [diff] [blame] | 593 | |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 594 | struct strbuf remotes_arg = STRBUF_INIT; |
| 595 | |
| 596 | strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name); |
| 597 | init_revisions(&rev, NULL); |
| 598 | sha1_copy = xstrdup(sha1_to_hex(new_sha1)); |
| 599 | argv[1] = sha1_copy; |
| 600 | argv[3] = remotes_arg.buf; |
| 601 | setup_revisions(argc, argv, &rev, NULL); |
| 602 | if (prepare_revision_walk(&rev)) |
| 603 | die("revision walk setup failed"); |
| 604 | |
Heiko Voigt | a762e51 | 2012-03-29 09:21:23 +0200 | [diff] [blame] | 605 | while ((commit = get_revision(&rev)) != NULL) |
| 606 | find_unpushed_submodule_commits(commit, needs_pushing); |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 607 | |
Heiko Voigt | bcc0a3e | 2012-03-29 09:21:21 +0200 | [diff] [blame] | 608 | reset_revision_walk(); |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 609 | free(sha1_copy); |
| 610 | strbuf_release(&remotes_arg); |
| 611 | |
Heiko Voigt | a762e51 | 2012-03-29 09:21:23 +0200 | [diff] [blame] | 612 | return needs_pushing->nr; |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 613 | } |
| 614 | |
Heiko Voigt | eb21c73 | 2012-03-29 09:21:24 +0200 | [diff] [blame] | 615 | static int push_submodule(const char *path) |
| 616 | { |
| 617 | if (add_submodule_odb(path)) |
| 618 | return 1; |
| 619 | |
| 620 | if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) { |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 621 | struct child_process cp = CHILD_PROCESS_INIT; |
Heiko Voigt | eb21c73 | 2012-03-29 09:21:24 +0200 | [diff] [blame] | 622 | const char *argv[] = {"push", NULL}; |
| 623 | |
Heiko Voigt | eb21c73 | 2012-03-29 09:21:24 +0200 | [diff] [blame] | 624 | cp.argv = argv; |
Jeff King | c12e865 | 2016-04-28 09:39:15 -0400 | [diff] [blame] | 625 | prepare_submodule_repo_env(&cp.env_array); |
Heiko Voigt | eb21c73 | 2012-03-29 09:21:24 +0200 | [diff] [blame] | 626 | cp.git_cmd = 1; |
| 627 | cp.no_stdin = 1; |
| 628 | cp.dir = path; |
| 629 | if (run_command(&cp)) |
| 630 | return 0; |
| 631 | close(cp.out); |
| 632 | } |
| 633 | |
| 634 | return 1; |
| 635 | } |
| 636 | |
| 637 | int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name) |
| 638 | { |
| 639 | int i, ret = 1; |
Tanay Abhra | f93d7c6 | 2014-07-18 02:19:00 -0700 | [diff] [blame] | 640 | struct string_list needs_pushing = STRING_LIST_INIT_DUP; |
Heiko Voigt | eb21c73 | 2012-03-29 09:21:24 +0200 | [diff] [blame] | 641 | |
| 642 | if (!find_unpushed_submodules(new_sha1, remotes_name, &needs_pushing)) |
| 643 | return 1; |
| 644 | |
| 645 | for (i = 0; i < needs_pushing.nr; i++) { |
| 646 | const char *path = needs_pushing.items[i].string; |
| 647 | fprintf(stderr, "Pushing submodule '%s'\n", path); |
| 648 | if (!push_submodule(path)) { |
| 649 | fprintf(stderr, "Unable to push submodule '%s'\n", path); |
| 650 | ret = 0; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | string_list_clear(&needs_pushing, 0); |
| 655 | |
| 656 | return ret; |
| 657 | } |
| 658 | |
Jens Lehmann | c16c3e4 | 2011-03-06 23:12:58 +0100 | [diff] [blame] | 659 | static int is_submodule_commit_present(const char *path, unsigned char sha1[20]) |
| 660 | { |
| 661 | int is_present = 0; |
| 662 | if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) { |
| 663 | /* Even if the submodule is checked out and the commit is |
| 664 | * present, make sure it is reachable from a ref. */ |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 665 | struct child_process cp = CHILD_PROCESS_INIT; |
Jens Lehmann | c16c3e4 | 2011-03-06 23:12:58 +0100 | [diff] [blame] | 666 | const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL}; |
| 667 | struct strbuf buf = STRBUF_INIT; |
| 668 | |
| 669 | argv[3] = sha1_to_hex(sha1); |
Jens Lehmann | c16c3e4 | 2011-03-06 23:12:58 +0100 | [diff] [blame] | 670 | cp.argv = argv; |
Jeff King | c12e865 | 2016-04-28 09:39:15 -0400 | [diff] [blame] | 671 | prepare_submodule_repo_env(&cp.env_array); |
Jens Lehmann | c16c3e4 | 2011-03-06 23:12:58 +0100 | [diff] [blame] | 672 | cp.git_cmd = 1; |
| 673 | cp.no_stdin = 1; |
Jens Lehmann | c16c3e4 | 2011-03-06 23:12:58 +0100 | [diff] [blame] | 674 | cp.dir = path; |
Jeff King | 1d4974c | 2015-03-22 23:53:56 -0400 | [diff] [blame] | 675 | if (!capture_command(&cp, &buf, 1024) && !buf.len) |
Jens Lehmann | c16c3e4 | 2011-03-06 23:12:58 +0100 | [diff] [blame] | 676 | is_present = 1; |
| 677 | |
Jens Lehmann | c16c3e4 | 2011-03-06 23:12:58 +0100 | [diff] [blame] | 678 | strbuf_release(&buf); |
| 679 | } |
| 680 | return is_present; |
| 681 | } |
| 682 | |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 683 | static void submodule_collect_changed_cb(struct diff_queue_struct *q, |
| 684 | struct diff_options *options, |
| 685 | void *data) |
| 686 | { |
| 687 | int i; |
| 688 | for (i = 0; i < q->nr; i++) { |
| 689 | struct diff_filepair *p = q->queue[i]; |
| 690 | if (!S_ISGITLINK(p->two->mode)) |
| 691 | continue; |
| 692 | |
| 693 | if (S_ISGITLINK(p->one->mode)) { |
| 694 | /* NEEDSWORK: We should honor the name configured in |
| 695 | * the .gitmodules file of the commit we are examining |
| 696 | * here to be able to correctly follow submodules |
| 697 | * being moved around. */ |
| 698 | struct string_list_item *path; |
| 699 | path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path); |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 700 | if (!path && !is_submodule_commit_present(p->two->path, p->two->oid.hash)) |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 701 | string_list_append(&changed_submodule_paths, xstrdup(p->two->path)); |
| 702 | } else { |
| 703 | /* Submodule is new or was moved here */ |
| 704 | /* NEEDSWORK: When the .git directories of submodules |
| 705 | * live inside the superprojects .git directory some |
| 706 | * day we should fetch new submodules directly into |
| 707 | * that location too when config or options request |
| 708 | * that so they can be checked out from there. */ |
| 709 | continue; |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | |
Michael Haggerty | 7290ef5 | 2015-05-25 18:39:07 +0000 | [diff] [blame] | 714 | static int add_sha1_to_array(const char *ref, const struct object_id *oid, |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 715 | int flags, void *data) |
| 716 | { |
Michael Haggerty | 7290ef5 | 2015-05-25 18:39:07 +0000 | [diff] [blame] | 717 | sha1_array_append(data, oid->hash); |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 718 | return 0; |
| 719 | } |
| 720 | |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 721 | void check_for_new_submodule_commits(unsigned char new_sha1[20]) |
| 722 | { |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 723 | if (!initialized_fetch_ref_tips) { |
| 724 | for_each_ref(add_sha1_to_array, &ref_tips_before_fetch); |
| 725 | initialized_fetch_ref_tips = 1; |
| 726 | } |
| 727 | |
| 728 | sha1_array_append(&ref_tips_after_fetch, new_sha1); |
| 729 | } |
| 730 | |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 731 | static void add_sha1_to_argv(const unsigned char sha1[20], void *data) |
| 732 | { |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 733 | argv_array_push(data, sha1_to_hex(sha1)); |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | static void calculate_changed_submodule_paths(void) |
| 737 | { |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 738 | struct rev_info rev; |
| 739 | struct commit *commit; |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 740 | struct argv_array argv = ARGV_ARRAY_INIT; |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 741 | |
Jens Lehmann | 18322ba | 2011-09-09 20:22:03 +0200 | [diff] [blame] | 742 | /* No need to check if there are no submodules configured */ |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 743 | if (!submodule_from_path(NULL, NULL)) |
Jens Lehmann | 18322ba | 2011-09-09 20:22:03 +0200 | [diff] [blame] | 744 | return; |
| 745 | |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 746 | init_revisions(&rev, NULL); |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 747 | argv_array_push(&argv, "--"); /* argv[0] program name */ |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 748 | sha1_array_for_each_unique(&ref_tips_after_fetch, |
| 749 | add_sha1_to_argv, &argv); |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 750 | argv_array_push(&argv, "--not"); |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 751 | sha1_array_for_each_unique(&ref_tips_before_fetch, |
| 752 | add_sha1_to_argv, &argv); |
| 753 | setup_revisions(argv.argc, argv.argv, &rev, NULL); |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 754 | if (prepare_revision_walk(&rev)) |
| 755 | die("revision walk setup failed"); |
| 756 | |
| 757 | /* |
| 758 | * Collect all submodules (whether checked out or not) for which new |
| 759 | * commits have been recorded upstream in "changed_submodule_paths". |
| 760 | */ |
| 761 | while ((commit = get_revision(&rev))) { |
| 762 | struct commit_list *parent = commit->parents; |
| 763 | while (parent) { |
| 764 | struct diff_options diff_opts; |
| 765 | diff_setup(&diff_opts); |
Jens Lehmann | ea2d325 | 2011-06-20 20:18:03 +0200 | [diff] [blame] | 766 | DIFF_OPT_SET(&diff_opts, RECURSIVE); |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 767 | diff_opts.output_format |= DIFF_FORMAT_CALLBACK; |
| 768 | diff_opts.format_callback = submodule_collect_changed_cb; |
Thomas Rast | 2845265 | 2012-08-03 14:16:24 +0200 | [diff] [blame] | 769 | diff_setup_done(&diff_opts); |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 770 | diff_tree_sha1(parent->item->object.oid.hash, commit->object.oid.hash, "", &diff_opts); |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 771 | diffcore_std(&diff_opts); |
| 772 | diff_flush(&diff_opts); |
| 773 | parent = parent->next; |
| 774 | } |
| 775 | } |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 776 | |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 777 | argv_array_clear(&argv); |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 778 | sha1_array_clear(&ref_tips_before_fetch); |
| 779 | sha1_array_clear(&ref_tips_after_fetch); |
| 780 | initialized_fetch_ref_tips = 0; |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 781 | } |
| 782 | |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 783 | struct submodule_parallel_fetch { |
| 784 | int count; |
| 785 | struct argv_array args; |
| 786 | const char *work_tree; |
| 787 | const char *prefix; |
| 788 | int command_line_option; |
| 789 | int quiet; |
| 790 | int result; |
| 791 | }; |
| 792 | #define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0} |
| 793 | |
| 794 | static int get_next_submodule(struct child_process *cp, |
| 795 | struct strbuf *err, void *data, void **task_cb) |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 796 | { |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 797 | int ret = 0; |
| 798 | struct submodule_parallel_fetch *spf = data; |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 799 | |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 800 | for (; spf->count < active_nr; spf->count++) { |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 801 | struct strbuf submodule_path = STRBUF_INIT; |
| 802 | struct strbuf submodule_git_dir = STRBUF_INIT; |
| 803 | struct strbuf submodule_prefix = STRBUF_INIT; |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 804 | const struct cache_entry *ce = active_cache[spf->count]; |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 805 | const char *git_dir, *default_argv; |
| 806 | const struct submodule *submodule; |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 807 | |
| 808 | if (!S_ISGITLINK(ce->ce_mode)) |
| 809 | continue; |
| 810 | |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 811 | submodule = submodule_from_path(null_sha1, ce->name); |
| 812 | if (!submodule) |
| 813 | submodule = submodule_from_name(null_sha1, ce->name); |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 814 | |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 815 | default_argv = "yes"; |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 816 | if (spf->command_line_option == RECURSE_SUBMODULES_DEFAULT) { |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 817 | if (submodule && |
| 818 | submodule->fetch_recurse != |
| 819 | RECURSE_SUBMODULES_NONE) { |
| 820 | if (submodule->fetch_recurse == |
| 821 | RECURSE_SUBMODULES_OFF) |
Jens Lehmann | c1a3c36 | 2010-11-11 00:55:41 +0100 | [diff] [blame] | 822 | continue; |
Heiko Voigt | 851e18c | 2015-08-17 17:21:59 -0700 | [diff] [blame] | 823 | if (submodule->fetch_recurse == |
| 824 | RECURSE_SUBMODULES_ON_DEMAND) { |
Jens Lehmann | bf42b38 | 2011-03-06 23:12:19 +0100 | [diff] [blame] | 825 | if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) |
| 826 | continue; |
| 827 | default_argv = "on-demand"; |
| 828 | } |
Jens Lehmann | c1a3c36 | 2010-11-11 00:55:41 +0100 | [diff] [blame] | 829 | } else { |
Jens Lehmann | d4e98b5 | 2011-05-14 18:26:58 +0200 | [diff] [blame] | 830 | if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) || |
| 831 | gitmodules_is_unmerged) |
Jens Lehmann | c1a3c36 | 2010-11-11 00:55:41 +0100 | [diff] [blame] | 832 | continue; |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 833 | if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) { |
| 834 | if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) |
| 835 | continue; |
| 836 | default_argv = "on-demand"; |
| 837 | } |
Jens Lehmann | c1a3c36 | 2010-11-11 00:55:41 +0100 | [diff] [blame] | 838 | } |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 839 | } else if (spf->command_line_option == RECURSE_SUBMODULES_ON_DEMAND) { |
Jens Lehmann | 8f0700d | 2011-03-06 23:11:21 +0100 | [diff] [blame] | 840 | if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) |
| 841 | continue; |
| 842 | default_argv = "on-demand"; |
Jens Lehmann | be254a0 | 2010-11-11 00:55:02 +0100 | [diff] [blame] | 843 | } |
| 844 | |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 845 | strbuf_addf(&submodule_path, "%s/%s", spf->work_tree, ce->name); |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 846 | strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf); |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 847 | strbuf_addf(&submodule_prefix, "%s%s/", spf->prefix, ce->name); |
Junio C Hamano | 13d6ec9 | 2011-08-22 14:04:56 -0700 | [diff] [blame] | 848 | git_dir = read_gitfile(submodule_git_dir.buf); |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 849 | if (!git_dir) |
| 850 | git_dir = submodule_git_dir.buf; |
| 851 | if (is_directory(git_dir)) { |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 852 | child_process_init(cp); |
| 853 | cp->dir = strbuf_detach(&submodule_path, NULL); |
Jeff King | c12e865 | 2016-04-28 09:39:15 -0400 | [diff] [blame] | 854 | prepare_submodule_repo_env(&cp->env_array); |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 855 | cp->git_cmd = 1; |
| 856 | if (!spf->quiet) |
| 857 | strbuf_addf(err, "Fetching submodule %s%s\n", |
| 858 | spf->prefix, ce->name); |
| 859 | argv_array_init(&cp->args); |
| 860 | argv_array_pushv(&cp->args, spf->args.argv); |
| 861 | argv_array_push(&cp->args, default_argv); |
| 862 | argv_array_push(&cp->args, "--submodule-prefix"); |
| 863 | argv_array_push(&cp->args, submodule_prefix.buf); |
| 864 | ret = 1; |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 865 | } |
| 866 | strbuf_release(&submodule_path); |
| 867 | strbuf_release(&submodule_git_dir); |
| 868 | strbuf_release(&submodule_prefix); |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 869 | if (ret) { |
| 870 | spf->count++; |
| 871 | return 1; |
| 872 | } |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 873 | } |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 874 | return 0; |
| 875 | } |
| 876 | |
Stefan Beller | 2a73b3d | 2016-02-29 13:57:06 -0800 | [diff] [blame] | 877 | static int fetch_start_failure(struct strbuf *err, |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 878 | void *cb, void *task_cb) |
| 879 | { |
| 880 | struct submodule_parallel_fetch *spf = cb; |
| 881 | |
| 882 | spf->result = 1; |
| 883 | |
| 884 | return 0; |
| 885 | } |
| 886 | |
Stefan Beller | 2a73b3d | 2016-02-29 13:57:06 -0800 | [diff] [blame] | 887 | static int fetch_finish(int retvalue, struct strbuf *err, |
| 888 | void *cb, void *task_cb) |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 889 | { |
| 890 | struct submodule_parallel_fetch *spf = cb; |
| 891 | |
| 892 | if (retvalue) |
| 893 | spf->result = 1; |
| 894 | |
| 895 | return 0; |
| 896 | } |
| 897 | |
| 898 | int fetch_populated_submodules(const struct argv_array *options, |
| 899 | const char *prefix, int command_line_option, |
Stefan Beller | 62104ba | 2015-12-15 16:04:12 -0800 | [diff] [blame] | 900 | int quiet, int max_parallel_jobs) |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 901 | { |
| 902 | int i; |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 903 | struct submodule_parallel_fetch spf = SPF_INIT; |
| 904 | |
| 905 | spf.work_tree = get_git_work_tree(); |
| 906 | spf.command_line_option = command_line_option; |
| 907 | spf.quiet = quiet; |
| 908 | spf.prefix = prefix; |
| 909 | |
| 910 | if (!spf.work_tree) |
| 911 | goto out; |
| 912 | |
| 913 | if (read_cache() < 0) |
| 914 | die("index file corrupt"); |
| 915 | |
| 916 | argv_array_push(&spf.args, "fetch"); |
| 917 | for (i = 0; i < options->argc; i++) |
| 918 | argv_array_push(&spf.args, options->argv[i]); |
| 919 | argv_array_push(&spf.args, "--recurse-submodules-default"); |
| 920 | /* default value, "--submodule-prefix" and its value are added later */ |
| 921 | |
Stefan Beller | a028a19 | 2016-02-29 18:07:13 -0800 | [diff] [blame] | 922 | if (max_parallel_jobs < 0) |
| 923 | max_parallel_jobs = parallel_jobs; |
| 924 | |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 925 | calculate_changed_submodule_paths(); |
| 926 | run_processes_parallel(max_parallel_jobs, |
| 927 | get_next_submodule, |
| 928 | fetch_start_failure, |
| 929 | fetch_finish, |
| 930 | &spf); |
| 931 | |
| 932 | argv_array_clear(&spf.args); |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 933 | out: |
| 934 | string_list_clear(&changed_submodule_paths, 1); |
Stefan Beller | fe85ee6 | 2015-12-15 16:04:11 -0800 | [diff] [blame] | 935 | return spf.result; |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 936 | } |
| 937 | |
Jens Lehmann | 3bfc450 | 2010-03-13 23:00:27 +0100 | [diff] [blame] | 938 | unsigned is_submodule_modified(const char *path, int ignore_untracked) |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 939 | { |
Jens Lehmann | c7e1a73 | 2010-03-04 22:20:33 +0100 | [diff] [blame] | 940 | ssize_t len; |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 941 | struct child_process cp = CHILD_PROCESS_INIT; |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 942 | const char *argv[] = { |
| 943 | "status", |
| 944 | "--porcelain", |
| 945 | NULL, |
Jens Lehmann | 3bfc450 | 2010-03-13 23:00:27 +0100 | [diff] [blame] | 946 | NULL, |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 947 | }; |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 948 | struct strbuf buf = STRBUF_INIT; |
Jens Lehmann | c7e1a73 | 2010-03-04 22:20:33 +0100 | [diff] [blame] | 949 | unsigned dirty_submodule = 0; |
| 950 | const char *line, *next_line; |
Jens Lehmann | eee49b6 | 2010-04-10 19:01:12 +0200 | [diff] [blame] | 951 | const char *git_dir; |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 952 | |
Jens Lehmann | eee49b6 | 2010-04-10 19:01:12 +0200 | [diff] [blame] | 953 | strbuf_addf(&buf, "%s/.git", path); |
Junio C Hamano | 13d6ec9 | 2011-08-22 14:04:56 -0700 | [diff] [blame] | 954 | git_dir = read_gitfile(buf.buf); |
Jens Lehmann | eee49b6 | 2010-04-10 19:01:12 +0200 | [diff] [blame] | 955 | if (!git_dir) |
| 956 | git_dir = buf.buf; |
| 957 | if (!is_directory(git_dir)) { |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 958 | strbuf_release(&buf); |
| 959 | /* The submodule is not checked out, so it is not modified */ |
| 960 | return 0; |
| 961 | |
| 962 | } |
| 963 | strbuf_reset(&buf); |
| 964 | |
Jens Lehmann | 3bfc450 | 2010-03-13 23:00:27 +0100 | [diff] [blame] | 965 | if (ignore_untracked) |
| 966 | argv[2] = "-uno"; |
| 967 | |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 968 | cp.argv = argv; |
Jeff King | c12e865 | 2016-04-28 09:39:15 -0400 | [diff] [blame] | 969 | prepare_submodule_repo_env(&cp.env_array); |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 970 | cp.git_cmd = 1; |
| 971 | cp.no_stdin = 1; |
| 972 | cp.out = -1; |
Jens Lehmann | eee49b6 | 2010-04-10 19:01:12 +0200 | [diff] [blame] | 973 | cp.dir = path; |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 974 | if (start_command(&cp)) |
Jens Lehmann | 6a5ceda | 2011-12-07 22:50:14 +0100 | [diff] [blame] | 975 | die("Could not run 'git status --porcelain' in submodule %s", path); |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 976 | |
| 977 | len = strbuf_read(&buf, cp.out, 1024); |
Jens Lehmann | c7e1a73 | 2010-03-04 22:20:33 +0100 | [diff] [blame] | 978 | line = buf.buf; |
| 979 | while (len > 2) { |
| 980 | if ((line[0] == '?') && (line[1] == '?')) { |
| 981 | dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED; |
| 982 | if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED) |
| 983 | break; |
| 984 | } else { |
| 985 | dirty_submodule |= DIRTY_SUBMODULE_MODIFIED; |
Jens Lehmann | 3bfc450 | 2010-03-13 23:00:27 +0100 | [diff] [blame] | 986 | if (ignore_untracked || |
| 987 | (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)) |
Jens Lehmann | c7e1a73 | 2010-03-04 22:20:33 +0100 | [diff] [blame] | 988 | break; |
| 989 | } |
| 990 | next_line = strchr(line, '\n'); |
| 991 | if (!next_line) |
| 992 | break; |
| 993 | next_line++; |
| 994 | len -= (next_line - line); |
| 995 | line = next_line; |
| 996 | } |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 997 | close(cp.out); |
| 998 | |
| 999 | if (finish_command(&cp)) |
Jens Lehmann | 6a5ceda | 2011-12-07 22:50:14 +0100 | [diff] [blame] | 1000 | die("'git status --porcelain' failed in submodule %s", path); |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 1001 | |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 1002 | strbuf_release(&buf); |
Jens Lehmann | c7e1a73 | 2010-03-04 22:20:33 +0100 | [diff] [blame] | 1003 | return dirty_submodule; |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 1004 | } |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 1005 | |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 1006 | int submodule_uses_gitfile(const char *path) |
| 1007 | { |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 1008 | struct child_process cp = CHILD_PROCESS_INIT; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 1009 | const char *argv[] = { |
| 1010 | "submodule", |
| 1011 | "foreach", |
| 1012 | "--quiet", |
| 1013 | "--recursive", |
| 1014 | "test -f .git", |
| 1015 | NULL, |
| 1016 | }; |
| 1017 | struct strbuf buf = STRBUF_INIT; |
| 1018 | const char *git_dir; |
| 1019 | |
| 1020 | strbuf_addf(&buf, "%s/.git", path); |
| 1021 | git_dir = read_gitfile(buf.buf); |
| 1022 | if (!git_dir) { |
| 1023 | strbuf_release(&buf); |
| 1024 | return 0; |
| 1025 | } |
| 1026 | strbuf_release(&buf); |
| 1027 | |
| 1028 | /* Now test that all nested submodules use a gitfile too */ |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 1029 | cp.argv = argv; |
Jeff King | c12e865 | 2016-04-28 09:39:15 -0400 | [diff] [blame] | 1030 | prepare_submodule_repo_env(&cp.env_array); |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 1031 | cp.git_cmd = 1; |
| 1032 | cp.no_stdin = 1; |
| 1033 | cp.no_stderr = 1; |
| 1034 | cp.no_stdout = 1; |
| 1035 | cp.dir = path; |
| 1036 | if (run_command(&cp)) |
| 1037 | return 0; |
| 1038 | |
| 1039 | return 1; |
| 1040 | } |
| 1041 | |
| 1042 | int ok_to_remove_submodule(const char *path) |
| 1043 | { |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 1044 | ssize_t len; |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 1045 | struct child_process cp = CHILD_PROCESS_INIT; |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 1046 | const char *argv[] = { |
| 1047 | "status", |
| 1048 | "--porcelain", |
| 1049 | "-u", |
| 1050 | "--ignore-submodules=none", |
| 1051 | NULL, |
| 1052 | }; |
| 1053 | struct strbuf buf = STRBUF_INIT; |
| 1054 | int ok_to_remove = 1; |
| 1055 | |
René Scharfe | dbe44fa | 2015-05-19 23:44:23 +0200 | [diff] [blame] | 1056 | if (!file_exists(path) || is_empty_dir(path)) |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 1057 | return 1; |
| 1058 | |
| 1059 | if (!submodule_uses_gitfile(path)) |
| 1060 | return 0; |
| 1061 | |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 1062 | cp.argv = argv; |
Jeff King | c12e865 | 2016-04-28 09:39:15 -0400 | [diff] [blame] | 1063 | prepare_submodule_repo_env(&cp.env_array); |
Jens Lehmann | 293ab15 | 2012-09-26 20:21:13 +0200 | [diff] [blame] | 1064 | cp.git_cmd = 1; |
| 1065 | cp.no_stdin = 1; |
| 1066 | cp.out = -1; |
| 1067 | cp.dir = path; |
| 1068 | if (start_command(&cp)) |
| 1069 | die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path); |
| 1070 | |
| 1071 | len = strbuf_read(&buf, cp.out, 1024); |
| 1072 | if (len > 2) |
| 1073 | ok_to_remove = 0; |
| 1074 | close(cp.out); |
| 1075 | |
| 1076 | if (finish_command(&cp)) |
| 1077 | die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path); |
| 1078 | |
| 1079 | strbuf_release(&buf); |
| 1080 | return ok_to_remove; |
| 1081 | } |
| 1082 | |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 1083 | static int find_first_merges(struct object_array *result, const char *path, |
| 1084 | struct commit *a, struct commit *b) |
| 1085 | { |
| 1086 | int i, j; |
Michael Haggerty | 3826902 | 2013-05-25 11:08:12 +0200 | [diff] [blame] | 1087 | struct object_array merges = OBJECT_ARRAY_INIT; |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 1088 | struct commit *commit; |
| 1089 | int contains_another; |
| 1090 | |
| 1091 | char merged_revision[42]; |
| 1092 | const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path", |
| 1093 | "--all", merged_revision, NULL }; |
| 1094 | struct rev_info revs; |
| 1095 | struct setup_revision_opt rev_opts; |
| 1096 | |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 1097 | memset(result, 0, sizeof(struct object_array)); |
| 1098 | memset(&rev_opts, 0, sizeof(rev_opts)); |
| 1099 | |
| 1100 | /* get all revisions that merge commit a */ |
| 1101 | snprintf(merged_revision, sizeof(merged_revision), "^%s", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1102 | oid_to_hex(&a->object.oid)); |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 1103 | init_revisions(&revs, NULL); |
| 1104 | rev_opts.submodule = path; |
Elia Pinto | 94c0cc8 | 2014-06-27 05:11:39 -0700 | [diff] [blame] | 1105 | setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts); |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 1106 | |
| 1107 | /* save all revisions from the above list that contain b */ |
| 1108 | if (prepare_revision_walk(&revs)) |
| 1109 | die("revision walk setup failed"); |
| 1110 | while ((commit = get_revision(&revs)) != NULL) { |
| 1111 | struct object *o = &(commit->object); |
Junio C Hamano | a20efee | 2012-08-27 14:46:01 -0700 | [diff] [blame] | 1112 | if (in_merge_bases(b, commit)) |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 1113 | add_object_array(o, NULL, &merges); |
| 1114 | } |
Heiko Voigt | bcc0a3e | 2012-03-29 09:21:21 +0200 | [diff] [blame] | 1115 | reset_revision_walk(); |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 1116 | |
| 1117 | /* Now we've got all merges that contain a and b. Prune all |
| 1118 | * merges that contain another found merge and save them in |
| 1119 | * result. |
| 1120 | */ |
| 1121 | for (i = 0; i < merges.nr; i++) { |
| 1122 | struct commit *m1 = (struct commit *) merges.objects[i].item; |
| 1123 | |
| 1124 | contains_another = 0; |
| 1125 | for (j = 0; j < merges.nr; j++) { |
| 1126 | struct commit *m2 = (struct commit *) merges.objects[j].item; |
Junio C Hamano | a20efee | 2012-08-27 14:46:01 -0700 | [diff] [blame] | 1127 | if (i != j && in_merge_bases(m2, m1)) { |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 1128 | contains_another = 1; |
| 1129 | break; |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | if (!contains_another) |
Michael Haggerty | 5de0c01 | 2013-05-25 11:08:13 +0200 | [diff] [blame] | 1134 | add_object_array(merges.objects[i].item, NULL, result); |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | free(merges.objects); |
| 1138 | return result->nr; |
| 1139 | } |
| 1140 | |
| 1141 | static void print_commit(struct commit *commit) |
| 1142 | { |
| 1143 | struct strbuf sb = STRBUF_INIT; |
| 1144 | struct pretty_print_context ctx = {0}; |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 1145 | ctx.date_mode.type = DATE_NORMAL; |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 1146 | format_commit_message(commit, " %h: %m %s", &sb, &ctx); |
| 1147 | fprintf(stderr, "%s\n", sb.buf); |
| 1148 | strbuf_release(&sb); |
| 1149 | } |
| 1150 | |
| 1151 | #define MERGE_WARNING(path, msg) \ |
| 1152 | warning("Failed to merge submodule %s (%s)", path, msg); |
| 1153 | |
| 1154 | int merge_submodule(unsigned char result[20], const char *path, |
| 1155 | const unsigned char base[20], const unsigned char a[20], |
Brad King | 8098878 | 2011-10-13 08:59:05 -0400 | [diff] [blame] | 1156 | const unsigned char b[20], int search) |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 1157 | { |
| 1158 | struct commit *commit_base, *commit_a, *commit_b; |
| 1159 | int parent_count; |
| 1160 | struct object_array merges; |
| 1161 | |
| 1162 | int i; |
| 1163 | |
| 1164 | /* store a in result in case we fail */ |
| 1165 | hashcpy(result, a); |
| 1166 | |
| 1167 | /* we can not handle deletion conflicts */ |
| 1168 | if (is_null_sha1(base)) |
| 1169 | return 0; |
| 1170 | if (is_null_sha1(a)) |
| 1171 | return 0; |
| 1172 | if (is_null_sha1(b)) |
| 1173 | return 0; |
| 1174 | |
| 1175 | if (add_submodule_odb(path)) { |
| 1176 | MERGE_WARNING(path, "not checked out"); |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
| 1180 | if (!(commit_base = lookup_commit_reference(base)) || |
| 1181 | !(commit_a = lookup_commit_reference(a)) || |
| 1182 | !(commit_b = lookup_commit_reference(b))) { |
| 1183 | MERGE_WARNING(path, "commits not present"); |
| 1184 | return 0; |
| 1185 | } |
| 1186 | |
| 1187 | /* check whether both changes are forward */ |
Junio C Hamano | a20efee | 2012-08-27 14:46:01 -0700 | [diff] [blame] | 1188 | if (!in_merge_bases(commit_base, commit_a) || |
| 1189 | !in_merge_bases(commit_base, commit_b)) { |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 1190 | MERGE_WARNING(path, "commits don't follow merge-base"); |
| 1191 | return 0; |
| 1192 | } |
| 1193 | |
| 1194 | /* Case #1: a is contained in b or vice versa */ |
Junio C Hamano | a20efee | 2012-08-27 14:46:01 -0700 | [diff] [blame] | 1195 | if (in_merge_bases(commit_a, commit_b)) { |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 1196 | hashcpy(result, b); |
| 1197 | return 1; |
| 1198 | } |
Junio C Hamano | a20efee | 2012-08-27 14:46:01 -0700 | [diff] [blame] | 1199 | if (in_merge_bases(commit_b, commit_a)) { |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 1200 | hashcpy(result, a); |
| 1201 | return 1; |
| 1202 | } |
| 1203 | |
| 1204 | /* |
| 1205 | * Case #2: There are one or more merges that contain a and b in |
| 1206 | * the submodule. If there is only one, then present it as a |
| 1207 | * suggestion to the user, but leave it marked unmerged so the |
| 1208 | * user needs to confirm the resolution. |
| 1209 | */ |
| 1210 | |
Brad King | 8098878 | 2011-10-13 08:59:05 -0400 | [diff] [blame] | 1211 | /* Skip the search if makes no sense to the calling context. */ |
| 1212 | if (!search) |
| 1213 | return 0; |
| 1214 | |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 1215 | /* find commit which merges them */ |
| 1216 | parent_count = find_first_merges(&merges, path, commit_a, commit_b); |
| 1217 | switch (parent_count) { |
| 1218 | case 0: |
| 1219 | MERGE_WARNING(path, "merge following commits not found"); |
| 1220 | break; |
| 1221 | |
| 1222 | case 1: |
| 1223 | MERGE_WARNING(path, "not fast-forward"); |
| 1224 | fprintf(stderr, "Found a possible merge resolution " |
| 1225 | "for the submodule:\n"); |
| 1226 | print_commit((struct commit *) merges.objects[0].item); |
| 1227 | fprintf(stderr, |
| 1228 | "If this is correct simply add it to the index " |
| 1229 | "for example\n" |
| 1230 | "by using:\n\n" |
| 1231 | " git update-index --cacheinfo 160000 %s \"%s\"\n\n" |
| 1232 | "which will accept this suggestion.\n", |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 1233 | oid_to_hex(&merges.objects[0].item->oid), path); |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 1234 | break; |
| 1235 | |
| 1236 | default: |
| 1237 | MERGE_WARNING(path, "multiple merges found"); |
| 1238 | for (i = 0; i < merges.nr; i++) |
| 1239 | print_commit((struct commit *) merges.objects[i].item); |
| 1240 | } |
| 1241 | |
| 1242 | free(merges.objects); |
| 1243 | return 0; |
| 1244 | } |
Jens Lehmann | a88c915 | 2013-07-30 21:50:03 +0200 | [diff] [blame] | 1245 | |
| 1246 | /* Update gitfile and core.worktree setting to connect work tree and git dir */ |
| 1247 | void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir) |
| 1248 | { |
| 1249 | struct strbuf file_name = STRBUF_INIT; |
| 1250 | struct strbuf rel_path = STRBUF_INIT; |
| 1251 | const char *real_work_tree = xstrdup(real_path(work_tree)); |
Jens Lehmann | a88c915 | 2013-07-30 21:50:03 +0200 | [diff] [blame] | 1252 | |
| 1253 | /* Update gitfile */ |
| 1254 | strbuf_addf(&file_name, "%s/.git", work_tree); |
Junio C Hamano | 1f76a10 | 2015-08-24 13:20:39 -0700 | [diff] [blame] | 1255 | write_file(file_name.buf, "gitdir: %s", |
Nguyễn Thái Ngọc Duy | 91aacda | 2014-11-30 15:24:46 +0700 | [diff] [blame] | 1256 | relative_path(git_dir, real_work_tree, &rel_path)); |
Jens Lehmann | a88c915 | 2013-07-30 21:50:03 +0200 | [diff] [blame] | 1257 | |
| 1258 | /* Update core.worktree setting */ |
| 1259 | strbuf_reset(&file_name); |
| 1260 | strbuf_addf(&file_name, "%s/config", git_dir); |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 1261 | git_config_set_in_file(file_name.buf, "core.worktree", |
| 1262 | relative_path(real_work_tree, git_dir, |
| 1263 | &rel_path)); |
Jens Lehmann | a88c915 | 2013-07-30 21:50:03 +0200 | [diff] [blame] | 1264 | |
| 1265 | strbuf_release(&file_name); |
| 1266 | strbuf_release(&rel_path); |
| 1267 | free((void *)real_work_tree); |
| 1268 | } |
Stefan Beller | a028a19 | 2016-02-29 18:07:13 -0800 | [diff] [blame] | 1269 | |
| 1270 | int parallel_submodules(void) |
| 1271 | { |
| 1272 | return parallel_jobs; |
| 1273 | } |
Junio C Hamano | e059388 | 2016-05-17 14:38:25 -0700 | [diff] [blame] | 1274 | |
Jeff King | 4638728 | 2016-04-28 09:38:20 -0400 | [diff] [blame] | 1275 | void prepare_submodule_repo_env(struct argv_array *out) |
| 1276 | { |
| 1277 | const char * const *var; |
| 1278 | |
| 1279 | for (var = local_repo_env; *var; var++) { |
Jeff King | 89044ba | 2016-05-04 21:22:19 -0400 | [diff] [blame] | 1280 | if (strcmp(*var, CONFIG_DATA_ENVIRONMENT)) |
Jeff King | 4638728 | 2016-04-28 09:38:20 -0400 | [diff] [blame] | 1281 | argv_array_push(out, *var); |
Jeff King | 4638728 | 2016-04-28 09:38:20 -0400 | [diff] [blame] | 1282 | } |
Junio C Hamano | 10f5c52 | 2016-09-01 13:51:48 -0700 | [diff] [blame] | 1283 | argv_array_push(out, "GIT_DIR=.git"); |
Jeff King | 4638728 | 2016-04-28 09:38:20 -0400 | [diff] [blame] | 1284 | } |