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 | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 13 | |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 14 | static struct string_list config_name_for_path; |
| 15 | static struct string_list config_fetch_recurse_submodules_for_name; |
| 16 | static struct string_list config_ignore_for_name; |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 17 | static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND; |
Junio C Hamano | 2071fb0 | 2011-04-04 15:02:01 -0700 | [diff] [blame] | 18 | static struct string_list changed_submodule_paths; |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 19 | static int initialized_fetch_ref_tips; |
| 20 | static struct sha1_array ref_tips_before_fetch; |
| 21 | static struct sha1_array ref_tips_after_fetch; |
| 22 | |
Jens Lehmann | d4e98b5 | 2011-05-14 18:26:58 +0200 | [diff] [blame] | 23 | /* |
| 24 | * The following flag is set if the .gitmodules file is unmerged. We then |
| 25 | * disable recursion for all submodules where .git/config doesn't have a |
| 26 | * matching config entry because we can't guess what might be configured in |
| 27 | * .gitmodules unless the user resolves the conflict. When a command line |
| 28 | * option is given (which always overrides configuration) this flag will be |
| 29 | * ignored. |
| 30 | */ |
| 31 | static int gitmodules_is_unmerged; |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 32 | |
Junio C Hamano | cb58c93 | 2010-01-11 22:31:58 -0800 | [diff] [blame] | 33 | static int add_submodule_odb(const char *path) |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 34 | { |
| 35 | struct strbuf objects_directory = STRBUF_INIT; |
| 36 | struct alternate_object_database *alt_odb; |
Jens Lehmann | de7a796 | 2010-01-31 17:43:49 +0100 | [diff] [blame] | 37 | int ret = 0; |
Jens Lehmann | eee49b6 | 2010-04-10 19:01:12 +0200 | [diff] [blame] | 38 | const char *git_dir; |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 39 | |
Jens Lehmann | eee49b6 | 2010-04-10 19:01:12 +0200 | [diff] [blame] | 40 | strbuf_addf(&objects_directory, "%s/.git", path); |
Junio C Hamano | 13d6ec9 | 2011-08-22 14:04:56 -0700 | [diff] [blame] | 41 | git_dir = read_gitfile(objects_directory.buf); |
Jens Lehmann | eee49b6 | 2010-04-10 19:01:12 +0200 | [diff] [blame] | 42 | if (git_dir) { |
| 43 | strbuf_reset(&objects_directory); |
| 44 | strbuf_addstr(&objects_directory, git_dir); |
| 45 | } |
| 46 | strbuf_addstr(&objects_directory, "/objects/"); |
Jens Lehmann | de7a796 | 2010-01-31 17:43:49 +0100 | [diff] [blame] | 47 | if (!is_directory(objects_directory.buf)) { |
| 48 | ret = -1; |
| 49 | goto done; |
| 50 | } |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 51 | /* avoid adding it twice */ |
| 52 | for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next) |
| 53 | if (alt_odb->name - alt_odb->base == objects_directory.len && |
| 54 | !strncmp(alt_odb->base, objects_directory.buf, |
| 55 | objects_directory.len)) |
Jens Lehmann | de7a796 | 2010-01-31 17:43:49 +0100 | [diff] [blame] | 56 | goto done; |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 57 | |
| 58 | alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb)); |
| 59 | alt_odb->next = alt_odb_list; |
| 60 | strcpy(alt_odb->base, objects_directory.buf); |
| 61 | alt_odb->name = alt_odb->base + objects_directory.len; |
| 62 | alt_odb->name[2] = '/'; |
| 63 | alt_odb->name[40] = '\0'; |
| 64 | alt_odb->name[41] = '\0'; |
| 65 | alt_odb_list = alt_odb; |
| 66 | prepare_alt_odb(); |
Jens Lehmann | de7a796 | 2010-01-31 17:43:49 +0100 | [diff] [blame] | 67 | done: |
| 68 | strbuf_release(&objects_directory); |
| 69 | return ret; |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 72 | void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt, |
| 73 | const char *path) |
| 74 | { |
| 75 | struct string_list_item *path_option, *ignore_option; |
| 76 | path_option = unsorted_string_list_lookup(&config_name_for_path, path); |
| 77 | if (path_option) { |
| 78 | ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util); |
| 79 | if (ignore_option) |
| 80 | handle_ignore_submodules_arg(diffopt, ignore_option->util); |
Jens Lehmann | d4e98b5 | 2011-05-14 18:26:58 +0200 | [diff] [blame] | 81 | else if (gitmodules_is_unmerged) |
| 82 | DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES); |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 86 | int submodule_config(const char *var, const char *value, void *cb) |
Jens Lehmann | 302ad7a | 2010-08-06 00:40:48 +0200 | [diff] [blame] | 87 | { |
| 88 | if (!prefixcmp(var, "submodule.")) |
| 89 | return parse_submodule_config_option(var, value); |
Jens Lehmann | be254a0 | 2010-11-11 00:55:02 +0100 | [diff] [blame] | 90 | else if (!strcmp(var, "fetch.recursesubmodules")) { |
Jens Lehmann | 1fb2550 | 2011-03-06 23:11:48 +0100 | [diff] [blame] | 91 | config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value); |
Jens Lehmann | be254a0 | 2010-11-11 00:55:02 +0100 | [diff] [blame] | 92 | return 0; |
| 93 | } |
Jens Lehmann | 302ad7a | 2010-08-06 00:40:48 +0200 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | void gitmodules_config(void) |
| 98 | { |
| 99 | const char *work_tree = get_git_work_tree(); |
| 100 | if (work_tree) { |
| 101 | struct strbuf gitmodules_path = STRBUF_INIT; |
Jens Lehmann | d4e98b5 | 2011-05-14 18:26:58 +0200 | [diff] [blame] | 102 | int pos; |
Jens Lehmann | 302ad7a | 2010-08-06 00:40:48 +0200 | [diff] [blame] | 103 | strbuf_addstr(&gitmodules_path, work_tree); |
| 104 | strbuf_addstr(&gitmodules_path, "/.gitmodules"); |
Jens Lehmann | d4e98b5 | 2011-05-14 18:26:58 +0200 | [diff] [blame] | 105 | if (read_cache() < 0) |
| 106 | die("index file corrupt"); |
| 107 | pos = cache_name_pos(".gitmodules", 11); |
| 108 | if (pos < 0) { /* .gitmodules not found or isn't merged */ |
| 109 | pos = -1 - pos; |
| 110 | if (active_nr > pos) { /* there is a .gitmodules */ |
| 111 | const struct cache_entry *ce = active_cache[pos]; |
| 112 | if (ce_namelen(ce) == 11 && |
| 113 | !memcmp(ce->name, ".gitmodules", 11)) |
| 114 | gitmodules_is_unmerged = 1; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (!gitmodules_is_unmerged) |
| 119 | git_config_from_file(submodule_config, gitmodules_path.buf, NULL); |
Jens Lehmann | 302ad7a | 2010-08-06 00:40:48 +0200 | [diff] [blame] | 120 | strbuf_release(&gitmodules_path); |
| 121 | } |
| 122 | } |
| 123 | |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 124 | int parse_submodule_config_option(const char *var, const char *value) |
| 125 | { |
| 126 | int len; |
| 127 | struct string_list_item *config; |
| 128 | struct strbuf submodname = STRBUF_INIT; |
| 129 | |
| 130 | var += 10; /* Skip "submodule." */ |
| 131 | |
| 132 | len = strlen(var); |
| 133 | if ((len > 5) && !strcmp(var + len - 5, ".path")) { |
| 134 | strbuf_add(&submodname, var, len - 5); |
| 135 | config = unsorted_string_list_lookup(&config_name_for_path, value); |
| 136 | if (config) |
| 137 | free(config->util); |
| 138 | else |
| 139 | config = string_list_append(&config_name_for_path, xstrdup(value)); |
| 140 | config->util = strbuf_detach(&submodname, NULL); |
| 141 | strbuf_release(&submodname); |
Jens Lehmann | c1a3c36 | 2010-11-11 00:55:41 +0100 | [diff] [blame] | 142 | } else if ((len > 23) && !strcmp(var + len - 23, ".fetchrecursesubmodules")) { |
| 143 | strbuf_add(&submodname, var, len - 23); |
| 144 | config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, submodname.buf); |
| 145 | if (!config) |
| 146 | config = string_list_append(&config_fetch_recurse_submodules_for_name, |
| 147 | strbuf_detach(&submodname, NULL)); |
Jens Lehmann | bf42b38 | 2011-03-06 23:12:19 +0100 | [diff] [blame] | 148 | config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value); |
Jens Lehmann | c1a3c36 | 2010-11-11 00:55:41 +0100 | [diff] [blame] | 149 | strbuf_release(&submodname); |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 150 | } else if ((len > 7) && !strcmp(var + len - 7, ".ignore")) { |
| 151 | if (strcmp(value, "untracked") && strcmp(value, "dirty") && |
| 152 | strcmp(value, "all") && strcmp(value, "none")) { |
| 153 | warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var); |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | strbuf_add(&submodname, var, len - 7); |
| 158 | config = unsorted_string_list_lookup(&config_ignore_for_name, submodname.buf); |
| 159 | if (config) |
| 160 | free(config->util); |
| 161 | else |
| 162 | config = string_list_append(&config_ignore_for_name, |
| 163 | strbuf_detach(&submodname, NULL)); |
| 164 | strbuf_release(&submodname); |
| 165 | config->util = xstrdup(value); |
| 166 | return 0; |
| 167 | } |
| 168 | return 0; |
| 169 | } |
| 170 | |
Jens Lehmann | 46a958b | 2010-06-25 16:56:47 +0200 | [diff] [blame] | 171 | void handle_ignore_submodules_arg(struct diff_options *diffopt, |
| 172 | const char *arg) |
| 173 | { |
Johannes Schindelin | be4f2b4 | 2010-08-05 10:49:55 +0200 | [diff] [blame] | 174 | DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES); |
| 175 | DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES); |
| 176 | DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES); |
| 177 | |
Jens Lehmann | 46a958b | 2010-06-25 16:56:47 +0200 | [diff] [blame] | 178 | if (!strcmp(arg, "all")) |
| 179 | DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES); |
| 180 | else if (!strcmp(arg, "untracked")) |
| 181 | DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES); |
| 182 | else if (!strcmp(arg, "dirty")) |
| 183 | DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES); |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 184 | else if (strcmp(arg, "none")) |
Jens Lehmann | 46a958b | 2010-06-25 16:56:47 +0200 | [diff] [blame] | 185 | die("bad --ignore-submodules argument: %s", arg); |
| 186 | } |
| 187 | |
Jonathan Nieder | 808a95d | 2011-03-16 02:14:11 -0500 | [diff] [blame] | 188 | static int prepare_submodule_summary(struct rev_info *rev, const char *path, |
| 189 | struct commit *left, struct commit *right, |
| 190 | int *fast_forward, int *fast_backward) |
| 191 | { |
| 192 | struct commit_list *merge_bases, *list; |
| 193 | |
| 194 | init_revisions(rev, NULL); |
| 195 | setup_revisions(0, NULL, rev, NULL); |
| 196 | rev->left_right = 1; |
| 197 | rev->first_parent_only = 1; |
| 198 | left->object.flags |= SYMMETRIC_LEFT; |
| 199 | add_pending_object(rev, &left->object, path); |
| 200 | add_pending_object(rev, &right->object, path); |
| 201 | merge_bases = get_merge_bases(left, right, 1); |
| 202 | if (merge_bases) { |
| 203 | if (merge_bases->item == left) |
| 204 | *fast_forward = 1; |
| 205 | else if (merge_bases->item == right) |
| 206 | *fast_backward = 1; |
| 207 | } |
| 208 | for (list = merge_bases; list; list = list->next) { |
| 209 | list->item->object.flags |= UNINTERESTING; |
| 210 | add_pending_object(rev, &list->item->object, |
| 211 | sha1_to_hex(list->item->object.sha1)); |
| 212 | } |
| 213 | return prepare_revision_walk(rev); |
| 214 | } |
| 215 | |
| 216 | static void print_submodule_summary(struct rev_info *rev, FILE *f, |
| 217 | const char *del, const char *add, const char *reset) |
| 218 | { |
| 219 | static const char format[] = " %m %s"; |
| 220 | struct strbuf sb = STRBUF_INIT; |
| 221 | struct commit *commit; |
| 222 | |
| 223 | while ((commit = get_revision(rev))) { |
| 224 | struct pretty_print_context ctx = {0}; |
| 225 | ctx.date_mode = rev->date_mode; |
| 226 | strbuf_setlen(&sb, 0); |
| 227 | if (commit->object.flags & SYMMETRIC_LEFT) { |
| 228 | if (del) |
| 229 | strbuf_addstr(&sb, del); |
| 230 | } |
| 231 | else if (add) |
| 232 | strbuf_addstr(&sb, add); |
| 233 | format_commit_message(commit, format, &sb, &ctx); |
| 234 | if (reset) |
| 235 | strbuf_addstr(&sb, reset); |
| 236 | strbuf_addch(&sb, '\n'); |
| 237 | fprintf(f, "%s", sb.buf); |
| 238 | } |
| 239 | strbuf_release(&sb); |
| 240 | } |
| 241 | |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 242 | int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg) |
| 243 | { |
| 244 | switch (git_config_maybe_bool(opt, arg)) { |
| 245 | case 1: |
| 246 | return RECURSE_SUBMODULES_ON; |
| 247 | case 0: |
| 248 | return RECURSE_SUBMODULES_OFF; |
| 249 | default: |
| 250 | if (!strcmp(arg, "on-demand")) |
| 251 | return RECURSE_SUBMODULES_ON_DEMAND; |
| 252 | die("bad %s argument: %s", opt, arg); |
| 253 | } |
| 254 | } |
| 255 | |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 256 | void show_submodule_summary(FILE *f, const char *path, |
| 257 | unsigned char one[20], unsigned char two[20], |
Jens Lehmann | 721ceec | 2010-01-24 15:09:00 +0100 | [diff] [blame] | 258 | unsigned dirty_submodule, |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 259 | const char *del, const char *add, const char *reset) |
| 260 | { |
| 261 | struct rev_info rev; |
Jonathan Nieder | 808a95d | 2011-03-16 02:14:11 -0500 | [diff] [blame] | 262 | struct commit *left = left, *right = right; |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 263 | const char *message = NULL; |
| 264 | struct strbuf sb = STRBUF_INIT; |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 265 | int fast_forward = 0, fast_backward = 0; |
| 266 | |
| 267 | if (is_null_sha1(two)) |
| 268 | message = "(submodule deleted)"; |
| 269 | else if (add_submodule_odb(path)) |
| 270 | message = "(not checked out)"; |
| 271 | else if (is_null_sha1(one)) |
| 272 | message = "(new submodule)"; |
| 273 | else if (!(left = lookup_commit_reference(one)) || |
| 274 | !(right = lookup_commit_reference(two))) |
| 275 | message = "(commits not present)"; |
| 276 | |
Jonathan Nieder | 808a95d | 2011-03-16 02:14:11 -0500 | [diff] [blame] | 277 | if (!message && |
| 278 | prepare_submodule_summary(&rev, path, left, right, |
| 279 | &fast_forward, &fast_backward)) |
| 280 | message = "(revision walker failed)"; |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 281 | |
Jens Lehmann | c7e1a73 | 2010-03-04 22:20:33 +0100 | [diff] [blame] | 282 | if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) |
| 283 | fprintf(f, "Submodule %s contains untracked content\n", path); |
| 284 | if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED) |
| 285 | fprintf(f, "Submodule %s contains modified content\n", path); |
| 286 | |
| 287 | if (!hashcmp(one, two)) { |
| 288 | strbuf_release(&sb); |
| 289 | return; |
| 290 | } |
| 291 | |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 292 | strbuf_addf(&sb, "Submodule %s %s..", path, |
| 293 | find_unique_abbrev(one, DEFAULT_ABBREV)); |
| 294 | if (!fast_backward && !fast_forward) |
| 295 | strbuf_addch(&sb, '.'); |
| 296 | strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV)); |
| 297 | if (message) |
| 298 | strbuf_addf(&sb, " %s\n", message); |
| 299 | else |
| 300 | strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : ""); |
| 301 | fwrite(sb.buf, sb.len, 1, f); |
| 302 | |
| 303 | if (!message) { |
Jonathan Nieder | 808a95d | 2011-03-16 02:14:11 -0500 | [diff] [blame] | 304 | print_submodule_summary(&rev, f, del, add, reset); |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 305 | clear_commit_marks(left, ~0); |
| 306 | clear_commit_marks(right, ~0); |
| 307 | } |
Jonathan Nieder | 808a95d | 2011-03-16 02:14:11 -0500 | [diff] [blame] | 308 | |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 309 | strbuf_release(&sb); |
| 310 | } |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 311 | |
Jens Lehmann | be254a0 | 2010-11-11 00:55:02 +0100 | [diff] [blame] | 312 | void set_config_fetch_recurse_submodules(int value) |
| 313 | { |
| 314 | config_fetch_recurse_submodules = value; |
| 315 | } |
| 316 | |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 317 | static int has_remote(const char *refname, const unsigned char *sha1, int flags, void *cb_data) |
| 318 | { |
| 319 | return 1; |
| 320 | } |
| 321 | |
| 322 | static int submodule_needs_pushing(const char *path, const unsigned char sha1[20]) |
| 323 | { |
| 324 | if (add_submodule_odb(path) || !lookup_commit_reference(sha1)) |
| 325 | return 0; |
| 326 | |
| 327 | if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) { |
| 328 | struct child_process cp; |
| 329 | const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL}; |
| 330 | struct strbuf buf = STRBUF_INIT; |
| 331 | int needs_pushing = 0; |
| 332 | |
| 333 | argv[1] = sha1_to_hex(sha1); |
| 334 | memset(&cp, 0, sizeof(cp)); |
| 335 | cp.argv = argv; |
| 336 | cp.env = local_repo_env; |
| 337 | cp.git_cmd = 1; |
| 338 | cp.no_stdin = 1; |
| 339 | cp.out = -1; |
| 340 | cp.dir = path; |
| 341 | if (start_command(&cp)) |
| 342 | die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s", |
| 343 | sha1_to_hex(sha1), path); |
| 344 | if (strbuf_read(&buf, cp.out, 41)) |
| 345 | needs_pushing = 1; |
| 346 | finish_command(&cp); |
| 347 | close(cp.out); |
| 348 | strbuf_release(&buf); |
| 349 | return needs_pushing; |
| 350 | } |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static void collect_submodules_from_diff(struct diff_queue_struct *q, |
| 356 | struct diff_options *options, |
| 357 | void *data) |
| 358 | { |
| 359 | int i; |
| 360 | int *needs_pushing = data; |
| 361 | |
| 362 | for (i = 0; i < q->nr; i++) { |
| 363 | struct diff_filepair *p = q->queue[i]; |
| 364 | if (!S_ISGITLINK(p->two->mode)) |
| 365 | continue; |
| 366 | if (submodule_needs_pushing(p->two->path, p->two->sha1)) { |
| 367 | *needs_pushing = 1; |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | |
René Scharfe | 78e98ea | 2011-12-17 11:27:19 +0100 | [diff] [blame] | 374 | static void commit_need_pushing(struct commit *commit, int *needs_pushing) |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 375 | { |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 376 | struct rev_info rev; |
| 377 | |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 378 | init_revisions(&rev, NULL); |
| 379 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; |
| 380 | rev.diffopt.format_callback = collect_submodules_from_diff; |
| 381 | rev.diffopt.format_callback_data = needs_pushing; |
René Scharfe | 78e98ea | 2011-12-17 11:27:19 +0100 | [diff] [blame] | 382 | diff_tree_combined_merge(commit, 1, &rev); |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remotes_name) |
| 386 | { |
| 387 | struct rev_info rev; |
| 388 | struct commit *commit; |
| 389 | const char *argv[] = {NULL, NULL, "--not", "NULL", NULL}; |
| 390 | int argc = ARRAY_SIZE(argv) - 1; |
| 391 | char *sha1_copy; |
| 392 | int needs_pushing = 0; |
| 393 | struct strbuf remotes_arg = STRBUF_INIT; |
| 394 | |
| 395 | strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name); |
| 396 | init_revisions(&rev, NULL); |
| 397 | sha1_copy = xstrdup(sha1_to_hex(new_sha1)); |
| 398 | argv[1] = sha1_copy; |
| 399 | argv[3] = remotes_arg.buf; |
| 400 | setup_revisions(argc, argv, &rev, NULL); |
| 401 | if (prepare_revision_walk(&rev)) |
| 402 | die("revision walk setup failed"); |
| 403 | |
| 404 | while ((commit = get_revision(&rev)) && !needs_pushing) |
René Scharfe | 78e98ea | 2011-12-17 11:27:19 +0100 | [diff] [blame] | 405 | commit_need_pushing(commit, &needs_pushing); |
Fredrik Gustafsson | d2b17b3 | 2011-08-20 00:08:47 +0200 | [diff] [blame] | 406 | |
| 407 | free(sha1_copy); |
| 408 | strbuf_release(&remotes_arg); |
| 409 | |
| 410 | return needs_pushing; |
| 411 | } |
| 412 | |
Jens Lehmann | c16c3e4 | 2011-03-06 23:12:58 +0100 | [diff] [blame] | 413 | static int is_submodule_commit_present(const char *path, unsigned char sha1[20]) |
| 414 | { |
| 415 | int is_present = 0; |
| 416 | if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) { |
| 417 | /* Even if the submodule is checked out and the commit is |
| 418 | * present, make sure it is reachable from a ref. */ |
| 419 | struct child_process cp; |
| 420 | const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL}; |
| 421 | struct strbuf buf = STRBUF_INIT; |
| 422 | |
| 423 | argv[3] = sha1_to_hex(sha1); |
| 424 | memset(&cp, 0, sizeof(cp)); |
| 425 | cp.argv = argv; |
| 426 | cp.env = local_repo_env; |
| 427 | cp.git_cmd = 1; |
| 428 | cp.no_stdin = 1; |
| 429 | cp.out = -1; |
| 430 | cp.dir = path; |
| 431 | if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024)) |
| 432 | is_present = 1; |
| 433 | |
| 434 | close(cp.out); |
| 435 | strbuf_release(&buf); |
| 436 | } |
| 437 | return is_present; |
| 438 | } |
| 439 | |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 440 | static void submodule_collect_changed_cb(struct diff_queue_struct *q, |
| 441 | struct diff_options *options, |
| 442 | void *data) |
| 443 | { |
| 444 | int i; |
| 445 | for (i = 0; i < q->nr; i++) { |
| 446 | struct diff_filepair *p = q->queue[i]; |
| 447 | if (!S_ISGITLINK(p->two->mode)) |
| 448 | continue; |
| 449 | |
| 450 | if (S_ISGITLINK(p->one->mode)) { |
| 451 | /* NEEDSWORK: We should honor the name configured in |
| 452 | * the .gitmodules file of the commit we are examining |
| 453 | * here to be able to correctly follow submodules |
| 454 | * being moved around. */ |
| 455 | struct string_list_item *path; |
| 456 | path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path); |
Jens Lehmann | c16c3e4 | 2011-03-06 23:12:58 +0100 | [diff] [blame] | 457 | if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1)) |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 458 | string_list_append(&changed_submodule_paths, xstrdup(p->two->path)); |
| 459 | } else { |
| 460 | /* Submodule is new or was moved here */ |
| 461 | /* NEEDSWORK: When the .git directories of submodules |
| 462 | * live inside the superprojects .git directory some |
| 463 | * day we should fetch new submodules directly into |
| 464 | * that location too when config or options request |
| 465 | * that so they can be checked out from there. */ |
| 466 | continue; |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 471 | static int add_sha1_to_array(const char *ref, const unsigned char *sha1, |
| 472 | int flags, void *data) |
| 473 | { |
| 474 | sha1_array_append(data, sha1); |
| 475 | return 0; |
| 476 | } |
| 477 | |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 478 | void check_for_new_submodule_commits(unsigned char new_sha1[20]) |
| 479 | { |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 480 | if (!initialized_fetch_ref_tips) { |
| 481 | for_each_ref(add_sha1_to_array, &ref_tips_before_fetch); |
| 482 | initialized_fetch_ref_tips = 1; |
| 483 | } |
| 484 | |
| 485 | sha1_array_append(&ref_tips_after_fetch, new_sha1); |
| 486 | } |
| 487 | |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 488 | static void add_sha1_to_argv(const unsigned char sha1[20], void *data) |
| 489 | { |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 490 | argv_array_push(data, sha1_to_hex(sha1)); |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | static void calculate_changed_submodule_paths(void) |
| 494 | { |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 495 | struct rev_info rev; |
| 496 | struct commit *commit; |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 497 | struct argv_array argv = ARGV_ARRAY_INIT; |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 498 | |
Jens Lehmann | 18322ba | 2011-09-09 20:22:03 +0200 | [diff] [blame] | 499 | /* No need to check if there are no submodules configured */ |
| 500 | if (!config_name_for_path.nr) |
| 501 | return; |
| 502 | |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 503 | init_revisions(&rev, NULL); |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 504 | argv_array_push(&argv, "--"); /* argv[0] program name */ |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 505 | sha1_array_for_each_unique(&ref_tips_after_fetch, |
| 506 | add_sha1_to_argv, &argv); |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 507 | argv_array_push(&argv, "--not"); |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 508 | sha1_array_for_each_unique(&ref_tips_before_fetch, |
| 509 | add_sha1_to_argv, &argv); |
| 510 | setup_revisions(argv.argc, argv.argv, &rev, NULL); |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 511 | if (prepare_revision_walk(&rev)) |
| 512 | die("revision walk setup failed"); |
| 513 | |
| 514 | /* |
| 515 | * Collect all submodules (whether checked out or not) for which new |
| 516 | * commits have been recorded upstream in "changed_submodule_paths". |
| 517 | */ |
| 518 | while ((commit = get_revision(&rev))) { |
| 519 | struct commit_list *parent = commit->parents; |
| 520 | while (parent) { |
| 521 | struct diff_options diff_opts; |
| 522 | diff_setup(&diff_opts); |
Jens Lehmann | ea2d325 | 2011-06-20 20:18:03 +0200 | [diff] [blame] | 523 | DIFF_OPT_SET(&diff_opts, RECURSIVE); |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 524 | diff_opts.output_format |= DIFF_FORMAT_CALLBACK; |
| 525 | diff_opts.format_callback = submodule_collect_changed_cb; |
Thomas Rast | 2845265 | 2012-08-03 14:16:24 +0200 | [diff] [blame^] | 526 | diff_setup_done(&diff_opts); |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 527 | diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts); |
| 528 | diffcore_std(&diff_opts); |
| 529 | diff_flush(&diff_opts); |
| 530 | parent = parent->next; |
| 531 | } |
| 532 | } |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 533 | |
Jeff King | c1189ca | 2011-09-13 17:57:57 -0400 | [diff] [blame] | 534 | argv_array_clear(&argv); |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 535 | sha1_array_clear(&ref_tips_before_fetch); |
| 536 | sha1_array_clear(&ref_tips_after_fetch); |
| 537 | initialized_fetch_ref_tips = 0; |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 538 | } |
| 539 | |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 540 | int fetch_populated_submodules(int num_options, const char **options, |
Jens Lehmann | 8f0700d | 2011-03-06 23:11:21 +0100 | [diff] [blame] | 541 | const char *prefix, int command_line_option, |
Jens Lehmann | be254a0 | 2010-11-11 00:55:02 +0100 | [diff] [blame] | 542 | int quiet) |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 543 | { |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 544 | int i, result = 0, argc = 0, default_argc; |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 545 | struct child_process cp; |
| 546 | const char **argv; |
| 547 | struct string_list_item *name_for_path; |
| 548 | const char *work_tree = get_git_work_tree(); |
| 549 | if (!work_tree) |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 550 | goto out; |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 551 | |
| 552 | if (!the_index.initialized) |
| 553 | if (read_cache() < 0) |
| 554 | die("index file corrupt"); |
| 555 | |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 556 | /* 6: "fetch" (options) --recurse-submodules-default default "--submodule-prefix" prefix NULL */ |
| 557 | argv = xcalloc(num_options + 6, sizeof(const char *)); |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 558 | argv[argc++] = "fetch"; |
| 559 | for (i = 0; i < num_options; i++) |
| 560 | argv[argc++] = options[i]; |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 561 | argv[argc++] = "--recurse-submodules-default"; |
| 562 | default_argc = argc++; |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 563 | argv[argc++] = "--submodule-prefix"; |
| 564 | |
| 565 | memset(&cp, 0, sizeof(cp)); |
| 566 | cp.argv = argv; |
| 567 | cp.env = local_repo_env; |
| 568 | cp.git_cmd = 1; |
| 569 | cp.no_stdin = 1; |
| 570 | |
Jeff King | 6859de4 | 2011-09-12 15:56:52 -0400 | [diff] [blame] | 571 | calculate_changed_submodule_paths(); |
| 572 | |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 573 | for (i = 0; i < active_nr; i++) { |
| 574 | struct strbuf submodule_path = STRBUF_INIT; |
| 575 | struct strbuf submodule_git_dir = STRBUF_INIT; |
| 576 | struct strbuf submodule_prefix = STRBUF_INIT; |
| 577 | struct cache_entry *ce = active_cache[i]; |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 578 | const char *git_dir, *name, *default_argv; |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 579 | |
| 580 | if (!S_ISGITLINK(ce->ce_mode)) |
| 581 | continue; |
| 582 | |
| 583 | name = ce->name; |
| 584 | name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name); |
| 585 | if (name_for_path) |
| 586 | name = name_for_path->util; |
| 587 | |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 588 | default_argv = "yes"; |
Jens Lehmann | 8f0700d | 2011-03-06 23:11:21 +0100 | [diff] [blame] | 589 | if (command_line_option == RECURSE_SUBMODULES_DEFAULT) { |
Jens Lehmann | c1a3c36 | 2010-11-11 00:55:41 +0100 | [diff] [blame] | 590 | struct string_list_item *fetch_recurse_submodules_option; |
| 591 | fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name); |
| 592 | if (fetch_recurse_submodules_option) { |
Jens Lehmann | bf42b38 | 2011-03-06 23:12:19 +0100 | [diff] [blame] | 593 | if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF) |
Jens Lehmann | c1a3c36 | 2010-11-11 00:55:41 +0100 | [diff] [blame] | 594 | continue; |
Jens Lehmann | bf42b38 | 2011-03-06 23:12:19 +0100 | [diff] [blame] | 595 | if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) { |
| 596 | if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) |
| 597 | continue; |
| 598 | default_argv = "on-demand"; |
| 599 | } |
Jens Lehmann | c1a3c36 | 2010-11-11 00:55:41 +0100 | [diff] [blame] | 600 | } else { |
Jens Lehmann | d4e98b5 | 2011-05-14 18:26:58 +0200 | [diff] [blame] | 601 | if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) || |
| 602 | gitmodules_is_unmerged) |
Jens Lehmann | c1a3c36 | 2010-11-11 00:55:41 +0100 | [diff] [blame] | 603 | continue; |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 604 | if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) { |
| 605 | if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) |
| 606 | continue; |
| 607 | default_argv = "on-demand"; |
| 608 | } |
Jens Lehmann | c1a3c36 | 2010-11-11 00:55:41 +0100 | [diff] [blame] | 609 | } |
Jens Lehmann | 8f0700d | 2011-03-06 23:11:21 +0100 | [diff] [blame] | 610 | } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) { |
| 611 | if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) |
| 612 | continue; |
| 613 | default_argv = "on-demand"; |
Jens Lehmann | be254a0 | 2010-11-11 00:55:02 +0100 | [diff] [blame] | 614 | } |
| 615 | |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 616 | strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name); |
| 617 | strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf); |
| 618 | strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name); |
Junio C Hamano | 13d6ec9 | 2011-08-22 14:04:56 -0700 | [diff] [blame] | 619 | git_dir = read_gitfile(submodule_git_dir.buf); |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 620 | if (!git_dir) |
| 621 | git_dir = submodule_git_dir.buf; |
| 622 | if (is_directory(git_dir)) { |
| 623 | if (!quiet) |
| 624 | printf("Fetching submodule %s%s\n", prefix, ce->name); |
| 625 | cp.dir = submodule_path.buf; |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 626 | argv[default_argc] = default_argv; |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 627 | argv[argc] = submodule_prefix.buf; |
| 628 | if (run_command(&cp)) |
| 629 | result = 1; |
| 630 | } |
| 631 | strbuf_release(&submodule_path); |
| 632 | strbuf_release(&submodule_git_dir); |
| 633 | strbuf_release(&submodule_prefix); |
| 634 | } |
| 635 | free(argv); |
Jens Lehmann | 88a2197 | 2011-03-06 23:10:46 +0100 | [diff] [blame] | 636 | out: |
| 637 | string_list_clear(&changed_submodule_paths, 1); |
Jens Lehmann | 7dce19d | 2010-11-12 13:54:52 +0100 | [diff] [blame] | 638 | return result; |
| 639 | } |
| 640 | |
Jens Lehmann | 3bfc450 | 2010-03-13 23:00:27 +0100 | [diff] [blame] | 641 | unsigned is_submodule_modified(const char *path, int ignore_untracked) |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 642 | { |
Jens Lehmann | c7e1a73 | 2010-03-04 22:20:33 +0100 | [diff] [blame] | 643 | ssize_t len; |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 644 | struct child_process cp; |
| 645 | const char *argv[] = { |
| 646 | "status", |
| 647 | "--porcelain", |
| 648 | NULL, |
Jens Lehmann | 3bfc450 | 2010-03-13 23:00:27 +0100 | [diff] [blame] | 649 | NULL, |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 650 | }; |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 651 | struct strbuf buf = STRBUF_INIT; |
Jens Lehmann | c7e1a73 | 2010-03-04 22:20:33 +0100 | [diff] [blame] | 652 | unsigned dirty_submodule = 0; |
| 653 | const char *line, *next_line; |
Jens Lehmann | eee49b6 | 2010-04-10 19:01:12 +0200 | [diff] [blame] | 654 | const char *git_dir; |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 655 | |
Jens Lehmann | eee49b6 | 2010-04-10 19:01:12 +0200 | [diff] [blame] | 656 | strbuf_addf(&buf, "%s/.git", path); |
Junio C Hamano | 13d6ec9 | 2011-08-22 14:04:56 -0700 | [diff] [blame] | 657 | git_dir = read_gitfile(buf.buf); |
Jens Lehmann | eee49b6 | 2010-04-10 19:01:12 +0200 | [diff] [blame] | 658 | if (!git_dir) |
| 659 | git_dir = buf.buf; |
| 660 | if (!is_directory(git_dir)) { |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 661 | strbuf_release(&buf); |
| 662 | /* The submodule is not checked out, so it is not modified */ |
| 663 | return 0; |
| 664 | |
| 665 | } |
| 666 | strbuf_reset(&buf); |
| 667 | |
Jens Lehmann | 3bfc450 | 2010-03-13 23:00:27 +0100 | [diff] [blame] | 668 | if (ignore_untracked) |
| 669 | argv[2] = "-uno"; |
| 670 | |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 671 | memset(&cp, 0, sizeof(cp)); |
| 672 | cp.argv = argv; |
Jens Lehmann | eee49b6 | 2010-04-10 19:01:12 +0200 | [diff] [blame] | 673 | cp.env = local_repo_env; |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 674 | cp.git_cmd = 1; |
| 675 | cp.no_stdin = 1; |
| 676 | cp.out = -1; |
Jens Lehmann | eee49b6 | 2010-04-10 19:01:12 +0200 | [diff] [blame] | 677 | cp.dir = path; |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 678 | if (start_command(&cp)) |
Jens Lehmann | 6a5ceda | 2011-12-07 22:50:14 +0100 | [diff] [blame] | 679 | die("Could not run 'git status --porcelain' in submodule %s", path); |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 680 | |
| 681 | len = strbuf_read(&buf, cp.out, 1024); |
Jens Lehmann | c7e1a73 | 2010-03-04 22:20:33 +0100 | [diff] [blame] | 682 | line = buf.buf; |
| 683 | while (len > 2) { |
| 684 | if ((line[0] == '?') && (line[1] == '?')) { |
| 685 | dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED; |
| 686 | if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED) |
| 687 | break; |
| 688 | } else { |
| 689 | dirty_submodule |= DIRTY_SUBMODULE_MODIFIED; |
Jens Lehmann | 3bfc450 | 2010-03-13 23:00:27 +0100 | [diff] [blame] | 690 | if (ignore_untracked || |
| 691 | (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)) |
Jens Lehmann | c7e1a73 | 2010-03-04 22:20:33 +0100 | [diff] [blame] | 692 | break; |
| 693 | } |
| 694 | next_line = strchr(line, '\n'); |
| 695 | if (!next_line) |
| 696 | break; |
| 697 | next_line++; |
| 698 | len -= (next_line - line); |
| 699 | line = next_line; |
| 700 | } |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 701 | close(cp.out); |
| 702 | |
| 703 | if (finish_command(&cp)) |
Jens Lehmann | 6a5ceda | 2011-12-07 22:50:14 +0100 | [diff] [blame] | 704 | die("'git status --porcelain' failed in submodule %s", path); |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 705 | |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 706 | strbuf_release(&buf); |
Jens Lehmann | c7e1a73 | 2010-03-04 22:20:33 +0100 | [diff] [blame] | 707 | return dirty_submodule; |
Jens Lehmann | ee6fc51 | 2010-01-16 18:42:24 +0100 | [diff] [blame] | 708 | } |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 709 | |
| 710 | static int find_first_merges(struct object_array *result, const char *path, |
| 711 | struct commit *a, struct commit *b) |
| 712 | { |
| 713 | int i, j; |
| 714 | struct object_array merges; |
| 715 | struct commit *commit; |
| 716 | int contains_another; |
| 717 | |
| 718 | char merged_revision[42]; |
| 719 | const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path", |
| 720 | "--all", merged_revision, NULL }; |
| 721 | struct rev_info revs; |
| 722 | struct setup_revision_opt rev_opts; |
| 723 | |
| 724 | memset(&merges, 0, sizeof(merges)); |
| 725 | memset(result, 0, sizeof(struct object_array)); |
| 726 | memset(&rev_opts, 0, sizeof(rev_opts)); |
| 727 | |
| 728 | /* get all revisions that merge commit a */ |
| 729 | snprintf(merged_revision, sizeof(merged_revision), "^%s", |
| 730 | sha1_to_hex(a->object.sha1)); |
| 731 | init_revisions(&revs, NULL); |
| 732 | rev_opts.submodule = path; |
| 733 | setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts); |
| 734 | |
| 735 | /* save all revisions from the above list that contain b */ |
| 736 | if (prepare_revision_walk(&revs)) |
| 737 | die("revision walk setup failed"); |
| 738 | while ((commit = get_revision(&revs)) != NULL) { |
| 739 | struct object *o = &(commit->object); |
| 740 | if (in_merge_bases(b, &commit, 1)) |
| 741 | add_object_array(o, NULL, &merges); |
| 742 | } |
| 743 | |
| 744 | /* Now we've got all merges that contain a and b. Prune all |
| 745 | * merges that contain another found merge and save them in |
| 746 | * result. |
| 747 | */ |
| 748 | for (i = 0; i < merges.nr; i++) { |
| 749 | struct commit *m1 = (struct commit *) merges.objects[i].item; |
| 750 | |
| 751 | contains_another = 0; |
| 752 | for (j = 0; j < merges.nr; j++) { |
| 753 | struct commit *m2 = (struct commit *) merges.objects[j].item; |
| 754 | if (i != j && in_merge_bases(m2, &m1, 1)) { |
| 755 | contains_another = 1; |
| 756 | break; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | if (!contains_another) |
| 761 | add_object_array(merges.objects[i].item, |
| 762 | merges.objects[i].name, result); |
| 763 | } |
| 764 | |
| 765 | free(merges.objects); |
| 766 | return result->nr; |
| 767 | } |
| 768 | |
| 769 | static void print_commit(struct commit *commit) |
| 770 | { |
| 771 | struct strbuf sb = STRBUF_INIT; |
| 772 | struct pretty_print_context ctx = {0}; |
| 773 | ctx.date_mode = DATE_NORMAL; |
| 774 | format_commit_message(commit, " %h: %m %s", &sb, &ctx); |
| 775 | fprintf(stderr, "%s\n", sb.buf); |
| 776 | strbuf_release(&sb); |
| 777 | } |
| 778 | |
| 779 | #define MERGE_WARNING(path, msg) \ |
| 780 | warning("Failed to merge submodule %s (%s)", path, msg); |
| 781 | |
| 782 | int merge_submodule(unsigned char result[20], const char *path, |
| 783 | const unsigned char base[20], const unsigned char a[20], |
Brad King | 8098878 | 2011-10-13 08:59:05 -0400 | [diff] [blame] | 784 | const unsigned char b[20], int search) |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 785 | { |
| 786 | struct commit *commit_base, *commit_a, *commit_b; |
| 787 | int parent_count; |
| 788 | struct object_array merges; |
| 789 | |
| 790 | int i; |
| 791 | |
| 792 | /* store a in result in case we fail */ |
| 793 | hashcpy(result, a); |
| 794 | |
| 795 | /* we can not handle deletion conflicts */ |
| 796 | if (is_null_sha1(base)) |
| 797 | return 0; |
| 798 | if (is_null_sha1(a)) |
| 799 | return 0; |
| 800 | if (is_null_sha1(b)) |
| 801 | return 0; |
| 802 | |
| 803 | if (add_submodule_odb(path)) { |
| 804 | MERGE_WARNING(path, "not checked out"); |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | if (!(commit_base = lookup_commit_reference(base)) || |
| 809 | !(commit_a = lookup_commit_reference(a)) || |
| 810 | !(commit_b = lookup_commit_reference(b))) { |
| 811 | MERGE_WARNING(path, "commits not present"); |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | /* check whether both changes are forward */ |
| 816 | if (!in_merge_bases(commit_base, &commit_a, 1) || |
| 817 | !in_merge_bases(commit_base, &commit_b, 1)) { |
| 818 | MERGE_WARNING(path, "commits don't follow merge-base"); |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | /* Case #1: a is contained in b or vice versa */ |
| 823 | if (in_merge_bases(commit_a, &commit_b, 1)) { |
| 824 | hashcpy(result, b); |
| 825 | return 1; |
| 826 | } |
| 827 | if (in_merge_bases(commit_b, &commit_a, 1)) { |
| 828 | hashcpy(result, a); |
| 829 | return 1; |
| 830 | } |
| 831 | |
| 832 | /* |
| 833 | * Case #2: There are one or more merges that contain a and b in |
| 834 | * the submodule. If there is only one, then present it as a |
| 835 | * suggestion to the user, but leave it marked unmerged so the |
| 836 | * user needs to confirm the resolution. |
| 837 | */ |
| 838 | |
Brad King | 8098878 | 2011-10-13 08:59:05 -0400 | [diff] [blame] | 839 | /* Skip the search if makes no sense to the calling context. */ |
| 840 | if (!search) |
| 841 | return 0; |
| 842 | |
Heiko Voigt | 68d03e4 | 2010-07-07 15:39:13 +0200 | [diff] [blame] | 843 | /* find commit which merges them */ |
| 844 | parent_count = find_first_merges(&merges, path, commit_a, commit_b); |
| 845 | switch (parent_count) { |
| 846 | case 0: |
| 847 | MERGE_WARNING(path, "merge following commits not found"); |
| 848 | break; |
| 849 | |
| 850 | case 1: |
| 851 | MERGE_WARNING(path, "not fast-forward"); |
| 852 | fprintf(stderr, "Found a possible merge resolution " |
| 853 | "for the submodule:\n"); |
| 854 | print_commit((struct commit *) merges.objects[0].item); |
| 855 | fprintf(stderr, |
| 856 | "If this is correct simply add it to the index " |
| 857 | "for example\n" |
| 858 | "by using:\n\n" |
| 859 | " git update-index --cacheinfo 160000 %s \"%s\"\n\n" |
| 860 | "which will accept this suggestion.\n", |
| 861 | sha1_to_hex(merges.objects[0].item->sha1), path); |
| 862 | break; |
| 863 | |
| 864 | default: |
| 865 | MERGE_WARNING(path, "multiple merges found"); |
| 866 | for (i = 0; i < merges.nr; i++) |
| 867 | print_commit((struct commit *) merges.objects[i].item); |
| 868 | } |
| 869 | |
| 870 | free(merges.objects); |
| 871 | return 0; |
| 872 | } |