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