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