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
9static int add_submodule_odb(const char *path)
10{
11 struct strbuf objects_directory = STRBUF_INIT;
12 struct alternate_object_database *alt_odb;
13 int ret = 0;
14
15 strbuf_addf(&objects_directory, "%s/.git/objects/", path);
16 if (!is_directory(objects_directory.buf)) {
17 ret = -1;
18 goto done;
19 }
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))
25 goto done;
26
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;
35 prepare_alt_odb();
36done:
37 strbuf_release(&objects_directory);
38 return ret;
39}
40
41void 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)
45{
46 struct rev_info rev;
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;
53
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)";
63
64 if (!message) {
65 init_revisions(&rev, NULL);
66 setup_revisions(0, NULL, &rev, NULL);
67 rev.left_right = 1;
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);
73 if (merge_bases) {
74 if (merge_bases->item == left)
75 fast_forward = 1;
76 else if (merge_bases->item == right)
77 fast_backward = 1;
78 }
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));
83 }
84 if (prepare_revision_walk(&rev))
85 message = "(revision walker failed)";
86 }
87
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));
93 if (dirty_submodule)
94 strbuf_add(&sb, "-dirty", 6);
95 if (message)
96 strbuf_addf(&sb, " %s\n", message);
97 else
98 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
99 fwrite(sb.buf, sb.len, 1, f);
100
101 if (!message) {
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) {
107 if (del)
108 strbuf_addstr(&sb, del);
109 }
110 else if (add)
111 strbuf_addstr(&sb, add);
112 format_commit_message(commit, format, &sb, &ctx);
113 if (reset)
114 strbuf_addstr(&sb, reset);
115 strbuf_addch(&sb, '\n');
116 fprintf(f, "%s", sb.buf);
117 }
118 clear_commit_marks(left, ~0);
119 clear_commit_marks(right, ~0);
120 }
121 strbuf_release(&sb);
122}
123
124int is_submodule_modified(const char *path)
125{
126 int len, i;
127 struct child_process cp;
128 const char *argv[] = {
129 "status",
130 "--porcelain",
131 NULL,
132 };
133 const char *env[LOCAL_REPO_ENV_SIZE + 3];
134 struct strbuf buf = STRBUF_INIT;
135
136 for (i = 0; i < LOCAL_REPO_ENV_SIZE; i++)
137 env[i] = local_repo_env[i];
138
139 strbuf_addf(&buf, "%s/.git/", path);
140 if (!is_directory(buf.buf)) {
141 strbuf_release(&buf);
142 /* The submodule is not checked out, so it is not modified */
143 return 0;
144
145 }
146 strbuf_reset(&buf);
147
148 strbuf_addf(&buf, "GIT_WORK_TREE=%s", path);
149 env[i++] = strbuf_detach(&buf, NULL);
150 strbuf_addf(&buf, "GIT_DIR=%s/.git", path);
151 env[i++] = strbuf_detach(&buf, NULL);
152 env[i] = NULL;
153
154 memset(&cp, 0, sizeof(cp));
155 cp.argv = argv;
156 cp.env = env;
157 cp.git_cmd = 1;
158 cp.no_stdin = 1;
159 cp.out = -1;
160 if (start_command(&cp))
161 die("Could not run git status --porcelain");
162
163 len = strbuf_read(&buf, cp.out, 1024);
164 close(cp.out);
165
166 if (finish_command(&cp))
167 die("git status --porcelain failed");
168
169 for (i = LOCAL_REPO_ENV_SIZE; env[i]; i++)
170 free((char *)env[i]);
171 strbuf_release(&buf);
172 return len != 0;
173}