7 #include "run-command.h"
10 #include "string-list.h"
12 struct string_list config_name_for_path;
13 struct string_list config_ignore_for_name;
15 static int add_submodule_odb(const char *path)
17 struct strbuf objects_directory = STRBUF_INIT;
18 struct alternate_object_database *alt_odb;
22 strbuf_addf(&objects_directory, "%s/.git", path);
23 git_dir = read_gitfile_gently(objects_directory.buf);
25 strbuf_reset(&objects_directory);
26 strbuf_addstr(&objects_directory, git_dir);
28 strbuf_addstr(&objects_directory, "/objects/");
29 if (!is_directory(objects_directory.buf)) {
33 /* avoid adding it twice */
34 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
35 if (alt_odb->name - alt_odb->base == objects_directory.len &&
36 !strncmp(alt_odb->base, objects_directory.buf,
37 objects_directory.len))
40 alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
41 alt_odb->next = alt_odb_list;
42 strcpy(alt_odb->base, objects_directory.buf);
43 alt_odb->name = alt_odb->base + objects_directory.len;
44 alt_odb->name[2] = '/';
45 alt_odb->name[40] = '\0';
46 alt_odb->name[41] = '\0';
47 alt_odb_list = alt_odb;
50 strbuf_release(&objects_directory);
54 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
57 struct string_list_item *path_option, *ignore_option;
58 path_option = unsorted_string_list_lookup(&config_name_for_path, path);
60 ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
62 handle_ignore_submodules_arg(diffopt, ignore_option->util);
66 static int submodule_config(const char *var, const char *value, void *cb)
68 if (!prefixcmp(var, "submodule."))
69 return parse_submodule_config_option(var, value);
73 void gitmodules_config(void)
75 const char *work_tree = get_git_work_tree();
77 struct strbuf gitmodules_path = STRBUF_INIT;
78 strbuf_addstr(&gitmodules_path, work_tree);
79 strbuf_addstr(&gitmodules_path, "/.gitmodules");
80 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
81 strbuf_release(&gitmodules_path);
85 int parse_submodule_config_option(const char *var, const char *value)
88 struct string_list_item *config;
89 struct strbuf submodname = STRBUF_INIT;
91 var += 10; /* Skip "submodule." */
94 if ((len > 5) && !strcmp(var + len - 5, ".path")) {
95 strbuf_add(&submodname, var, len - 5);
96 config = unsorted_string_list_lookup(&config_name_for_path, value);
100 config = string_list_append(&config_name_for_path, xstrdup(value));
101 config->util = strbuf_detach(&submodname, NULL);
102 strbuf_release(&submodname);
103 } else if ((len > 7) && !strcmp(var + len - 7, ".ignore")) {
104 if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
105 strcmp(value, "all") && strcmp(value, "none")) {
106 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
110 strbuf_add(&submodname, var, len - 7);
111 config = unsorted_string_list_lookup(&config_ignore_for_name, submodname.buf);
115 config = string_list_append(&config_ignore_for_name,
116 strbuf_detach(&submodname, NULL));
117 strbuf_release(&submodname);
118 config->util = xstrdup(value);
124 void handle_ignore_submodules_arg(struct diff_options *diffopt,
127 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
128 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
129 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
131 if (!strcmp(arg, "all"))
132 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
133 else if (!strcmp(arg, "untracked"))
134 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
135 else if (!strcmp(arg, "dirty"))
136 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
137 else if (strcmp(arg, "none"))
138 die("bad --ignore-submodules argument: %s", arg);
141 void show_submodule_summary(FILE *f, const char *path,
142 unsigned char one[20], unsigned char two[20],
143 unsigned dirty_submodule,
144 const char *del, const char *add, const char *reset)
147 struct commit *commit, *left = left, *right = right;
148 struct commit_list *merge_bases, *list;
149 const char *message = NULL;
150 struct strbuf sb = STRBUF_INIT;
151 static const char *format = " %m %s";
152 int fast_forward = 0, fast_backward = 0;
154 if (is_null_sha1(two))
155 message = "(submodule deleted)";
156 else if (add_submodule_odb(path))
157 message = "(not checked out)";
158 else if (is_null_sha1(one))
159 message = "(new submodule)";
160 else if (!(left = lookup_commit_reference(one)) ||
161 !(right = lookup_commit_reference(two)))
162 message = "(commits not present)";
165 init_revisions(&rev, NULL);
166 setup_revisions(0, NULL, &rev, NULL);
168 rev.first_parent_only = 1;
169 left->object.flags |= SYMMETRIC_LEFT;
170 add_pending_object(&rev, &left->object, path);
171 add_pending_object(&rev, &right->object, path);
172 merge_bases = get_merge_bases(left, right, 1);
174 if (merge_bases->item == left)
176 else if (merge_bases->item == right)
179 for (list = merge_bases; list; list = list->next) {
180 list->item->object.flags |= UNINTERESTING;
181 add_pending_object(&rev, &list->item->object,
182 sha1_to_hex(list->item->object.sha1));
184 if (prepare_revision_walk(&rev))
185 message = "(revision walker failed)";
188 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
189 fprintf(f, "Submodule %s contains untracked content\n", path);
190 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
191 fprintf(f, "Submodule %s contains modified content\n", path);
193 if (!hashcmp(one, two)) {
198 strbuf_addf(&sb, "Submodule %s %s..", path,
199 find_unique_abbrev(one, DEFAULT_ABBREV));
200 if (!fast_backward && !fast_forward)
201 strbuf_addch(&sb, '.');
202 strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
204 strbuf_addf(&sb, " %s\n", message);
206 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
207 fwrite(sb.buf, sb.len, 1, f);
210 while ((commit = get_revision(&rev))) {
211 struct pretty_print_context ctx = {0};
212 ctx.date_mode = rev.date_mode;
213 strbuf_setlen(&sb, 0);
214 if (commit->object.flags & SYMMETRIC_LEFT) {
216 strbuf_addstr(&sb, del);
219 strbuf_addstr(&sb, add);
220 format_commit_message(commit, format, &sb, &ctx);
222 strbuf_addstr(&sb, reset);
223 strbuf_addch(&sb, '\n');
224 fprintf(f, "%s", sb.buf);
226 clear_commit_marks(left, ~0);
227 clear_commit_marks(right, ~0);
232 unsigned is_submodule_modified(const char *path, int ignore_untracked)
235 struct child_process cp;
236 const char *argv[] = {
242 struct strbuf buf = STRBUF_INIT;
243 unsigned dirty_submodule = 0;
244 const char *line, *next_line;
247 strbuf_addf(&buf, "%s/.git", path);
248 git_dir = read_gitfile_gently(buf.buf);
251 if (!is_directory(git_dir)) {
252 strbuf_release(&buf);
253 /* The submodule is not checked out, so it is not modified */
259 if (ignore_untracked)
262 memset(&cp, 0, sizeof(cp));
264 cp.env = local_repo_env;
269 if (start_command(&cp))
270 die("Could not run git status --porcelain");
272 len = strbuf_read(&buf, cp.out, 1024);
275 if ((line[0] == '?') && (line[1] == '?')) {
276 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
277 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
280 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
281 if (ignore_untracked ||
282 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
285 next_line = strchr(line, '\n');
289 len -= (next_line - line);
294 if (finish_command(&cp))
295 die("git status --porcelain failed");
297 strbuf_release(&buf);
298 return dirty_submodule;
301 static int find_first_merges(struct object_array *result, const char *path,
302 struct commit *a, struct commit *b)
305 struct object_array merges;
306 struct commit *commit;
307 int contains_another;
309 char merged_revision[42];
310 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
311 "--all", merged_revision, NULL };
312 struct rev_info revs;
313 struct setup_revision_opt rev_opts;
315 memset(&merges, 0, sizeof(merges));
316 memset(result, 0, sizeof(struct object_array));
317 memset(&rev_opts, 0, sizeof(rev_opts));
319 /* get all revisions that merge commit a */
320 snprintf(merged_revision, sizeof(merged_revision), "^%s",
321 sha1_to_hex(a->object.sha1));
322 init_revisions(&revs, NULL);
323 rev_opts.submodule = path;
324 setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
326 /* save all revisions from the above list that contain b */
327 if (prepare_revision_walk(&revs))
328 die("revision walk setup failed");
329 while ((commit = get_revision(&revs)) != NULL) {
330 struct object *o = &(commit->object);
331 if (in_merge_bases(b, &commit, 1))
332 add_object_array(o, NULL, &merges);
335 /* Now we've got all merges that contain a and b. Prune all
336 * merges that contain another found merge and save them in
339 for (i = 0; i < merges.nr; i++) {
340 struct commit *m1 = (struct commit *) merges.objects[i].item;
342 contains_another = 0;
343 for (j = 0; j < merges.nr; j++) {
344 struct commit *m2 = (struct commit *) merges.objects[j].item;
345 if (i != j && in_merge_bases(m2, &m1, 1)) {
346 contains_another = 1;
351 if (!contains_another)
352 add_object_array(merges.objects[i].item,
353 merges.objects[i].name, result);
356 free(merges.objects);
360 static void print_commit(struct commit *commit)
362 struct strbuf sb = STRBUF_INIT;
363 struct pretty_print_context ctx = {0};
364 ctx.date_mode = DATE_NORMAL;
365 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
366 fprintf(stderr, "%s\n", sb.buf);
370 #define MERGE_WARNING(path, msg) \
371 warning("Failed to merge submodule %s (%s)", path, msg);
373 int merge_submodule(unsigned char result[20], const char *path,
374 const unsigned char base[20], const unsigned char a[20],
375 const unsigned char b[20])
377 struct commit *commit_base, *commit_a, *commit_b;
379 struct object_array merges;
383 /* store a in result in case we fail */
386 /* we can not handle deletion conflicts */
387 if (is_null_sha1(base))
394 if (add_submodule_odb(path)) {
395 MERGE_WARNING(path, "not checked out");
399 if (!(commit_base = lookup_commit_reference(base)) ||
400 !(commit_a = lookup_commit_reference(a)) ||
401 !(commit_b = lookup_commit_reference(b))) {
402 MERGE_WARNING(path, "commits not present");
406 /* check whether both changes are forward */
407 if (!in_merge_bases(commit_base, &commit_a, 1) ||
408 !in_merge_bases(commit_base, &commit_b, 1)) {
409 MERGE_WARNING(path, "commits don't follow merge-base");
413 /* Case #1: a is contained in b or vice versa */
414 if (in_merge_bases(commit_a, &commit_b, 1)) {
418 if (in_merge_bases(commit_b, &commit_a, 1)) {
424 * Case #2: There are one or more merges that contain a and b in
425 * the submodule. If there is only one, then present it as a
426 * suggestion to the user, but leave it marked unmerged so the
427 * user needs to confirm the resolution.
430 /* find commit which merges them */
431 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
432 switch (parent_count) {
434 MERGE_WARNING(path, "merge following commits not found");
438 MERGE_WARNING(path, "not fast-forward");
439 fprintf(stderr, "Found a possible merge resolution "
440 "for the submodule:\n");
441 print_commit((struct commit *) merges.objects[0].item);
443 "If this is correct simply add it to the index "
446 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
447 "which will accept this suggestion.\n",
448 sha1_to_hex(merges.objects[0].item->sha1), path);
452 MERGE_WARNING(path, "multiple merges found");
453 for (i = 0; i < merges.nr; i++)
454 print_commit((struct commit *) merges.objects[i].item);
457 free(merges.objects);