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