git status: Fix false positive "new commits" output for dirty submodules
[git/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
c7e1a736 133unsigned is_submodule_modified(const char *path)
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,
142 };
5ce9086d 143 const char *env[LOCAL_REPO_ENV_SIZE + 3];
ee6fc514 144 struct strbuf buf = STRBUF_INIT;
c7e1a736
JL
145 unsigned dirty_submodule = 0;
146 const char *line, *next_line;
ee6fc514 147
5ce9086d
GB
148 for (i = 0; i < LOCAL_REPO_ENV_SIZE; i++)
149 env[i] = local_repo_env[i];
150
ee6fc514
JL
151 strbuf_addf(&buf, "%s/.git/", path);
152 if (!is_directory(buf.buf)) {
153 strbuf_release(&buf);
154 /* The submodule is not checked out, so it is not modified */
155 return 0;
156
157 }
158 strbuf_reset(&buf);
159
160 strbuf_addf(&buf, "GIT_WORK_TREE=%s", path);
5ce9086d 161 env[i++] = strbuf_detach(&buf, NULL);
ee6fc514 162 strbuf_addf(&buf, "GIT_DIR=%s/.git", path);
5ce9086d
GB
163 env[i++] = strbuf_detach(&buf, NULL);
164 env[i] = NULL;
ee6fc514
JL
165
166 memset(&cp, 0, sizeof(cp));
167 cp.argv = argv;
5ce9086d 168 cp.env = env;
ee6fc514
JL
169 cp.git_cmd = 1;
170 cp.no_stdin = 1;
171 cp.out = -1;
172 if (start_command(&cp))
173 die("Could not run git status --porcelain");
174
175 len = strbuf_read(&buf, cp.out, 1024);
c7e1a736
JL
176 line = buf.buf;
177 while (len > 2) {
178 if ((line[0] == '?') && (line[1] == '?')) {
179 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
180 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
181 break;
182 } else {
183 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
184 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
185 break;
186 }
187 next_line = strchr(line, '\n');
188 if (!next_line)
189 break;
190 next_line++;
191 len -= (next_line - line);
192 line = next_line;
193 }
ee6fc514
JL
194 close(cp.out);
195
196 if (finish_command(&cp))
197 die("git status --porcelain failed");
198
5ce9086d
GB
199 for (i = LOCAL_REPO_ENV_SIZE; env[i]; i++)
200 free((char *)env[i]);
ee6fc514 201 strbuf_release(&buf);
c7e1a736 202 return dirty_submodule;
ee6fc514 203}