]> git.ipfire.org Git - thirdparty/git.git/blame - submodule.c
git status: ignoring untracked files must apply to submodules too
[thirdparty/git.git] / submodule.c
CommitLineData
752c0c24
JS
1#include "cache.h"
2#include "submodule.h"
3#include "dir.h"
4#include "diff.h"
5#include "commit.h"
6#include "revision.h"
ee6fc514 7#include "run-command.h"
c7e1a736 8#include "diffcore.h"
752c0c24 9
cb58c932 10static int add_submodule_odb(const char *path)
752c0c24
JS
11{
12 struct strbuf objects_directory = STRBUF_INIT;
13 struct alternate_object_database *alt_odb;
de7a7960 14 int ret = 0;
752c0c24
JS
15
16 strbuf_addf(&objects_directory, "%s/.git/objects/", path);
de7a7960
JL
17 if (!is_directory(objects_directory.buf)) {
18 ret = -1;
19 goto done;
20 }
752c0c24
JS
21 /* avoid adding it twice */
22 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
23 if (alt_odb->name - alt_odb->base == objects_directory.len &&
24 !strncmp(alt_odb->base, objects_directory.buf,
25 objects_directory.len))
de7a7960 26 goto done;
752c0c24
JS
27
28 alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
29 alt_odb->next = alt_odb_list;
30 strcpy(alt_odb->base, objects_directory.buf);
31 alt_odb->name = alt_odb->base + objects_directory.len;
32 alt_odb->name[2] = '/';
33 alt_odb->name[40] = '\0';
34 alt_odb->name[41] = '\0';
35 alt_odb_list = alt_odb;
36 prepare_alt_odb();
de7a7960
JL
37done:
38 strbuf_release(&objects_directory);
39 return ret;
752c0c24
JS
40}
41
42void show_submodule_summary(FILE *f, const char *path,
43 unsigned char one[20], unsigned char two[20],
721ceec1 44 unsigned dirty_submodule,
752c0c24
JS
45 const char *del, const char *add, const char *reset)
46{
47 struct rev_info rev;
75b9a8a6 48 struct commit *commit, *left = left, *right = right;
752c0c24
JS
49 struct commit_list *merge_bases, *list;
50 const char *message = NULL;
51 struct strbuf sb = STRBUF_INIT;
52 static const char *format = " %m %s";
53 int fast_forward = 0, fast_backward = 0;
54
55 if (is_null_sha1(two))
56 message = "(submodule deleted)";
57 else if (add_submodule_odb(path))
58 message = "(not checked out)";
59 else if (is_null_sha1(one))
60 message = "(new submodule)";
61 else if (!(left = lookup_commit_reference(one)) ||
62 !(right = lookup_commit_reference(two)))
63 message = "(commits not present)";
64
65 if (!message) {
66 init_revisions(&rev, NULL);
67 setup_revisions(0, NULL, &rev, NULL);
68 rev.left_right = 1;
69 rev.first_parent_only = 1;
70 left->object.flags |= SYMMETRIC_LEFT;
71 add_pending_object(&rev, &left->object, path);
72 add_pending_object(&rev, &right->object, path);
73 merge_bases = get_merge_bases(left, right, 1);
74 if (merge_bases) {
75 if (merge_bases->item == left)
76 fast_forward = 1;
77 else if (merge_bases->item == right)
78 fast_backward = 1;
79 }
80 for (list = merge_bases; list; list = list->next) {
81 list->item->object.flags |= UNINTERESTING;
82 add_pending_object(&rev, &list->item->object,
83 sha1_to_hex(list->item->object.sha1));
84 }
85 if (prepare_revision_walk(&rev))
86 message = "(revision walker failed)";
87 }
88
c7e1a736
JL
89 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
90 fprintf(f, "Submodule %s contains untracked content\n", path);
91 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
92 fprintf(f, "Submodule %s contains modified content\n", path);
93
94 if (!hashcmp(one, two)) {
95 strbuf_release(&sb);
96 return;
97 }
98
752c0c24
JS
99 strbuf_addf(&sb, "Submodule %s %s..", path,
100 find_unique_abbrev(one, DEFAULT_ABBREV));
101 if (!fast_backward && !fast_forward)
102 strbuf_addch(&sb, '.');
103 strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
104 if (message)
105 strbuf_addf(&sb, " %s\n", message);
106 else
107 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
108 fwrite(sb.buf, sb.len, 1, f);
109
110 if (!message) {
111 while ((commit = get_revision(&rev))) {
5f809ff5
JH
112 struct pretty_print_context ctx = {0};
113 ctx.date_mode = rev.date_mode;
752c0c24
JS
114 strbuf_setlen(&sb, 0);
115 if (commit->object.flags & SYMMETRIC_LEFT) {
116 if (del)
117 strbuf_addstr(&sb, del);
118 }
119 else if (add)
120 strbuf_addstr(&sb, add);
5f809ff5 121 format_commit_message(commit, format, &sb, &ctx);
752c0c24
JS
122 if (reset)
123 strbuf_addstr(&sb, reset);
124 strbuf_addch(&sb, '\n');
125 fprintf(f, "%s", sb.buf);
126 }
127 clear_commit_marks(left, ~0);
128 clear_commit_marks(right, ~0);
129 }
130 strbuf_release(&sb);
131}
ee6fc514 132
3bfc4504 133unsigned is_submodule_modified(const char *path, int ignore_untracked)
ee6fc514 134{
c7e1a736
JL
135 int i;
136 ssize_t len;
ee6fc514
JL
137 struct child_process cp;
138 const char *argv[] = {
139 "status",
140 "--porcelain",
141 NULL,
3bfc4504 142 NULL,
ee6fc514 143 };
5ce9086d 144 const char *env[LOCAL_REPO_ENV_SIZE + 3];
ee6fc514 145 struct strbuf buf = STRBUF_INIT;
c7e1a736
JL
146 unsigned dirty_submodule = 0;
147 const char *line, *next_line;
ee6fc514 148
5ce9086d
GB
149 for (i = 0; i < LOCAL_REPO_ENV_SIZE; i++)
150 env[i] = local_repo_env[i];
151
ee6fc514
JL
152 strbuf_addf(&buf, "%s/.git/", path);
153 if (!is_directory(buf.buf)) {
154 strbuf_release(&buf);
155 /* The submodule is not checked out, so it is not modified */
156 return 0;
157
158 }
159 strbuf_reset(&buf);
160
161 strbuf_addf(&buf, "GIT_WORK_TREE=%s", path);
5ce9086d 162 env[i++] = strbuf_detach(&buf, NULL);
ee6fc514 163 strbuf_addf(&buf, "GIT_DIR=%s/.git", path);
5ce9086d
GB
164 env[i++] = strbuf_detach(&buf, NULL);
165 env[i] = NULL;
ee6fc514 166
3bfc4504
JL
167 if (ignore_untracked)
168 argv[2] = "-uno";
169
ee6fc514
JL
170 memset(&cp, 0, sizeof(cp));
171 cp.argv = argv;
5ce9086d 172 cp.env = env;
ee6fc514
JL
173 cp.git_cmd = 1;
174 cp.no_stdin = 1;
175 cp.out = -1;
176 if (start_command(&cp))
177 die("Could not run git status --porcelain");
178
179 len = strbuf_read(&buf, cp.out, 1024);
c7e1a736
JL
180 line = buf.buf;
181 while (len > 2) {
182 if ((line[0] == '?') && (line[1] == '?')) {
183 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
184 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
185 break;
186 } else {
187 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
3bfc4504
JL
188 if (ignore_untracked ||
189 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
c7e1a736
JL
190 break;
191 }
192 next_line = strchr(line, '\n');
193 if (!next_line)
194 break;
195 next_line++;
196 len -= (next_line - line);
197 line = next_line;
198 }
ee6fc514
JL
199 close(cp.out);
200
201 if (finish_command(&cp))
202 die("git status --porcelain failed");
203
5ce9086d
GB
204 for (i = LOCAL_REPO_ENV_SIZE; env[i]; i++)
205 free((char *)env[i]);
ee6fc514 206 strbuf_release(&buf);
c7e1a736 207 return dirty_submodule;
ee6fc514 208}