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