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