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