blob: 62c4356c50d4a41381336559f4e9af27e520ad0d [file] [log] [blame]
Johannes Schindelin752c0c22009-10-19 14:38:32 +02001#include "cache.h"
Heiko Voigt851e18c2015-08-17 17:21:59 -07002#include "submodule-config.h"
Johannes Schindelin752c0c22009-10-19 14:38:32 +02003#include "submodule.h"
4#include "dir.h"
5#include "diff.h"
6#include "commit.h"
7#include "revision.h"
Jens Lehmannee6fc512010-01-16 18:42:24 +01008#include "run-command.h"
Jens Lehmannc7e1a732010-03-04 22:20:33 +01009#include "diffcore.h"
Heiko Voigt68d03e42010-07-07 15:39:13 +020010#include "refs.h"
Jens Lehmannaee9c7d2010-08-06 00:39:25 +020011#include "string-list.h"
Jeff King6859de42011-09-12 15:56:52 -040012#include "sha1-array.h"
Jeff Kingc1189ca2011-09-13 17:57:57 -040013#include "argv-array.h"
Jens Lehmann5fee9952013-07-30 21:50:34 +020014#include "blob.h"
Stefan Bellerfe85ee62015-12-15 16:04:11 -080015#include "thread-utils.h"
Jens Lehmannaee9c7d2010-08-06 00:39:25 +020016
Jens Lehmann88a21972011-03-06 23:10:46 +010017static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
Junio C Hamano2071fb02011-04-04 15:02:01 -070018static struct string_list changed_submodule_paths;
Jeff King6859de42011-09-12 15:56:52 -040019static int initialized_fetch_ref_tips;
20static struct sha1_array ref_tips_before_fetch;
21static struct sha1_array ref_tips_after_fetch;
22
Jens Lehmannd4e98b52011-05-14 18:26:58 +020023/*
24 * The following flag is set if the .gitmodules file is unmerged. We then
25 * disable recursion for all submodules where .git/config doesn't have a
26 * matching config entry because we can't guess what might be configured in
27 * .gitmodules unless the user resolves the conflict. When a command line
28 * option is given (which always overrides configuration) this flag will be
29 * ignored.
30 */
31static int gitmodules_is_unmerged;
Johannes Schindelin752c0c22009-10-19 14:38:32 +020032
Jens Lehmann5fee9952013-07-30 21:50:34 +020033/*
34 * This flag is set if the .gitmodules file had unstaged modifications on
35 * startup. This must be checked before allowing modifications to the
36 * .gitmodules file with the intention to stage them later, because when
37 * continuing we would stage the modifications the user didn't stage herself
38 * too. That might change in a future version when we learn to stage the
39 * changes we do ourselves without staging any previous modifications.
40 */
41static int gitmodules_is_modified;
42
Jens Lehmann5fee9952013-07-30 21:50:34 +020043int is_staging_gitmodules_ok(void)
44{
45 return !gitmodules_is_modified;
46}
47
Jens Lehmann06567812013-08-06 21:15:11 +020048/*
49 * Try to update the "path" entry in the "submodule.<name>" section of the
50 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
51 * with the correct path=<oldpath> setting was found and we could update it.
52 */
53int update_path_in_gitmodules(const char *oldpath, const char *newpath)
54{
55 struct strbuf entry = STRBUF_INIT;
Heiko Voigt851e18c2015-08-17 17:21:59 -070056 const struct submodule *submodule;
Jens Lehmann06567812013-08-06 21:15:11 +020057
58 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
59 return -1;
60
61 if (gitmodules_is_unmerged)
62 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
63
Heiko Voigt851e18c2015-08-17 17:21:59 -070064 submodule = submodule_from_path(null_sha1, oldpath);
65 if (!submodule || !submodule->name) {
Jens Lehmann06567812013-08-06 21:15:11 +020066 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
67 return -1;
68 }
69 strbuf_addstr(&entry, "submodule.");
Heiko Voigt851e18c2015-08-17 17:21:59 -070070 strbuf_addstr(&entry, submodule->name);
Jens Lehmann06567812013-08-06 21:15:11 +020071 strbuf_addstr(&entry, ".path");
Patrick Steinhardt30598ad2016-02-22 12:23:35 +010072 if (git_config_set_in_file_gently(".gitmodules", entry.buf, newpath) < 0) {
Jens Lehmann06567812013-08-06 21:15:11 +020073 /* Maybe the user already did that, don't error out here */
74 warning(_("Could not update .gitmodules entry %s"), entry.buf);
75 strbuf_release(&entry);
76 return -1;
77 }
78 strbuf_release(&entry);
79 return 0;
80}
81
Jens Lehmann95c16412013-08-06 21:15:25 +020082/*
83 * Try to remove the "submodule.<name>" section from .gitmodules where the given
84 * path is configured. Return 0 only if a .gitmodules file was found, a section
85 * with the correct path=<path> setting was found and we could remove it.
86 */
87int remove_path_from_gitmodules(const char *path)
88{
89 struct strbuf sect = STRBUF_INIT;
Heiko Voigt851e18c2015-08-17 17:21:59 -070090 const struct submodule *submodule;
Jens Lehmann95c16412013-08-06 21:15:25 +020091
92 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
93 return -1;
94
95 if (gitmodules_is_unmerged)
96 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
97
Heiko Voigt851e18c2015-08-17 17:21:59 -070098 submodule = submodule_from_path(null_sha1, path);
99 if (!submodule || !submodule->name) {
Jens Lehmann95c16412013-08-06 21:15:25 +0200100 warning(_("Could not find section in .gitmodules where path=%s"), path);
101 return -1;
102 }
103 strbuf_addstr(&sect, "submodule.");
Heiko Voigt851e18c2015-08-17 17:21:59 -0700104 strbuf_addstr(&sect, submodule->name);
Jens Lehmann95c16412013-08-06 21:15:25 +0200105 if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) {
106 /* Maybe the user already did that, don't error out here */
107 warning(_("Could not remove .gitmodules entry for %s"), path);
108 strbuf_release(&sect);
109 return -1;
110 }
111 strbuf_release(&sect);
112 return 0;
113}
114
Jens Lehmann5fee9952013-07-30 21:50:34 +0200115void stage_updated_gitmodules(void)
116{
Jens Lehmannbc8d6b92013-11-07 15:33:43 +0100117 if (add_file_to_cache(".gitmodules", 0))
Jens Lehmann5fee9952013-07-30 21:50:34 +0200118 die(_("staging updated .gitmodules failed"));
119}
120
Junio C Hamanocb58c932010-01-11 22:31:58 -0800121static int add_submodule_odb(const char *path)
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200122{
123 struct strbuf objects_directory = STRBUF_INIT;
124 struct alternate_object_database *alt_odb;
Jens Lehmannde7a7962010-01-31 17:43:49 +0100125 int ret = 0;
Jeff King50a6c8e2016-02-22 17:44:35 -0500126 size_t alloc;
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200127
Max Kirillov35fb4d22015-09-14 01:17:41 +0300128 strbuf_git_path_submodule(&objects_directory, path, "objects/");
Jens Lehmannde7a7962010-01-31 17:43:49 +0100129 if (!is_directory(objects_directory.buf)) {
130 ret = -1;
131 goto done;
132 }
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200133 /* avoid adding it twice */
Jeff King9a6e4f02015-10-28 10:07:25 -0400134 prepare_alt_odb();
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200135 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
136 if (alt_odb->name - alt_odb->base == objects_directory.len &&
137 !strncmp(alt_odb->base, objects_directory.buf,
138 objects_directory.len))
Jens Lehmannde7a7962010-01-31 17:43:49 +0100139 goto done;
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200140
Jeff King50a6c8e2016-02-22 17:44:35 -0500141 alloc = st_add(objects_directory.len, 42); /* for "12/345..." sha1 */
142 alt_odb = xmalloc(st_add(sizeof(*alt_odb), alloc));
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200143 alt_odb->next = alt_odb_list;
Jeff Kingc7ab0ba2015-09-24 17:08:12 -0400144 xsnprintf(alt_odb->base, alloc, "%s", objects_directory.buf);
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200145 alt_odb->name = alt_odb->base + objects_directory.len;
146 alt_odb->name[2] = '/';
147 alt_odb->name[40] = '\0';
148 alt_odb->name[41] = '\0';
149 alt_odb_list = alt_odb;
Heiko Voigt5e736332012-05-14 18:24:45 +0200150
151 /* add possible alternates from the submodule */
152 read_info_alternates(objects_directory.buf, 0);
Jens Lehmannde7a7962010-01-31 17:43:49 +0100153done:
154 strbuf_release(&objects_directory);
155 return ret;
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200156}
157
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200158void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
159 const char *path)
160{
Heiko Voigt851e18c2015-08-17 17:21:59 -0700161 const struct submodule *submodule = submodule_from_path(null_sha1, path);
162 if (submodule) {
163 if (submodule->ignore)
164 handle_ignore_submodules_arg(diffopt, submodule->ignore);
Jens Lehmannd4e98b52011-05-14 18:26:58 +0200165 else if (gitmodules_is_unmerged)
166 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200167 }
168}
169
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100170int submodule_config(const char *var, const char *value, void *cb)
Jens Lehmann302ad7a2010-08-06 00:40:48 +0200171{
Christian Couder59556542013-11-30 21:55:40 +0100172 if (starts_with(var, "submodule."))
Jens Lehmann302ad7a2010-08-06 00:40:48 +0200173 return parse_submodule_config_option(var, value);
Jens Lehmannbe254a02010-11-11 00:55:02 +0100174 else if (!strcmp(var, "fetch.recursesubmodules")) {
Jens Lehmann1fb25502011-03-06 23:11:48 +0100175 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
Jens Lehmannbe254a02010-11-11 00:55:02 +0100176 return 0;
177 }
Jens Lehmann302ad7a2010-08-06 00:40:48 +0200178 return 0;
179}
180
181void gitmodules_config(void)
182{
183 const char *work_tree = get_git_work_tree();
184 if (work_tree) {
185 struct strbuf gitmodules_path = STRBUF_INIT;
Jens Lehmannd4e98b52011-05-14 18:26:58 +0200186 int pos;
Jens Lehmann302ad7a2010-08-06 00:40:48 +0200187 strbuf_addstr(&gitmodules_path, work_tree);
188 strbuf_addstr(&gitmodules_path, "/.gitmodules");
Jens Lehmannd4e98b52011-05-14 18:26:58 +0200189 if (read_cache() < 0)
190 die("index file corrupt");
191 pos = cache_name_pos(".gitmodules", 11);
192 if (pos < 0) { /* .gitmodules not found or isn't merged */
193 pos = -1 - pos;
194 if (active_nr > pos) { /* there is a .gitmodules */
195 const struct cache_entry *ce = active_cache[pos];
196 if (ce_namelen(ce) == 11 &&
197 !memcmp(ce->name, ".gitmodules", 11))
198 gitmodules_is_unmerged = 1;
199 }
Jens Lehmann5fee9952013-07-30 21:50:34 +0200200 } else if (pos < active_nr) {
201 struct stat st;
202 if (lstat(".gitmodules", &st) == 0 &&
203 ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED)
204 gitmodules_is_modified = 1;
Jens Lehmannd4e98b52011-05-14 18:26:58 +0200205 }
206
207 if (!gitmodules_is_unmerged)
208 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
Jens Lehmann302ad7a2010-08-06 00:40:48 +0200209 strbuf_release(&gitmodules_path);
210 }
211}
212
Jens Lehmann46a958b2010-06-25 16:56:47 +0200213void handle_ignore_submodules_arg(struct diff_options *diffopt,
214 const char *arg)
215{
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +0200216 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
217 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
218 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
219
Jens Lehmann46a958b2010-06-25 16:56:47 +0200220 if (!strcmp(arg, "all"))
221 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
222 else if (!strcmp(arg, "untracked"))
223 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
224 else if (!strcmp(arg, "dirty"))
225 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200226 else if (strcmp(arg, "none"))
Jens Lehmann46a958b2010-06-25 16:56:47 +0200227 die("bad --ignore-submodules argument: %s", arg);
228}
229
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500230static int prepare_submodule_summary(struct rev_info *rev, const char *path,
231 struct commit *left, struct commit *right,
232 int *fast_forward, int *fast_backward)
233{
234 struct commit_list *merge_bases, *list;
235
236 init_revisions(rev, NULL);
237 setup_revisions(0, NULL, rev, NULL);
238 rev->left_right = 1;
239 rev->first_parent_only = 1;
240 left->object.flags |= SYMMETRIC_LEFT;
241 add_pending_object(rev, &left->object, path);
242 add_pending_object(rev, &right->object, path);
Junio C Hamano2ce406c2014-10-30 12:20:44 -0700243 merge_bases = get_merge_bases(left, right);
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500244 if (merge_bases) {
245 if (merge_bases->item == left)
246 *fast_forward = 1;
247 else if (merge_bases->item == right)
248 *fast_backward = 1;
249 }
250 for (list = merge_bases; list; list = list->next) {
251 list->item->object.flags |= UNINTERESTING;
252 add_pending_object(rev, &list->item->object,
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000253 oid_to_hex(&list->item->object.oid));
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500254 }
255 return prepare_revision_walk(rev);
256}
257
258static void print_submodule_summary(struct rev_info *rev, FILE *f,
John Keeping0f33a062013-04-05 17:12:08 +0100259 const char *line_prefix,
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500260 const char *del, const char *add, const char *reset)
261{
262 static const char format[] = " %m %s";
263 struct strbuf sb = STRBUF_INIT;
264 struct commit *commit;
265
266 while ((commit = get_revision(rev))) {
267 struct pretty_print_context ctx = {0};
268 ctx.date_mode = rev->date_mode;
Alexey Shumkinecaee802013-06-26 14:19:50 +0400269 ctx.output_encoding = get_log_output_encoding();
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500270 strbuf_setlen(&sb, 0);
John Keeping0f33a062013-04-05 17:12:08 +0100271 strbuf_addstr(&sb, line_prefix);
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500272 if (commit->object.flags & SYMMETRIC_LEFT) {
273 if (del)
274 strbuf_addstr(&sb, del);
275 }
276 else if (add)
277 strbuf_addstr(&sb, add);
278 format_commit_message(commit, format, &sb, &ctx);
279 if (reset)
280 strbuf_addstr(&sb, reset);
281 strbuf_addch(&sb, '\n');
282 fprintf(f, "%s", sb.buf);
283 }
284 strbuf_release(&sb);
285}
286
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200287void show_submodule_summary(FILE *f, const char *path,
John Keeping0f33a062013-04-05 17:12:08 +0100288 const char *line_prefix,
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200289 unsigned char one[20], unsigned char two[20],
Ramkumar Ramachandra4e215132012-11-13 21:12:47 +0530290 unsigned dirty_submodule, const char *meta,
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200291 const char *del, const char *add, const char *reset)
292{
293 struct rev_info rev;
Jeff King83715492013-03-22 12:19:56 -0400294 struct commit *left = NULL, *right = NULL;
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200295 const char *message = NULL;
296 struct strbuf sb = STRBUF_INIT;
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200297 int fast_forward = 0, fast_backward = 0;
298
299 if (is_null_sha1(two))
300 message = "(submodule deleted)";
301 else if (add_submodule_odb(path))
302 message = "(not checked out)";
303 else if (is_null_sha1(one))
304 message = "(new submodule)";
305 else if (!(left = lookup_commit_reference(one)) ||
306 !(right = lookup_commit_reference(two)))
307 message = "(commits not present)";
Jeff King83715492013-03-22 12:19:56 -0400308 else if (prepare_submodule_summary(&rev, path, left, right,
309 &fast_forward, &fast_backward))
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500310 message = "(revision walker failed)";
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200311
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100312 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
John Keeping0f33a062013-04-05 17:12:08 +0100313 fprintf(f, "%sSubmodule %s contains untracked content\n",
314 line_prefix, path);
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100315 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
John Keeping0f33a062013-04-05 17:12:08 +0100316 fprintf(f, "%sSubmodule %s contains modified content\n",
317 line_prefix, path);
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100318
319 if (!hashcmp(one, two)) {
320 strbuf_release(&sb);
321 return;
322 }
323
John Keeping0f33a062013-04-05 17:12:08 +0100324 strbuf_addf(&sb, "%s%sSubmodule %s %s..", line_prefix, meta, path,
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200325 find_unique_abbrev(one, DEFAULT_ABBREV));
326 if (!fast_backward && !fast_forward)
327 strbuf_addch(&sb, '.');
328 strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
329 if (message)
Ramkumar Ramachandra4e215132012-11-13 21:12:47 +0530330 strbuf_addf(&sb, " %s%s\n", message, reset);
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200331 else
Ramkumar Ramachandra4e215132012-11-13 21:12:47 +0530332 strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200333 fwrite(sb.buf, sb.len, 1, f);
334
Jeff King83715492013-03-22 12:19:56 -0400335 if (!message) /* only NULL if we succeeded in setting up the walk */
John Keeping0f33a062013-04-05 17:12:08 +0100336 print_submodule_summary(&rev, f, line_prefix, del, add, reset);
Jeff King83715492013-03-22 12:19:56 -0400337 if (left)
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200338 clear_commit_marks(left, ~0);
Jeff King83715492013-03-22 12:19:56 -0400339 if (right)
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200340 clear_commit_marks(right, ~0);
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500341
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200342 strbuf_release(&sb);
343}
Jens Lehmannee6fc512010-01-16 18:42:24 +0100344
Jens Lehmannbe254a02010-11-11 00:55:02 +0100345void set_config_fetch_recurse_submodules(int value)
346{
347 config_fetch_recurse_submodules = value;
348}
349
Michael Haggerty7290ef52015-05-25 18:39:07 +0000350static int has_remote(const char *refname, const struct object_id *oid,
351 int flags, void *cb_data)
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200352{
353 return 1;
354}
355
356static int submodule_needs_pushing(const char *path, const unsigned char sha1[20])
357{
358 if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
359 return 0;
360
361 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
René Scharfed3180272014-08-19 21:09:35 +0200362 struct child_process cp = CHILD_PROCESS_INIT;
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200363 const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL};
364 struct strbuf buf = STRBUF_INIT;
365 int needs_pushing = 0;
366
367 argv[1] = sha1_to_hex(sha1);
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200368 cp.argv = argv;
369 cp.env = local_repo_env;
370 cp.git_cmd = 1;
371 cp.no_stdin = 1;
372 cp.out = -1;
373 cp.dir = path;
374 if (start_command(&cp))
375 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
376 sha1_to_hex(sha1), path);
377 if (strbuf_read(&buf, cp.out, 41))
378 needs_pushing = 1;
379 finish_command(&cp);
380 close(cp.out);
381 strbuf_release(&buf);
382 return needs_pushing;
383 }
384
385 return 0;
386}
387
388static void collect_submodules_from_diff(struct diff_queue_struct *q,
389 struct diff_options *options,
390 void *data)
391{
392 int i;
Heiko Voigta762e512012-03-29 09:21:23 +0200393 struct string_list *needs_pushing = data;
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200394
395 for (i = 0; i < q->nr; i++) {
396 struct diff_filepair *p = q->queue[i];
397 if (!S_ISGITLINK(p->two->mode))
398 continue;
Heiko Voigta762e512012-03-29 09:21:23 +0200399 if (submodule_needs_pushing(p->two->path, p->two->sha1))
400 string_list_insert(needs_pushing, p->two->path);
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200401 }
402}
403
Heiko Voigta762e512012-03-29 09:21:23 +0200404static void find_unpushed_submodule_commits(struct commit *commit,
405 struct string_list *needs_pushing)
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200406{
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200407 struct rev_info rev;
408
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200409 init_revisions(&rev, NULL);
410 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
411 rev.diffopt.format_callback = collect_submodules_from_diff;
412 rev.diffopt.format_callback_data = needs_pushing;
René Scharfe78e98ea2011-12-17 11:27:19 +0100413 diff_tree_combined_merge(commit, 1, &rev);
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200414}
415
Heiko Voigta762e512012-03-29 09:21:23 +0200416int find_unpushed_submodules(unsigned char new_sha1[20],
417 const char *remotes_name, struct string_list *needs_pushing)
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200418{
419 struct rev_info rev;
420 struct commit *commit;
421 const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
422 int argc = ARRAY_SIZE(argv) - 1;
423 char *sha1_copy;
Heiko Voigta762e512012-03-29 09:21:23 +0200424
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200425 struct strbuf remotes_arg = STRBUF_INIT;
426
427 strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);
428 init_revisions(&rev, NULL);
429 sha1_copy = xstrdup(sha1_to_hex(new_sha1));
430 argv[1] = sha1_copy;
431 argv[3] = remotes_arg.buf;
432 setup_revisions(argc, argv, &rev, NULL);
433 if (prepare_revision_walk(&rev))
434 die("revision walk setup failed");
435
Heiko Voigta762e512012-03-29 09:21:23 +0200436 while ((commit = get_revision(&rev)) != NULL)
437 find_unpushed_submodule_commits(commit, needs_pushing);
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200438
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +0200439 reset_revision_walk();
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200440 free(sha1_copy);
441 strbuf_release(&remotes_arg);
442
Heiko Voigta762e512012-03-29 09:21:23 +0200443 return needs_pushing->nr;
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200444}
445
Heiko Voigteb21c732012-03-29 09:21:24 +0200446static int push_submodule(const char *path)
447{
448 if (add_submodule_odb(path))
449 return 1;
450
451 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
René Scharfed3180272014-08-19 21:09:35 +0200452 struct child_process cp = CHILD_PROCESS_INIT;
Heiko Voigteb21c732012-03-29 09:21:24 +0200453 const char *argv[] = {"push", NULL};
454
Heiko Voigteb21c732012-03-29 09:21:24 +0200455 cp.argv = argv;
456 cp.env = local_repo_env;
457 cp.git_cmd = 1;
458 cp.no_stdin = 1;
459 cp.dir = path;
460 if (run_command(&cp))
461 return 0;
462 close(cp.out);
463 }
464
465 return 1;
466}
467
468int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name)
469{
470 int i, ret = 1;
Tanay Abhraf93d7c62014-07-18 02:19:00 -0700471 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
Heiko Voigteb21c732012-03-29 09:21:24 +0200472
473 if (!find_unpushed_submodules(new_sha1, remotes_name, &needs_pushing))
474 return 1;
475
476 for (i = 0; i < needs_pushing.nr; i++) {
477 const char *path = needs_pushing.items[i].string;
478 fprintf(stderr, "Pushing submodule '%s'\n", path);
479 if (!push_submodule(path)) {
480 fprintf(stderr, "Unable to push submodule '%s'\n", path);
481 ret = 0;
482 }
483 }
484
485 string_list_clear(&needs_pushing, 0);
486
487 return ret;
488}
489
Jens Lehmannc16c3e42011-03-06 23:12:58 +0100490static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
491{
492 int is_present = 0;
493 if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
494 /* Even if the submodule is checked out and the commit is
495 * present, make sure it is reachable from a ref. */
René Scharfed3180272014-08-19 21:09:35 +0200496 struct child_process cp = CHILD_PROCESS_INIT;
Jens Lehmannc16c3e42011-03-06 23:12:58 +0100497 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
498 struct strbuf buf = STRBUF_INIT;
499
500 argv[3] = sha1_to_hex(sha1);
Jens Lehmannc16c3e42011-03-06 23:12:58 +0100501 cp.argv = argv;
502 cp.env = local_repo_env;
503 cp.git_cmd = 1;
504 cp.no_stdin = 1;
Jens Lehmannc16c3e42011-03-06 23:12:58 +0100505 cp.dir = path;
Jeff King1d4974c2015-03-22 23:53:56 -0400506 if (!capture_command(&cp, &buf, 1024) && !buf.len)
Jens Lehmannc16c3e42011-03-06 23:12:58 +0100507 is_present = 1;
508
Jens Lehmannc16c3e42011-03-06 23:12:58 +0100509 strbuf_release(&buf);
510 }
511 return is_present;
512}
513
Jens Lehmann88a21972011-03-06 23:10:46 +0100514static void submodule_collect_changed_cb(struct diff_queue_struct *q,
515 struct diff_options *options,
516 void *data)
517{
518 int i;
519 for (i = 0; i < q->nr; i++) {
520 struct diff_filepair *p = q->queue[i];
521 if (!S_ISGITLINK(p->two->mode))
522 continue;
523
524 if (S_ISGITLINK(p->one->mode)) {
525 /* NEEDSWORK: We should honor the name configured in
526 * the .gitmodules file of the commit we are examining
527 * here to be able to correctly follow submodules
528 * being moved around. */
529 struct string_list_item *path;
530 path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
Jens Lehmannc16c3e42011-03-06 23:12:58 +0100531 if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
Jens Lehmann88a21972011-03-06 23:10:46 +0100532 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
533 } else {
534 /* Submodule is new or was moved here */
535 /* NEEDSWORK: When the .git directories of submodules
536 * live inside the superprojects .git directory some
537 * day we should fetch new submodules directly into
538 * that location too when config or options request
539 * that so they can be checked out from there. */
540 continue;
541 }
542 }
543}
544
Michael Haggerty7290ef52015-05-25 18:39:07 +0000545static int add_sha1_to_array(const char *ref, const struct object_id *oid,
Jeff King6859de42011-09-12 15:56:52 -0400546 int flags, void *data)
547{
Michael Haggerty7290ef52015-05-25 18:39:07 +0000548 sha1_array_append(data, oid->hash);
Jeff King6859de42011-09-12 15:56:52 -0400549 return 0;
550}
551
Jens Lehmann88a21972011-03-06 23:10:46 +0100552void check_for_new_submodule_commits(unsigned char new_sha1[20])
553{
Jeff King6859de42011-09-12 15:56:52 -0400554 if (!initialized_fetch_ref_tips) {
555 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
556 initialized_fetch_ref_tips = 1;
557 }
558
559 sha1_array_append(&ref_tips_after_fetch, new_sha1);
560}
561
Jeff King6859de42011-09-12 15:56:52 -0400562static void add_sha1_to_argv(const unsigned char sha1[20], void *data)
563{
Jeff Kingc1189ca2011-09-13 17:57:57 -0400564 argv_array_push(data, sha1_to_hex(sha1));
Jeff King6859de42011-09-12 15:56:52 -0400565}
566
567static void calculate_changed_submodule_paths(void)
568{
Jens Lehmann88a21972011-03-06 23:10:46 +0100569 struct rev_info rev;
570 struct commit *commit;
Jeff Kingc1189ca2011-09-13 17:57:57 -0400571 struct argv_array argv = ARGV_ARRAY_INIT;
Jens Lehmann88a21972011-03-06 23:10:46 +0100572
Jens Lehmann18322ba2011-09-09 20:22:03 +0200573 /* No need to check if there are no submodules configured */
Heiko Voigt851e18c2015-08-17 17:21:59 -0700574 if (!submodule_from_path(NULL, NULL))
Jens Lehmann18322ba2011-09-09 20:22:03 +0200575 return;
576
Jens Lehmann88a21972011-03-06 23:10:46 +0100577 init_revisions(&rev, NULL);
Jeff Kingc1189ca2011-09-13 17:57:57 -0400578 argv_array_push(&argv, "--"); /* argv[0] program name */
Jeff King6859de42011-09-12 15:56:52 -0400579 sha1_array_for_each_unique(&ref_tips_after_fetch,
580 add_sha1_to_argv, &argv);
Jeff Kingc1189ca2011-09-13 17:57:57 -0400581 argv_array_push(&argv, "--not");
Jeff King6859de42011-09-12 15:56:52 -0400582 sha1_array_for_each_unique(&ref_tips_before_fetch,
583 add_sha1_to_argv, &argv);
584 setup_revisions(argv.argc, argv.argv, &rev, NULL);
Jens Lehmann88a21972011-03-06 23:10:46 +0100585 if (prepare_revision_walk(&rev))
586 die("revision walk setup failed");
587
588 /*
589 * Collect all submodules (whether checked out or not) for which new
590 * commits have been recorded upstream in "changed_submodule_paths".
591 */
592 while ((commit = get_revision(&rev))) {
593 struct commit_list *parent = commit->parents;
594 while (parent) {
595 struct diff_options diff_opts;
596 diff_setup(&diff_opts);
Jens Lehmannea2d3252011-06-20 20:18:03 +0200597 DIFF_OPT_SET(&diff_opts, RECURSIVE);
Jens Lehmann88a21972011-03-06 23:10:46 +0100598 diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
599 diff_opts.format_callback = submodule_collect_changed_cb;
Thomas Rast28452652012-08-03 14:16:24 +0200600 diff_setup_done(&diff_opts);
brian m. carlsoned1c9972015-11-10 02:22:29 +0000601 diff_tree_sha1(parent->item->object.oid.hash, commit->object.oid.hash, "", &diff_opts);
Jens Lehmann88a21972011-03-06 23:10:46 +0100602 diffcore_std(&diff_opts);
603 diff_flush(&diff_opts);
604 parent = parent->next;
605 }
606 }
Jeff King6859de42011-09-12 15:56:52 -0400607
Jeff Kingc1189ca2011-09-13 17:57:57 -0400608 argv_array_clear(&argv);
Jeff King6859de42011-09-12 15:56:52 -0400609 sha1_array_clear(&ref_tips_before_fetch);
610 sha1_array_clear(&ref_tips_after_fetch);
611 initialized_fetch_ref_tips = 0;
Jens Lehmann88a21972011-03-06 23:10:46 +0100612}
613
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800614struct submodule_parallel_fetch {
615 int count;
616 struct argv_array args;
617 const char *work_tree;
618 const char *prefix;
619 int command_line_option;
620 int quiet;
621 int result;
622};
623#define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0}
624
625static int get_next_submodule(struct child_process *cp,
626 struct strbuf *err, void *data, void **task_cb)
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100627{
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800628 int ret = 0;
629 struct submodule_parallel_fetch *spf = data;
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100630
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800631 for (; spf->count < active_nr; spf->count++) {
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100632 struct strbuf submodule_path = STRBUF_INIT;
633 struct strbuf submodule_git_dir = STRBUF_INIT;
634 struct strbuf submodule_prefix = STRBUF_INIT;
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800635 const struct cache_entry *ce = active_cache[spf->count];
Heiko Voigt851e18c2015-08-17 17:21:59 -0700636 const char *git_dir, *default_argv;
637 const struct submodule *submodule;
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100638
639 if (!S_ISGITLINK(ce->ce_mode))
640 continue;
641
Heiko Voigt851e18c2015-08-17 17:21:59 -0700642 submodule = submodule_from_path(null_sha1, ce->name);
643 if (!submodule)
644 submodule = submodule_from_name(null_sha1, ce->name);
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100645
Jens Lehmann88a21972011-03-06 23:10:46 +0100646 default_argv = "yes";
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800647 if (spf->command_line_option == RECURSE_SUBMODULES_DEFAULT) {
Heiko Voigt851e18c2015-08-17 17:21:59 -0700648 if (submodule &&
649 submodule->fetch_recurse !=
650 RECURSE_SUBMODULES_NONE) {
651 if (submodule->fetch_recurse ==
652 RECURSE_SUBMODULES_OFF)
Jens Lehmannc1a3c362010-11-11 00:55:41 +0100653 continue;
Heiko Voigt851e18c2015-08-17 17:21:59 -0700654 if (submodule->fetch_recurse ==
655 RECURSE_SUBMODULES_ON_DEMAND) {
Jens Lehmannbf42b382011-03-06 23:12:19 +0100656 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
657 continue;
658 default_argv = "on-demand";
659 }
Jens Lehmannc1a3c362010-11-11 00:55:41 +0100660 } else {
Jens Lehmannd4e98b52011-05-14 18:26:58 +0200661 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
662 gitmodules_is_unmerged)
Jens Lehmannc1a3c362010-11-11 00:55:41 +0100663 continue;
Jens Lehmann88a21972011-03-06 23:10:46 +0100664 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
665 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
666 continue;
667 default_argv = "on-demand";
668 }
Jens Lehmannc1a3c362010-11-11 00:55:41 +0100669 }
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800670 } else if (spf->command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
Jens Lehmann8f0700d2011-03-06 23:11:21 +0100671 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
672 continue;
673 default_argv = "on-demand";
Jens Lehmannbe254a02010-11-11 00:55:02 +0100674 }
675
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800676 strbuf_addf(&submodule_path, "%s/%s", spf->work_tree, ce->name);
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100677 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800678 strbuf_addf(&submodule_prefix, "%s%s/", spf->prefix, ce->name);
Junio C Hamano13d6ec92011-08-22 14:04:56 -0700679 git_dir = read_gitfile(submodule_git_dir.buf);
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100680 if (!git_dir)
681 git_dir = submodule_git_dir.buf;
682 if (is_directory(git_dir)) {
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800683 child_process_init(cp);
684 cp->dir = strbuf_detach(&submodule_path, NULL);
685 cp->env = local_repo_env;
686 cp->git_cmd = 1;
687 if (!spf->quiet)
688 strbuf_addf(err, "Fetching submodule %s%s\n",
689 spf->prefix, ce->name);
690 argv_array_init(&cp->args);
691 argv_array_pushv(&cp->args, spf->args.argv);
692 argv_array_push(&cp->args, default_argv);
693 argv_array_push(&cp->args, "--submodule-prefix");
694 argv_array_push(&cp->args, submodule_prefix.buf);
695 ret = 1;
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100696 }
697 strbuf_release(&submodule_path);
698 strbuf_release(&submodule_git_dir);
699 strbuf_release(&submodule_prefix);
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800700 if (ret) {
701 spf->count++;
702 return 1;
703 }
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100704 }
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800705 return 0;
706}
707
Stefan Beller2a73b3d2016-02-29 13:57:06 -0800708static int fetch_start_failure(struct strbuf *err,
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800709 void *cb, void *task_cb)
710{
711 struct submodule_parallel_fetch *spf = cb;
712
713 spf->result = 1;
714
715 return 0;
716}
717
Stefan Beller2a73b3d2016-02-29 13:57:06 -0800718static int fetch_finish(int retvalue, struct strbuf *err,
719 void *cb, void *task_cb)
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800720{
721 struct submodule_parallel_fetch *spf = cb;
722
723 if (retvalue)
724 spf->result = 1;
725
726 return 0;
727}
728
729int fetch_populated_submodules(const struct argv_array *options,
730 const char *prefix, int command_line_option,
Stefan Beller62104ba2015-12-15 16:04:12 -0800731 int quiet, int max_parallel_jobs)
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800732{
733 int i;
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800734 struct submodule_parallel_fetch spf = SPF_INIT;
735
736 spf.work_tree = get_git_work_tree();
737 spf.command_line_option = command_line_option;
738 spf.quiet = quiet;
739 spf.prefix = prefix;
740
741 if (!spf.work_tree)
742 goto out;
743
744 if (read_cache() < 0)
745 die("index file corrupt");
746
747 argv_array_push(&spf.args, "fetch");
748 for (i = 0; i < options->argc; i++)
749 argv_array_push(&spf.args, options->argv[i]);
750 argv_array_push(&spf.args, "--recurse-submodules-default");
751 /* default value, "--submodule-prefix" and its value are added later */
752
753 calculate_changed_submodule_paths();
754 run_processes_parallel(max_parallel_jobs,
755 get_next_submodule,
756 fetch_start_failure,
757 fetch_finish,
758 &spf);
759
760 argv_array_clear(&spf.args);
Jens Lehmann88a21972011-03-06 23:10:46 +0100761out:
762 string_list_clear(&changed_submodule_paths, 1);
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800763 return spf.result;
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100764}
765
Jens Lehmann3bfc4502010-03-13 23:00:27 +0100766unsigned is_submodule_modified(const char *path, int ignore_untracked)
Jens Lehmannee6fc512010-01-16 18:42:24 +0100767{
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100768 ssize_t len;
René Scharfed3180272014-08-19 21:09:35 +0200769 struct child_process cp = CHILD_PROCESS_INIT;
Jens Lehmannee6fc512010-01-16 18:42:24 +0100770 const char *argv[] = {
771 "status",
772 "--porcelain",
773 NULL,
Jens Lehmann3bfc4502010-03-13 23:00:27 +0100774 NULL,
Jens Lehmannee6fc512010-01-16 18:42:24 +0100775 };
Jens Lehmannee6fc512010-01-16 18:42:24 +0100776 struct strbuf buf = STRBUF_INIT;
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100777 unsigned dirty_submodule = 0;
778 const char *line, *next_line;
Jens Lehmanneee49b62010-04-10 19:01:12 +0200779 const char *git_dir;
Jens Lehmannee6fc512010-01-16 18:42:24 +0100780
Jens Lehmanneee49b62010-04-10 19:01:12 +0200781 strbuf_addf(&buf, "%s/.git", path);
Junio C Hamano13d6ec92011-08-22 14:04:56 -0700782 git_dir = read_gitfile(buf.buf);
Jens Lehmanneee49b62010-04-10 19:01:12 +0200783 if (!git_dir)
784 git_dir = buf.buf;
785 if (!is_directory(git_dir)) {
Jens Lehmannee6fc512010-01-16 18:42:24 +0100786 strbuf_release(&buf);
787 /* The submodule is not checked out, so it is not modified */
788 return 0;
789
790 }
791 strbuf_reset(&buf);
792
Jens Lehmann3bfc4502010-03-13 23:00:27 +0100793 if (ignore_untracked)
794 argv[2] = "-uno";
795
Jens Lehmannee6fc512010-01-16 18:42:24 +0100796 cp.argv = argv;
Jens Lehmanneee49b62010-04-10 19:01:12 +0200797 cp.env = local_repo_env;
Jens Lehmannee6fc512010-01-16 18:42:24 +0100798 cp.git_cmd = 1;
799 cp.no_stdin = 1;
800 cp.out = -1;
Jens Lehmanneee49b62010-04-10 19:01:12 +0200801 cp.dir = path;
Jens Lehmannee6fc512010-01-16 18:42:24 +0100802 if (start_command(&cp))
Jens Lehmann6a5ceda2011-12-07 22:50:14 +0100803 die("Could not run 'git status --porcelain' in submodule %s", path);
Jens Lehmannee6fc512010-01-16 18:42:24 +0100804
805 len = strbuf_read(&buf, cp.out, 1024);
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100806 line = buf.buf;
807 while (len > 2) {
808 if ((line[0] == '?') && (line[1] == '?')) {
809 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
810 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
811 break;
812 } else {
813 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
Jens Lehmann3bfc4502010-03-13 23:00:27 +0100814 if (ignore_untracked ||
815 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100816 break;
817 }
818 next_line = strchr(line, '\n');
819 if (!next_line)
820 break;
821 next_line++;
822 len -= (next_line - line);
823 line = next_line;
824 }
Jens Lehmannee6fc512010-01-16 18:42:24 +0100825 close(cp.out);
826
827 if (finish_command(&cp))
Jens Lehmann6a5ceda2011-12-07 22:50:14 +0100828 die("'git status --porcelain' failed in submodule %s", path);
Jens Lehmannee6fc512010-01-16 18:42:24 +0100829
Jens Lehmannee6fc512010-01-16 18:42:24 +0100830 strbuf_release(&buf);
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100831 return dirty_submodule;
Jens Lehmannee6fc512010-01-16 18:42:24 +0100832}
Heiko Voigt68d03e42010-07-07 15:39:13 +0200833
Jens Lehmann293ab152012-09-26 20:21:13 +0200834int submodule_uses_gitfile(const char *path)
835{
René Scharfed3180272014-08-19 21:09:35 +0200836 struct child_process cp = CHILD_PROCESS_INIT;
Jens Lehmann293ab152012-09-26 20:21:13 +0200837 const char *argv[] = {
838 "submodule",
839 "foreach",
840 "--quiet",
841 "--recursive",
842 "test -f .git",
843 NULL,
844 };
845 struct strbuf buf = STRBUF_INIT;
846 const char *git_dir;
847
848 strbuf_addf(&buf, "%s/.git", path);
849 git_dir = read_gitfile(buf.buf);
850 if (!git_dir) {
851 strbuf_release(&buf);
852 return 0;
853 }
854 strbuf_release(&buf);
855
856 /* Now test that all nested submodules use a gitfile too */
Jens Lehmann293ab152012-09-26 20:21:13 +0200857 cp.argv = argv;
858 cp.env = local_repo_env;
859 cp.git_cmd = 1;
860 cp.no_stdin = 1;
861 cp.no_stderr = 1;
862 cp.no_stdout = 1;
863 cp.dir = path;
864 if (run_command(&cp))
865 return 0;
866
867 return 1;
868}
869
870int ok_to_remove_submodule(const char *path)
871{
Jens Lehmann293ab152012-09-26 20:21:13 +0200872 ssize_t len;
René Scharfed3180272014-08-19 21:09:35 +0200873 struct child_process cp = CHILD_PROCESS_INIT;
Jens Lehmann293ab152012-09-26 20:21:13 +0200874 const char *argv[] = {
875 "status",
876 "--porcelain",
877 "-u",
878 "--ignore-submodules=none",
879 NULL,
880 };
881 struct strbuf buf = STRBUF_INIT;
882 int ok_to_remove = 1;
883
René Scharfedbe44fa2015-05-19 23:44:23 +0200884 if (!file_exists(path) || is_empty_dir(path))
Jens Lehmann293ab152012-09-26 20:21:13 +0200885 return 1;
886
887 if (!submodule_uses_gitfile(path))
888 return 0;
889
Jens Lehmann293ab152012-09-26 20:21:13 +0200890 cp.argv = argv;
891 cp.env = local_repo_env;
892 cp.git_cmd = 1;
893 cp.no_stdin = 1;
894 cp.out = -1;
895 cp.dir = path;
896 if (start_command(&cp))
897 die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path);
898
899 len = strbuf_read(&buf, cp.out, 1024);
900 if (len > 2)
901 ok_to_remove = 0;
902 close(cp.out);
903
904 if (finish_command(&cp))
905 die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path);
906
907 strbuf_release(&buf);
908 return ok_to_remove;
909}
910
Heiko Voigt68d03e42010-07-07 15:39:13 +0200911static int find_first_merges(struct object_array *result, const char *path,
912 struct commit *a, struct commit *b)
913{
914 int i, j;
Michael Haggerty38269022013-05-25 11:08:12 +0200915 struct object_array merges = OBJECT_ARRAY_INIT;
Heiko Voigt68d03e42010-07-07 15:39:13 +0200916 struct commit *commit;
917 int contains_another;
918
919 char merged_revision[42];
920 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
921 "--all", merged_revision, NULL };
922 struct rev_info revs;
923 struct setup_revision_opt rev_opts;
924
Heiko Voigt68d03e42010-07-07 15:39:13 +0200925 memset(result, 0, sizeof(struct object_array));
926 memset(&rev_opts, 0, sizeof(rev_opts));
927
928 /* get all revisions that merge commit a */
929 snprintf(merged_revision, sizeof(merged_revision), "^%s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000930 oid_to_hex(&a->object.oid));
Heiko Voigt68d03e42010-07-07 15:39:13 +0200931 init_revisions(&revs, NULL);
932 rev_opts.submodule = path;
Elia Pinto94c0cc82014-06-27 05:11:39 -0700933 setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
Heiko Voigt68d03e42010-07-07 15:39:13 +0200934
935 /* save all revisions from the above list that contain b */
936 if (prepare_revision_walk(&revs))
937 die("revision walk setup failed");
938 while ((commit = get_revision(&revs)) != NULL) {
939 struct object *o = &(commit->object);
Junio C Hamanoa20efee2012-08-27 14:46:01 -0700940 if (in_merge_bases(b, commit))
Heiko Voigt68d03e42010-07-07 15:39:13 +0200941 add_object_array(o, NULL, &merges);
942 }
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +0200943 reset_revision_walk();
Heiko Voigt68d03e42010-07-07 15:39:13 +0200944
945 /* Now we've got all merges that contain a and b. Prune all
946 * merges that contain another found merge and save them in
947 * result.
948 */
949 for (i = 0; i < merges.nr; i++) {
950 struct commit *m1 = (struct commit *) merges.objects[i].item;
951
952 contains_another = 0;
953 for (j = 0; j < merges.nr; j++) {
954 struct commit *m2 = (struct commit *) merges.objects[j].item;
Junio C Hamanoa20efee2012-08-27 14:46:01 -0700955 if (i != j && in_merge_bases(m2, m1)) {
Heiko Voigt68d03e42010-07-07 15:39:13 +0200956 contains_another = 1;
957 break;
958 }
959 }
960
961 if (!contains_another)
Michael Haggerty5de0c012013-05-25 11:08:13 +0200962 add_object_array(merges.objects[i].item, NULL, result);
Heiko Voigt68d03e42010-07-07 15:39:13 +0200963 }
964
965 free(merges.objects);
966 return result->nr;
967}
968
969static void print_commit(struct commit *commit)
970{
971 struct strbuf sb = STRBUF_INIT;
972 struct pretty_print_context ctx = {0};
Jeff Kinga5481a62015-06-25 12:55:02 -0400973 ctx.date_mode.type = DATE_NORMAL;
Heiko Voigt68d03e42010-07-07 15:39:13 +0200974 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
975 fprintf(stderr, "%s\n", sb.buf);
976 strbuf_release(&sb);
977}
978
979#define MERGE_WARNING(path, msg) \
980 warning("Failed to merge submodule %s (%s)", path, msg);
981
982int merge_submodule(unsigned char result[20], const char *path,
983 const unsigned char base[20], const unsigned char a[20],
Brad King80988782011-10-13 08:59:05 -0400984 const unsigned char b[20], int search)
Heiko Voigt68d03e42010-07-07 15:39:13 +0200985{
986 struct commit *commit_base, *commit_a, *commit_b;
987 int parent_count;
988 struct object_array merges;
989
990 int i;
991
992 /* store a in result in case we fail */
993 hashcpy(result, a);
994
995 /* we can not handle deletion conflicts */
996 if (is_null_sha1(base))
997 return 0;
998 if (is_null_sha1(a))
999 return 0;
1000 if (is_null_sha1(b))
1001 return 0;
1002
1003 if (add_submodule_odb(path)) {
1004 MERGE_WARNING(path, "not checked out");
1005 return 0;
1006 }
1007
1008 if (!(commit_base = lookup_commit_reference(base)) ||
1009 !(commit_a = lookup_commit_reference(a)) ||
1010 !(commit_b = lookup_commit_reference(b))) {
1011 MERGE_WARNING(path, "commits not present");
1012 return 0;
1013 }
1014
1015 /* check whether both changes are forward */
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001016 if (!in_merge_bases(commit_base, commit_a) ||
1017 !in_merge_bases(commit_base, commit_b)) {
Heiko Voigt68d03e42010-07-07 15:39:13 +02001018 MERGE_WARNING(path, "commits don't follow merge-base");
1019 return 0;
1020 }
1021
1022 /* Case #1: a is contained in b or vice versa */
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001023 if (in_merge_bases(commit_a, commit_b)) {
Heiko Voigt68d03e42010-07-07 15:39:13 +02001024 hashcpy(result, b);
1025 return 1;
1026 }
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001027 if (in_merge_bases(commit_b, commit_a)) {
Heiko Voigt68d03e42010-07-07 15:39:13 +02001028 hashcpy(result, a);
1029 return 1;
1030 }
1031
1032 /*
1033 * Case #2: There are one or more merges that contain a and b in
1034 * the submodule. If there is only one, then present it as a
1035 * suggestion to the user, but leave it marked unmerged so the
1036 * user needs to confirm the resolution.
1037 */
1038
Brad King80988782011-10-13 08:59:05 -04001039 /* Skip the search if makes no sense to the calling context. */
1040 if (!search)
1041 return 0;
1042
Heiko Voigt68d03e42010-07-07 15:39:13 +02001043 /* find commit which merges them */
1044 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
1045 switch (parent_count) {
1046 case 0:
1047 MERGE_WARNING(path, "merge following commits not found");
1048 break;
1049
1050 case 1:
1051 MERGE_WARNING(path, "not fast-forward");
1052 fprintf(stderr, "Found a possible merge resolution "
1053 "for the submodule:\n");
1054 print_commit((struct commit *) merges.objects[0].item);
1055 fprintf(stderr,
1056 "If this is correct simply add it to the index "
1057 "for example\n"
1058 "by using:\n\n"
1059 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1060 "which will accept this suggestion.\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001061 oid_to_hex(&merges.objects[0].item->oid), path);
Heiko Voigt68d03e42010-07-07 15:39:13 +02001062 break;
1063
1064 default:
1065 MERGE_WARNING(path, "multiple merges found");
1066 for (i = 0; i < merges.nr; i++)
1067 print_commit((struct commit *) merges.objects[i].item);
1068 }
1069
1070 free(merges.objects);
1071 return 0;
1072}
Jens Lehmanna88c9152013-07-30 21:50:03 +02001073
1074/* Update gitfile and core.worktree setting to connect work tree and git dir */
1075void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
1076{
1077 struct strbuf file_name = STRBUF_INIT;
1078 struct strbuf rel_path = STRBUF_INIT;
1079 const char *real_work_tree = xstrdup(real_path(work_tree));
Jens Lehmanna88c9152013-07-30 21:50:03 +02001080
1081 /* Update gitfile */
1082 strbuf_addf(&file_name, "%s/.git", work_tree);
Junio C Hamano1f76a102015-08-24 13:20:39 -07001083 write_file(file_name.buf, "gitdir: %s",
Nguyễn Thái Ngọc Duy91aacda2014-11-30 15:24:46 +07001084 relative_path(git_dir, real_work_tree, &rel_path));
Jens Lehmanna88c9152013-07-30 21:50:03 +02001085
1086 /* Update core.worktree setting */
1087 strbuf_reset(&file_name);
1088 strbuf_addf(&file_name, "%s/config", git_dir);
Patrick Steinhardt3d180642016-02-22 12:23:36 +01001089 git_config_set_in_file(file_name.buf, "core.worktree",
1090 relative_path(real_work_tree, git_dir,
1091 &rel_path));
Jens Lehmanna88c9152013-07-30 21:50:03 +02001092
1093 strbuf_release(&file_name);
1094 strbuf_release(&rel_path);
1095 free((void *)real_work_tree);
1096}