Add the --submodule option to the diff option family
[git/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
8 int add_submodule_odb(const char *path)
9 {
10 struct strbuf objects_directory = STRBUF_INIT;
11 struct alternate_object_database *alt_odb;
12
13 strbuf_addf(&objects_directory, "%s/.git/objects/", path);
14 if (!is_directory(objects_directory.buf))
15 return -1;
16
17 /* avoid adding it twice */
18 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
19 if (alt_odb->name - alt_odb->base == objects_directory.len &&
20 !strncmp(alt_odb->base, objects_directory.buf,
21 objects_directory.len))
22 return 0;
23
24 alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
25 alt_odb->next = alt_odb_list;
26 strcpy(alt_odb->base, objects_directory.buf);
27 alt_odb->name = alt_odb->base + objects_directory.len;
28 alt_odb->name[2] = '/';
29 alt_odb->name[40] = '\0';
30 alt_odb->name[41] = '\0';
31 alt_odb_list = alt_odb;
32 prepare_alt_odb();
33 return 0;
34 }
35
36 void show_submodule_summary(FILE *f, const char *path,
37 unsigned char one[20], unsigned char two[20],
38 const char *del, const char *add, const char *reset)
39 {
40 struct rev_info rev;
41 struct commit *commit, *left = left, *right;
42 struct commit_list *merge_bases, *list;
43 const char *message = NULL;
44 struct strbuf sb = STRBUF_INIT;
45 static const char *format = " %m %s";
46 int fast_forward = 0, fast_backward = 0;
47
48 if (is_null_sha1(two))
49 message = "(submodule deleted)";
50 else if (add_submodule_odb(path))
51 message = "(not checked out)";
52 else if (is_null_sha1(one))
53 message = "(new submodule)";
54 else if (!(left = lookup_commit_reference(one)) ||
55 !(right = lookup_commit_reference(two)))
56 message = "(commits not present)";
57
58 if (!message) {
59 init_revisions(&rev, NULL);
60 setup_revisions(0, NULL, &rev, NULL);
61 rev.left_right = 1;
62 rev.first_parent_only = 1;
63 left->object.flags |= SYMMETRIC_LEFT;
64 add_pending_object(&rev, &left->object, path);
65 add_pending_object(&rev, &right->object, path);
66 merge_bases = get_merge_bases(left, right, 1);
67 if (merge_bases) {
68 if (merge_bases->item == left)
69 fast_forward = 1;
70 else if (merge_bases->item == right)
71 fast_backward = 1;
72 }
73 for (list = merge_bases; list; list = list->next) {
74 list->item->object.flags |= UNINTERESTING;
75 add_pending_object(&rev, &list->item->object,
76 sha1_to_hex(list->item->object.sha1));
77 }
78 if (prepare_revision_walk(&rev))
79 message = "(revision walker failed)";
80 }
81
82 strbuf_addf(&sb, "Submodule %s %s..", path,
83 find_unique_abbrev(one, DEFAULT_ABBREV));
84 if (!fast_backward && !fast_forward)
85 strbuf_addch(&sb, '.');
86 strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
87 if (message)
88 strbuf_addf(&sb, " %s\n", message);
89 else
90 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
91 fwrite(sb.buf, sb.len, 1, f);
92
93 if (!message) {
94 while ((commit = get_revision(&rev))) {
95 strbuf_setlen(&sb, 0);
96 if (commit->object.flags & SYMMETRIC_LEFT) {
97 if (del)
98 strbuf_addstr(&sb, del);
99 }
100 else if (add)
101 strbuf_addstr(&sb, add);
102 format_commit_message(commit, format, &sb,
103 rev.date_mode);
104 if (reset)
105 strbuf_addstr(&sb, reset);
106 strbuf_addch(&sb, '\n');
107 fprintf(f, "%s", sb.buf);
108 }
109 clear_commit_marks(left, ~0);
110 clear_commit_marks(right, ~0);
111 }
112 strbuf_release(&sb);
113 }