setup_revisions(): Allow walking history in a submodule
[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;
eee49b6c 15 const char *git_dir;
752c0c24 16
eee49b6c
JL
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);
22 }
23 strbuf_addstr(&objects_directory, "/objects/");
de7a7960
JL
24 if (!is_directory(objects_directory.buf)) {
25 ret = -1;
26 goto done;
27 }
752c0c24
JS
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))
de7a7960 33 goto done;
752c0c24
JS
34
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();
de7a7960
JL
44done:
45 strbuf_release(&objects_directory);
46 return ret;
752c0c24
JS
47}
48
46a958b3
JL
49void handle_ignore_submodules_arg(struct diff_options *diffopt,
50 const char *arg)
51{
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);
60}
61
752c0c24
JS
62void show_submodule_summary(FILE *f, const char *path,
63 unsigned char one[20], unsigned char two[20],
721ceec1 64 unsigned dirty_submodule,
752c0c24
JS
65 const char *del, const char *add, const char *reset)
66{
67 struct rev_info rev;
75b9a8a6 68 struct commit *commit, *left = left, *right = right;
752c0c24
JS
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;
74
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)";
84
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;
99 }
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));
104 }
105 if (prepare_revision_walk(&rev))
106 message = "(revision walker failed)";
107 }
108
c7e1a736
JL
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);
113
114 if (!hashcmp(one, two)) {
115 strbuf_release(&sb);
116 return;
117 }
118
752c0c24
JS
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);
129
130 if (!message) {
131 while ((commit = get_revision(&rev))) {
5f809ff5
JH
132 struct pretty_print_context ctx = {0};
133 ctx.date_mode = rev.date_mode;
752c0c24
JS
134 strbuf_setlen(&sb, 0);
135 if (commit->object.flags & SYMMETRIC_LEFT) {
136 if (del)
137 strbuf_addstr(&sb, del);
138 }
139 else if (add)
140 strbuf_addstr(&sb, add);
5f809ff5 141 format_commit_message(commit, format, &sb, &ctx);
752c0c24
JS
142 if (reset)
143 strbuf_addstr(&sb, reset);
144 strbuf_addch(&sb, '\n');
145 fprintf(f, "%s", sb.buf);
146 }
147 clear_commit_marks(left, ~0);
148 clear_commit_marks(right, ~0);
149 }
150 strbuf_release(&sb);
151}
ee6fc514 152
3bfc4504 153unsigned is_submodule_modified(const char *path, int ignore_untracked)
ee6fc514 154{
c7e1a736 155 ssize_t len;
ee6fc514
JL
156 struct child_process cp;
157 const char *argv[] = {
158 "status",
159 "--porcelain",
160 NULL,
3bfc4504 161 NULL,
ee6fc514 162 };
ee6fc514 163 struct strbuf buf = STRBUF_INIT;
c7e1a736
JL
164 unsigned dirty_submodule = 0;
165 const char *line, *next_line;
eee49b6c 166 const char *git_dir;
ee6fc514 167
eee49b6c
JL
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)) {
ee6fc514
JL
173 strbuf_release(&buf);
174 /* The submodule is not checked out, so it is not modified */
175 return 0;
176
177 }
178 strbuf_reset(&buf);
179
3bfc4504
JL
180 if (ignore_untracked)
181 argv[2] = "-uno";
182
ee6fc514
JL
183 memset(&cp, 0, sizeof(cp));
184 cp.argv = argv;
eee49b6c 185 cp.env = local_repo_env;
ee6fc514
JL
186 cp.git_cmd = 1;
187 cp.no_stdin = 1;
188 cp.out = -1;
eee49b6c 189 cp.dir = path;
ee6fc514
JL
190 if (start_command(&cp))
191 die("Could not run git status --porcelain");
192
193 len = strbuf_read(&buf, cp.out, 1024);
c7e1a736
JL
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;
3bfc4504
JL
202 if (ignore_untracked ||
203 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
c7e1a736
JL
204 break;
205 }
206 next_line = strchr(line, '\n');
207 if (!next_line)
208 break;
209 next_line++;
210 len -= (next_line - line);
211 line = next_line;
212 }
ee6fc514
JL
213 close(cp.out);
214
215 if (finish_command(&cp))
216 die("git status --porcelain failed");
217
ee6fc514 218 strbuf_release(&buf);
c7e1a736 219 return dirty_submodule;
ee6fc514 220}