Merge branch 'rr/fmt-merge-msg'
[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"
11
12 struct string_list config_name_for_path;
13 struct string_list config_ignore_for_name;
14
15 static int add_submodule_odb(const char *path)
16 {
17         struct strbuf objects_directory = STRBUF_INIT;
18         struct alternate_object_database *alt_odb;
19         int ret = 0;
20         const char *git_dir;
21
22         strbuf_addf(&objects_directory, "%s/.git", path);
23         git_dir = read_gitfile_gently(objects_directory.buf);
24         if (git_dir) {
25                 strbuf_reset(&objects_directory);
26                 strbuf_addstr(&objects_directory, git_dir);
27         }
28         strbuf_addstr(&objects_directory, "/objects/");
29         if (!is_directory(objects_directory.buf)) {
30                 ret = -1;
31                 goto done;
32         }
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))
38                         goto done;
39
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;
48         prepare_alt_odb();
49 done:
50         strbuf_release(&objects_directory);
51         return ret;
52 }
53
54 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
55                                              const char *path)
56 {
57         struct string_list_item *path_option, *ignore_option;
58         path_option = unsorted_string_list_lookup(&config_name_for_path, path);
59         if (path_option) {
60                 ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
61                 if (ignore_option)
62                         handle_ignore_submodules_arg(diffopt, ignore_option->util);
63         }
64 }
65
66 static int submodule_config(const char *var, const char *value, void *cb)
67 {
68         if (!prefixcmp(var, "submodule."))
69                 return parse_submodule_config_option(var, value);
70         return 0;
71 }
72
73 void gitmodules_config(void)
74 {
75         const char *work_tree = get_git_work_tree();
76         if (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);
82         }
83 }
84
85 int parse_submodule_config_option(const char *var, const char *value)
86 {
87         int len;
88         struct string_list_item *config;
89         struct strbuf submodname = STRBUF_INIT;
90
91         var += 10;              /* Skip "submodule." */
92
93         len = strlen(var);
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);
97                 if (config)
98                         free(config->util);
99                 else
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);
107                         return 0;
108                 }
109
110                 strbuf_add(&submodname, var, len - 7);
111                 config = unsorted_string_list_lookup(&config_ignore_for_name, submodname.buf);
112                 if (config)
113                         free(config->util);
114                 else
115                         config = string_list_append(&config_ignore_for_name,
116                                                     strbuf_detach(&submodname, NULL));
117                 strbuf_release(&submodname);
118                 config->util = xstrdup(value);
119                 return 0;
120         }
121         return 0;
122 }
123
124 void handle_ignore_submodules_arg(struct diff_options *diffopt,
125                                   const char *arg)
126 {
127         DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
128         DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
129         DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
130
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);
139 }
140
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)
145 {
146         struct rev_info rev;
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;
153
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)";
163
164         if (!message) {
165                 init_revisions(&rev, NULL);
166                 setup_revisions(0, NULL, &rev, NULL);
167                 rev.left_right = 1;
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);
173                 if (merge_bases) {
174                         if (merge_bases->item == left)
175                                 fast_forward = 1;
176                         else if (merge_bases->item == right)
177                                 fast_backward = 1;
178                 }
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));
183                 }
184                 if (prepare_revision_walk(&rev))
185                         message = "(revision walker failed)";
186         }
187
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);
192
193         if (!hashcmp(one, two)) {
194                 strbuf_release(&sb);
195                 return;
196         }
197
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));
203         if (message)
204                 strbuf_addf(&sb, " %s\n", message);
205         else
206                 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
207         fwrite(sb.buf, sb.len, 1, f);
208
209         if (!message) {
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) {
215                                 if (del)
216                                         strbuf_addstr(&sb, del);
217                         }
218                         else if (add)
219                                 strbuf_addstr(&sb, add);
220                         format_commit_message(commit, format, &sb, &ctx);
221                         if (reset)
222                                 strbuf_addstr(&sb, reset);
223                         strbuf_addch(&sb, '\n');
224                         fprintf(f, "%s", sb.buf);
225                 }
226                 clear_commit_marks(left, ~0);
227                 clear_commit_marks(right, ~0);
228         }
229         strbuf_release(&sb);
230 }
231
232 unsigned is_submodule_modified(const char *path, int ignore_untracked)
233 {
234         ssize_t len;
235         struct child_process cp;
236         const char *argv[] = {
237                 "status",
238                 "--porcelain",
239                 NULL,
240                 NULL,
241         };
242         struct strbuf buf = STRBUF_INIT;
243         unsigned dirty_submodule = 0;
244         const char *line, *next_line;
245         const char *git_dir;
246
247         strbuf_addf(&buf, "%s/.git", path);
248         git_dir = read_gitfile_gently(buf.buf);
249         if (!git_dir)
250                 git_dir = buf.buf;
251         if (!is_directory(git_dir)) {
252                 strbuf_release(&buf);
253                 /* The submodule is not checked out, so it is not modified */
254                 return 0;
255
256         }
257         strbuf_reset(&buf);
258
259         if (ignore_untracked)
260                 argv[2] = "-uno";
261
262         memset(&cp, 0, sizeof(cp));
263         cp.argv = argv;
264         cp.env = local_repo_env;
265         cp.git_cmd = 1;
266         cp.no_stdin = 1;
267         cp.out = -1;
268         cp.dir = path;
269         if (start_command(&cp))
270                 die("Could not run git status --porcelain");
271
272         len = strbuf_read(&buf, cp.out, 1024);
273         line = buf.buf;
274         while (len > 2) {
275                 if ((line[0] == '?') && (line[1] == '?')) {
276                         dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
277                         if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
278                                 break;
279                 } else {
280                         dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
281                         if (ignore_untracked ||
282                             (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
283                                 break;
284                 }
285                 next_line = strchr(line, '\n');
286                 if (!next_line)
287                         break;
288                 next_line++;
289                 len -= (next_line - line);
290                 line = next_line;
291         }
292         close(cp.out);
293
294         if (finish_command(&cp))
295                 die("git status --porcelain failed");
296
297         strbuf_release(&buf);
298         return dirty_submodule;
299 }
300
301 static int find_first_merges(struct object_array *result, const char *path,
302                 struct commit *a, struct commit *b)
303 {
304         int i, j;
305         struct object_array merges;
306         struct commit *commit;
307         int contains_another;
308
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;
314
315         memset(&merges, 0, sizeof(merges));
316         memset(result, 0, sizeof(struct object_array));
317         memset(&rev_opts, 0, sizeof(rev_opts));
318
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);
325
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);
333         }
334
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
337          * result.
338          */
339         for (i = 0; i < merges.nr; i++) {
340                 struct commit *m1 = (struct commit *) merges.objects[i].item;
341
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;
347                                 break;
348                         }
349                 }
350
351                 if (!contains_another)
352                         add_object_array(merges.objects[i].item,
353                                          merges.objects[i].name, result);
354         }
355
356         free(merges.objects);
357         return result->nr;
358 }
359
360 static void print_commit(struct commit *commit)
361 {
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);
367         strbuf_release(&sb);
368 }
369
370 #define MERGE_WARNING(path, msg) \
371         warning("Failed to merge submodule %s (%s)", path, msg);
372
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])
376 {
377         struct commit *commit_base, *commit_a, *commit_b;
378         int parent_count;
379         struct object_array merges;
380
381         int i;
382
383         /* store a in result in case we fail */
384         hashcpy(result, a);
385
386         /* we can not handle deletion conflicts */
387         if (is_null_sha1(base))
388                 return 0;
389         if (is_null_sha1(a))
390                 return 0;
391         if (is_null_sha1(b))
392                 return 0;
393
394         if (add_submodule_odb(path)) {
395                 MERGE_WARNING(path, "not checked out");
396                 return 0;
397         }
398
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");
403                 return 0;
404         }
405
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");
410                 return 0;
411         }
412
413         /* Case #1: a is contained in b or vice versa */
414         if (in_merge_bases(commit_a, &commit_b, 1)) {
415                 hashcpy(result, b);
416                 return 1;
417         }
418         if (in_merge_bases(commit_b, &commit_a, 1)) {
419                 hashcpy(result, a);
420                 return 1;
421         }
422
423         /*
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.
428          */
429
430         /* find commit which merges them */
431         parent_count = find_first_merges(&merges, path, commit_a, commit_b);
432         switch (parent_count) {
433         case 0:
434                 MERGE_WARNING(path, "merge following commits not found");
435                 break;
436
437         case 1:
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);
442                 fprintf(stderr,
443                         "If this is correct simply add it to the index "
444                         "for example\n"
445                         "by using:\n\n"
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);
449                 break;
450
451         default:
452                 MERGE_WARNING(path, "multiple merges found");
453                 for (i = 0; i < merges.nr; i++)
454                         print_commit((struct commit *) merges.objects[i].item);
455         }
456
457         free(merges.objects);
458         return 0;
459 }