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