7 #include "run-command.h"
9 static int add_submodule_odb(const char *path)
11 struct strbuf objects_directory = STRBUF_INIT;
12 struct alternate_object_database *alt_odb;
15 strbuf_addf(&objects_directory, "%s/.git/objects/", path);
16 if (!is_directory(objects_directory.buf)) {
20 /* avoid adding it twice */
21 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
22 if (alt_odb->name - alt_odb->base == objects_directory.len &&
23 !strncmp(alt_odb->base, objects_directory.buf,
24 objects_directory.len))
27 alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
28 alt_odb->next = alt_odb_list;
29 strcpy(alt_odb->base, objects_directory.buf);
30 alt_odb->name = alt_odb->base + objects_directory.len;
31 alt_odb->name[2] = '/';
32 alt_odb->name[40] = '\0';
33 alt_odb->name[41] = '\0';
34 alt_odb_list = alt_odb;
37 strbuf_release(&objects_directory);
41 void show_submodule_summary(FILE *f, const char *path,
42 unsigned char one[20], unsigned char two[20],
43 unsigned dirty_submodule,
44 const char *del, const char *add, const char *reset)
47 struct commit *commit, *left = left, *right = right;
48 struct commit_list *merge_bases, *list;
49 const char *message = NULL;
50 struct strbuf sb = STRBUF_INIT;
51 static const char *format = " %m %s";
52 int fast_forward = 0, fast_backward = 0;
54 if (is_null_sha1(two))
55 message = "(submodule deleted)";
56 else if (add_submodule_odb(path))
57 message = "(not checked out)";
58 else if (is_null_sha1(one))
59 message = "(new submodule)";
60 else if (!(left = lookup_commit_reference(one)) ||
61 !(right = lookup_commit_reference(two)))
62 message = "(commits not present)";
65 init_revisions(&rev, NULL);
66 setup_revisions(0, NULL, &rev, NULL);
68 rev.first_parent_only = 1;
69 left->object.flags |= SYMMETRIC_LEFT;
70 add_pending_object(&rev, &left->object, path);
71 add_pending_object(&rev, &right->object, path);
72 merge_bases = get_merge_bases(left, right, 1);
74 if (merge_bases->item == left)
76 else if (merge_bases->item == right)
79 for (list = merge_bases; list; list = list->next) {
80 list->item->object.flags |= UNINTERESTING;
81 add_pending_object(&rev, &list->item->object,
82 sha1_to_hex(list->item->object.sha1));
84 if (prepare_revision_walk(&rev))
85 message = "(revision walker failed)";
88 strbuf_addf(&sb, "Submodule %s %s..", path,
89 find_unique_abbrev(one, DEFAULT_ABBREV));
90 if (!fast_backward && !fast_forward)
91 strbuf_addch(&sb, '.');
92 strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
94 strbuf_add(&sb, "-dirty", 6);
96 strbuf_addf(&sb, " %s\n", message);
98 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
99 fwrite(sb.buf, sb.len, 1, f);
102 while ((commit = get_revision(&rev))) {
103 struct pretty_print_context ctx = {0};
104 ctx.date_mode = rev.date_mode;
105 strbuf_setlen(&sb, 0);
106 if (commit->object.flags & SYMMETRIC_LEFT) {
108 strbuf_addstr(&sb, del);
111 strbuf_addstr(&sb, add);
112 format_commit_message(commit, format, &sb, &ctx);
114 strbuf_addstr(&sb, reset);
115 strbuf_addch(&sb, '\n');
116 fprintf(f, "%s", sb.buf);
118 clear_commit_marks(left, ~0);
119 clear_commit_marks(right, ~0);
124 int is_submodule_modified(const char *path)
127 struct child_process cp;
128 const char *argv[] = {
134 struct strbuf buf = STRBUF_INIT;
136 strbuf_addf(&buf, "%s/.git/", path);
137 if (!is_directory(buf.buf)) {
138 strbuf_release(&buf);
139 /* The submodule is not checked out, so it is not modified */
145 strbuf_addf(&buf, "GIT_WORK_TREE=%s", path);
146 env[0] = strbuf_detach(&buf, NULL);
147 strbuf_addf(&buf, "GIT_DIR=%s/.git", path);
148 env[1] = strbuf_detach(&buf, NULL);
149 strbuf_addf(&buf, "GIT_INDEX_FILE");
150 env[2] = strbuf_detach(&buf, NULL);
153 memset(&cp, 0, sizeof(cp));
155 cp.env = (const char *const *)env;
159 if (start_command(&cp))
160 die("Could not run git status --porcelain");
162 len = strbuf_read(&buf, cp.out, 1024);
165 if (finish_command(&cp))
166 die("git status --porcelain failed");
171 strbuf_release(&buf);