Code

Submodules: Add the "fetchRecurseSubmodules" config option
[git.git] / submodule.c
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)
18 {
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);
29         }
30         strbuf_addstr(&objects_directory, "/objects/");
31         if (!is_directory(objects_directory.buf)) {
32                 ret = -1;
33                 goto done;
34         }
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;
54 }
56 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
57                                              const char *path)
58 {
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);
65         }
66 }
68 int submodule_config(const char *var, const char *value, void *cb)
69 {
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;
75         }
76         return 0;
77 }
79 void gitmodules_config(void)
80 {
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);
88         }
89 }
91 int parse_submodule_config_option(const char *var, const char *value)
92 {
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;
122                 }
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;
134         }
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;
192                 }
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));
197                 }
198                 if (prepare_revision_walk(&rev))
199                         message = "(revision walker failed)";
200         }
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;
210         }
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);
231                         }
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);
239                 }
240                 clear_commit_marks(left, ~0);
241                 clear_commit_marks(right, ~0);
242         }
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         argv = xcalloc(num_options + 5, sizeof(const char *));
268         argv[argc++] = "fetch";
269         for (i = 0; i < num_options; i++)
270                 argv[argc++] = options[i];
271         argv[argc++] = "--submodule-prefix";
273         memset(&cp, 0, sizeof(cp));
274         cp.argv = argv;
275         cp.env = local_repo_env;
276         cp.git_cmd = 1;
277         cp.no_stdin = 1;
279         for (i = 0; i < active_nr; i++) {
280                 struct strbuf submodule_path = STRBUF_INIT;
281                 struct strbuf submodule_git_dir = STRBUF_INIT;
282                 struct strbuf submodule_prefix = STRBUF_INIT;
283                 struct cache_entry *ce = active_cache[i];
284                 const char *git_dir, *name;
286                 if (!S_ISGITLINK(ce->ce_mode))
287                         continue;
289                 name = ce->name;
290                 name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
291                 if (name_for_path)
292                         name = name_for_path->util;
294                 if (!ignore_config) {
295                         struct string_list_item *fetch_recurse_submodules_option;
296                         fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
297                         if (fetch_recurse_submodules_option) {
298                                 if (!fetch_recurse_submodules_option->util)
299                                         continue;
300                         } else {
301                                 if (!config_fetch_recurse_submodules)
302                                         continue;
303                         }
304                 }
306                 strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
307                 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
308                 strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
309                 git_dir = read_gitfile_gently(submodule_git_dir.buf);
310                 if (!git_dir)
311                         git_dir = submodule_git_dir.buf;
312                 if (is_directory(git_dir)) {
313                         if (!quiet)
314                                 printf("Fetching submodule %s%s\n", prefix, ce->name);
315                         cp.dir = submodule_path.buf;
316                         argv[argc] = submodule_prefix.buf;
317                         if (run_command(&cp))
318                                 result = 1;
319                 }
320                 strbuf_release(&submodule_path);
321                 strbuf_release(&submodule_git_dir);
322                 strbuf_release(&submodule_prefix);
323         }
324         free(argv);
325         return result;
328 unsigned is_submodule_modified(const char *path, int ignore_untracked)
330         ssize_t len;
331         struct child_process cp;
332         const char *argv[] = {
333                 "status",
334                 "--porcelain",
335                 NULL,
336                 NULL,
337         };
338         struct strbuf buf = STRBUF_INIT;
339         unsigned dirty_submodule = 0;
340         const char *line, *next_line;
341         const char *git_dir;
343         strbuf_addf(&buf, "%s/.git", path);
344         git_dir = read_gitfile_gently(buf.buf);
345         if (!git_dir)
346                 git_dir = buf.buf;
347         if (!is_directory(git_dir)) {
348                 strbuf_release(&buf);
349                 /* The submodule is not checked out, so it is not modified */
350                 return 0;
352         }
353         strbuf_reset(&buf);
355         if (ignore_untracked)
356                 argv[2] = "-uno";
358         memset(&cp, 0, sizeof(cp));
359         cp.argv = argv;
360         cp.env = local_repo_env;
361         cp.git_cmd = 1;
362         cp.no_stdin = 1;
363         cp.out = -1;
364         cp.dir = path;
365         if (start_command(&cp))
366                 die("Could not run git status --porcelain");
368         len = strbuf_read(&buf, cp.out, 1024);
369         line = buf.buf;
370         while (len > 2) {
371                 if ((line[0] == '?') && (line[1] == '?')) {
372                         dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
373                         if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
374                                 break;
375                 } else {
376                         dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
377                         if (ignore_untracked ||
378                             (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
379                                 break;
380                 }
381                 next_line = strchr(line, '\n');
382                 if (!next_line)
383                         break;
384                 next_line++;
385                 len -= (next_line - line);
386                 line = next_line;
387         }
388         close(cp.out);
390         if (finish_command(&cp))
391                 die("git status --porcelain failed");
393         strbuf_release(&buf);
394         return dirty_submodule;
397 static int find_first_merges(struct object_array *result, const char *path,
398                 struct commit *a, struct commit *b)
400         int i, j;
401         struct object_array merges;
402         struct commit *commit;
403         int contains_another;
405         char merged_revision[42];
406         const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
407                                    "--all", merged_revision, NULL };
408         struct rev_info revs;
409         struct setup_revision_opt rev_opts;
411         memset(&merges, 0, sizeof(merges));
412         memset(result, 0, sizeof(struct object_array));
413         memset(&rev_opts, 0, sizeof(rev_opts));
415         /* get all revisions that merge commit a */
416         snprintf(merged_revision, sizeof(merged_revision), "^%s",
417                         sha1_to_hex(a->object.sha1));
418         init_revisions(&revs, NULL);
419         rev_opts.submodule = path;
420         setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
422         /* save all revisions from the above list that contain b */
423         if (prepare_revision_walk(&revs))
424                 die("revision walk setup failed");
425         while ((commit = get_revision(&revs)) != NULL) {
426                 struct object *o = &(commit->object);
427                 if (in_merge_bases(b, &commit, 1))
428                         add_object_array(o, NULL, &merges);
429         }
431         /* Now we've got all merges that contain a and b. Prune all
432          * merges that contain another found merge and save them in
433          * result.
434          */
435         for (i = 0; i < merges.nr; i++) {
436                 struct commit *m1 = (struct commit *) merges.objects[i].item;
438                 contains_another = 0;
439                 for (j = 0; j < merges.nr; j++) {
440                         struct commit *m2 = (struct commit *) merges.objects[j].item;
441                         if (i != j && in_merge_bases(m2, &m1, 1)) {
442                                 contains_another = 1;
443                                 break;
444                         }
445                 }
447                 if (!contains_another)
448                         add_object_array(merges.objects[i].item,
449                                          merges.objects[i].name, result);
450         }
452         free(merges.objects);
453         return result->nr;
456 static void print_commit(struct commit *commit)
458         struct strbuf sb = STRBUF_INIT;
459         struct pretty_print_context ctx = {0};
460         ctx.date_mode = DATE_NORMAL;
461         format_commit_message(commit, " %h: %m %s", &sb, &ctx);
462         fprintf(stderr, "%s\n", sb.buf);
463         strbuf_release(&sb);
466 #define MERGE_WARNING(path, msg) \
467         warning("Failed to merge submodule %s (%s)", path, msg);
469 int merge_submodule(unsigned char result[20], const char *path,
470                     const unsigned char base[20], const unsigned char a[20],
471                     const unsigned char b[20])
473         struct commit *commit_base, *commit_a, *commit_b;
474         int parent_count;
475         struct object_array merges;
477         int i;
479         /* store a in result in case we fail */
480         hashcpy(result, a);
482         /* we can not handle deletion conflicts */
483         if (is_null_sha1(base))
484                 return 0;
485         if (is_null_sha1(a))
486                 return 0;
487         if (is_null_sha1(b))
488                 return 0;
490         if (add_submodule_odb(path)) {
491                 MERGE_WARNING(path, "not checked out");
492                 return 0;
493         }
495         if (!(commit_base = lookup_commit_reference(base)) ||
496             !(commit_a = lookup_commit_reference(a)) ||
497             !(commit_b = lookup_commit_reference(b))) {
498                 MERGE_WARNING(path, "commits not present");
499                 return 0;
500         }
502         /* check whether both changes are forward */
503         if (!in_merge_bases(commit_base, &commit_a, 1) ||
504             !in_merge_bases(commit_base, &commit_b, 1)) {
505                 MERGE_WARNING(path, "commits don't follow merge-base");
506                 return 0;
507         }
509         /* Case #1: a is contained in b or vice versa */
510         if (in_merge_bases(commit_a, &commit_b, 1)) {
511                 hashcpy(result, b);
512                 return 1;
513         }
514         if (in_merge_bases(commit_b, &commit_a, 1)) {
515                 hashcpy(result, a);
516                 return 1;
517         }
519         /*
520          * Case #2: There are one or more merges that contain a and b in
521          * the submodule. If there is only one, then present it as a
522          * suggestion to the user, but leave it marked unmerged so the
523          * user needs to confirm the resolution.
524          */
526         /* find commit which merges them */
527         parent_count = find_first_merges(&merges, path, commit_a, commit_b);
528         switch (parent_count) {
529         case 0:
530                 MERGE_WARNING(path, "merge following commits not found");
531                 break;
533         case 1:
534                 MERGE_WARNING(path, "not fast-forward");
535                 fprintf(stderr, "Found a possible merge resolution "
536                                 "for the submodule:\n");
537                 print_commit((struct commit *) merges.objects[0].item);
538                 fprintf(stderr,
539                         "If this is correct simply add it to the index "
540                         "for example\n"
541                         "by using:\n\n"
542                         "  git update-index --cacheinfo 160000 %s \"%s\"\n\n"
543                         "which will accept this suggestion.\n",
544                         sha1_to_hex(merges.objects[0].item->sha1), path);
545                 break;
547         default:
548                 MERGE_WARNING(path, "multiple merges found");
549                 for (i = 0; i < merges.nr; i++)
550                         print_commit((struct commit *) merges.objects[i].item);
551         }
553         free(merges.objects);
554         return 0;