blob: 4532b11d66f8cdc0abdf003b3a16a632bd923890 [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"
Jeff King46387282016-04-28 09:38:20 -040016#include "quote.h"
Jens Lehmannaee9c7d2010-08-06 00:39:25 +020017
Jens Lehmann88a21972011-03-06 23:10:46 +010018static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
Stefan Bellera028a192016-02-29 18:07:13 -080019static int parallel_jobs = 1;
Junio C Hamano2071fb02011-04-04 15:02:01 -070020static struct string_list changed_submodule_paths;
Jeff King6859de42011-09-12 15:56:52 -040021static int initialized_fetch_ref_tips;
22static struct sha1_array ref_tips_before_fetch;
23static struct sha1_array ref_tips_after_fetch;
24
Jens Lehmannd4e98b52011-05-14 18:26:58 +020025/*
26 * The following flag is set if the .gitmodules file is unmerged. We then
27 * disable recursion for all submodules where .git/config doesn't have a
28 * matching config entry because we can't guess what might be configured in
29 * .gitmodules unless the user resolves the conflict. When a command line
30 * option is given (which always overrides configuration) this flag will be
31 * ignored.
32 */
33static int gitmodules_is_unmerged;
Johannes Schindelin752c0c22009-10-19 14:38:32 +020034
Jens Lehmann5fee9952013-07-30 21:50:34 +020035/*
36 * This flag is set if the .gitmodules file had unstaged modifications on
37 * startup. This must be checked before allowing modifications to the
38 * .gitmodules file with the intention to stage them later, because when
39 * continuing we would stage the modifications the user didn't stage herself
40 * too. That might change in a future version when we learn to stage the
41 * changes we do ourselves without staging any previous modifications.
42 */
43static int gitmodules_is_modified;
44
Jens Lehmann5fee9952013-07-30 21:50:34 +020045int 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;
Heiko Voigt851e18c2015-08-17 17:21:59 -070058 const struct submodule *submodule;
Jens Lehmann06567812013-08-06 21:15:11 +020059
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
Heiko Voigt851e18c2015-08-17 17:21:59 -070066 submodule = submodule_from_path(null_sha1, oldpath);
67 if (!submodule || !submodule->name) {
Jens Lehmann06567812013-08-06 21:15:11 +020068 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
69 return -1;
70 }
71 strbuf_addstr(&entry, "submodule.");
Heiko Voigt851e18c2015-08-17 17:21:59 -070072 strbuf_addstr(&entry, submodule->name);
Jens Lehmann06567812013-08-06 21:15:11 +020073 strbuf_addstr(&entry, ".path");
Patrick Steinhardt30598ad2016-02-22 12:23:35 +010074 if (git_config_set_in_file_gently(".gitmodules", entry.buf, newpath) < 0) {
Jens Lehmann06567812013-08-06 21:15:11 +020075 /* 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;
Heiko Voigt851e18c2015-08-17 17:21:59 -070092 const struct submodule *submodule;
Jens Lehmann95c16412013-08-06 21:15:25 +020093
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
Heiko Voigt851e18c2015-08-17 17:21:59 -0700100 submodule = submodule_from_path(null_sha1, path);
101 if (!submodule || !submodule->name) {
Jens Lehmann95c16412013-08-06 21:15:25 +0200102 warning(_("Could not find section in .gitmodules where path=%s"), path);
103 return -1;
104 }
105 strbuf_addstr(&sect, "submodule.");
Heiko Voigt851e18c2015-08-17 17:21:59 -0700106 strbuf_addstr(&sect, submodule->name);
Jens Lehmann95c16412013-08-06 21:15:25 +0200107 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{
Jens Lehmannbc8d6b92013-11-07 15:33:43 +0100119 if (add_file_to_cache(".gitmodules", 0))
Jens Lehmann5fee9952013-07-30 21:50:34 +0200120 die(_("staging updated .gitmodules failed"));
121}
122
Junio C Hamanocb58c932010-01-11 22:31:58 -0800123static int add_submodule_odb(const char *path)
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200124{
125 struct strbuf objects_directory = STRBUF_INIT;
126 struct alternate_object_database *alt_odb;
Jens Lehmannde7a7962010-01-31 17:43:49 +0100127 int ret = 0;
Jeff King50a6c8e2016-02-22 17:44:35 -0500128 size_t alloc;
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200129
Max Kirillov35fb4d22015-09-14 01:17:41 +0300130 strbuf_git_path_submodule(&objects_directory, path, "objects/");
Jens Lehmannde7a7962010-01-31 17:43:49 +0100131 if (!is_directory(objects_directory.buf)) {
132 ret = -1;
133 goto done;
134 }
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200135 /* avoid adding it twice */
Jeff King9a6e4f02015-10-28 10:07:25 -0400136 prepare_alt_odb();
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200137 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
138 if (alt_odb->name - alt_odb->base == objects_directory.len &&
139 !strncmp(alt_odb->base, objects_directory.buf,
140 objects_directory.len))
Jens Lehmannde7a7962010-01-31 17:43:49 +0100141 goto done;
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200142
Jeff King50a6c8e2016-02-22 17:44:35 -0500143 alloc = st_add(objects_directory.len, 42); /* for "12/345..." sha1 */
144 alt_odb = xmalloc(st_add(sizeof(*alt_odb), alloc));
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200145 alt_odb->next = alt_odb_list;
Jeff Kingc7ab0ba2015-09-24 17:08:12 -0400146 xsnprintf(alt_odb->base, alloc, "%s", objects_directory.buf);
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200147 alt_odb->name = alt_odb->base + objects_directory.len;
148 alt_odb->name[2] = '/';
149 alt_odb->name[40] = '\0';
150 alt_odb->name[41] = '\0';
151 alt_odb_list = alt_odb;
Heiko Voigt5e736332012-05-14 18:24:45 +0200152
153 /* add possible alternates from the submodule */
154 read_info_alternates(objects_directory.buf, 0);
Jens Lehmannde7a7962010-01-31 17:43:49 +0100155done:
156 strbuf_release(&objects_directory);
157 return ret;
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200158}
159
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200160void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
161 const char *path)
162{
Heiko Voigt851e18c2015-08-17 17:21:59 -0700163 const struct submodule *submodule = submodule_from_path(null_sha1, path);
164 if (submodule) {
165 if (submodule->ignore)
166 handle_ignore_submodules_arg(diffopt, submodule->ignore);
Jens Lehmannd4e98b52011-05-14 18:26:58 +0200167 else if (gitmodules_is_unmerged)
168 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200169 }
170}
171
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100172int submodule_config(const char *var, const char *value, void *cb)
Jens Lehmann302ad7a2010-08-06 00:40:48 +0200173{
Stefan Bellera028a192016-02-29 18:07:13 -0800174 if (!strcmp(var, "submodule.fetchjobs")) {
175 parallel_jobs = git_config_int(var, value);
176 if (parallel_jobs < 0)
177 die(_("negative values not allowed for submodule.fetchJobs"));
178 return 0;
179 } else if (starts_with(var, "submodule."))
Jens Lehmann302ad7a2010-08-06 00:40:48 +0200180 return parse_submodule_config_option(var, value);
Jens Lehmannbe254a02010-11-11 00:55:02 +0100181 else if (!strcmp(var, "fetch.recursesubmodules")) {
Jens Lehmann1fb25502011-03-06 23:11:48 +0100182 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
Jens Lehmannbe254a02010-11-11 00:55:02 +0100183 return 0;
184 }
Jens Lehmann302ad7a2010-08-06 00:40:48 +0200185 return 0;
186}
187
188void gitmodules_config(void)
189{
190 const char *work_tree = get_git_work_tree();
191 if (work_tree) {
192 struct strbuf gitmodules_path = STRBUF_INIT;
Jens Lehmannd4e98b52011-05-14 18:26:58 +0200193 int pos;
Jens Lehmann302ad7a2010-08-06 00:40:48 +0200194 strbuf_addstr(&gitmodules_path, work_tree);
195 strbuf_addstr(&gitmodules_path, "/.gitmodules");
Jens Lehmannd4e98b52011-05-14 18:26:58 +0200196 if (read_cache() < 0)
197 die("index file corrupt");
198 pos = cache_name_pos(".gitmodules", 11);
199 if (pos < 0) { /* .gitmodules not found or isn't merged */
200 pos = -1 - pos;
201 if (active_nr > pos) { /* there is a .gitmodules */
202 const struct cache_entry *ce = active_cache[pos];
203 if (ce_namelen(ce) == 11 &&
204 !memcmp(ce->name, ".gitmodules", 11))
205 gitmodules_is_unmerged = 1;
206 }
Jens Lehmann5fee9952013-07-30 21:50:34 +0200207 } else if (pos < active_nr) {
208 struct stat st;
209 if (lstat(".gitmodules", &st) == 0 &&
210 ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED)
211 gitmodules_is_modified = 1;
Jens Lehmannd4e98b52011-05-14 18:26:58 +0200212 }
213
214 if (!gitmodules_is_unmerged)
215 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
Jens Lehmann302ad7a2010-08-06 00:40:48 +0200216 strbuf_release(&gitmodules_path);
217 }
218}
219
Stefan Bellerea2fa5a2016-02-29 18:07:11 -0800220int parse_submodule_update_strategy(const char *value,
221 struct submodule_update_strategy *dst)
222{
223 free((void*)dst->command);
224 dst->command = NULL;
225 if (!strcmp(value, "none"))
226 dst->type = SM_UPDATE_NONE;
227 else if (!strcmp(value, "checkout"))
228 dst->type = SM_UPDATE_CHECKOUT;
229 else if (!strcmp(value, "rebase"))
230 dst->type = SM_UPDATE_REBASE;
231 else if (!strcmp(value, "merge"))
232 dst->type = SM_UPDATE_MERGE;
233 else if (skip_prefix(value, "!", &value)) {
234 dst->type = SM_UPDATE_COMMAND;
235 dst->command = xstrdup(value);
236 } else
237 return -1;
238 return 0;
239}
240
Stefan Beller36042422016-04-15 17:50:13 -0700241const char *submodule_strategy_to_string(const struct submodule_update_strategy *s)
242{
243 struct strbuf sb = STRBUF_INIT;
244 switch (s->type) {
245 case SM_UPDATE_CHECKOUT:
246 return "checkout";
247 case SM_UPDATE_MERGE:
248 return "merge";
249 case SM_UPDATE_REBASE:
250 return "rebase";
251 case SM_UPDATE_NONE:
252 return "none";
253 case SM_UPDATE_UNSPECIFIED:
254 return NULL;
255 case SM_UPDATE_COMMAND:
256 strbuf_addf(&sb, "!%s", s->command);
257 return strbuf_detach(&sb, NULL);
258 }
259 return NULL;
260}
261
Jens Lehmann46a958b2010-06-25 16:56:47 +0200262void handle_ignore_submodules_arg(struct diff_options *diffopt,
263 const char *arg)
264{
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +0200265 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
266 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
267 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
268
Jens Lehmann46a958b2010-06-25 16:56:47 +0200269 if (!strcmp(arg, "all"))
270 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
271 else if (!strcmp(arg, "untracked"))
272 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
273 else if (!strcmp(arg, "dirty"))
274 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
Jens Lehmannaee9c7d2010-08-06 00:39:25 +0200275 else if (strcmp(arg, "none"))
Jens Lehmann46a958b2010-06-25 16:56:47 +0200276 die("bad --ignore-submodules argument: %s", arg);
277}
278
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500279static int prepare_submodule_summary(struct rev_info *rev, const char *path,
280 struct commit *left, struct commit *right,
281 int *fast_forward, int *fast_backward)
282{
283 struct commit_list *merge_bases, *list;
284
285 init_revisions(rev, NULL);
286 setup_revisions(0, NULL, rev, NULL);
287 rev->left_right = 1;
288 rev->first_parent_only = 1;
289 left->object.flags |= SYMMETRIC_LEFT;
290 add_pending_object(rev, &left->object, path);
291 add_pending_object(rev, &right->object, path);
Junio C Hamano2ce406c2014-10-30 12:20:44 -0700292 merge_bases = get_merge_bases(left, right);
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500293 if (merge_bases) {
294 if (merge_bases->item == left)
295 *fast_forward = 1;
296 else if (merge_bases->item == right)
297 *fast_backward = 1;
298 }
299 for (list = merge_bases; list; list = list->next) {
300 list->item->object.flags |= UNINTERESTING;
301 add_pending_object(rev, &list->item->object,
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000302 oid_to_hex(&list->item->object.oid));
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500303 }
304 return prepare_revision_walk(rev);
305}
306
307static void print_submodule_summary(struct rev_info *rev, FILE *f,
John Keeping0f33a062013-04-05 17:12:08 +0100308 const char *line_prefix,
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500309 const char *del, const char *add, const char *reset)
310{
311 static const char format[] = " %m %s";
312 struct strbuf sb = STRBUF_INIT;
313 struct commit *commit;
314
315 while ((commit = get_revision(rev))) {
316 struct pretty_print_context ctx = {0};
317 ctx.date_mode = rev->date_mode;
Alexey Shumkinecaee802013-06-26 14:19:50 +0400318 ctx.output_encoding = get_log_output_encoding();
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500319 strbuf_setlen(&sb, 0);
John Keeping0f33a062013-04-05 17:12:08 +0100320 strbuf_addstr(&sb, line_prefix);
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500321 if (commit->object.flags & SYMMETRIC_LEFT) {
322 if (del)
323 strbuf_addstr(&sb, del);
324 }
325 else if (add)
326 strbuf_addstr(&sb, add);
327 format_commit_message(commit, format, &sb, &ctx);
328 if (reset)
329 strbuf_addstr(&sb, reset);
330 strbuf_addch(&sb, '\n');
331 fprintf(f, "%s", sb.buf);
332 }
333 strbuf_release(&sb);
334}
335
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200336void show_submodule_summary(FILE *f, const char *path,
John Keeping0f33a062013-04-05 17:12:08 +0100337 const char *line_prefix,
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200338 unsigned char one[20], unsigned char two[20],
Ramkumar Ramachandra4e215132012-11-13 21:12:47 +0530339 unsigned dirty_submodule, const char *meta,
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200340 const char *del, const char *add, const char *reset)
341{
342 struct rev_info rev;
Jeff King83715492013-03-22 12:19:56 -0400343 struct commit *left = NULL, *right = NULL;
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200344 const char *message = NULL;
345 struct strbuf sb = STRBUF_INIT;
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200346 int fast_forward = 0, fast_backward = 0;
347
348 if (is_null_sha1(two))
349 message = "(submodule deleted)";
350 else if (add_submodule_odb(path))
351 message = "(not checked out)";
352 else if (is_null_sha1(one))
353 message = "(new submodule)";
354 else if (!(left = lookup_commit_reference(one)) ||
355 !(right = lookup_commit_reference(two)))
356 message = "(commits not present)";
Jeff King83715492013-03-22 12:19:56 -0400357 else if (prepare_submodule_summary(&rev, path, left, right,
358 &fast_forward, &fast_backward))
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500359 message = "(revision walker failed)";
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200360
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100361 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
John Keeping0f33a062013-04-05 17:12:08 +0100362 fprintf(f, "%sSubmodule %s contains untracked content\n",
363 line_prefix, path);
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100364 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
John Keeping0f33a062013-04-05 17:12:08 +0100365 fprintf(f, "%sSubmodule %s contains modified content\n",
366 line_prefix, path);
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100367
368 if (!hashcmp(one, two)) {
369 strbuf_release(&sb);
370 return;
371 }
372
John Keeping0f33a062013-04-05 17:12:08 +0100373 strbuf_addf(&sb, "%s%sSubmodule %s %s..", line_prefix, meta, path,
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200374 find_unique_abbrev(one, DEFAULT_ABBREV));
375 if (!fast_backward && !fast_forward)
376 strbuf_addch(&sb, '.');
377 strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
378 if (message)
Ramkumar Ramachandra4e215132012-11-13 21:12:47 +0530379 strbuf_addf(&sb, " %s%s\n", message, reset);
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200380 else
Ramkumar Ramachandra4e215132012-11-13 21:12:47 +0530381 strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200382 fwrite(sb.buf, sb.len, 1, f);
383
Jeff King83715492013-03-22 12:19:56 -0400384 if (!message) /* only NULL if we succeeded in setting up the walk */
John Keeping0f33a062013-04-05 17:12:08 +0100385 print_submodule_summary(&rev, f, line_prefix, del, add, reset);
Jeff King83715492013-03-22 12:19:56 -0400386 if (left)
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200387 clear_commit_marks(left, ~0);
Jeff King83715492013-03-22 12:19:56 -0400388 if (right)
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200389 clear_commit_marks(right, ~0);
Jonathan Nieder808a95d2011-03-16 02:14:11 -0500390
Johannes Schindelin752c0c22009-10-19 14:38:32 +0200391 strbuf_release(&sb);
392}
Jens Lehmannee6fc512010-01-16 18:42:24 +0100393
Jens Lehmannbe254a02010-11-11 00:55:02 +0100394void set_config_fetch_recurse_submodules(int value)
395{
396 config_fetch_recurse_submodules = value;
397}
398
Michael Haggerty7290ef52015-05-25 18:39:07 +0000399static int has_remote(const char *refname, const struct object_id *oid,
400 int flags, void *cb_data)
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200401{
402 return 1;
403}
404
405static int submodule_needs_pushing(const char *path, const unsigned char sha1[20])
406{
407 if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
408 return 0;
409
410 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
René Scharfed3180272014-08-19 21:09:35 +0200411 struct child_process cp = CHILD_PROCESS_INIT;
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200412 const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL};
413 struct strbuf buf = STRBUF_INIT;
414 int needs_pushing = 0;
415
416 argv[1] = sha1_to_hex(sha1);
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200417 cp.argv = argv;
Jeff Kingc12e8652016-04-28 09:39:15 -0400418 prepare_submodule_repo_env(&cp.env_array);
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200419 cp.git_cmd = 1;
420 cp.no_stdin = 1;
421 cp.out = -1;
422 cp.dir = path;
423 if (start_command(&cp))
424 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
425 sha1_to_hex(sha1), path);
426 if (strbuf_read(&buf, cp.out, 41))
427 needs_pushing = 1;
428 finish_command(&cp);
429 close(cp.out);
430 strbuf_release(&buf);
431 return needs_pushing;
432 }
433
434 return 0;
435}
436
437static void collect_submodules_from_diff(struct diff_queue_struct *q,
438 struct diff_options *options,
439 void *data)
440{
441 int i;
Heiko Voigta762e512012-03-29 09:21:23 +0200442 struct string_list *needs_pushing = data;
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200443
444 for (i = 0; i < q->nr; i++) {
445 struct diff_filepair *p = q->queue[i];
446 if (!S_ISGITLINK(p->two->mode))
447 continue;
Heiko Voigta762e512012-03-29 09:21:23 +0200448 if (submodule_needs_pushing(p->two->path, p->two->sha1))
449 string_list_insert(needs_pushing, p->two->path);
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200450 }
451}
452
Heiko Voigta762e512012-03-29 09:21:23 +0200453static void find_unpushed_submodule_commits(struct commit *commit,
454 struct string_list *needs_pushing)
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200455{
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200456 struct rev_info rev;
457
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200458 init_revisions(&rev, NULL);
459 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
460 rev.diffopt.format_callback = collect_submodules_from_diff;
461 rev.diffopt.format_callback_data = needs_pushing;
René Scharfe78e98ea2011-12-17 11:27:19 +0100462 diff_tree_combined_merge(commit, 1, &rev);
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200463}
464
Heiko Voigta762e512012-03-29 09:21:23 +0200465int find_unpushed_submodules(unsigned char new_sha1[20],
466 const char *remotes_name, struct string_list *needs_pushing)
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200467{
468 struct rev_info rev;
469 struct commit *commit;
470 const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
471 int argc = ARRAY_SIZE(argv) - 1;
472 char *sha1_copy;
Heiko Voigta762e512012-03-29 09:21:23 +0200473
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200474 struct strbuf remotes_arg = STRBUF_INIT;
475
476 strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);
477 init_revisions(&rev, NULL);
478 sha1_copy = xstrdup(sha1_to_hex(new_sha1));
479 argv[1] = sha1_copy;
480 argv[3] = remotes_arg.buf;
481 setup_revisions(argc, argv, &rev, NULL);
482 if (prepare_revision_walk(&rev))
483 die("revision walk setup failed");
484
Heiko Voigta762e512012-03-29 09:21:23 +0200485 while ((commit = get_revision(&rev)) != NULL)
486 find_unpushed_submodule_commits(commit, needs_pushing);
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200487
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +0200488 reset_revision_walk();
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200489 free(sha1_copy);
490 strbuf_release(&remotes_arg);
491
Heiko Voigta762e512012-03-29 09:21:23 +0200492 return needs_pushing->nr;
Fredrik Gustafssond2b17b32011-08-20 00:08:47 +0200493}
494
Heiko Voigteb21c732012-03-29 09:21:24 +0200495static int push_submodule(const char *path)
496{
497 if (add_submodule_odb(path))
498 return 1;
499
500 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
René Scharfed3180272014-08-19 21:09:35 +0200501 struct child_process cp = CHILD_PROCESS_INIT;
Heiko Voigteb21c732012-03-29 09:21:24 +0200502 const char *argv[] = {"push", NULL};
503
Heiko Voigteb21c732012-03-29 09:21:24 +0200504 cp.argv = argv;
Jeff Kingc12e8652016-04-28 09:39:15 -0400505 prepare_submodule_repo_env(&cp.env_array);
Heiko Voigteb21c732012-03-29 09:21:24 +0200506 cp.git_cmd = 1;
507 cp.no_stdin = 1;
508 cp.dir = path;
509 if (run_command(&cp))
510 return 0;
511 close(cp.out);
512 }
513
514 return 1;
515}
516
517int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name)
518{
519 int i, ret = 1;
Tanay Abhraf93d7c62014-07-18 02:19:00 -0700520 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
Heiko Voigteb21c732012-03-29 09:21:24 +0200521
522 if (!find_unpushed_submodules(new_sha1, remotes_name, &needs_pushing))
523 return 1;
524
525 for (i = 0; i < needs_pushing.nr; i++) {
526 const char *path = needs_pushing.items[i].string;
527 fprintf(stderr, "Pushing submodule '%s'\n", path);
528 if (!push_submodule(path)) {
529 fprintf(stderr, "Unable to push submodule '%s'\n", path);
530 ret = 0;
531 }
532 }
533
534 string_list_clear(&needs_pushing, 0);
535
536 return ret;
537}
538
Jens Lehmannc16c3e42011-03-06 23:12:58 +0100539static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
540{
541 int is_present = 0;
542 if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
543 /* Even if the submodule is checked out and the commit is
544 * present, make sure it is reachable from a ref. */
René Scharfed3180272014-08-19 21:09:35 +0200545 struct child_process cp = CHILD_PROCESS_INIT;
Jens Lehmannc16c3e42011-03-06 23:12:58 +0100546 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
547 struct strbuf buf = STRBUF_INIT;
548
549 argv[3] = sha1_to_hex(sha1);
Jens Lehmannc16c3e42011-03-06 23:12:58 +0100550 cp.argv = argv;
Jeff Kingc12e8652016-04-28 09:39:15 -0400551 prepare_submodule_repo_env(&cp.env_array);
Jens Lehmannc16c3e42011-03-06 23:12:58 +0100552 cp.git_cmd = 1;
553 cp.no_stdin = 1;
Jens Lehmannc16c3e42011-03-06 23:12:58 +0100554 cp.dir = path;
Jeff King1d4974c2015-03-22 23:53:56 -0400555 if (!capture_command(&cp, &buf, 1024) && !buf.len)
Jens Lehmannc16c3e42011-03-06 23:12:58 +0100556 is_present = 1;
557
Jens Lehmannc16c3e42011-03-06 23:12:58 +0100558 strbuf_release(&buf);
559 }
560 return is_present;
561}
562
Jens Lehmann88a21972011-03-06 23:10:46 +0100563static void submodule_collect_changed_cb(struct diff_queue_struct *q,
564 struct diff_options *options,
565 void *data)
566{
567 int i;
568 for (i = 0; i < q->nr; i++) {
569 struct diff_filepair *p = q->queue[i];
570 if (!S_ISGITLINK(p->two->mode))
571 continue;
572
573 if (S_ISGITLINK(p->one->mode)) {
574 /* NEEDSWORK: We should honor the name configured in
575 * the .gitmodules file of the commit we are examining
576 * here to be able to correctly follow submodules
577 * being moved around. */
578 struct string_list_item *path;
579 path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
Jens Lehmannc16c3e42011-03-06 23:12:58 +0100580 if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
Jens Lehmann88a21972011-03-06 23:10:46 +0100581 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
582 } else {
583 /* Submodule is new or was moved here */
584 /* NEEDSWORK: When the .git directories of submodules
585 * live inside the superprojects .git directory some
586 * day we should fetch new submodules directly into
587 * that location too when config or options request
588 * that so they can be checked out from there. */
589 continue;
590 }
591 }
592}
593
Michael Haggerty7290ef52015-05-25 18:39:07 +0000594static int add_sha1_to_array(const char *ref, const struct object_id *oid,
Jeff King6859de42011-09-12 15:56:52 -0400595 int flags, void *data)
596{
Michael Haggerty7290ef52015-05-25 18:39:07 +0000597 sha1_array_append(data, oid->hash);
Jeff King6859de42011-09-12 15:56:52 -0400598 return 0;
599}
600
Jens Lehmann88a21972011-03-06 23:10:46 +0100601void check_for_new_submodule_commits(unsigned char new_sha1[20])
602{
Jeff King6859de42011-09-12 15:56:52 -0400603 if (!initialized_fetch_ref_tips) {
604 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
605 initialized_fetch_ref_tips = 1;
606 }
607
608 sha1_array_append(&ref_tips_after_fetch, new_sha1);
609}
610
Jeff King6859de42011-09-12 15:56:52 -0400611static void add_sha1_to_argv(const unsigned char sha1[20], void *data)
612{
Jeff Kingc1189ca2011-09-13 17:57:57 -0400613 argv_array_push(data, sha1_to_hex(sha1));
Jeff King6859de42011-09-12 15:56:52 -0400614}
615
616static void calculate_changed_submodule_paths(void)
617{
Jens Lehmann88a21972011-03-06 23:10:46 +0100618 struct rev_info rev;
619 struct commit *commit;
Jeff Kingc1189ca2011-09-13 17:57:57 -0400620 struct argv_array argv = ARGV_ARRAY_INIT;
Jens Lehmann88a21972011-03-06 23:10:46 +0100621
Jens Lehmann18322ba2011-09-09 20:22:03 +0200622 /* No need to check if there are no submodules configured */
Heiko Voigt851e18c2015-08-17 17:21:59 -0700623 if (!submodule_from_path(NULL, NULL))
Jens Lehmann18322ba2011-09-09 20:22:03 +0200624 return;
625
Jens Lehmann88a21972011-03-06 23:10:46 +0100626 init_revisions(&rev, NULL);
Jeff Kingc1189ca2011-09-13 17:57:57 -0400627 argv_array_push(&argv, "--"); /* argv[0] program name */
Jeff King6859de42011-09-12 15:56:52 -0400628 sha1_array_for_each_unique(&ref_tips_after_fetch,
629 add_sha1_to_argv, &argv);
Jeff Kingc1189ca2011-09-13 17:57:57 -0400630 argv_array_push(&argv, "--not");
Jeff King6859de42011-09-12 15:56:52 -0400631 sha1_array_for_each_unique(&ref_tips_before_fetch,
632 add_sha1_to_argv, &argv);
633 setup_revisions(argv.argc, argv.argv, &rev, NULL);
Jens Lehmann88a21972011-03-06 23:10:46 +0100634 if (prepare_revision_walk(&rev))
635 die("revision walk setup failed");
636
637 /*
638 * Collect all submodules (whether checked out or not) for which new
639 * commits have been recorded upstream in "changed_submodule_paths".
640 */
641 while ((commit = get_revision(&rev))) {
642 struct commit_list *parent = commit->parents;
643 while (parent) {
644 struct diff_options diff_opts;
645 diff_setup(&diff_opts);
Jens Lehmannea2d3252011-06-20 20:18:03 +0200646 DIFF_OPT_SET(&diff_opts, RECURSIVE);
Jens Lehmann88a21972011-03-06 23:10:46 +0100647 diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
648 diff_opts.format_callback = submodule_collect_changed_cb;
Thomas Rast28452652012-08-03 14:16:24 +0200649 diff_setup_done(&diff_opts);
brian m. carlsoned1c9972015-11-10 02:22:29 +0000650 diff_tree_sha1(parent->item->object.oid.hash, commit->object.oid.hash, "", &diff_opts);
Jens Lehmann88a21972011-03-06 23:10:46 +0100651 diffcore_std(&diff_opts);
652 diff_flush(&diff_opts);
653 parent = parent->next;
654 }
655 }
Jeff King6859de42011-09-12 15:56:52 -0400656
Jeff Kingc1189ca2011-09-13 17:57:57 -0400657 argv_array_clear(&argv);
Jeff King6859de42011-09-12 15:56:52 -0400658 sha1_array_clear(&ref_tips_before_fetch);
659 sha1_array_clear(&ref_tips_after_fetch);
660 initialized_fetch_ref_tips = 0;
Jens Lehmann88a21972011-03-06 23:10:46 +0100661}
662
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800663struct submodule_parallel_fetch {
664 int count;
665 struct argv_array args;
666 const char *work_tree;
667 const char *prefix;
668 int command_line_option;
669 int quiet;
670 int result;
671};
672#define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0}
673
674static int get_next_submodule(struct child_process *cp,
675 struct strbuf *err, void *data, void **task_cb)
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100676{
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800677 int ret = 0;
678 struct submodule_parallel_fetch *spf = data;
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100679
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800680 for (; spf->count < active_nr; spf->count++) {
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100681 struct strbuf submodule_path = STRBUF_INIT;
682 struct strbuf submodule_git_dir = STRBUF_INIT;
683 struct strbuf submodule_prefix = STRBUF_INIT;
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800684 const struct cache_entry *ce = active_cache[spf->count];
Heiko Voigt851e18c2015-08-17 17:21:59 -0700685 const char *git_dir, *default_argv;
686 const struct submodule *submodule;
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100687
688 if (!S_ISGITLINK(ce->ce_mode))
689 continue;
690
Heiko Voigt851e18c2015-08-17 17:21:59 -0700691 submodule = submodule_from_path(null_sha1, ce->name);
692 if (!submodule)
693 submodule = submodule_from_name(null_sha1, ce->name);
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100694
Jens Lehmann88a21972011-03-06 23:10:46 +0100695 default_argv = "yes";
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800696 if (spf->command_line_option == RECURSE_SUBMODULES_DEFAULT) {
Heiko Voigt851e18c2015-08-17 17:21:59 -0700697 if (submodule &&
698 submodule->fetch_recurse !=
699 RECURSE_SUBMODULES_NONE) {
700 if (submodule->fetch_recurse ==
701 RECURSE_SUBMODULES_OFF)
Jens Lehmannc1a3c362010-11-11 00:55:41 +0100702 continue;
Heiko Voigt851e18c2015-08-17 17:21:59 -0700703 if (submodule->fetch_recurse ==
704 RECURSE_SUBMODULES_ON_DEMAND) {
Jens Lehmannbf42b382011-03-06 23:12:19 +0100705 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
706 continue;
707 default_argv = "on-demand";
708 }
Jens Lehmannc1a3c362010-11-11 00:55:41 +0100709 } else {
Jens Lehmannd4e98b52011-05-14 18:26:58 +0200710 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
711 gitmodules_is_unmerged)
Jens Lehmannc1a3c362010-11-11 00:55:41 +0100712 continue;
Jens Lehmann88a21972011-03-06 23:10:46 +0100713 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
714 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
715 continue;
716 default_argv = "on-demand";
717 }
Jens Lehmannc1a3c362010-11-11 00:55:41 +0100718 }
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800719 } else if (spf->command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
Jens Lehmann8f0700d2011-03-06 23:11:21 +0100720 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
721 continue;
722 default_argv = "on-demand";
Jens Lehmannbe254a02010-11-11 00:55:02 +0100723 }
724
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800725 strbuf_addf(&submodule_path, "%s/%s", spf->work_tree, ce->name);
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100726 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800727 strbuf_addf(&submodule_prefix, "%s%s/", spf->prefix, ce->name);
Junio C Hamano13d6ec92011-08-22 14:04:56 -0700728 git_dir = read_gitfile(submodule_git_dir.buf);
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100729 if (!git_dir)
730 git_dir = submodule_git_dir.buf;
731 if (is_directory(git_dir)) {
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800732 child_process_init(cp);
733 cp->dir = strbuf_detach(&submodule_path, NULL);
Jeff Kingc12e8652016-04-28 09:39:15 -0400734 prepare_submodule_repo_env(&cp->env_array);
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800735 cp->git_cmd = 1;
736 if (!spf->quiet)
737 strbuf_addf(err, "Fetching submodule %s%s\n",
738 spf->prefix, ce->name);
739 argv_array_init(&cp->args);
740 argv_array_pushv(&cp->args, spf->args.argv);
741 argv_array_push(&cp->args, default_argv);
742 argv_array_push(&cp->args, "--submodule-prefix");
743 argv_array_push(&cp->args, submodule_prefix.buf);
744 ret = 1;
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100745 }
746 strbuf_release(&submodule_path);
747 strbuf_release(&submodule_git_dir);
748 strbuf_release(&submodule_prefix);
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800749 if (ret) {
750 spf->count++;
751 return 1;
752 }
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100753 }
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800754 return 0;
755}
756
Stefan Beller2a73b3d2016-02-29 13:57:06 -0800757static int fetch_start_failure(struct strbuf *err,
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800758 void *cb, void *task_cb)
759{
760 struct submodule_parallel_fetch *spf = cb;
761
762 spf->result = 1;
763
764 return 0;
765}
766
Stefan Beller2a73b3d2016-02-29 13:57:06 -0800767static int fetch_finish(int retvalue, struct strbuf *err,
768 void *cb, void *task_cb)
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800769{
770 struct submodule_parallel_fetch *spf = cb;
771
772 if (retvalue)
773 spf->result = 1;
774
775 return 0;
776}
777
778int fetch_populated_submodules(const struct argv_array *options,
779 const char *prefix, int command_line_option,
Stefan Beller62104ba2015-12-15 16:04:12 -0800780 int quiet, int max_parallel_jobs)
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800781{
782 int i;
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800783 struct submodule_parallel_fetch spf = SPF_INIT;
784
785 spf.work_tree = get_git_work_tree();
786 spf.command_line_option = command_line_option;
787 spf.quiet = quiet;
788 spf.prefix = prefix;
789
790 if (!spf.work_tree)
791 goto out;
792
793 if (read_cache() < 0)
794 die("index file corrupt");
795
796 argv_array_push(&spf.args, "fetch");
797 for (i = 0; i < options->argc; i++)
798 argv_array_push(&spf.args, options->argv[i]);
799 argv_array_push(&spf.args, "--recurse-submodules-default");
800 /* default value, "--submodule-prefix" and its value are added later */
801
Stefan Bellera028a192016-02-29 18:07:13 -0800802 if (max_parallel_jobs < 0)
803 max_parallel_jobs = parallel_jobs;
804
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800805 calculate_changed_submodule_paths();
806 run_processes_parallel(max_parallel_jobs,
807 get_next_submodule,
808 fetch_start_failure,
809 fetch_finish,
810 &spf);
811
812 argv_array_clear(&spf.args);
Jens Lehmann88a21972011-03-06 23:10:46 +0100813out:
814 string_list_clear(&changed_submodule_paths, 1);
Stefan Bellerfe85ee62015-12-15 16:04:11 -0800815 return spf.result;
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100816}
817
Jens Lehmann3bfc4502010-03-13 23:00:27 +0100818unsigned is_submodule_modified(const char *path, int ignore_untracked)
Jens Lehmannee6fc512010-01-16 18:42:24 +0100819{
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100820 ssize_t len;
René Scharfed3180272014-08-19 21:09:35 +0200821 struct child_process cp = CHILD_PROCESS_INIT;
Jens Lehmannee6fc512010-01-16 18:42:24 +0100822 const char *argv[] = {
823 "status",
824 "--porcelain",
825 NULL,
Jens Lehmann3bfc4502010-03-13 23:00:27 +0100826 NULL,
Jens Lehmannee6fc512010-01-16 18:42:24 +0100827 };
Jens Lehmannee6fc512010-01-16 18:42:24 +0100828 struct strbuf buf = STRBUF_INIT;
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100829 unsigned dirty_submodule = 0;
830 const char *line, *next_line;
Jens Lehmanneee49b62010-04-10 19:01:12 +0200831 const char *git_dir;
Jens Lehmannee6fc512010-01-16 18:42:24 +0100832
Jens Lehmanneee49b62010-04-10 19:01:12 +0200833 strbuf_addf(&buf, "%s/.git", path);
Junio C Hamano13d6ec92011-08-22 14:04:56 -0700834 git_dir = read_gitfile(buf.buf);
Jens Lehmanneee49b62010-04-10 19:01:12 +0200835 if (!git_dir)
836 git_dir = buf.buf;
837 if (!is_directory(git_dir)) {
Jens Lehmannee6fc512010-01-16 18:42:24 +0100838 strbuf_release(&buf);
839 /* The submodule is not checked out, so it is not modified */
840 return 0;
841
842 }
843 strbuf_reset(&buf);
844
Jens Lehmann3bfc4502010-03-13 23:00:27 +0100845 if (ignore_untracked)
846 argv[2] = "-uno";
847
Jens Lehmannee6fc512010-01-16 18:42:24 +0100848 cp.argv = argv;
Jeff Kingc12e8652016-04-28 09:39:15 -0400849 prepare_submodule_repo_env(&cp.env_array);
Jens Lehmannee6fc512010-01-16 18:42:24 +0100850 cp.git_cmd = 1;
851 cp.no_stdin = 1;
852 cp.out = -1;
Jens Lehmanneee49b62010-04-10 19:01:12 +0200853 cp.dir = path;
Jens Lehmannee6fc512010-01-16 18:42:24 +0100854 if (start_command(&cp))
Jens Lehmann6a5ceda2011-12-07 22:50:14 +0100855 die("Could not run 'git status --porcelain' in submodule %s", path);
Jens Lehmannee6fc512010-01-16 18:42:24 +0100856
857 len = strbuf_read(&buf, cp.out, 1024);
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100858 line = buf.buf;
859 while (len > 2) {
860 if ((line[0] == '?') && (line[1] == '?')) {
861 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
862 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
863 break;
864 } else {
865 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
Jens Lehmann3bfc4502010-03-13 23:00:27 +0100866 if (ignore_untracked ||
867 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100868 break;
869 }
870 next_line = strchr(line, '\n');
871 if (!next_line)
872 break;
873 next_line++;
874 len -= (next_line - line);
875 line = next_line;
876 }
Jens Lehmannee6fc512010-01-16 18:42:24 +0100877 close(cp.out);
878
879 if (finish_command(&cp))
Jens Lehmann6a5ceda2011-12-07 22:50:14 +0100880 die("'git status --porcelain' failed in submodule %s", path);
Jens Lehmannee6fc512010-01-16 18:42:24 +0100881
Jens Lehmannee6fc512010-01-16 18:42:24 +0100882 strbuf_release(&buf);
Jens Lehmannc7e1a732010-03-04 22:20:33 +0100883 return dirty_submodule;
Jens Lehmannee6fc512010-01-16 18:42:24 +0100884}
Heiko Voigt68d03e42010-07-07 15:39:13 +0200885
Jens Lehmann293ab152012-09-26 20:21:13 +0200886int submodule_uses_gitfile(const char *path)
887{
René Scharfed3180272014-08-19 21:09:35 +0200888 struct child_process cp = CHILD_PROCESS_INIT;
Jens Lehmann293ab152012-09-26 20:21:13 +0200889 const char *argv[] = {
890 "submodule",
891 "foreach",
892 "--quiet",
893 "--recursive",
894 "test -f .git",
895 NULL,
896 };
897 struct strbuf buf = STRBUF_INIT;
898 const char *git_dir;
899
900 strbuf_addf(&buf, "%s/.git", path);
901 git_dir = read_gitfile(buf.buf);
902 if (!git_dir) {
903 strbuf_release(&buf);
904 return 0;
905 }
906 strbuf_release(&buf);
907
908 /* Now test that all nested submodules use a gitfile too */
Jens Lehmann293ab152012-09-26 20:21:13 +0200909 cp.argv = argv;
Jeff Kingc12e8652016-04-28 09:39:15 -0400910 prepare_submodule_repo_env(&cp.env_array);
Jens Lehmann293ab152012-09-26 20:21:13 +0200911 cp.git_cmd = 1;
912 cp.no_stdin = 1;
913 cp.no_stderr = 1;
914 cp.no_stdout = 1;
915 cp.dir = path;
916 if (run_command(&cp))
917 return 0;
918
919 return 1;
920}
921
922int ok_to_remove_submodule(const char *path)
923{
Jens Lehmann293ab152012-09-26 20:21:13 +0200924 ssize_t len;
René Scharfed3180272014-08-19 21:09:35 +0200925 struct child_process cp = CHILD_PROCESS_INIT;
Jens Lehmann293ab152012-09-26 20:21:13 +0200926 const char *argv[] = {
927 "status",
928 "--porcelain",
929 "-u",
930 "--ignore-submodules=none",
931 NULL,
932 };
933 struct strbuf buf = STRBUF_INIT;
934 int ok_to_remove = 1;
935
René Scharfedbe44fa2015-05-19 23:44:23 +0200936 if (!file_exists(path) || is_empty_dir(path))
Jens Lehmann293ab152012-09-26 20:21:13 +0200937 return 1;
938
939 if (!submodule_uses_gitfile(path))
940 return 0;
941
Jens Lehmann293ab152012-09-26 20:21:13 +0200942 cp.argv = argv;
Jeff Kingc12e8652016-04-28 09:39:15 -0400943 prepare_submodule_repo_env(&cp.env_array);
Jens Lehmann293ab152012-09-26 20:21:13 +0200944 cp.git_cmd = 1;
945 cp.no_stdin = 1;
946 cp.out = -1;
947 cp.dir = path;
948 if (start_command(&cp))
949 die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path);
950
951 len = strbuf_read(&buf, cp.out, 1024);
952 if (len > 2)
953 ok_to_remove = 0;
954 close(cp.out);
955
956 if (finish_command(&cp))
957 die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path);
958
959 strbuf_release(&buf);
960 return ok_to_remove;
961}
962
Heiko Voigt68d03e42010-07-07 15:39:13 +0200963static int find_first_merges(struct object_array *result, const char *path,
964 struct commit *a, struct commit *b)
965{
966 int i, j;
Michael Haggerty38269022013-05-25 11:08:12 +0200967 struct object_array merges = OBJECT_ARRAY_INIT;
Heiko Voigt68d03e42010-07-07 15:39:13 +0200968 struct commit *commit;
969 int contains_another;
970
971 char merged_revision[42];
972 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
973 "--all", merged_revision, NULL };
974 struct rev_info revs;
975 struct setup_revision_opt rev_opts;
976
Heiko Voigt68d03e42010-07-07 15:39:13 +0200977 memset(result, 0, sizeof(struct object_array));
978 memset(&rev_opts, 0, sizeof(rev_opts));
979
980 /* get all revisions that merge commit a */
981 snprintf(merged_revision, sizeof(merged_revision), "^%s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000982 oid_to_hex(&a->object.oid));
Heiko Voigt68d03e42010-07-07 15:39:13 +0200983 init_revisions(&revs, NULL);
984 rev_opts.submodule = path;
Elia Pinto94c0cc82014-06-27 05:11:39 -0700985 setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
Heiko Voigt68d03e42010-07-07 15:39:13 +0200986
987 /* save all revisions from the above list that contain b */
988 if (prepare_revision_walk(&revs))
989 die("revision walk setup failed");
990 while ((commit = get_revision(&revs)) != NULL) {
991 struct object *o = &(commit->object);
Junio C Hamanoa20efee2012-08-27 14:46:01 -0700992 if (in_merge_bases(b, commit))
Heiko Voigt68d03e42010-07-07 15:39:13 +0200993 add_object_array(o, NULL, &merges);
994 }
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +0200995 reset_revision_walk();
Heiko Voigt68d03e42010-07-07 15:39:13 +0200996
997 /* Now we've got all merges that contain a and b. Prune all
998 * merges that contain another found merge and save them in
999 * result.
1000 */
1001 for (i = 0; i < merges.nr; i++) {
1002 struct commit *m1 = (struct commit *) merges.objects[i].item;
1003
1004 contains_another = 0;
1005 for (j = 0; j < merges.nr; j++) {
1006 struct commit *m2 = (struct commit *) merges.objects[j].item;
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001007 if (i != j && in_merge_bases(m2, m1)) {
Heiko Voigt68d03e42010-07-07 15:39:13 +02001008 contains_another = 1;
1009 break;
1010 }
1011 }
1012
1013 if (!contains_another)
Michael Haggerty5de0c012013-05-25 11:08:13 +02001014 add_object_array(merges.objects[i].item, NULL, result);
Heiko Voigt68d03e42010-07-07 15:39:13 +02001015 }
1016
1017 free(merges.objects);
1018 return result->nr;
1019}
1020
1021static void print_commit(struct commit *commit)
1022{
1023 struct strbuf sb = STRBUF_INIT;
1024 struct pretty_print_context ctx = {0};
Jeff Kinga5481a62015-06-25 12:55:02 -04001025 ctx.date_mode.type = DATE_NORMAL;
Heiko Voigt68d03e42010-07-07 15:39:13 +02001026 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
1027 fprintf(stderr, "%s\n", sb.buf);
1028 strbuf_release(&sb);
1029}
1030
1031#define MERGE_WARNING(path, msg) \
1032 warning("Failed to merge submodule %s (%s)", path, msg);
1033
1034int merge_submodule(unsigned char result[20], const char *path,
1035 const unsigned char base[20], const unsigned char a[20],
Brad King80988782011-10-13 08:59:05 -04001036 const unsigned char b[20], int search)
Heiko Voigt68d03e42010-07-07 15:39:13 +02001037{
1038 struct commit *commit_base, *commit_a, *commit_b;
1039 int parent_count;
1040 struct object_array merges;
1041
1042 int i;
1043
1044 /* store a in result in case we fail */
1045 hashcpy(result, a);
1046
1047 /* we can not handle deletion conflicts */
1048 if (is_null_sha1(base))
1049 return 0;
1050 if (is_null_sha1(a))
1051 return 0;
1052 if (is_null_sha1(b))
1053 return 0;
1054
1055 if (add_submodule_odb(path)) {
1056 MERGE_WARNING(path, "not checked out");
1057 return 0;
1058 }
1059
1060 if (!(commit_base = lookup_commit_reference(base)) ||
1061 !(commit_a = lookup_commit_reference(a)) ||
1062 !(commit_b = lookup_commit_reference(b))) {
1063 MERGE_WARNING(path, "commits not present");
1064 return 0;
1065 }
1066
1067 /* check whether both changes are forward */
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001068 if (!in_merge_bases(commit_base, commit_a) ||
1069 !in_merge_bases(commit_base, commit_b)) {
Heiko Voigt68d03e42010-07-07 15:39:13 +02001070 MERGE_WARNING(path, "commits don't follow merge-base");
1071 return 0;
1072 }
1073
1074 /* Case #1: a is contained in b or vice versa */
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001075 if (in_merge_bases(commit_a, commit_b)) {
Heiko Voigt68d03e42010-07-07 15:39:13 +02001076 hashcpy(result, b);
1077 return 1;
1078 }
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001079 if (in_merge_bases(commit_b, commit_a)) {
Heiko Voigt68d03e42010-07-07 15:39:13 +02001080 hashcpy(result, a);
1081 return 1;
1082 }
1083
1084 /*
1085 * Case #2: There are one or more merges that contain a and b in
1086 * the submodule. If there is only one, then present it as a
1087 * suggestion to the user, but leave it marked unmerged so the
1088 * user needs to confirm the resolution.
1089 */
1090
Brad King80988782011-10-13 08:59:05 -04001091 /* Skip the search if makes no sense to the calling context. */
1092 if (!search)
1093 return 0;
1094
Heiko Voigt68d03e42010-07-07 15:39:13 +02001095 /* find commit which merges them */
1096 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
1097 switch (parent_count) {
1098 case 0:
1099 MERGE_WARNING(path, "merge following commits not found");
1100 break;
1101
1102 case 1:
1103 MERGE_WARNING(path, "not fast-forward");
1104 fprintf(stderr, "Found a possible merge resolution "
1105 "for the submodule:\n");
1106 print_commit((struct commit *) merges.objects[0].item);
1107 fprintf(stderr,
1108 "If this is correct simply add it to the index "
1109 "for example\n"
1110 "by using:\n\n"
1111 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1112 "which will accept this suggestion.\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001113 oid_to_hex(&merges.objects[0].item->oid), path);
Heiko Voigt68d03e42010-07-07 15:39:13 +02001114 break;
1115
1116 default:
1117 MERGE_WARNING(path, "multiple merges found");
1118 for (i = 0; i < merges.nr; i++)
1119 print_commit((struct commit *) merges.objects[i].item);
1120 }
1121
1122 free(merges.objects);
1123 return 0;
1124}
Jens Lehmanna88c9152013-07-30 21:50:03 +02001125
1126/* Update gitfile and core.worktree setting to connect work tree and git dir */
1127void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
1128{
1129 struct strbuf file_name = STRBUF_INIT;
1130 struct strbuf rel_path = STRBUF_INIT;
1131 const char *real_work_tree = xstrdup(real_path(work_tree));
Jens Lehmanna88c9152013-07-30 21:50:03 +02001132
1133 /* Update gitfile */
1134 strbuf_addf(&file_name, "%s/.git", work_tree);
Junio C Hamano1f76a102015-08-24 13:20:39 -07001135 write_file(file_name.buf, "gitdir: %s",
Nguyễn Thái Ngọc Duy91aacda2014-11-30 15:24:46 +07001136 relative_path(git_dir, real_work_tree, &rel_path));
Jens Lehmanna88c9152013-07-30 21:50:03 +02001137
1138 /* Update core.worktree setting */
1139 strbuf_reset(&file_name);
1140 strbuf_addf(&file_name, "%s/config", git_dir);
Patrick Steinhardt3d180642016-02-22 12:23:36 +01001141 git_config_set_in_file(file_name.buf, "core.worktree",
1142 relative_path(real_work_tree, git_dir,
1143 &rel_path));
Jens Lehmanna88c9152013-07-30 21:50:03 +02001144
1145 strbuf_release(&file_name);
1146 strbuf_release(&rel_path);
1147 free((void *)real_work_tree);
1148}
Stefan Bellera028a192016-02-29 18:07:13 -08001149
1150int parallel_submodules(void)
1151{
1152 return parallel_jobs;
1153}
Junio C Hamanoe0593882016-05-17 14:38:25 -07001154
Jeff King46387282016-04-28 09:38:20 -04001155void prepare_submodule_repo_env(struct argv_array *out)
1156{
1157 const char * const *var;
1158
1159 for (var = local_repo_env; *var; var++) {
Jeff King89044ba2016-05-04 21:22:19 -04001160 if (strcmp(*var, CONFIG_DATA_ENVIRONMENT))
Jeff King46387282016-04-28 09:38:20 -04001161 argv_array_push(out, *var);
Jeff King46387282016-04-28 09:38:20 -04001162 }
Jeff King46387282016-04-28 09:38:20 -04001163}