Use dev_t for device id (st_dev) from stat in setup_git_directory_gently()
[alt-git.git] / submodule.c
blob61cb6e21ddfc789ce59597c96204e7904cd9359e
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"
10 static int add_submodule_odb(const char *path)
12 struct strbuf objects_directory = STRBUF_INIT;
13 struct alternate_object_database *alt_odb;
14 int ret = 0;
15 const char *git_dir;
17 strbuf_addf(&objects_directory, "%s/.git", path);
18 git_dir = read_gitfile_gently(objects_directory.buf);
19 if (git_dir) {
20 strbuf_reset(&objects_directory);
21 strbuf_addstr(&objects_directory, git_dir);
23 strbuf_addstr(&objects_directory, "/objects/");
24 if (!is_directory(objects_directory.buf)) {
25 ret = -1;
26 goto done;
28 /* avoid adding it twice */
29 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
30 if (alt_odb->name - alt_odb->base == objects_directory.len &&
31 !strncmp(alt_odb->base, objects_directory.buf,
32 objects_directory.len))
33 goto done;
35 alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
36 alt_odb->next = alt_odb_list;
37 strcpy(alt_odb->base, objects_directory.buf);
38 alt_odb->name = alt_odb->base + objects_directory.len;
39 alt_odb->name[2] = '/';
40 alt_odb->name[40] = '\0';
41 alt_odb->name[41] = '\0';
42 alt_odb_list = alt_odb;
43 prepare_alt_odb();
44 done:
45 strbuf_release(&objects_directory);
46 return ret;
49 void handle_ignore_submodules_arg(struct diff_options *diffopt,
50 const char *arg)
52 if (!strcmp(arg, "all"))
53 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
54 else if (!strcmp(arg, "untracked"))
55 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
56 else if (!strcmp(arg, "dirty"))
57 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
58 else
59 die("bad --ignore-submodules argument: %s", arg);
62 void show_submodule_summary(FILE *f, const char *path,
63 unsigned char one[20], unsigned char two[20],
64 unsigned dirty_submodule,
65 const char *del, const char *add, const char *reset)
67 struct rev_info rev;
68 struct commit *commit, *left = left, *right = right;
69 struct commit_list *merge_bases, *list;
70 const char *message = NULL;
71 struct strbuf sb = STRBUF_INIT;
72 static const char *format = " %m %s";
73 int fast_forward = 0, fast_backward = 0;
75 if (is_null_sha1(two))
76 message = "(submodule deleted)";
77 else if (add_submodule_odb(path))
78 message = "(not checked out)";
79 else if (is_null_sha1(one))
80 message = "(new submodule)";
81 else if (!(left = lookup_commit_reference(one)) ||
82 !(right = lookup_commit_reference(two)))
83 message = "(commits not present)";
85 if (!message) {
86 init_revisions(&rev, NULL);
87 setup_revisions(0, NULL, &rev, NULL);
88 rev.left_right = 1;
89 rev.first_parent_only = 1;
90 left->object.flags |= SYMMETRIC_LEFT;
91 add_pending_object(&rev, &left->object, path);
92 add_pending_object(&rev, &right->object, path);
93 merge_bases = get_merge_bases(left, right, 1);
94 if (merge_bases) {
95 if (merge_bases->item == left)
96 fast_forward = 1;
97 else if (merge_bases->item == right)
98 fast_backward = 1;
100 for (list = merge_bases; list; list = list->next) {
101 list->item->object.flags |= UNINTERESTING;
102 add_pending_object(&rev, &list->item->object,
103 sha1_to_hex(list->item->object.sha1));
105 if (prepare_revision_walk(&rev))
106 message = "(revision walker failed)";
109 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
110 fprintf(f, "Submodule %s contains untracked content\n", path);
111 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
112 fprintf(f, "Submodule %s contains modified content\n", path);
114 if (!hashcmp(one, two)) {
115 strbuf_release(&sb);
116 return;
119 strbuf_addf(&sb, "Submodule %s %s..", path,
120 find_unique_abbrev(one, DEFAULT_ABBREV));
121 if (!fast_backward && !fast_forward)
122 strbuf_addch(&sb, '.');
123 strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
124 if (message)
125 strbuf_addf(&sb, " %s\n", message);
126 else
127 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
128 fwrite(sb.buf, sb.len, 1, f);
130 if (!message) {
131 while ((commit = get_revision(&rev))) {
132 struct pretty_print_context ctx = {0};
133 ctx.date_mode = rev.date_mode;
134 strbuf_setlen(&sb, 0);
135 if (commit->object.flags & SYMMETRIC_LEFT) {
136 if (del)
137 strbuf_addstr(&sb, del);
139 else if (add)
140 strbuf_addstr(&sb, add);
141 format_commit_message(commit, format, &sb, &ctx);
142 if (reset)
143 strbuf_addstr(&sb, reset);
144 strbuf_addch(&sb, '\n');
145 fprintf(f, "%s", sb.buf);
147 clear_commit_marks(left, ~0);
148 clear_commit_marks(right, ~0);
150 strbuf_release(&sb);
153 unsigned is_submodule_modified(const char *path, int ignore_untracked)
155 ssize_t len;
156 struct child_process cp;
157 const char *argv[] = {
158 "status",
159 "--porcelain",
160 NULL,
161 NULL,
163 struct strbuf buf = STRBUF_INIT;
164 unsigned dirty_submodule = 0;
165 const char *line, *next_line;
166 const char *git_dir;
168 strbuf_addf(&buf, "%s/.git", path);
169 git_dir = read_gitfile_gently(buf.buf);
170 if (!git_dir)
171 git_dir = buf.buf;
172 if (!is_directory(git_dir)) {
173 strbuf_release(&buf);
174 /* The submodule is not checked out, so it is not modified */
175 return 0;
178 strbuf_reset(&buf);
180 if (ignore_untracked)
181 argv[2] = "-uno";
183 memset(&cp, 0, sizeof(cp));
184 cp.argv = argv;
185 cp.env = local_repo_env;
186 cp.git_cmd = 1;
187 cp.no_stdin = 1;
188 cp.out = -1;
189 cp.dir = path;
190 if (start_command(&cp))
191 die("Could not run git status --porcelain");
193 len = strbuf_read(&buf, cp.out, 1024);
194 line = buf.buf;
195 while (len > 2) {
196 if ((line[0] == '?') && (line[1] == '?')) {
197 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
198 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
199 break;
200 } else {
201 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
202 if (ignore_untracked ||
203 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
204 break;
206 next_line = strchr(line, '\n');
207 if (!next_line)
208 break;
209 next_line++;
210 len -= (next_line - line);
211 line = next_line;
213 close(cp.out);
215 if (finish_command(&cp))
216 die("git status --porcelain failed");
218 strbuf_release(&buf);
219 return dirty_submodule;