Start preparing release notes to 1.7.5
[git/jrn.git] / submodule.c
blob6f1c10722f744f4a27f18dae4867b15ecbf57d49
1 #include "cache.h"
2 #include "submodule.h"
3 #include "dir.h"
4 #include "diff.h"
5 #include "commit.h"
6 #include "revision.h"
7 #include "run-command.h"
8 #include "diffcore.h"
9 #include "refs.h"
10 #include "string-list.h"
12 struct string_list config_name_for_path;
13 struct string_list config_fetch_recurse_submodules_for_name;
14 struct string_list config_ignore_for_name;
15 static int config_fetch_recurse_submodules;
17 static int add_submodule_odb(const char *path)
19 struct strbuf objects_directory = STRBUF_INIT;
20 struct alternate_object_database *alt_odb;
21 int ret = 0;
22 const char *git_dir;
24 strbuf_addf(&objects_directory, "%s/.git", path);
25 git_dir = read_gitfile_gently(objects_directory.buf);
26 if (git_dir) {
27 strbuf_reset(&objects_directory);
28 strbuf_addstr(&objects_directory, git_dir);
30 strbuf_addstr(&objects_directory, "/objects/");
31 if (!is_directory(objects_directory.buf)) {
32 ret = -1;
33 goto done;
35 /* avoid adding it twice */
36 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
37 if (alt_odb->name - alt_odb->base == objects_directory.len &&
38 !strncmp(alt_odb->base, objects_directory.buf,
39 objects_directory.len))
40 goto done;
42 alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
43 alt_odb->next = alt_odb_list;
44 strcpy(alt_odb->base, objects_directory.buf);
45 alt_odb->name = alt_odb->base + objects_directory.len;
46 alt_odb->name[2] = '/';
47 alt_odb->name[40] = '\0';
48 alt_odb->name[41] = '\0';
49 alt_odb_list = alt_odb;
50 prepare_alt_odb();
51 done:
52 strbuf_release(&objects_directory);
53 return ret;
56 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
57 const char *path)
59 struct string_list_item *path_option, *ignore_option;
60 path_option = unsorted_string_list_lookup(&config_name_for_path, path);
61 if (path_option) {
62 ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
63 if (ignore_option)
64 handle_ignore_submodules_arg(diffopt, ignore_option->util);
68 int submodule_config(const char *var, const char *value, void *cb)
70 if (!prefixcmp(var, "submodule."))
71 return parse_submodule_config_option(var, value);
72 else if (!strcmp(var, "fetch.recursesubmodules")) {
73 config_fetch_recurse_submodules = git_config_bool(var, value);
74 return 0;
76 return 0;
79 void gitmodules_config(void)
81 const char *work_tree = get_git_work_tree();
82 if (work_tree) {
83 struct strbuf gitmodules_path = STRBUF_INIT;
84 strbuf_addstr(&gitmodules_path, work_tree);
85 strbuf_addstr(&gitmodules_path, "/.gitmodules");
86 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
87 strbuf_release(&gitmodules_path);
91 int parse_submodule_config_option(const char *var, const char *value)
93 int len;
94 struct string_list_item *config;
95 struct strbuf submodname = STRBUF_INIT;
97 var += 10; /* Skip "submodule." */
99 len = strlen(var);
100 if ((len > 5) && !strcmp(var + len - 5, ".path")) {
101 strbuf_add(&submodname, var, len - 5);
102 config = unsorted_string_list_lookup(&config_name_for_path, value);
103 if (config)
104 free(config->util);
105 else
106 config = string_list_append(&config_name_for_path, xstrdup(value));
107 config->util = strbuf_detach(&submodname, NULL);
108 strbuf_release(&submodname);
109 } else if ((len > 23) && !strcmp(var + len - 23, ".fetchrecursesubmodules")) {
110 strbuf_add(&submodname, var, len - 23);
111 config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, submodname.buf);
112 if (!config)
113 config = string_list_append(&config_fetch_recurse_submodules_for_name,
114 strbuf_detach(&submodname, NULL));
115 config->util = git_config_bool(var, value) ? (void *)1 : NULL;
116 strbuf_release(&submodname);
117 } else if ((len > 7) && !strcmp(var + len - 7, ".ignore")) {
118 if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
119 strcmp(value, "all") && strcmp(value, "none")) {
120 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
121 return 0;
124 strbuf_add(&submodname, var, len - 7);
125 config = unsorted_string_list_lookup(&config_ignore_for_name, submodname.buf);
126 if (config)
127 free(config->util);
128 else
129 config = string_list_append(&config_ignore_for_name,
130 strbuf_detach(&submodname, NULL));
131 strbuf_release(&submodname);
132 config->util = xstrdup(value);
133 return 0;
135 return 0;
138 void handle_ignore_submodules_arg(struct diff_options *diffopt,
139 const char *arg)
141 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
142 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
143 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
145 if (!strcmp(arg, "all"))
146 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
147 else if (!strcmp(arg, "untracked"))
148 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
149 else if (!strcmp(arg, "dirty"))
150 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
151 else if (strcmp(arg, "none"))
152 die("bad --ignore-submodules argument: %s", arg);
155 void show_submodule_summary(FILE *f, const char *path,
156 unsigned char one[20], unsigned char two[20],
157 unsigned dirty_submodule,
158 const char *del, const char *add, const char *reset)
160 struct rev_info rev;
161 struct commit *commit, *left = left, *right = right;
162 struct commit_list *merge_bases, *list;
163 const char *message = NULL;
164 struct strbuf sb = STRBUF_INIT;
165 static const char *format = " %m %s";
166 int fast_forward = 0, fast_backward = 0;
168 if (is_null_sha1(two))
169 message = "(submodule deleted)";
170 else if (add_submodule_odb(path))
171 message = "(not checked out)";
172 else if (is_null_sha1(one))
173 message = "(new submodule)";
174 else if (!(left = lookup_commit_reference(one)) ||
175 !(right = lookup_commit_reference(two)))
176 message = "(commits not present)";
178 if (!message) {
179 init_revisions(&rev, NULL);
180 setup_revisions(0, NULL, &rev, NULL);
181 rev.left_right = 1;
182 rev.first_parent_only = 1;
183 left->object.flags |= SYMMETRIC_LEFT;
184 add_pending_object(&rev, &left->object, path);
185 add_pending_object(&rev, &right->object, path);
186 merge_bases = get_merge_bases(left, right, 1);
187 if (merge_bases) {
188 if (merge_bases->item == left)
189 fast_forward = 1;
190 else if (merge_bases->item == right)
191 fast_backward = 1;
193 for (list = merge_bases; list; list = list->next) {
194 list->item->object.flags |= UNINTERESTING;
195 add_pending_object(&rev, &list->item->object,
196 sha1_to_hex(list->item->object.sha1));
198 if (prepare_revision_walk(&rev))
199 message = "(revision walker failed)";
202 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
203 fprintf(f, "Submodule %s contains untracked content\n", path);
204 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
205 fprintf(f, "Submodule %s contains modified content\n", path);
207 if (!hashcmp(one, two)) {
208 strbuf_release(&sb);
209 return;
212 strbuf_addf(&sb, "Submodule %s %s..", path,
213 find_unique_abbrev(one, DEFAULT_ABBREV));
214 if (!fast_backward && !fast_forward)
215 strbuf_addch(&sb, '.');
216 strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
217 if (message)
218 strbuf_addf(&sb, " %s\n", message);
219 else
220 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
221 fwrite(sb.buf, sb.len, 1, f);
223 if (!message) {
224 while ((commit = get_revision(&rev))) {
225 struct pretty_print_context ctx = {0};
226 ctx.date_mode = rev.date_mode;
227 strbuf_setlen(&sb, 0);
228 if (commit->object.flags & SYMMETRIC_LEFT) {
229 if (del)
230 strbuf_addstr(&sb, del);
232 else if (add)
233 strbuf_addstr(&sb, add);
234 format_commit_message(commit, format, &sb, &ctx);
235 if (reset)
236 strbuf_addstr(&sb, reset);
237 strbuf_addch(&sb, '\n');
238 fprintf(f, "%s", sb.buf);
240 clear_commit_marks(left, ~0);
241 clear_commit_marks(right, ~0);
243 strbuf_release(&sb);
246 void set_config_fetch_recurse_submodules(int value)
248 config_fetch_recurse_submodules = value;
251 int fetch_populated_submodules(int num_options, const char **options,
252 const char *prefix, int ignore_config,
253 int quiet)
255 int i, result = 0, argc = 0;
256 struct child_process cp;
257 const char **argv;
258 struct string_list_item *name_for_path;
259 const char *work_tree = get_git_work_tree();
260 if (!work_tree)
261 return 0;
263 if (!the_index.initialized)
264 if (read_cache() < 0)
265 die("index file corrupt");
267 /* 4: "fetch" (options) "--submodule-prefix" prefix NULL */
268 argv = xcalloc(num_options + 4, sizeof(const char *));
269 argv[argc++] = "fetch";
270 for (i = 0; i < num_options; i++)
271 argv[argc++] = options[i];
272 argv[argc++] = "--submodule-prefix";
274 memset(&cp, 0, sizeof(cp));
275 cp.argv = argv;
276 cp.env = local_repo_env;
277 cp.git_cmd = 1;
278 cp.no_stdin = 1;
280 for (i = 0; i < active_nr; i++) {
281 struct strbuf submodule_path = STRBUF_INIT;
282 struct strbuf submodule_git_dir = STRBUF_INIT;
283 struct strbuf submodule_prefix = STRBUF_INIT;
284 struct cache_entry *ce = active_cache[i];
285 const char *git_dir, *name;
287 if (!S_ISGITLINK(ce->ce_mode))
288 continue;
290 name = ce->name;
291 name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
292 if (name_for_path)
293 name = name_for_path->util;
295 if (!ignore_config) {
296 struct string_list_item *fetch_recurse_submodules_option;
297 fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
298 if (fetch_recurse_submodules_option) {
299 if (!fetch_recurse_submodules_option->util)
300 continue;
301 } else {
302 if (!config_fetch_recurse_submodules)
303 continue;
307 strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
308 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
309 strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
310 git_dir = read_gitfile_gently(submodule_git_dir.buf);
311 if (!git_dir)
312 git_dir = submodule_git_dir.buf;
313 if (is_directory(git_dir)) {
314 if (!quiet)
315 printf("Fetching submodule %s%s\n", prefix, ce->name);
316 cp.dir = submodule_path.buf;
317 argv[argc] = submodule_prefix.buf;
318 if (run_command(&cp))
319 result = 1;
321 strbuf_release(&submodule_path);
322 strbuf_release(&submodule_git_dir);
323 strbuf_release(&submodule_prefix);
325 free(argv);
326 return result;
329 unsigned is_submodule_modified(const char *path, int ignore_untracked)
331 ssize_t len;
332 struct child_process cp;
333 const char *argv[] = {
334 "status",
335 "--porcelain",
336 NULL,
337 NULL,
339 struct strbuf buf = STRBUF_INIT;
340 unsigned dirty_submodule = 0;
341 const char *line, *next_line;
342 const char *git_dir;
344 strbuf_addf(&buf, "%s/.git", path);
345 git_dir = read_gitfile_gently(buf.buf);
346 if (!git_dir)
347 git_dir = buf.buf;
348 if (!is_directory(git_dir)) {
349 strbuf_release(&buf);
350 /* The submodule is not checked out, so it is not modified */
351 return 0;
354 strbuf_reset(&buf);
356 if (ignore_untracked)
357 argv[2] = "-uno";
359 memset(&cp, 0, sizeof(cp));
360 cp.argv = argv;
361 cp.env = local_repo_env;
362 cp.git_cmd = 1;
363 cp.no_stdin = 1;
364 cp.out = -1;
365 cp.dir = path;
366 if (start_command(&cp))
367 die("Could not run git status --porcelain");
369 len = strbuf_read(&buf, cp.out, 1024);
370 line = buf.buf;
371 while (len > 2) {
372 if ((line[0] == '?') && (line[1] == '?')) {
373 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
374 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
375 break;
376 } else {
377 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
378 if (ignore_untracked ||
379 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
380 break;
382 next_line = strchr(line, '\n');
383 if (!next_line)
384 break;
385 next_line++;
386 len -= (next_line - line);
387 line = next_line;
389 close(cp.out);
391 if (finish_command(&cp))
392 die("git status --porcelain failed");
394 strbuf_release(&buf);
395 return dirty_submodule;
398 static int find_first_merges(struct object_array *result, const char *path,
399 struct commit *a, struct commit *b)
401 int i, j;
402 struct object_array merges;
403 struct commit *commit;
404 int contains_another;
406 char merged_revision[42];
407 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
408 "--all", merged_revision, NULL };
409 struct rev_info revs;
410 struct setup_revision_opt rev_opts;
412 memset(&merges, 0, sizeof(merges));
413 memset(result, 0, sizeof(struct object_array));
414 memset(&rev_opts, 0, sizeof(rev_opts));
416 /* get all revisions that merge commit a */
417 snprintf(merged_revision, sizeof(merged_revision), "^%s",
418 sha1_to_hex(a->object.sha1));
419 init_revisions(&revs, NULL);
420 rev_opts.submodule = path;
421 setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
423 /* save all revisions from the above list that contain b */
424 if (prepare_revision_walk(&revs))
425 die("revision walk setup failed");
426 while ((commit = get_revision(&revs)) != NULL) {
427 struct object *o = &(commit->object);
428 if (in_merge_bases(b, &commit, 1))
429 add_object_array(o, NULL, &merges);
432 /* Now we've got all merges that contain a and b. Prune all
433 * merges that contain another found merge and save them in
434 * result.
436 for (i = 0; i < merges.nr; i++) {
437 struct commit *m1 = (struct commit *) merges.objects[i].item;
439 contains_another = 0;
440 for (j = 0; j < merges.nr; j++) {
441 struct commit *m2 = (struct commit *) merges.objects[j].item;
442 if (i != j && in_merge_bases(m2, &m1, 1)) {
443 contains_another = 1;
444 break;
448 if (!contains_another)
449 add_object_array(merges.objects[i].item,
450 merges.objects[i].name, result);
453 free(merges.objects);
454 return result->nr;
457 static void print_commit(struct commit *commit)
459 struct strbuf sb = STRBUF_INIT;
460 struct pretty_print_context ctx = {0};
461 ctx.date_mode = DATE_NORMAL;
462 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
463 fprintf(stderr, "%s\n", sb.buf);
464 strbuf_release(&sb);
467 #define MERGE_WARNING(path, msg) \
468 warning("Failed to merge submodule %s (%s)", path, msg);
470 int merge_submodule(unsigned char result[20], const char *path,
471 const unsigned char base[20], const unsigned char a[20],
472 const unsigned char b[20])
474 struct commit *commit_base, *commit_a, *commit_b;
475 int parent_count;
476 struct object_array merges;
478 int i;
480 /* store a in result in case we fail */
481 hashcpy(result, a);
483 /* we can not handle deletion conflicts */
484 if (is_null_sha1(base))
485 return 0;
486 if (is_null_sha1(a))
487 return 0;
488 if (is_null_sha1(b))
489 return 0;
491 if (add_submodule_odb(path)) {
492 MERGE_WARNING(path, "not checked out");
493 return 0;
496 if (!(commit_base = lookup_commit_reference(base)) ||
497 !(commit_a = lookup_commit_reference(a)) ||
498 !(commit_b = lookup_commit_reference(b))) {
499 MERGE_WARNING(path, "commits not present");
500 return 0;
503 /* check whether both changes are forward */
504 if (!in_merge_bases(commit_base, &commit_a, 1) ||
505 !in_merge_bases(commit_base, &commit_b, 1)) {
506 MERGE_WARNING(path, "commits don't follow merge-base");
507 return 0;
510 /* Case #1: a is contained in b or vice versa */
511 if (in_merge_bases(commit_a, &commit_b, 1)) {
512 hashcpy(result, b);
513 return 1;
515 if (in_merge_bases(commit_b, &commit_a, 1)) {
516 hashcpy(result, a);
517 return 1;
521 * Case #2: There are one or more merges that contain a and b in
522 * the submodule. If there is only one, then present it as a
523 * suggestion to the user, but leave it marked unmerged so the
524 * user needs to confirm the resolution.
527 /* find commit which merges them */
528 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
529 switch (parent_count) {
530 case 0:
531 MERGE_WARNING(path, "merge following commits not found");
532 break;
534 case 1:
535 MERGE_WARNING(path, "not fast-forward");
536 fprintf(stderr, "Found a possible merge resolution "
537 "for the submodule:\n");
538 print_commit((struct commit *) merges.objects[0].item);
539 fprintf(stderr,
540 "If this is correct simply add it to the index "
541 "for example\n"
542 "by using:\n\n"
543 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
544 "which will accept this suggestion.\n",
545 sha1_to_hex(merges.objects[0].item->sha1), path);
546 break;
548 default:
549 MERGE_WARNING(path, "multiple merges found");
550 for (i = 0; i < merges.nr; i++)
551 print_commit((struct commit *) merges.objects[i].item);
554 free(merges.objects);
555 return 0;