blob: 1905d75b2b09ee7e1503b35bf072710a659ea1ef [file] [log] [blame]
Johannes Schindelin752c0c22009-10-19 14:38:32 +02001#include "cache.h"
2#include "submodule.h"
3#include "dir.h"
4#include "diff.h"
5#include "commit.h"
6#include "revision.h"
Jens Lehmannee6fc512010-01-16 18:42:24 +01007#include "run-command.h"
Jens Lehmannc7e1a732010-03-04 22:20:33 +01008#include "diffcore.h"
Heiko Voigt68d03e42010-07-07 15:39:13 +02009#include "refs.h"
Jens Lehmannaee9c7d2010-08-06 00:39:25 +020010#include "string-list.h"
Jeff King6859de42011-09-12 15:56:52 -040011#include "sha1-array.h"
Jeff Kingc1189ca2011-09-13 17:57:57 -040012#include "argv-array.h"
Jens Lehmann5fee9952013-07-30 21:50:34 +020013#include "blob.h"
Jens Lehmannaee9c7d2010-08-06 00:39:25 +020014
Stephen Boydc2e86ad2011-03-22 00:51:05 -070015static struct string_list config_name_for_path;
16static struct string_list config_fetch_recurse_submodules_for_name;
17static struct string_list config_ignore_for_name;
Jens Lehmann88a21972011-03-06 23:10:46 +010018static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
Junio C Hamano2071fb02011-04-04 15:02:01 -070019static struct string_list changed_submodule_paths;
Jeff King6859de42011-09-12 15:56:52 -040020static int initialized_fetch_ref_tips;
21static struct sha1_array ref_tips_before_fetch;
22static struct sha1_array ref_tips_after_fetch;
23
Jens Lehmannd4e98b52011-05-14 18:26:58 +020024/*
25 * The following flag is set if the .gitmodules file is unmerged. We then
26 * disable recursion for all submodules where .git/config doesn't have a
27 * matching config entry because we can't guess what might be configured in
28 * .gitmodules unless the user resolves the conflict. When a command line
29 * option is given (which always overrides configuration) this flag will be
30 * ignored.
31 */
32static int gitmodules_is_unmerged;
Johannes Schindelin752c0c22009-10-19 14:38:32 +020033
Jens Lehmann5fee9952013-07-30 21:50:34 +020034/*
35 * This flag is set if the .gitmodules file had unstaged modifications on
36 * startup. This must be checked before allowing modifications to the
37 * .gitmodules file with the intention to stage them later, because when
38 * continuing we would stage the modifications the user didn't stage herself
39 * too. That might change in a future version when we learn to stage the
40 * changes we do ourselves without staging any previous modifications.
41 */
42static int gitmodules_is_modified;
43
44
45int is_staging_gitmodules_ok(void)
46{
47 return !gitmodules_is_modified;
48}
49
Jens Lehmann06567812013-08-06 21:15:11 +020050/*
51 * Try to update the "path" entry in the "submodule.<name>" section of the
52 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
53 * with the correct path=<oldpath> setting was found and we could update it.
54 */
55int update_path_in_gitmodules(const char *oldpath, const char *newpath)
56{
57 struct strbuf entry = STRBUF_INIT;
58 struct string_list_item *path_option;
59
60 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
61 return -1;
62
63 if (gitmodules_is_unmerged)
64 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
65
66 path_option = unsorted_string_list_lookup(&config_name_for_path, oldpath);
67 if (!path_option) {
68 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
69 return -1;
70 }
71 strbuf_addstr(&entry, "submodule.");
72 strbuf_addstr(&entry, path_option->util);
73 strbuf_addstr(&entry, ".path");
74 if (git_config_set_in_file(".gitmodules", entry.buf, newpath) < 0) {
75 /* Maybe the user already did that, don't error out here */
76 warning(_("Could not update .gitmodules entry %s"), entry.buf);
77 strbuf_release(&entry);
78 return -1;
79 }
80 strbuf_release(&entry);
81 return 0;
82}
83
Jens Lehmann95c16412013-08-06 21:15:25 +020084/*
85 * Try to remove the "submodule.<name>" section from .gitmodules where the given
86 * path is configured. Return 0 only if a .gitmodules file was found, a section
87 * with the correct path=<path> setting was found and we could remove it.
88 */
89int remove_path_from_gitmodules(const char *path)
90{
91 struct strbuf sect = STRBUF_INIT;
92 struct string_list_item *path_option;
93
94 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
95 return -1;
96
97 if (gitmodules_is_unmerged)
98 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
99
100 path_option = unsorted_string_list_lookup(&config_name_for_path, path);
101 if (!path_option) {
102 warning(_("Could not find section in .gitmodules where path=%s"), path);
103 return -1;
104 }
105 strbuf_addstr(&sect, "submodule.");
106 strbuf_addstr(&sect, path_option->util);
107 if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) {
108 /* Maybe the user already did that, don't error out here */
109 warning(_("Could not remove .gitmodules entry for %s"), path);
110 strbuf_release(&sect);
111 return -1;
112 }
113 strbuf_release(&sect);
114 return 0;
115}
116
Jens Lehmann5fee9952013-07-30 21:50:34 +0200117void stage_updated_gitmodules(void)
118{
119 struct strbuf buf = STRBUF_INIT;
120 struct stat st;
121 int pos;
122 struct cache_entry *ce;
123 int namelen = strlen(".gitmodules");
124
125 pos = cache_name_pos(".gitmodules", namelen);
126 if (pos < 0) {
127 warning(_("could not find .gitmodules in index"));
128 return;
129 }
130 ce = active_cache[pos];
131 ce->ce_flags = namelen;
132 if (strbuf_read_file(&buf, ".gitmodules", 0) < 0)
133 die(_("reading updated .gitmodules failed"));
134 if (lstat(".gitmodules", &st) < 0)
135 die_errno(_("unable to stat updated .gitmodules"));
136 fill_stat_cache_info(ce, &st);
137 ce->ce_mode = ce_mode_from_stat(ce, st.st_mode);
138 if (remove_cache_entry_at(pos) < 0)
139 die(_("unable to remove .gitmodules from index"));
140 if (write_sha1_file(buf.buf, buf.len, blob_type, ce->sha1))
141 die(_("adding updated .gitmodules failed"));
142 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))
143 die(_("staging updated .gitmodules failed"));
144}
145
Junio C Hamanocb58c932010-01-11 22:31:58 -0800146static int add_submodule_odb(const char *path)
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200147{
148 struct strbuf objects_directory = STRBUF_INIT;
149 struct alternate_object_database *alt_odb;
Jens Lehmannde7a7962010-01-31 17:43:49 +0100150 int ret = 0;
Jens Lehmanneee49b62010-04-10 19:01:12 +0200151 const char *git_dir;
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200152
Jens Lehmanneee49b62010-04-10 19:01:12 +0200153 strbuf_addf(&objects_directory, "%s/.git", path);
Junio C Hamano13d6ec92011-08-22 14:04:56 -0700154 git_dir = read_gitfile(objects_directory.buf);
Jens Lehmanneee49b62010-04-10 19:01:12 +0200155 if (git_dir) {
156 strbuf_reset(&objects_directory);
157 strbuf_addstr(&objects_directory, git_dir);
158 }
159 strbuf_addstr(&objects_directory, "/objects/");
Jens Lehmannde7a7962010-01-31 17:43:49 +0100160 if (!is_directory(objects_directory.buf)) {
161 ret = -1;
162 goto done;
163 }
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200164 /* avoid adding it twice */
165 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
166 if (alt_odb->name - alt_odb->base == objects_directory.len &&
167 !strncmp(alt_odb->base, objects_directory.buf,
168 objects_directory.len))
Jens Lehmannde7a7962010-01-31 17:43:49 +0100169 goto done;
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200170
171 alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
172 alt_odb->next = alt_odb_list;
173 strcpy(alt_odb->base, objects_directory.buf);
174 alt_odb->name = alt_odb->base + objects_directory.len;
175 alt_odb->name[2] = '/';
176 alt_odb->name[40] = '\0';
177 alt_odb->name[41] = '\0';
178 alt_odb_list = alt_odb;
Heiko Voigt5e736332012-05-14 18:24:45 +0200179
180 /* add possible alternates from the submodule */
181 read_info_alternates(objects_directory.buf, 0);
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200182 prepare_alt_odb();
Jens Lehmannde7a7962010-01-31 17:43:49 +0100183done:
184 strbuf_release(&objects_directory);
185 return ret;
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200186}
187
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200188void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
189 const char *path)
190{
191 struct string_list_item *path_option, *ignore_option;
192 path_option = unsorted_string_list_lookup(&config_name_for_path, path);
193 if (path_option) {
194 ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
195 if (ignore_option)
196 handle_ignore_submodules_arg(diffopt, ignore_option->util);
Jens Lehmannd4e98b52011-05-14 18:26:58 +0200197 else if (gitmodules_is_unmerged)
198 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200199 }
200}
201
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100202int submodule_config(const char *var, const char *value, void *cb)
Jens Lehmann302ad7a2010-08-06 00:40:48 +0200203{
204 if (!prefixcmp(var, "submodule."))
205 return parse_submodule_config_option(var, value);
Jens Lehmannbe254a02010-11-11 00:55:02 +0100206 else if (!strcmp(var, "fetch.recursesubmodules")) {
Jens Lehmann1fb25502011-03-06 23:11:48 +0100207 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
Jens Lehmannbe254a02010-11-11 00:55:02 +0100208 return 0;
209 }
Jens Lehmann302ad7a2010-08-06 00:40:48 +0200210 return 0;
211}
212
213void gitmodules_config(void)
214{
215 const char *work_tree = get_git_work_tree();
216 if (work_tree) {
217 struct strbuf gitmodules_path = STRBUF_INIT;
Jens Lehmannd4e98b52011-05-14 18:26:58 +0200218 int pos;
Jens Lehmann302ad7a2010-08-06 00:40:48 +0200219 strbuf_addstr(&gitmodules_path, work_tree);
220 strbuf_addstr(&gitmodules_path, "/.gitmodules");
Jens Lehmannd4e98b52011-05-14 18:26:58 +0200221 if (read_cache() < 0)
222 die("index file corrupt");
223 pos = cache_name_pos(".gitmodules", 11);
224 if (pos < 0) { /* .gitmodules not found or isn't merged */
225 pos = -1 - pos;
226 if (active_nr > pos) { /* there is a .gitmodules */
227 const struct cache_entry *ce = active_cache[pos];
228 if (ce_namelen(ce) == 11 &&
229 !memcmp(ce->name, ".gitmodules", 11))
230 gitmodules_is_unmerged = 1;
231 }
Jens Lehmann5fee9952013-07-30 21:50:34 +0200232 } else if (pos < active_nr) {
233 struct stat st;
234 if (lstat(".gitmodules", &st) == 0 &&
235 ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED)
236 gitmodules_is_modified = 1;
Jens Lehmannd4e98b52011-05-14 18:26:58 +0200237 }
238
239 if (!gitmodules_is_unmerged)
240 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
Jens Lehmann302ad7a2010-08-06 00:40:48 +0200241 strbuf_release(&gitmodules_path);
242 }
243}
244
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200245int parse_submodule_config_option(const char *var, const char *value)
246{
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200247 struct string_list_item *config;
Jeff King9edbb8b2013-01-23 01:25:22 -0500248 const char *name, *key;
249 int namelen;
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200250
Jeff King9edbb8b2013-01-23 01:25:22 -0500251 if (parse_config_key(var, "submodule", &name, &namelen, &key) < 0 || !name)
252 return 0;
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200253
Jeff King9edbb8b2013-01-23 01:25:22 -0500254 if (!strcmp(key, "path")) {
Jharrod LaFon4b054402013-08-19 09:26:56 -0700255 if (!value)
256 return config_error_nonbool(var);
257
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200258 config = unsorted_string_list_lookup(&config_name_for_path, value);
259 if (config)
260 free(config->util);
261 else
262 config = string_list_append(&config_name_for_path, xstrdup(value));
Jeff King6bfe19e2013-01-23 01:26:42 -0500263 config->util = xmemdupz(name, namelen);
Jeff King9edbb8b2013-01-23 01:25:22 -0500264 } else if (!strcmp(key, "fetchrecursesubmodules")) {
Jeff King6bfe19e2013-01-23 01:26:42 -0500265 char *name_cstr = xmemdupz(name, namelen);
266 config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr);
Jens Lehmannc1a3c362010-11-11 00:55:41 +0100267 if (!config)
Jeff King6bfe19e2013-01-23 01:26:42 -0500268 config = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr);
269 else
270 free(name_cstr);
Jens Lehmannbf42b382011-03-06 23:12:19 +0100271 config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
Jeff King9edbb8b2013-01-23 01:25:22 -0500272 } else if (!strcmp(key, "ignore")) {
Jeff King6bfe19e2013-01-23 01:26:42 -0500273 char *name_cstr;
274
Jharrod LaFon4b054402013-08-19 09:26:56 -0700275 if (!value)
276 return config_error_nonbool(var);
277
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200278 if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
279 strcmp(value, "all") && strcmp(value, "none")) {
280 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
281 return 0;
282 }
283
Jeff King6bfe19e2013-01-23 01:26:42 -0500284 name_cstr = xmemdupz(name, namelen);
285 config = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr);
286 if (config) {
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200287 free(config->util);
Jeff King6bfe19e2013-01-23 01:26:42 -0500288 free(name_cstr);
289 } else
290 config = string_list_append(&config_ignore_for_name, name_cstr);
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200291 config->util = xstrdup(value);
292 return 0;
293 }
294 return 0;
295}
296
Jens Lehmann46a958b2010-06-25 16:56:47 +0200297void handle_ignore_submodules_arg(struct diff_options *diffopt,
298 const char *arg)
299{
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +0200300 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
301 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
302 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
303
Jens Lehmann46a958b2010-06-25 16:56:47 +0200304 if (!strcmp(arg, "all"))
305 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
306 else if (!strcmp(arg, "untracked"))
307 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
308 else if (!strcmp(arg, "dirty"))
309 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200310 else if (strcmp(arg, "none"))
Jens Lehmann46a958b2010-06-25 16:56:47 +0200311 die("bad --ignore-submodules argument: %s", arg);
312}
313
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500314static int prepare_submodule_summary(struct rev_info *rev, const char *path,
315 struct commit *left, struct commit *right,
316 int *fast_forward, int *fast_backward)
317{
318 struct commit_list *merge_bases, *list;
319
320 init_revisions(rev, NULL);
321 setup_revisions(0, NULL, rev, NULL);
322 rev->left_right = 1;
323 rev->first_parent_only = 1;
324 left->object.flags |= SYMMETRIC_LEFT;
325 add_pending_object(rev, &left->object, path);
326 add_pending_object(rev, &right->object, path);
327 merge_bases = get_merge_bases(left, right, 1);
328 if (merge_bases) {
329 if (merge_bases->item == left)
330 *fast_forward = 1;
331 else if (merge_bases->item == right)
332 *fast_backward = 1;
333 }
334 for (list = merge_bases; list; list = list->next) {
335 list->item->object.flags |= UNINTERESTING;
336 add_pending_object(rev, &list->item->object,
337 sha1_to_hex(list->item->object.sha1));
338 }
339 return prepare_revision_walk(rev);
340}
341
342static void print_submodule_summary(struct rev_info *rev, FILE *f,
John Keeping0f33a062013-04-05 17:12:08 +0100343 const char *line_prefix,
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500344 const char *del, const char *add, const char *reset)
345{
346 static const char format[] = " %m %s";
347 struct strbuf sb = STRBUF_INIT;
348 struct commit *commit;
349
350 while ((commit = get_revision(rev))) {
351 struct pretty_print_context ctx = {0};
352 ctx.date_mode = rev->date_mode;
Alexey Shumkinecaee802013-06-26 14:19:50 +0400353 ctx.output_encoding = get_log_output_encoding();
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500354 strbuf_setlen(&sb, 0);
John Keeping0f33a062013-04-05 17:12:08 +0100355 strbuf_addstr(&sb, line_prefix);
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500356 if (commit->object.flags & SYMMETRIC_LEFT) {
357 if (del)
358 strbuf_addstr(&sb, del);
359 }
360 else if (add)
361 strbuf_addstr(&sb, add);
362 format_commit_message(commit, format, &sb, &ctx);
363 if (reset)
364 strbuf_addstr(&sb, reset);
365 strbuf_addch(&sb, '\n');
366 fprintf(f, "%s", sb.buf);
367 }
368 strbuf_release(&sb);
369}
370
Jens Lehmann88a21972011-03-06 23:10:46 +0100371int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
372{
373 switch (git_config_maybe_bool(opt, arg)) {
374 case 1:
375 return RECURSE_SUBMODULES_ON;
376 case 0:
377 return RECURSE_SUBMODULES_OFF;
378 default:
379 if (!strcmp(arg, "on-demand"))
380 return RECURSE_SUBMODULES_ON_DEMAND;
381 die("bad %s argument: %s", opt, arg);
382 }
383}
384
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200385void show_submodule_summary(FILE *f, const char *path,
John Keeping0f33a062013-04-05 17:12:08 +0100386 const char *line_prefix,
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200387 unsigned char one[20], unsigned char two[20],
Ramkumar Ramachandra4e215132012-11-13 21:12:47 +0530388 unsigned dirty_submodule, const char *meta,
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200389 const char *del, const char *add, const char *reset)
390{
391 struct rev_info rev;
Jeff King83715492013-03-22 12:19:56 -0400392 struct commit *left = NULL, *right = NULL;
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200393 const char *message = NULL;
394 struct strbuf sb = STRBUF_INIT;
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200395 int fast_forward = 0, fast_backward = 0;
396
397 if (is_null_sha1(two))
398 message = "(submodule deleted)";
399 else if (add_submodule_odb(path))
400 message = "(not checked out)";
401 else if (is_null_sha1(one))
402 message = "(new submodule)";
403 else if (!(left = lookup_commit_reference(one)) ||
404 !(right = lookup_commit_reference(two)))
405 message = "(commits not present)";
Jeff King83715492013-03-22 12:19:56 -0400406 else if (prepare_submodule_summary(&rev, path, left, right,
407 &fast_forward, &fast_backward))
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500408 message = "(revision walker failed)";
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200409
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100410 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
John Keeping0f33a062013-04-05 17:12:08 +0100411 fprintf(f, "%sSubmodule %s contains untracked content\n",
412 line_prefix, path);
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100413 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
John Keeping0f33a062013-04-05 17:12:08 +0100414 fprintf(f, "%sSubmodule %s contains modified content\n",
415 line_prefix, path);
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100416
417 if (!hashcmp(one, two)) {
418 strbuf_release(&sb);
419 return;
420 }
421
John Keeping0f33a062013-04-05 17:12:08 +0100422 strbuf_addf(&sb, "%s%sSubmodule %s %s..", line_prefix, meta, path,
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200423 find_unique_abbrev(one, DEFAULT_ABBREV));
424 if (!fast_backward && !fast_forward)
425 strbuf_addch(&sb, '.');
426 strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
427 if (message)
Ramkumar Ramachandra4e215132012-11-13 21:12:47 +0530428 strbuf_addf(&sb, " %s%s\n", message, reset);
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200429 else
Ramkumar Ramachandra4e215132012-11-13 21:12:47 +0530430 strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200431 fwrite(sb.buf, sb.len, 1, f);
432
Jeff King83715492013-03-22 12:19:56 -0400433 if (!message) /* only NULL if we succeeded in setting up the walk */
John Keeping0f33a062013-04-05 17:12:08 +0100434 print_submodule_summary(&rev, f, line_prefix, del, add, reset);
Jeff King83715492013-03-22 12:19:56 -0400435 if (left)
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200436 clear_commit_marks(left, ~0);
Jeff King83715492013-03-22 12:19:56 -0400437 if (right)
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200438 clear_commit_marks(right, ~0);
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500439
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200440 strbuf_release(&sb);
441}
Jens Lehmannee6fc512010-01-16 18:42:24 +0100442
Jens Lehmannbe254a02010-11-11 00:55:02 +0100443void set_config_fetch_recurse_submodules(int value)
444{
445 config_fetch_recurse_submodules = value;
446}
447
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200448static int has_remote(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
449{
450 return 1;
451}
452
453static int submodule_needs_pushing(const char *path, const unsigned char sha1[20])
454{
455 if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
456 return 0;
457
458 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
459 struct child_process cp;
460 const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL};
461 struct strbuf buf = STRBUF_INIT;
462 int needs_pushing = 0;
463
464 argv[1] = sha1_to_hex(sha1);
465 memset(&cp, 0, sizeof(cp));
466 cp.argv = argv;
467 cp.env = local_repo_env;
468 cp.git_cmd = 1;
469 cp.no_stdin = 1;
470 cp.out = -1;
471 cp.dir = path;
472 if (start_command(&cp))
473 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
474 sha1_to_hex(sha1), path);
475 if (strbuf_read(&buf, cp.out, 41))
476 needs_pushing = 1;
477 finish_command(&cp);
478 close(cp.out);
479 strbuf_release(&buf);
480 return needs_pushing;
481 }
482
483 return 0;
484}
485
486static void collect_submodules_from_diff(struct diff_queue_struct *q,
487 struct diff_options *options,
488 void *data)
489{
490 int i;
Heiko Voigta762e512012-03-29 09:21:23 +0200491 struct string_list *needs_pushing = data;
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200492
493 for (i = 0; i < q->nr; i++) {
494 struct diff_filepair *p = q->queue[i];
495 if (!S_ISGITLINK(p->two->mode))
496 continue;
Heiko Voigta762e512012-03-29 09:21:23 +0200497 if (submodule_needs_pushing(p->two->path, p->two->sha1))
498 string_list_insert(needs_pushing, p->two->path);
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200499 }
500}
501
Heiko Voigta762e512012-03-29 09:21:23 +0200502static void find_unpushed_submodule_commits(struct commit *commit,
503 struct string_list *needs_pushing)
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200504{
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200505 struct rev_info rev;
506
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200507 init_revisions(&rev, NULL);
508 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
509 rev.diffopt.format_callback = collect_submodules_from_diff;
510 rev.diffopt.format_callback_data = needs_pushing;
René Scharfe78e98ea2011-12-17 11:27:19 +0100511 diff_tree_combined_merge(commit, 1, &rev);
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200512}
513
Heiko Voigta762e512012-03-29 09:21:23 +0200514int find_unpushed_submodules(unsigned char new_sha1[20],
515 const char *remotes_name, struct string_list *needs_pushing)
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200516{
517 struct rev_info rev;
518 struct commit *commit;
519 const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
520 int argc = ARRAY_SIZE(argv) - 1;
521 char *sha1_copy;
Heiko Voigta762e512012-03-29 09:21:23 +0200522
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200523 struct strbuf remotes_arg = STRBUF_INIT;
524
525 strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);
526 init_revisions(&rev, NULL);
527 sha1_copy = xstrdup(sha1_to_hex(new_sha1));
528 argv[1] = sha1_copy;
529 argv[3] = remotes_arg.buf;
530 setup_revisions(argc, argv, &rev, NULL);
531 if (prepare_revision_walk(&rev))
532 die("revision walk setup failed");
533
Heiko Voigta762e512012-03-29 09:21:23 +0200534 while ((commit = get_revision(&rev)) != NULL)
535 find_unpushed_submodule_commits(commit, needs_pushing);
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200536
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +0200537 reset_revision_walk();
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200538 free(sha1_copy);
539 strbuf_release(&remotes_arg);
540
Heiko Voigta762e512012-03-29 09:21:23 +0200541 return needs_pushing->nr;
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200542}
543
Heiko Voigteb21c732012-03-29 09:21:24 +0200544static int push_submodule(const char *path)
545{
546 if (add_submodule_odb(path))
547 return 1;
548
549 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
550 struct child_process cp;
551 const char *argv[] = {"push", NULL};
552
553 memset(&cp, 0, sizeof(cp));
554 cp.argv = argv;
555 cp.env = local_repo_env;
556 cp.git_cmd = 1;
557 cp.no_stdin = 1;
558 cp.dir = path;
559 if (run_command(&cp))
560 return 0;
561 close(cp.out);
562 }
563
564 return 1;
565}
566
567int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name)
568{
569 int i, ret = 1;
570 struct string_list needs_pushing;
571
572 memset(&needs_pushing, 0, sizeof(struct string_list));
573 needs_pushing.strdup_strings = 1;
574
575 if (!find_unpushed_submodules(new_sha1, remotes_name, &needs_pushing))
576 return 1;
577
578 for (i = 0; i < needs_pushing.nr; i++) {
579 const char *path = needs_pushing.items[i].string;
580 fprintf(stderr, "Pushing submodule '%s'\n", path);
581 if (!push_submodule(path)) {
582 fprintf(stderr, "Unable to push submodule '%s'\n", path);
583 ret = 0;
584 }
585 }
586
587 string_list_clear(&needs_pushing, 0);
588
589 return ret;
590}
591
Jens Lehmannc16c3e42011-03-06 23:12:58 +0100592static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
593{
594 int is_present = 0;
595 if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
596 /* Even if the submodule is checked out and the commit is
597 * present, make sure it is reachable from a ref. */
598 struct child_process cp;
599 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
600 struct strbuf buf = STRBUF_INIT;
601
602 argv[3] = sha1_to_hex(sha1);
603 memset(&cp, 0, sizeof(cp));
604 cp.argv = argv;
605 cp.env = local_repo_env;
606 cp.git_cmd = 1;
607 cp.no_stdin = 1;
608 cp.out = -1;
609 cp.dir = path;
610 if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024))
611 is_present = 1;
612
613 close(cp.out);
614 strbuf_release(&buf);
615 }
616 return is_present;
617}
618
Jens Lehmann88a21972011-03-06 23:10:46 +0100619static void submodule_collect_changed_cb(struct diff_queue_struct *q,
620 struct diff_options *options,
621 void *data)
622{
623 int i;
624 for (i = 0; i < q->nr; i++) {
625 struct diff_filepair *p = q->queue[i];
626 if (!S_ISGITLINK(p->two->mode))
627 continue;
628
629 if (S_ISGITLINK(p->one->mode)) {
630 /* NEEDSWORK: We should honor the name configured in
631 * the .gitmodules file of the commit we are examining
632 * here to be able to correctly follow submodules
633 * being moved around. */
634 struct string_list_item *path;
635 path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
Jens Lehmannc16c3e42011-03-06 23:12:58 +0100636 if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
Jens Lehmann88a21972011-03-06 23:10:46 +0100637 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
638 } else {
639 /* Submodule is new or was moved here */
640 /* NEEDSWORK: When the .git directories of submodules
641 * live inside the superprojects .git directory some
642 * day we should fetch new submodules directly into
643 * that location too when config or options request
644 * that so they can be checked out from there. */
645 continue;
646 }
647 }
648}
649
Jeff King6859de42011-09-12 15:56:52 -0400650static int add_sha1_to_array(const char *ref, const unsigned char *sha1,
651 int flags, void *data)
652{
653 sha1_array_append(data, sha1);
654 return 0;
655}
656
Jens Lehmann88a21972011-03-06 23:10:46 +0100657void check_for_new_submodule_commits(unsigned char new_sha1[20])
658{
Jeff King6859de42011-09-12 15:56:52 -0400659 if (!initialized_fetch_ref_tips) {
660 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
661 initialized_fetch_ref_tips = 1;
662 }
663
664 sha1_array_append(&ref_tips_after_fetch, new_sha1);
665}
666
Jeff King6859de42011-09-12 15:56:52 -0400667static void add_sha1_to_argv(const unsigned char sha1[20], void *data)
668{
Jeff Kingc1189ca2011-09-13 17:57:57 -0400669 argv_array_push(data, sha1_to_hex(sha1));
Jeff King6859de42011-09-12 15:56:52 -0400670}
671
672static void calculate_changed_submodule_paths(void)
673{
Jens Lehmann88a21972011-03-06 23:10:46 +0100674 struct rev_info rev;
675 struct commit *commit;
Jeff Kingc1189ca2011-09-13 17:57:57 -0400676 struct argv_array argv = ARGV_ARRAY_INIT;
Jens Lehmann88a21972011-03-06 23:10:46 +0100677
Jens Lehmann18322ba2011-09-09 20:22:03 +0200678 /* No need to check if there are no submodules configured */
679 if (!config_name_for_path.nr)
680 return;
681
Jens Lehmann88a21972011-03-06 23:10:46 +0100682 init_revisions(&rev, NULL);
Jeff Kingc1189ca2011-09-13 17:57:57 -0400683 argv_array_push(&argv, "--"); /* argv[0] program name */
Jeff King6859de42011-09-12 15:56:52 -0400684 sha1_array_for_each_unique(&ref_tips_after_fetch,
685 add_sha1_to_argv, &argv);
Jeff Kingc1189ca2011-09-13 17:57:57 -0400686 argv_array_push(&argv, "--not");
Jeff King6859de42011-09-12 15:56:52 -0400687 sha1_array_for_each_unique(&ref_tips_before_fetch,
688 add_sha1_to_argv, &argv);
689 setup_revisions(argv.argc, argv.argv, &rev, NULL);
Jens Lehmann88a21972011-03-06 23:10:46 +0100690 if (prepare_revision_walk(&rev))
691 die("revision walk setup failed");
692
693 /*
694 * Collect all submodules (whether checked out or not) for which new
695 * commits have been recorded upstream in "changed_submodule_paths".
696 */
697 while ((commit = get_revision(&rev))) {
698 struct commit_list *parent = commit->parents;
699 while (parent) {
700 struct diff_options diff_opts;
701 diff_setup(&diff_opts);
Jens Lehmannea2d3252011-06-20 20:18:03 +0200702 DIFF_OPT_SET(&diff_opts, RECURSIVE);
Jens Lehmann88a21972011-03-06 23:10:46 +0100703 diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
704 diff_opts.format_callback = submodule_collect_changed_cb;
Thomas Rast28452652012-08-03 14:16:24 +0200705 diff_setup_done(&diff_opts);
Jens Lehmann88a21972011-03-06 23:10:46 +0100706 diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts);
707 diffcore_std(&diff_opts);
708 diff_flush(&diff_opts);
709 parent = parent->next;
710 }
711 }
Jeff King6859de42011-09-12 15:56:52 -0400712
Jeff Kingc1189ca2011-09-13 17:57:57 -0400713 argv_array_clear(&argv);
Jeff King6859de42011-09-12 15:56:52 -0400714 sha1_array_clear(&ref_tips_before_fetch);
715 sha1_array_clear(&ref_tips_after_fetch);
716 initialized_fetch_ref_tips = 0;
Jens Lehmann88a21972011-03-06 23:10:46 +0100717}
718
Jens Lehmann50d89ad2012-09-01 17:27:06 +0200719int fetch_populated_submodules(const struct argv_array *options,
Jens Lehmann8f0700d2011-03-06 23:11:21 +0100720 const char *prefix, int command_line_option,
Jens Lehmannbe254a02010-11-11 00:55:02 +0100721 int quiet)
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100722{
Jens Lehmann50d89ad2012-09-01 17:27:06 +0200723 int i, result = 0;
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100724 struct child_process cp;
Jens Lehmann50d89ad2012-09-01 17:27:06 +0200725 struct argv_array argv = ARGV_ARRAY_INIT;
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100726 struct string_list_item *name_for_path;
727 const char *work_tree = get_git_work_tree();
728 if (!work_tree)
Jens Lehmann88a21972011-03-06 23:10:46 +0100729 goto out;
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100730
René Scharfe467b8fe2013-06-09 18:33:45 +0200731 if (read_cache() < 0)
732 die("index file corrupt");
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100733
Jens Lehmann50d89ad2012-09-01 17:27:06 +0200734 argv_array_push(&argv, "fetch");
735 for (i = 0; i < options->argc; i++)
736 argv_array_push(&argv, options->argv[i]);
737 argv_array_push(&argv, "--recurse-submodules-default");
738 /* default value, "--submodule-prefix" and its value are added later */
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100739
740 memset(&cp, 0, sizeof(cp));
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100741 cp.env = local_repo_env;
742 cp.git_cmd = 1;
743 cp.no_stdin = 1;
744
Jeff King6859de42011-09-12 15:56:52 -0400745 calculate_changed_submodule_paths();
746
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100747 for (i = 0; i < active_nr; i++) {
748 struct strbuf submodule_path = STRBUF_INIT;
749 struct strbuf submodule_git_dir = STRBUF_INIT;
750 struct strbuf submodule_prefix = STRBUF_INIT;
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700751 const struct cache_entry *ce = active_cache[i];
Jens Lehmann88a21972011-03-06 23:10:46 +0100752 const char *git_dir, *name, *default_argv;
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100753
754 if (!S_ISGITLINK(ce->ce_mode))
755 continue;
756
757 name = ce->name;
758 name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
759 if (name_for_path)
760 name = name_for_path->util;
761
Jens Lehmann88a21972011-03-06 23:10:46 +0100762 default_argv = "yes";
Jens Lehmann8f0700d2011-03-06 23:11:21 +0100763 if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
Jens Lehmannc1a3c362010-11-11 00:55:41 +0100764 struct string_list_item *fetch_recurse_submodules_option;
765 fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
766 if (fetch_recurse_submodules_option) {
Jens Lehmannbf42b382011-03-06 23:12:19 +0100767 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF)
Jens Lehmannc1a3c362010-11-11 00:55:41 +0100768 continue;
Jens Lehmannbf42b382011-03-06 23:12:19 +0100769 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) {
770 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
771 continue;
772 default_argv = "on-demand";
773 }
Jens Lehmannc1a3c362010-11-11 00:55:41 +0100774 } else {
Jens Lehmannd4e98b52011-05-14 18:26:58 +0200775 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
776 gitmodules_is_unmerged)
Jens Lehmannc1a3c362010-11-11 00:55:41 +0100777 continue;
Jens Lehmann88a21972011-03-06 23:10:46 +0100778 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
779 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
780 continue;
781 default_argv = "on-demand";
782 }
Jens Lehmannc1a3c362010-11-11 00:55:41 +0100783 }
Jens Lehmann8f0700d2011-03-06 23:11:21 +0100784 } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
785 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
786 continue;
787 default_argv = "on-demand";
Jens Lehmannbe254a02010-11-11 00:55:02 +0100788 }
789
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100790 strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
791 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
792 strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
Junio C Hamano13d6ec92011-08-22 14:04:56 -0700793 git_dir = read_gitfile(submodule_git_dir.buf);
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100794 if (!git_dir)
795 git_dir = submodule_git_dir.buf;
796 if (is_directory(git_dir)) {
797 if (!quiet)
798 printf("Fetching submodule %s%s\n", prefix, ce->name);
799 cp.dir = submodule_path.buf;
Jens Lehmann50d89ad2012-09-01 17:27:06 +0200800 argv_array_push(&argv, default_argv);
801 argv_array_push(&argv, "--submodule-prefix");
802 argv_array_push(&argv, submodule_prefix.buf);
803 cp.argv = argv.argv;
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100804 if (run_command(&cp))
805 result = 1;
Jens Lehmann50d89ad2012-09-01 17:27:06 +0200806 argv_array_pop(&argv);
807 argv_array_pop(&argv);
808 argv_array_pop(&argv);
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100809 }
810 strbuf_release(&submodule_path);
811 strbuf_release(&submodule_git_dir);
812 strbuf_release(&submodule_prefix);
813 }
Jens Lehmann50d89ad2012-09-01 17:27:06 +0200814 argv_array_clear(&argv);
Jens Lehmann88a21972011-03-06 23:10:46 +0100815out:
816 string_list_clear(&changed_submodule_paths, 1);
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100817 return result;
818}
819
Jens Lehmann3bfc4502010-03-13 23:00:27 +0100820unsigned is_submodule_modified(const char *path, int ignore_untracked)
Jens Lehmannee6fc512010-01-16 18:42:24 +0100821{
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100822 ssize_t len;
Jens Lehmannee6fc512010-01-16 18:42:24 +0100823 struct child_process cp;
824 const char *argv[] = {
825 "status",
826 "--porcelain",
827 NULL,
Jens Lehmann3bfc4502010-03-13 23:00:27 +0100828 NULL,
Jens Lehmannee6fc512010-01-16 18:42:24 +0100829 };
Jens Lehmannee6fc512010-01-16 18:42:24 +0100830 struct strbuf buf = STRBUF_INIT;
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100831 unsigned dirty_submodule = 0;
832 const char *line, *next_line;
Jens Lehmanneee49b62010-04-10 19:01:12 +0200833 const char *git_dir;
Jens Lehmannee6fc512010-01-16 18:42:24 +0100834
Jens Lehmanneee49b62010-04-10 19:01:12 +0200835 strbuf_addf(&buf, "%s/.git", path);
Junio C Hamano13d6ec92011-08-22 14:04:56 -0700836 git_dir = read_gitfile(buf.buf);
Jens Lehmanneee49b62010-04-10 19:01:12 +0200837 if (!git_dir)
838 git_dir = buf.buf;
839 if (!is_directory(git_dir)) {
Jens Lehmannee6fc512010-01-16 18:42:24 +0100840 strbuf_release(&buf);
841 /* The submodule is not checked out, so it is not modified */
842 return 0;
843
844 }
845 strbuf_reset(&buf);
846
Jens Lehmann3bfc4502010-03-13 23:00:27 +0100847 if (ignore_untracked)
848 argv[2] = "-uno";
849
Jens Lehmannee6fc512010-01-16 18:42:24 +0100850 memset(&cp, 0, sizeof(cp));
851 cp.argv = argv;
Jens Lehmanneee49b62010-04-10 19:01:12 +0200852 cp.env = local_repo_env;
Jens Lehmannee6fc512010-01-16 18:42:24 +0100853 cp.git_cmd = 1;
854 cp.no_stdin = 1;
855 cp.out = -1;
Jens Lehmanneee49b62010-04-10 19:01:12 +0200856 cp.dir = path;
Jens Lehmannee6fc512010-01-16 18:42:24 +0100857 if (start_command(&cp))
Jens Lehmann6a5ceda2011-12-07 22:50:14 +0100858 die("Could not run 'git status --porcelain' in submodule %s", path);
Jens Lehmannee6fc512010-01-16 18:42:24 +0100859
860 len = strbuf_read(&buf, cp.out, 1024);
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100861 line = buf.buf;
862 while (len > 2) {
863 if ((line[0] == '?') && (line[1] == '?')) {
864 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
865 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
866 break;
867 } else {
868 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
Jens Lehmann3bfc4502010-03-13 23:00:27 +0100869 if (ignore_untracked ||
870 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100871 break;
872 }
873 next_line = strchr(line, '\n');
874 if (!next_line)
875 break;
876 next_line++;
877 len -= (next_line - line);
878 line = next_line;
879 }
Jens Lehmannee6fc512010-01-16 18:42:24 +0100880 close(cp.out);
881
882 if (finish_command(&cp))
Jens Lehmann6a5ceda2011-12-07 22:50:14 +0100883 die("'git status --porcelain' failed in submodule %s", path);
Jens Lehmannee6fc512010-01-16 18:42:24 +0100884
Jens Lehmannee6fc512010-01-16 18:42:24 +0100885 strbuf_release(&buf);
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100886 return dirty_submodule;
Jens Lehmannee6fc512010-01-16 18:42:24 +0100887}
Heiko Voigt68d03e42010-07-07 15:39:13 +0200888
Jens Lehmann293ab152012-09-26 20:21:13 +0200889int submodule_uses_gitfile(const char *path)
890{
891 struct child_process cp;
892 const char *argv[] = {
893 "submodule",
894 "foreach",
895 "--quiet",
896 "--recursive",
897 "test -f .git",
898 NULL,
899 };
900 struct strbuf buf = STRBUF_INIT;
901 const char *git_dir;
902
903 strbuf_addf(&buf, "%s/.git", path);
904 git_dir = read_gitfile(buf.buf);
905 if (!git_dir) {
906 strbuf_release(&buf);
907 return 0;
908 }
909 strbuf_release(&buf);
910
911 /* Now test that all nested submodules use a gitfile too */
912 memset(&cp, 0, sizeof(cp));
913 cp.argv = argv;
914 cp.env = local_repo_env;
915 cp.git_cmd = 1;
916 cp.no_stdin = 1;
917 cp.no_stderr = 1;
918 cp.no_stdout = 1;
919 cp.dir = path;
920 if (run_command(&cp))
921 return 0;
922
923 return 1;
924}
925
926int ok_to_remove_submodule(const char *path)
927{
928 struct stat st;
929 ssize_t len;
930 struct child_process cp;
931 const char *argv[] = {
932 "status",
933 "--porcelain",
934 "-u",
935 "--ignore-submodules=none",
936 NULL,
937 };
938 struct strbuf buf = STRBUF_INIT;
939 int ok_to_remove = 1;
940
941 if ((lstat(path, &st) < 0) || is_empty_dir(path))
942 return 1;
943
944 if (!submodule_uses_gitfile(path))
945 return 0;
946
947 memset(&cp, 0, sizeof(cp));
948 cp.argv = argv;
949 cp.env = local_repo_env;
950 cp.git_cmd = 1;
951 cp.no_stdin = 1;
952 cp.out = -1;
953 cp.dir = path;
954 if (start_command(&cp))
955 die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path);
956
957 len = strbuf_read(&buf, cp.out, 1024);
958 if (len > 2)
959 ok_to_remove = 0;
960 close(cp.out);
961
962 if (finish_command(&cp))
963 die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path);
964
965 strbuf_release(&buf);
966 return ok_to_remove;
967}
968
Heiko Voigt68d03e42010-07-07 15:39:13 +0200969static int find_first_merges(struct object_array *result, const char *path,
970 struct commit *a, struct commit *b)
971{
972 int i, j;
Michael Haggerty38269022013-05-25 11:08:12 +0200973 struct object_array merges = OBJECT_ARRAY_INIT;
Heiko Voigt68d03e42010-07-07 15:39:13 +0200974 struct commit *commit;
975 int contains_another;
976
977 char merged_revision[42];
978 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
979 "--all", merged_revision, NULL };
980 struct rev_info revs;
981 struct setup_revision_opt rev_opts;
982
Heiko Voigt68d03e42010-07-07 15:39:13 +0200983 memset(result, 0, sizeof(struct object_array));
984 memset(&rev_opts, 0, sizeof(rev_opts));
985
986 /* get all revisions that merge commit a */
987 snprintf(merged_revision, sizeof(merged_revision), "^%s",
988 sha1_to_hex(a->object.sha1));
989 init_revisions(&revs, NULL);
990 rev_opts.submodule = path;
991 setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
992
993 /* save all revisions from the above list that contain b */
994 if (prepare_revision_walk(&revs))
995 die("revision walk setup failed");
996 while ((commit = get_revision(&revs)) != NULL) {
997 struct object *o = &(commit->object);
Junio C Hamanoa20efee2012-08-27 14:46:01 -0700998 if (in_merge_bases(b, commit))
Heiko Voigt68d03e42010-07-07 15:39:13 +0200999 add_object_array(o, NULL, &merges);
1000 }
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +02001001 reset_revision_walk();
Heiko Voigt68d03e42010-07-07 15:39:13 +02001002
1003 /* Now we've got all merges that contain a and b. Prune all
1004 * merges that contain another found merge and save them in
1005 * result.
1006 */
1007 for (i = 0; i < merges.nr; i++) {
1008 struct commit *m1 = (struct commit *) merges.objects[i].item;
1009
1010 contains_another = 0;
1011 for (j = 0; j < merges.nr; j++) {
1012 struct commit *m2 = (struct commit *) merges.objects[j].item;
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001013 if (i != j && in_merge_bases(m2, m1)) {
Heiko Voigt68d03e42010-07-07 15:39:13 +02001014 contains_another = 1;
1015 break;
1016 }
1017 }
1018
1019 if (!contains_another)
Michael Haggerty5de0c012013-05-25 11:08:13 +02001020 add_object_array(merges.objects[i].item, NULL, result);
Heiko Voigt68d03e42010-07-07 15:39:13 +02001021 }
1022
1023 free(merges.objects);
1024 return result->nr;
1025}
1026
1027static void print_commit(struct commit *commit)
1028{
1029 struct strbuf sb = STRBUF_INIT;
1030 struct pretty_print_context ctx = {0};
1031 ctx.date_mode = DATE_NORMAL;
1032 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
1033 fprintf(stderr, "%s\n", sb.buf);
1034 strbuf_release(&sb);
1035}
1036
1037#define MERGE_WARNING(path, msg) \
1038 warning("Failed to merge submodule %s (%s)", path, msg);
1039
1040int merge_submodule(unsigned char result[20], const char *path,
1041 const unsigned char base[20], const unsigned char a[20],
Brad King80988782011-10-13 08:59:05 -04001042 const unsigned char b[20], int search)
Heiko Voigt68d03e42010-07-07 15:39:13 +02001043{
1044 struct commit *commit_base, *commit_a, *commit_b;
1045 int parent_count;
1046 struct object_array merges;
1047
1048 int i;
1049
1050 /* store a in result in case we fail */
1051 hashcpy(result, a);
1052
1053 /* we can not handle deletion conflicts */
1054 if (is_null_sha1(base))
1055 return 0;
1056 if (is_null_sha1(a))
1057 return 0;
1058 if (is_null_sha1(b))
1059 return 0;
1060
1061 if (add_submodule_odb(path)) {
1062 MERGE_WARNING(path, "not checked out");
1063 return 0;
1064 }
1065
1066 if (!(commit_base = lookup_commit_reference(base)) ||
1067 !(commit_a = lookup_commit_reference(a)) ||
1068 !(commit_b = lookup_commit_reference(b))) {
1069 MERGE_WARNING(path, "commits not present");
1070 return 0;
1071 }
1072
1073 /* check whether both changes are forward */
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001074 if (!in_merge_bases(commit_base, commit_a) ||
1075 !in_merge_bases(commit_base, commit_b)) {
Heiko Voigt68d03e42010-07-07 15:39:13 +02001076 MERGE_WARNING(path, "commits don't follow merge-base");
1077 return 0;
1078 }
1079
1080 /* Case #1: a is contained in b or vice versa */
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001081 if (in_merge_bases(commit_a, commit_b)) {
Heiko Voigt68d03e42010-07-07 15:39:13 +02001082 hashcpy(result, b);
1083 return 1;
1084 }
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001085 if (in_merge_bases(commit_b, commit_a)) {
Heiko Voigt68d03e42010-07-07 15:39:13 +02001086 hashcpy(result, a);
1087 return 1;
1088 }
1089
1090 /*
1091 * Case #2: There are one or more merges that contain a and b in
1092 * the submodule. If there is only one, then present it as a
1093 * suggestion to the user, but leave it marked unmerged so the
1094 * user needs to confirm the resolution.
1095 */
1096
Brad King80988782011-10-13 08:59:05 -04001097 /* Skip the search if makes no sense to the calling context. */
1098 if (!search)
1099 return 0;
1100
Heiko Voigt68d03e42010-07-07 15:39:13 +02001101 /* find commit which merges them */
1102 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
1103 switch (parent_count) {
1104 case 0:
1105 MERGE_WARNING(path, "merge following commits not found");
1106 break;
1107
1108 case 1:
1109 MERGE_WARNING(path, "not fast-forward");
1110 fprintf(stderr, "Found a possible merge resolution "
1111 "for the submodule:\n");
1112 print_commit((struct commit *) merges.objects[0].item);
1113 fprintf(stderr,
1114 "If this is correct simply add it to the index "
1115 "for example\n"
1116 "by using:\n\n"
1117 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1118 "which will accept this suggestion.\n",
1119 sha1_to_hex(merges.objects[0].item->sha1), path);
1120 break;
1121
1122 default:
1123 MERGE_WARNING(path, "multiple merges found");
1124 for (i = 0; i < merges.nr; i++)
1125 print_commit((struct commit *) merges.objects[i].item);
1126 }
1127
1128 free(merges.objects);
1129 return 0;
1130}
Jens Lehmanna88c9152013-07-30 21:50:03 +02001131
1132/* Update gitfile and core.worktree setting to connect work tree and git dir */
1133void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
1134{
1135 struct strbuf file_name = STRBUF_INIT;
1136 struct strbuf rel_path = STRBUF_INIT;
1137 const char *real_work_tree = xstrdup(real_path(work_tree));
1138 FILE *fp;
1139
1140 /* Update gitfile */
1141 strbuf_addf(&file_name, "%s/.git", work_tree);
1142 fp = fopen(file_name.buf, "w");
1143 if (!fp)
1144 die(_("Could not create git link %s"), file_name.buf);
1145 fprintf(fp, "gitdir: %s\n", relative_path(git_dir, real_work_tree,
1146 &rel_path));
1147 fclose(fp);
1148
1149 /* Update core.worktree setting */
1150 strbuf_reset(&file_name);
1151 strbuf_addf(&file_name, "%s/config", git_dir);
1152 if (git_config_set_in_file(file_name.buf, "core.worktree",
1153 relative_path(real_work_tree, git_dir,
1154 &rel_path)))
1155 die(_("Could not set core.worktree in %s"),
1156 file_name.buf);
1157
1158 strbuf_release(&file_name);
1159 strbuf_release(&rel_path);
1160 free((void *)real_work_tree);
1161}