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