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