/src/git/builtin/submodule--helper.c
Line | Count | Source (jump to first uncovered line) |
1 | | #define USE_THE_INDEX_VARIABLE |
2 | | #include "builtin.h" |
3 | | #include "abspath.h" |
4 | | #include "environment.h" |
5 | | #include "gettext.h" |
6 | | #include "hex.h" |
7 | | #include "repository.h" |
8 | | #include "config.h" |
9 | | #include "parse-options.h" |
10 | | #include "quote.h" |
11 | | #include "path.h" |
12 | | #include "pathspec.h" |
13 | | #include "preload-index.h" |
14 | | #include "dir.h" |
15 | | #include "read-cache.h" |
16 | | #include "setup.h" |
17 | | #include "sparse-index.h" |
18 | | #include "submodule.h" |
19 | | #include "submodule-config.h" |
20 | | #include "string-list.h" |
21 | | #include "run-command.h" |
22 | | #include "remote.h" |
23 | | #include "refs.h" |
24 | | #include "refspec.h" |
25 | | #include "connect.h" |
26 | | #include "revision.h" |
27 | | #include "diffcore.h" |
28 | | #include "diff.h" |
29 | | #include "object-file.h" |
30 | | #include "object-name.h" |
31 | | #include "object-store-ll.h" |
32 | | #include "advice.h" |
33 | | #include "branch.h" |
34 | | #include "list-objects-filter-options.h" |
35 | | |
36 | 0 | #define OPT_QUIET (1 << 0) |
37 | 0 | #define OPT_CACHED (1 << 1) |
38 | 0 | #define OPT_RECURSIVE (1 << 2) |
39 | 0 | #define OPT_FORCE (1 << 3) |
40 | | |
41 | | typedef void (*each_submodule_fn)(const struct cache_entry *list_item, |
42 | | void *cb_data); |
43 | | |
44 | | static int repo_get_default_remote(struct repository *repo, char **default_remote) |
45 | 0 | { |
46 | 0 | char *dest = NULL; |
47 | 0 | struct strbuf sb = STRBUF_INIT; |
48 | 0 | struct ref_store *store = get_main_ref_store(repo); |
49 | 0 | const char *refname = refs_resolve_ref_unsafe(store, "HEAD", 0, NULL, |
50 | 0 | NULL); |
51 | |
|
52 | 0 | if (!refname) |
53 | 0 | return die_message(_("No such ref: %s"), "HEAD"); |
54 | | |
55 | | /* detached HEAD */ |
56 | 0 | if (!strcmp(refname, "HEAD")) { |
57 | 0 | *default_remote = xstrdup("origin"); |
58 | 0 | return 0; |
59 | 0 | } |
60 | | |
61 | 0 | if (!skip_prefix(refname, "refs/heads/", &refname)) |
62 | 0 | return die_message(_("Expecting a full ref name, got %s"), |
63 | 0 | refname); |
64 | | |
65 | 0 | strbuf_addf(&sb, "branch.%s.remote", refname); |
66 | 0 | if (repo_config_get_string(repo, sb.buf, &dest)) |
67 | 0 | *default_remote = xstrdup("origin"); |
68 | 0 | else |
69 | 0 | *default_remote = dest; |
70 | |
|
71 | 0 | strbuf_release(&sb); |
72 | 0 | return 0; |
73 | 0 | } |
74 | | |
75 | | static int get_default_remote_submodule(const char *module_path, char **default_remote) |
76 | 0 | { |
77 | 0 | struct repository subrepo; |
78 | 0 | int ret; |
79 | |
|
80 | 0 | if (repo_submodule_init(&subrepo, the_repository, module_path, |
81 | 0 | null_oid()) < 0) |
82 | 0 | return die_message(_("could not get a repository handle for submodule '%s'"), |
83 | 0 | module_path); |
84 | 0 | ret = repo_get_default_remote(&subrepo, default_remote); |
85 | 0 | repo_clear(&subrepo); |
86 | |
|
87 | 0 | return ret; |
88 | 0 | } |
89 | | |
90 | | static char *get_default_remote(void) |
91 | 0 | { |
92 | 0 | char *default_remote; |
93 | 0 | int code = repo_get_default_remote(the_repository, &default_remote); |
94 | |
|
95 | 0 | if (code) |
96 | 0 | exit(code); |
97 | | |
98 | 0 | return default_remote; |
99 | 0 | } |
100 | | |
101 | | static char *resolve_relative_url(const char *rel_url, const char *up_path, int quiet) |
102 | 0 | { |
103 | 0 | char *remoteurl, *resolved_url; |
104 | 0 | char *remote = get_default_remote(); |
105 | 0 | struct strbuf remotesb = STRBUF_INIT; |
106 | |
|
107 | 0 | strbuf_addf(&remotesb, "remote.%s.url", remote); |
108 | 0 | if (git_config_get_string(remotesb.buf, &remoteurl)) { |
109 | 0 | if (!quiet) |
110 | 0 | warning(_("could not look up configuration '%s'. " |
111 | 0 | "Assuming this repository is its own " |
112 | 0 | "authoritative upstream."), |
113 | 0 | remotesb.buf); |
114 | 0 | remoteurl = xgetcwd(); |
115 | 0 | } |
116 | 0 | resolved_url = relative_url(remoteurl, rel_url, up_path); |
117 | |
|
118 | 0 | free(remote); |
119 | 0 | free(remoteurl); |
120 | 0 | strbuf_release(&remotesb); |
121 | |
|
122 | 0 | return resolved_url; |
123 | 0 | } |
124 | | |
125 | | /* the result should be freed by the caller. */ |
126 | | static char *get_submodule_displaypath(const char *path, const char *prefix, |
127 | | const char *super_prefix) |
128 | 0 | { |
129 | 0 | if (prefix && super_prefix) { |
130 | 0 | BUG("cannot have prefix '%s' and superprefix '%s'", |
131 | 0 | prefix, super_prefix); |
132 | 0 | } else if (prefix) { |
133 | 0 | struct strbuf sb = STRBUF_INIT; |
134 | 0 | char *displaypath = xstrdup(relative_path(path, prefix, &sb)); |
135 | 0 | strbuf_release(&sb); |
136 | 0 | return displaypath; |
137 | 0 | } else if (super_prefix) { |
138 | 0 | return xstrfmt("%s%s", super_prefix, path); |
139 | 0 | } else { |
140 | 0 | return xstrdup(path); |
141 | 0 | } |
142 | 0 | } |
143 | | |
144 | | static char *compute_rev_name(const char *sub_path, const char* object_id) |
145 | 0 | { |
146 | 0 | struct strbuf sb = STRBUF_INIT; |
147 | 0 | const char ***d; |
148 | |
|
149 | 0 | static const char *describe_bare[] = { NULL }; |
150 | |
|
151 | 0 | static const char *describe_tags[] = { "--tags", NULL }; |
152 | |
|
153 | 0 | static const char *describe_contains[] = { "--contains", NULL }; |
154 | |
|
155 | 0 | static const char *describe_all_always[] = { "--all", "--always", NULL }; |
156 | |
|
157 | 0 | static const char **describe_argv[] = { describe_bare, describe_tags, |
158 | 0 | describe_contains, |
159 | 0 | describe_all_always, NULL }; |
160 | |
|
161 | 0 | for (d = describe_argv; *d; d++) { |
162 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
163 | 0 | prepare_submodule_repo_env(&cp.env); |
164 | 0 | cp.dir = sub_path; |
165 | 0 | cp.git_cmd = 1; |
166 | 0 | cp.no_stderr = 1; |
167 | |
|
168 | 0 | strvec_push(&cp.args, "describe"); |
169 | 0 | strvec_pushv(&cp.args, *d); |
170 | 0 | strvec_push(&cp.args, object_id); |
171 | |
|
172 | 0 | if (!capture_command(&cp, &sb, 0)) { |
173 | 0 | strbuf_strip_suffix(&sb, "\n"); |
174 | 0 | return strbuf_detach(&sb, NULL); |
175 | 0 | } |
176 | 0 | } |
177 | | |
178 | 0 | strbuf_release(&sb); |
179 | 0 | return NULL; |
180 | 0 | } |
181 | | |
182 | | struct module_list { |
183 | | const struct cache_entry **entries; |
184 | | int alloc, nr; |
185 | | }; |
186 | 0 | #define MODULE_LIST_INIT { 0 } |
187 | | |
188 | | static void module_list_release(struct module_list *ml) |
189 | 0 | { |
190 | 0 | free(ml->entries); |
191 | 0 | } |
192 | | |
193 | | static int module_list_compute(const char **argv, |
194 | | const char *prefix, |
195 | | struct pathspec *pathspec, |
196 | | struct module_list *list) |
197 | 0 | { |
198 | 0 | int i, result = 0; |
199 | 0 | char *ps_matched = NULL; |
200 | |
|
201 | 0 | parse_pathspec(pathspec, 0, |
202 | 0 | PATHSPEC_PREFER_FULL, |
203 | 0 | prefix, argv); |
204 | |
|
205 | 0 | if (pathspec->nr) |
206 | 0 | ps_matched = xcalloc(pathspec->nr, 1); |
207 | |
|
208 | 0 | if (repo_read_index(the_repository) < 0) |
209 | 0 | die(_("index file corrupt")); |
210 | | |
211 | 0 | for (i = 0; i < the_index.cache_nr; i++) { |
212 | 0 | const struct cache_entry *ce = the_index.cache[i]; |
213 | |
|
214 | 0 | if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce), |
215 | 0 | 0, ps_matched, 1) || |
216 | 0 | !S_ISGITLINK(ce->ce_mode)) |
217 | 0 | continue; |
218 | | |
219 | 0 | ALLOC_GROW(list->entries, list->nr + 1, list->alloc); |
220 | 0 | list->entries[list->nr++] = ce; |
221 | 0 | while (i + 1 < the_index.cache_nr && |
222 | 0 | !strcmp(ce->name, the_index.cache[i + 1]->name)) |
223 | | /* |
224 | | * Skip entries with the same name in different stages |
225 | | * to make sure an entry is returned only once. |
226 | | */ |
227 | 0 | i++; |
228 | 0 | } |
229 | |
|
230 | 0 | if (ps_matched && report_path_error(ps_matched, pathspec)) |
231 | 0 | result = -1; |
232 | |
|
233 | 0 | free(ps_matched); |
234 | |
|
235 | 0 | return result; |
236 | 0 | } |
237 | | |
238 | | static void module_list_active(struct module_list *list) |
239 | 0 | { |
240 | 0 | int i; |
241 | 0 | struct module_list active_modules = MODULE_LIST_INIT; |
242 | |
|
243 | 0 | for (i = 0; i < list->nr; i++) { |
244 | 0 | const struct cache_entry *ce = list->entries[i]; |
245 | |
|
246 | 0 | if (!is_submodule_active(the_repository, ce->name)) |
247 | 0 | continue; |
248 | | |
249 | 0 | ALLOC_GROW(active_modules.entries, |
250 | 0 | active_modules.nr + 1, |
251 | 0 | active_modules.alloc); |
252 | 0 | active_modules.entries[active_modules.nr++] = ce; |
253 | 0 | } |
254 | |
|
255 | 0 | module_list_release(list); |
256 | 0 | *list = active_modules; |
257 | 0 | } |
258 | | |
259 | | static char *get_up_path(const char *path) |
260 | 0 | { |
261 | 0 | int i; |
262 | 0 | struct strbuf sb = STRBUF_INIT; |
263 | |
|
264 | 0 | for (i = count_slashes(path); i; i--) |
265 | 0 | strbuf_addstr(&sb, "../"); |
266 | | |
267 | | /* |
268 | | * Check if 'path' ends with slash or not |
269 | | * for having the same output for dir/sub_dir |
270 | | * and dir/sub_dir/ |
271 | | */ |
272 | 0 | if (!is_dir_sep(path[strlen(path) - 1])) |
273 | 0 | strbuf_addstr(&sb, "../"); |
274 | |
|
275 | 0 | return strbuf_detach(&sb, NULL); |
276 | 0 | } |
277 | | |
278 | | static void for_each_listed_submodule(const struct module_list *list, |
279 | | each_submodule_fn fn, void *cb_data) |
280 | 0 | { |
281 | 0 | int i; |
282 | |
|
283 | 0 | for (i = 0; i < list->nr; i++) |
284 | 0 | fn(list->entries[i], cb_data); |
285 | 0 | } |
286 | | |
287 | | struct foreach_cb { |
288 | | int argc; |
289 | | const char **argv; |
290 | | const char *prefix; |
291 | | const char *super_prefix; |
292 | | int quiet; |
293 | | int recursive; |
294 | | }; |
295 | 0 | #define FOREACH_CB_INIT { 0 } |
296 | | |
297 | | static void runcommand_in_submodule_cb(const struct cache_entry *list_item, |
298 | | void *cb_data) |
299 | 0 | { |
300 | 0 | struct foreach_cb *info = cb_data; |
301 | 0 | const char *path = list_item->name; |
302 | 0 | const struct object_id *ce_oid = &list_item->oid; |
303 | 0 | const struct submodule *sub; |
304 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
305 | 0 | char *displaypath; |
306 | |
|
307 | 0 | displaypath = get_submodule_displaypath(path, info->prefix, |
308 | 0 | info->super_prefix); |
309 | |
|
310 | 0 | sub = submodule_from_path(the_repository, null_oid(), path); |
311 | |
|
312 | 0 | if (!sub) |
313 | 0 | die(_("No url found for submodule path '%s' in .gitmodules"), |
314 | 0 | displaypath); |
315 | | |
316 | 0 | if (!is_submodule_populated_gently(path, NULL)) |
317 | 0 | goto cleanup; |
318 | | |
319 | 0 | prepare_submodule_repo_env(&cp.env); |
320 | | |
321 | | /* |
322 | | * For the purpose of executing <command> in the submodule, |
323 | | * separate shell is used for the purpose of running the |
324 | | * child process. |
325 | | */ |
326 | 0 | cp.use_shell = 1; |
327 | 0 | cp.dir = path; |
328 | | |
329 | | /* |
330 | | * NEEDSWORK: the command currently has access to the variables $name, |
331 | | * $sm_path, $displaypath, $sha1 and $toplevel only when the command |
332 | | * contains a single argument. This is done for maintaining a faithful |
333 | | * translation from shell script. |
334 | | */ |
335 | 0 | if (info->argc == 1) { |
336 | 0 | char *toplevel = xgetcwd(); |
337 | 0 | struct strbuf sb = STRBUF_INIT; |
338 | |
|
339 | 0 | strvec_pushf(&cp.env, "name=%s", sub->name); |
340 | 0 | strvec_pushf(&cp.env, "sm_path=%s", path); |
341 | 0 | strvec_pushf(&cp.env, "displaypath=%s", displaypath); |
342 | 0 | strvec_pushf(&cp.env, "sha1=%s", |
343 | 0 | oid_to_hex(ce_oid)); |
344 | 0 | strvec_pushf(&cp.env, "toplevel=%s", toplevel); |
345 | | |
346 | | /* |
347 | | * Since the path variable was accessible from the script |
348 | | * before porting, it is also made available after porting. |
349 | | * The environment variable "PATH" has a very special purpose |
350 | | * on windows. And since environment variables are |
351 | | * case-insensitive in windows, it interferes with the |
352 | | * existing PATH variable. Hence, to avoid that, we expose |
353 | | * path via the args strvec and not via env. |
354 | | */ |
355 | 0 | sq_quote_buf(&sb, path); |
356 | 0 | strvec_pushf(&cp.args, "path=%s; %s", |
357 | 0 | sb.buf, info->argv[0]); |
358 | 0 | strbuf_release(&sb); |
359 | 0 | free(toplevel); |
360 | 0 | } else { |
361 | 0 | strvec_pushv(&cp.args, info->argv); |
362 | 0 | } |
363 | |
|
364 | 0 | if (!info->quiet) |
365 | 0 | printf(_("Entering '%s'\n"), displaypath); |
366 | |
|
367 | 0 | if (info->argv[0] && run_command(&cp)) |
368 | 0 | die(_("run_command returned non-zero status for %s\n."), |
369 | 0 | displaypath); |
370 | | |
371 | 0 | if (info->recursive) { |
372 | 0 | struct child_process cpr = CHILD_PROCESS_INIT; |
373 | |
|
374 | 0 | cpr.git_cmd = 1; |
375 | 0 | cpr.dir = path; |
376 | 0 | prepare_submodule_repo_env(&cpr.env); |
377 | |
|
378 | 0 | strvec_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive", |
379 | 0 | NULL); |
380 | 0 | strvec_pushl(&cpr.args, "--super-prefix", NULL); |
381 | 0 | strvec_pushf(&cpr.args, "%s/", displaypath); |
382 | |
|
383 | 0 | if (info->quiet) |
384 | 0 | strvec_push(&cpr.args, "--quiet"); |
385 | |
|
386 | 0 | strvec_push(&cpr.args, "--"); |
387 | 0 | strvec_pushv(&cpr.args, info->argv); |
388 | |
|
389 | 0 | if (run_command(&cpr)) |
390 | 0 | die(_("run_command returned non-zero status while " |
391 | 0 | "recursing in the nested submodules of %s\n."), |
392 | 0 | displaypath); |
393 | 0 | } |
394 | | |
395 | 0 | cleanup: |
396 | 0 | free(displaypath); |
397 | 0 | } |
398 | | |
399 | | static int module_foreach(int argc, const char **argv, const char *prefix) |
400 | 0 | { |
401 | 0 | struct foreach_cb info = FOREACH_CB_INIT; |
402 | 0 | struct pathspec pathspec = { 0 }; |
403 | 0 | struct module_list list = MODULE_LIST_INIT; |
404 | 0 | struct option module_foreach_options[] = { |
405 | 0 | OPT__SUPER_PREFIX(&info.super_prefix), |
406 | 0 | OPT__QUIET(&info.quiet, N_("suppress output of entering each submodule command")), |
407 | 0 | OPT_BOOL(0, "recursive", &info.recursive, |
408 | 0 | N_("recurse into nested submodules")), |
409 | 0 | OPT_END() |
410 | 0 | }; |
411 | 0 | const char *const git_submodule_helper_usage[] = { |
412 | 0 | N_("git submodule foreach [--quiet] [--recursive] [--] <command>"), |
413 | 0 | NULL |
414 | 0 | }; |
415 | 0 | int ret = 1; |
416 | |
|
417 | 0 | argc = parse_options(argc, argv, prefix, module_foreach_options, |
418 | 0 | git_submodule_helper_usage, 0); |
419 | |
|
420 | 0 | if (module_list_compute(NULL, prefix, &pathspec, &list) < 0) |
421 | 0 | goto cleanup; |
422 | | |
423 | 0 | info.argc = argc; |
424 | 0 | info.argv = argv; |
425 | 0 | info.prefix = prefix; |
426 | |
|
427 | 0 | for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info); |
428 | |
|
429 | 0 | ret = 0; |
430 | 0 | cleanup: |
431 | 0 | module_list_release(&list); |
432 | 0 | clear_pathspec(&pathspec); |
433 | 0 | return ret; |
434 | 0 | } |
435 | | |
436 | | static int starts_with_dot_slash(const char *const path) |
437 | 0 | { |
438 | 0 | return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH | |
439 | 0 | PATH_MATCH_XPLATFORM); |
440 | 0 | } |
441 | | |
442 | | static int starts_with_dot_dot_slash(const char *const path) |
443 | 0 | { |
444 | 0 | return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH | |
445 | 0 | PATH_MATCH_XPLATFORM); |
446 | 0 | } |
447 | | |
448 | | struct init_cb { |
449 | | const char *prefix; |
450 | | const char *super_prefix; |
451 | | unsigned int flags; |
452 | | }; |
453 | 0 | #define INIT_CB_INIT { 0 } |
454 | | |
455 | | static void init_submodule(const char *path, const char *prefix, |
456 | | const char *super_prefix, |
457 | | unsigned int flags) |
458 | 0 | { |
459 | 0 | const struct submodule *sub; |
460 | 0 | struct strbuf sb = STRBUF_INIT; |
461 | 0 | const char *upd; |
462 | 0 | char *url = NULL, *displaypath; |
463 | |
|
464 | 0 | displaypath = get_submodule_displaypath(path, prefix, super_prefix); |
465 | |
|
466 | 0 | sub = submodule_from_path(the_repository, null_oid(), path); |
467 | |
|
468 | 0 | if (!sub) |
469 | 0 | die(_("No url found for submodule path '%s' in .gitmodules"), |
470 | 0 | displaypath); |
471 | | |
472 | | /* |
473 | | * NEEDSWORK: In a multi-working-tree world, this needs to be |
474 | | * set in the per-worktree config. |
475 | | * |
476 | | * Set active flag for the submodule being initialized |
477 | | */ |
478 | 0 | if (!is_submodule_active(the_repository, path)) { |
479 | 0 | strbuf_addf(&sb, "submodule.%s.active", sub->name); |
480 | 0 | git_config_set_gently(sb.buf, "true"); |
481 | 0 | strbuf_reset(&sb); |
482 | 0 | } |
483 | | |
484 | | /* |
485 | | * Copy url setting when it is not set yet. |
486 | | * To look up the url in .git/config, we must not fall back to |
487 | | * .gitmodules, so look it up directly. |
488 | | */ |
489 | 0 | strbuf_addf(&sb, "submodule.%s.url", sub->name); |
490 | 0 | if (git_config_get_string(sb.buf, &url)) { |
491 | 0 | if (!sub->url) |
492 | 0 | die(_("No url found for submodule path '%s' in .gitmodules"), |
493 | 0 | displaypath); |
494 | | |
495 | 0 | url = xstrdup(sub->url); |
496 | | |
497 | | /* Possibly a url relative to parent */ |
498 | 0 | if (starts_with_dot_dot_slash(url) || |
499 | 0 | starts_with_dot_slash(url)) { |
500 | 0 | char *oldurl = url; |
501 | |
|
502 | 0 | url = resolve_relative_url(oldurl, NULL, 0); |
503 | 0 | free(oldurl); |
504 | 0 | } |
505 | |
|
506 | 0 | if (git_config_set_gently(sb.buf, url)) |
507 | 0 | die(_("Failed to register url for submodule path '%s'"), |
508 | 0 | displaypath); |
509 | 0 | if (!(flags & OPT_QUIET)) |
510 | 0 | fprintf(stderr, |
511 | 0 | _("Submodule '%s' (%s) registered for path '%s'\n"), |
512 | 0 | sub->name, url, displaypath); |
513 | 0 | } |
514 | 0 | strbuf_reset(&sb); |
515 | | |
516 | | /* Copy "update" setting when it is not set yet */ |
517 | 0 | strbuf_addf(&sb, "submodule.%s.update", sub->name); |
518 | 0 | if (git_config_get_string_tmp(sb.buf, &upd) && |
519 | 0 | sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) { |
520 | 0 | if (sub->update_strategy.type == SM_UPDATE_COMMAND) { |
521 | 0 | fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"), |
522 | 0 | sub->name); |
523 | 0 | upd = "none"; |
524 | 0 | } else { |
525 | 0 | upd = submodule_update_type_to_string(sub->update_strategy.type); |
526 | 0 | } |
527 | |
|
528 | 0 | if (git_config_set_gently(sb.buf, upd)) |
529 | 0 | die(_("Failed to register update mode for submodule path '%s'"), displaypath); |
530 | 0 | } |
531 | 0 | strbuf_release(&sb); |
532 | 0 | free(displaypath); |
533 | 0 | free(url); |
534 | 0 | } |
535 | | |
536 | | static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data) |
537 | 0 | { |
538 | 0 | struct init_cb *info = cb_data; |
539 | |
|
540 | 0 | init_submodule(list_item->name, info->prefix, info->super_prefix, |
541 | 0 | info->flags); |
542 | 0 | } |
543 | | |
544 | | static int module_init(int argc, const char **argv, const char *prefix) |
545 | 0 | { |
546 | 0 | struct init_cb info = INIT_CB_INIT; |
547 | 0 | struct pathspec pathspec = { 0 }; |
548 | 0 | struct module_list list = MODULE_LIST_INIT; |
549 | 0 | int quiet = 0; |
550 | 0 | struct option module_init_options[] = { |
551 | 0 | OPT__QUIET(&quiet, N_("suppress output for initializing a submodule")), |
552 | 0 | OPT_END() |
553 | 0 | }; |
554 | 0 | const char *const git_submodule_helper_usage[] = { |
555 | 0 | N_("git submodule init [<options>] [<path>]"), |
556 | 0 | NULL |
557 | 0 | }; |
558 | 0 | int ret = 1; |
559 | |
|
560 | 0 | argc = parse_options(argc, argv, prefix, module_init_options, |
561 | 0 | git_submodule_helper_usage, 0); |
562 | |
|
563 | 0 | if (module_list_compute(argv, prefix, &pathspec, &list) < 0) |
564 | 0 | goto cleanup; |
565 | | |
566 | | /* |
567 | | * If there are no path args and submodule.active is set then, |
568 | | * by default, only initialize 'active' modules. |
569 | | */ |
570 | 0 | if (!argc && !git_config_get("submodule.active")) |
571 | 0 | module_list_active(&list); |
572 | |
|
573 | 0 | info.prefix = prefix; |
574 | 0 | if (quiet) |
575 | 0 | info.flags |= OPT_QUIET; |
576 | |
|
577 | 0 | for_each_listed_submodule(&list, init_submodule_cb, &info); |
578 | |
|
579 | 0 | ret = 0; |
580 | 0 | cleanup: |
581 | 0 | module_list_release(&list); |
582 | 0 | clear_pathspec(&pathspec); |
583 | 0 | return ret; |
584 | 0 | } |
585 | | |
586 | | struct status_cb { |
587 | | const char *prefix; |
588 | | const char *super_prefix; |
589 | | unsigned int flags; |
590 | | }; |
591 | 0 | #define STATUS_CB_INIT { 0 } |
592 | | |
593 | | static void print_status(unsigned int flags, char state, const char *path, |
594 | | const struct object_id *oid, const char *displaypath) |
595 | 0 | { |
596 | 0 | if (flags & OPT_QUIET) |
597 | 0 | return; |
598 | | |
599 | 0 | printf("%c%s %s", state, oid_to_hex(oid), displaypath); |
600 | |
|
601 | 0 | if (state == ' ' || state == '+') { |
602 | 0 | char *name = compute_rev_name(path, oid_to_hex(oid)); |
603 | |
|
604 | 0 | if (name) |
605 | 0 | printf(" (%s)", name); |
606 | 0 | free(name); |
607 | 0 | } |
608 | |
|
609 | 0 | printf("\n"); |
610 | 0 | } |
611 | | |
612 | | static int handle_submodule_head_ref(const char *refname UNUSED, |
613 | | const struct object_id *oid, |
614 | | int flags UNUSED, |
615 | | void *cb_data) |
616 | 0 | { |
617 | 0 | struct object_id *output = cb_data; |
618 | |
|
619 | 0 | if (oid) |
620 | 0 | oidcpy(output, oid); |
621 | |
|
622 | 0 | return 0; |
623 | 0 | } |
624 | | |
625 | | static void status_submodule(const char *path, const struct object_id *ce_oid, |
626 | | unsigned int ce_flags, const char *prefix, |
627 | | const char *super_prefix, unsigned int flags) |
628 | 0 | { |
629 | 0 | char *displaypath; |
630 | 0 | struct strvec diff_files_args = STRVEC_INIT; |
631 | 0 | struct rev_info rev = REV_INFO_INIT; |
632 | 0 | struct strbuf buf = STRBUF_INIT; |
633 | 0 | const char *git_dir; |
634 | 0 | struct setup_revision_opt opt = { |
635 | 0 | .free_removed_argv_elements = 1, |
636 | 0 | }; |
637 | |
|
638 | 0 | if (!submodule_from_path(the_repository, null_oid(), path)) |
639 | 0 | die(_("no submodule mapping found in .gitmodules for path '%s'"), |
640 | 0 | path); |
641 | | |
642 | 0 | displaypath = get_submodule_displaypath(path, prefix, super_prefix); |
643 | |
|
644 | 0 | if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) { |
645 | 0 | print_status(flags, 'U', path, null_oid(), displaypath); |
646 | 0 | goto cleanup; |
647 | 0 | } |
648 | | |
649 | 0 | strbuf_addf(&buf, "%s/.git", path); |
650 | 0 | git_dir = read_gitfile(buf.buf); |
651 | 0 | if (!git_dir) |
652 | 0 | git_dir = buf.buf; |
653 | |
|
654 | 0 | if (!is_submodule_active(the_repository, path) || |
655 | 0 | !is_git_directory(git_dir)) { |
656 | 0 | print_status(flags, '-', path, ce_oid, displaypath); |
657 | 0 | strbuf_release(&buf); |
658 | 0 | goto cleanup; |
659 | 0 | } |
660 | 0 | strbuf_release(&buf); |
661 | |
|
662 | 0 | strvec_pushl(&diff_files_args, "diff-files", |
663 | 0 | "--ignore-submodules=dirty", "--quiet", "--", |
664 | 0 | path, NULL); |
665 | |
|
666 | 0 | git_config(git_diff_basic_config, NULL); |
667 | |
|
668 | 0 | repo_init_revisions(the_repository, &rev, NULL); |
669 | 0 | rev.abbrev = 0; |
670 | 0 | setup_revisions(diff_files_args.nr, diff_files_args.v, &rev, &opt); |
671 | 0 | run_diff_files(&rev, 0); |
672 | |
|
673 | 0 | if (!diff_result_code(&rev.diffopt)) { |
674 | 0 | print_status(flags, ' ', path, ce_oid, |
675 | 0 | displaypath); |
676 | 0 | } else if (!(flags & OPT_CACHED)) { |
677 | 0 | struct object_id oid; |
678 | 0 | struct ref_store *refs = get_submodule_ref_store(path); |
679 | |
|
680 | 0 | if (!refs) { |
681 | 0 | print_status(flags, '-', path, ce_oid, displaypath); |
682 | 0 | goto cleanup; |
683 | 0 | } |
684 | 0 | if (refs_head_ref(refs, handle_submodule_head_ref, &oid)) |
685 | 0 | die(_("could not resolve HEAD ref inside the " |
686 | 0 | "submodule '%s'"), path); |
687 | | |
688 | 0 | print_status(flags, '+', path, &oid, displaypath); |
689 | 0 | } else { |
690 | 0 | print_status(flags, '+', path, ce_oid, displaypath); |
691 | 0 | } |
692 | | |
693 | 0 | if (flags & OPT_RECURSIVE) { |
694 | 0 | struct child_process cpr = CHILD_PROCESS_INIT; |
695 | |
|
696 | 0 | cpr.git_cmd = 1; |
697 | 0 | cpr.dir = path; |
698 | 0 | prepare_submodule_repo_env(&cpr.env); |
699 | |
|
700 | 0 | strvec_pushl(&cpr.args, "submodule--helper", "status", |
701 | 0 | "--recursive", NULL); |
702 | 0 | strvec_push(&cpr.args, "--super-prefix"); |
703 | 0 | strvec_pushf(&cpr.args, "%s/", displaypath); |
704 | |
|
705 | 0 | if (flags & OPT_CACHED) |
706 | 0 | strvec_push(&cpr.args, "--cached"); |
707 | |
|
708 | 0 | if (flags & OPT_QUIET) |
709 | 0 | strvec_push(&cpr.args, "--quiet"); |
710 | |
|
711 | 0 | if (run_command(&cpr)) |
712 | 0 | die(_("failed to recurse into submodule '%s'"), path); |
713 | 0 | } |
714 | | |
715 | 0 | cleanup: |
716 | 0 | strvec_clear(&diff_files_args); |
717 | 0 | free(displaypath); |
718 | 0 | release_revisions(&rev); |
719 | 0 | } |
720 | | |
721 | | static void status_submodule_cb(const struct cache_entry *list_item, |
722 | | void *cb_data) |
723 | 0 | { |
724 | 0 | struct status_cb *info = cb_data; |
725 | |
|
726 | 0 | status_submodule(list_item->name, &list_item->oid, list_item->ce_flags, |
727 | 0 | info->prefix, info->super_prefix, info->flags); |
728 | 0 | } |
729 | | |
730 | | static int module_status(int argc, const char **argv, const char *prefix) |
731 | 0 | { |
732 | 0 | struct status_cb info = STATUS_CB_INIT; |
733 | 0 | struct pathspec pathspec = { 0 }; |
734 | 0 | struct module_list list = MODULE_LIST_INIT; |
735 | 0 | int quiet = 0; |
736 | 0 | struct option module_status_options[] = { |
737 | 0 | OPT__SUPER_PREFIX(&info.super_prefix), |
738 | 0 | OPT__QUIET(&quiet, N_("suppress submodule status output")), |
739 | 0 | OPT_BIT(0, "cached", &info.flags, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED), |
740 | 0 | OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE), |
741 | 0 | OPT_END() |
742 | 0 | }; |
743 | 0 | const char *const git_submodule_helper_usage[] = { |
744 | 0 | N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"), |
745 | 0 | NULL |
746 | 0 | }; |
747 | 0 | int ret = 1; |
748 | |
|
749 | 0 | argc = parse_options(argc, argv, prefix, module_status_options, |
750 | 0 | git_submodule_helper_usage, 0); |
751 | |
|
752 | 0 | if (module_list_compute(argv, prefix, &pathspec, &list) < 0) |
753 | 0 | goto cleanup; |
754 | | |
755 | 0 | info.prefix = prefix; |
756 | 0 | if (quiet) |
757 | 0 | info.flags |= OPT_QUIET; |
758 | |
|
759 | 0 | for_each_listed_submodule(&list, status_submodule_cb, &info); |
760 | |
|
761 | 0 | ret = 0; |
762 | 0 | cleanup: |
763 | 0 | module_list_release(&list); |
764 | 0 | clear_pathspec(&pathspec); |
765 | 0 | return ret; |
766 | 0 | } |
767 | | |
768 | | struct module_cb { |
769 | | unsigned int mod_src; |
770 | | unsigned int mod_dst; |
771 | | struct object_id oid_src; |
772 | | struct object_id oid_dst; |
773 | | char status; |
774 | | char *sm_path; |
775 | | }; |
776 | | #define MODULE_CB_INIT { 0 } |
777 | | |
778 | | static void module_cb_release(struct module_cb *mcb) |
779 | 0 | { |
780 | 0 | free(mcb->sm_path); |
781 | 0 | } |
782 | | |
783 | | struct module_cb_list { |
784 | | struct module_cb **entries; |
785 | | int alloc, nr; |
786 | | }; |
787 | 0 | #define MODULE_CB_LIST_INIT { 0 } |
788 | | |
789 | | static void module_cb_list_release(struct module_cb_list *mcbl) |
790 | 0 | { |
791 | 0 | int i; |
792 | |
|
793 | 0 | for (i = 0; i < mcbl->nr; i++) { |
794 | 0 | struct module_cb *mcb = mcbl->entries[i]; |
795 | |
|
796 | 0 | module_cb_release(mcb); |
797 | 0 | free(mcb); |
798 | 0 | } |
799 | 0 | free(mcbl->entries); |
800 | 0 | } |
801 | | |
802 | | struct summary_cb { |
803 | | int argc; |
804 | | const char **argv; |
805 | | const char *prefix; |
806 | | const char *super_prefix; |
807 | | unsigned int cached: 1; |
808 | | unsigned int for_status: 1; |
809 | | unsigned int files: 1; |
810 | | int summary_limit; |
811 | | }; |
812 | 0 | #define SUMMARY_CB_INIT { 0 } |
813 | | |
814 | | enum diff_cmd { |
815 | | DIFF_INDEX, |
816 | | DIFF_FILES |
817 | | }; |
818 | | |
819 | | static char *verify_submodule_committish(const char *sm_path, |
820 | | const char *committish) |
821 | 0 | { |
822 | 0 | struct child_process cp_rev_parse = CHILD_PROCESS_INIT; |
823 | 0 | struct strbuf result = STRBUF_INIT; |
824 | |
|
825 | 0 | cp_rev_parse.git_cmd = 1; |
826 | 0 | cp_rev_parse.dir = sm_path; |
827 | 0 | prepare_submodule_repo_env(&cp_rev_parse.env); |
828 | 0 | strvec_pushl(&cp_rev_parse.args, "rev-parse", "-q", "--short", NULL); |
829 | 0 | strvec_pushf(&cp_rev_parse.args, "%s^0", committish); |
830 | 0 | strvec_push(&cp_rev_parse.args, "--"); |
831 | |
|
832 | 0 | if (capture_command(&cp_rev_parse, &result, 0)) |
833 | 0 | return NULL; |
834 | | |
835 | 0 | strbuf_trim_trailing_newline(&result); |
836 | 0 | return strbuf_detach(&result, NULL); |
837 | 0 | } |
838 | | |
839 | | static void print_submodule_summary(struct summary_cb *info, const char *errmsg, |
840 | | int total_commits, const char *displaypath, |
841 | | const char *src_abbrev, const char *dst_abbrev, |
842 | | struct module_cb *p) |
843 | 0 | { |
844 | 0 | if (p->status == 'T') { |
845 | 0 | if (S_ISGITLINK(p->mod_dst)) |
846 | 0 | printf(_("* %s %s(blob)->%s(submodule)"), |
847 | 0 | displaypath, src_abbrev, dst_abbrev); |
848 | 0 | else |
849 | 0 | printf(_("* %s %s(submodule)->%s(blob)"), |
850 | 0 | displaypath, src_abbrev, dst_abbrev); |
851 | 0 | } else { |
852 | 0 | printf("* %s %s...%s", |
853 | 0 | displaypath, src_abbrev, dst_abbrev); |
854 | 0 | } |
855 | |
|
856 | 0 | if (total_commits < 0) |
857 | 0 | printf(":\n"); |
858 | 0 | else |
859 | 0 | printf(" (%d):\n", total_commits); |
860 | |
|
861 | 0 | if (errmsg) { |
862 | 0 | printf(_("%s"), errmsg); |
863 | 0 | } else if (total_commits > 0) { |
864 | 0 | struct child_process cp_log = CHILD_PROCESS_INIT; |
865 | |
|
866 | 0 | cp_log.git_cmd = 1; |
867 | 0 | cp_log.dir = p->sm_path; |
868 | 0 | prepare_submodule_repo_env(&cp_log.env); |
869 | 0 | strvec_pushl(&cp_log.args, "log", NULL); |
870 | |
|
871 | 0 | if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst)) { |
872 | 0 | if (info->summary_limit > 0) |
873 | 0 | strvec_pushf(&cp_log.args, "-%d", |
874 | 0 | info->summary_limit); |
875 | |
|
876 | 0 | strvec_pushl(&cp_log.args, "--pretty= %m %s", |
877 | 0 | "--first-parent", NULL); |
878 | 0 | strvec_pushf(&cp_log.args, "%s...%s", |
879 | 0 | src_abbrev, dst_abbrev); |
880 | 0 | } else if (S_ISGITLINK(p->mod_dst)) { |
881 | 0 | strvec_pushl(&cp_log.args, "--pretty= > %s", |
882 | 0 | "-1", dst_abbrev, NULL); |
883 | 0 | } else { |
884 | 0 | strvec_pushl(&cp_log.args, "--pretty= < %s", |
885 | 0 | "-1", src_abbrev, NULL); |
886 | 0 | } |
887 | 0 | run_command(&cp_log); |
888 | 0 | } |
889 | 0 | printf("\n"); |
890 | 0 | } |
891 | | |
892 | | static void generate_submodule_summary(struct summary_cb *info, |
893 | | struct module_cb *p) |
894 | 0 | { |
895 | 0 | char *displaypath, *src_abbrev = NULL, *dst_abbrev; |
896 | 0 | int missing_src = 0, missing_dst = 0; |
897 | 0 | struct strbuf errmsg = STRBUF_INIT; |
898 | 0 | int total_commits = -1; |
899 | |
|
900 | 0 | if (!info->cached && oideq(&p->oid_dst, null_oid())) { |
901 | 0 | if (S_ISGITLINK(p->mod_dst)) { |
902 | 0 | struct ref_store *refs = get_submodule_ref_store(p->sm_path); |
903 | |
|
904 | 0 | if (refs) |
905 | 0 | refs_head_ref(refs, handle_submodule_head_ref, &p->oid_dst); |
906 | 0 | } else if (S_ISLNK(p->mod_dst) || S_ISREG(p->mod_dst)) { |
907 | 0 | struct stat st; |
908 | 0 | int fd = open(p->sm_path, O_RDONLY); |
909 | |
|
910 | 0 | if (fd < 0 || fstat(fd, &st) < 0 || |
911 | 0 | index_fd(&the_index, &p->oid_dst, fd, &st, OBJ_BLOB, |
912 | 0 | p->sm_path, 0)) |
913 | 0 | error(_("couldn't hash object from '%s'"), p->sm_path); |
914 | 0 | } else { |
915 | | /* for a submodule removal (mode:0000000), don't warn */ |
916 | 0 | if (p->mod_dst) |
917 | 0 | warning(_("unexpected mode %o\n"), p->mod_dst); |
918 | 0 | } |
919 | 0 | } |
920 | |
|
921 | 0 | if (S_ISGITLINK(p->mod_src)) { |
922 | 0 | if (p->status != 'D') |
923 | 0 | src_abbrev = verify_submodule_committish(p->sm_path, |
924 | 0 | oid_to_hex(&p->oid_src)); |
925 | 0 | if (!src_abbrev) { |
926 | 0 | missing_src = 1; |
927 | | /* |
928 | | * As `rev-parse` failed, we fallback to getting |
929 | | * the abbreviated hash using oid_src. We do |
930 | | * this as we might still need the abbreviated |
931 | | * hash in cases like a submodule type change, etc. |
932 | | */ |
933 | 0 | src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7); |
934 | 0 | } |
935 | 0 | } else { |
936 | | /* |
937 | | * The source does not point to a submodule. |
938 | | * So, we fallback to getting the abbreviation using |
939 | | * oid_src as we might still need the abbreviated |
940 | | * hash in cases like submodule add, etc. |
941 | | */ |
942 | 0 | src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7); |
943 | 0 | } |
944 | |
|
945 | 0 | if (S_ISGITLINK(p->mod_dst)) { |
946 | 0 | dst_abbrev = verify_submodule_committish(p->sm_path, |
947 | 0 | oid_to_hex(&p->oid_dst)); |
948 | 0 | if (!dst_abbrev) { |
949 | 0 | missing_dst = 1; |
950 | | /* |
951 | | * As `rev-parse` failed, we fallback to getting |
952 | | * the abbreviated hash using oid_dst. We do |
953 | | * this as we might still need the abbreviated |
954 | | * hash in cases like a submodule type change, etc. |
955 | | */ |
956 | 0 | dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7); |
957 | 0 | } |
958 | 0 | } else { |
959 | | /* |
960 | | * The destination does not point to a submodule. |
961 | | * So, we fallback to getting the abbreviation using |
962 | | * oid_dst as we might still need the abbreviated |
963 | | * hash in cases like a submodule removal, etc. |
964 | | */ |
965 | 0 | dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7); |
966 | 0 | } |
967 | |
|
968 | 0 | displaypath = get_submodule_displaypath(p->sm_path, info->prefix, |
969 | 0 | info->super_prefix); |
970 | |
|
971 | 0 | if (!missing_src && !missing_dst) { |
972 | 0 | struct child_process cp_rev_list = CHILD_PROCESS_INIT; |
973 | 0 | struct strbuf sb_rev_list = STRBUF_INIT; |
974 | |
|
975 | 0 | strvec_pushl(&cp_rev_list.args, "rev-list", |
976 | 0 | "--first-parent", "--count", NULL); |
977 | 0 | if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst)) |
978 | 0 | strvec_pushf(&cp_rev_list.args, "%s...%s", |
979 | 0 | src_abbrev, dst_abbrev); |
980 | 0 | else |
981 | 0 | strvec_push(&cp_rev_list.args, S_ISGITLINK(p->mod_src) ? |
982 | 0 | src_abbrev : dst_abbrev); |
983 | 0 | strvec_push(&cp_rev_list.args, "--"); |
984 | |
|
985 | 0 | cp_rev_list.git_cmd = 1; |
986 | 0 | cp_rev_list.dir = p->sm_path; |
987 | 0 | prepare_submodule_repo_env(&cp_rev_list.env); |
988 | |
|
989 | 0 | if (!capture_command(&cp_rev_list, &sb_rev_list, 0)) |
990 | 0 | total_commits = atoi(sb_rev_list.buf); |
991 | |
|
992 | 0 | strbuf_release(&sb_rev_list); |
993 | 0 | } else { |
994 | | /* |
995 | | * Don't give error msg for modification whose dst is not |
996 | | * submodule, i.e., deleted or changed to blob |
997 | | */ |
998 | 0 | if (S_ISGITLINK(p->mod_dst)) { |
999 | 0 | if (missing_src && missing_dst) { |
1000 | 0 | strbuf_addf(&errmsg, " Warn: %s doesn't contain commits %s and %s\n", |
1001 | 0 | displaypath, oid_to_hex(&p->oid_src), |
1002 | 0 | oid_to_hex(&p->oid_dst)); |
1003 | 0 | } else { |
1004 | 0 | strbuf_addf(&errmsg, " Warn: %s doesn't contain commit %s\n", |
1005 | 0 | displaypath, missing_src ? |
1006 | 0 | oid_to_hex(&p->oid_src) : |
1007 | 0 | oid_to_hex(&p->oid_dst)); |
1008 | 0 | } |
1009 | 0 | } |
1010 | 0 | } |
1011 | |
|
1012 | 0 | print_submodule_summary(info, errmsg.len ? errmsg.buf : NULL, |
1013 | 0 | total_commits, displaypath, src_abbrev, |
1014 | 0 | dst_abbrev, p); |
1015 | |
|
1016 | 0 | free(displaypath); |
1017 | 0 | free(src_abbrev); |
1018 | 0 | free(dst_abbrev); |
1019 | 0 | strbuf_release(&errmsg); |
1020 | 0 | } |
1021 | | |
1022 | | static void prepare_submodule_summary(struct summary_cb *info, |
1023 | | struct module_cb_list *list) |
1024 | 0 | { |
1025 | 0 | int i; |
1026 | 0 | for (i = 0; i < list->nr; i++) { |
1027 | 0 | const struct submodule *sub; |
1028 | 0 | struct module_cb *p = list->entries[i]; |
1029 | 0 | struct strbuf sm_gitdir = STRBUF_INIT; |
1030 | |
|
1031 | 0 | if (p->status == 'D' || p->status == 'T') { |
1032 | 0 | generate_submodule_summary(info, p); |
1033 | 0 | continue; |
1034 | 0 | } |
1035 | | |
1036 | 0 | if (info->for_status && p->status != 'A' && |
1037 | 0 | (sub = submodule_from_path(the_repository, |
1038 | 0 | null_oid(), p->sm_path))) { |
1039 | 0 | char *config_key = NULL; |
1040 | 0 | const char *value; |
1041 | 0 | int ignore_all = 0; |
1042 | |
|
1043 | 0 | config_key = xstrfmt("submodule.%s.ignore", |
1044 | 0 | sub->name); |
1045 | 0 | if (!git_config_get_string_tmp(config_key, &value)) |
1046 | 0 | ignore_all = !strcmp(value, "all"); |
1047 | 0 | else if (sub->ignore) |
1048 | 0 | ignore_all = !strcmp(sub->ignore, "all"); |
1049 | |
|
1050 | 0 | free(config_key); |
1051 | 0 | if (ignore_all) |
1052 | 0 | continue; |
1053 | 0 | } |
1054 | | |
1055 | | /* Also show added or modified modules which are checked out */ |
1056 | 0 | strbuf_addstr(&sm_gitdir, p->sm_path); |
1057 | 0 | if (is_nonbare_repository_dir(&sm_gitdir)) |
1058 | 0 | generate_submodule_summary(info, p); |
1059 | 0 | strbuf_release(&sm_gitdir); |
1060 | 0 | } |
1061 | 0 | } |
1062 | | |
1063 | | static void submodule_summary_callback(struct diff_queue_struct *q, |
1064 | | struct diff_options *options UNUSED, |
1065 | | void *data) |
1066 | 0 | { |
1067 | 0 | int i; |
1068 | 0 | struct module_cb_list *list = data; |
1069 | 0 | for (i = 0; i < q->nr; i++) { |
1070 | 0 | struct diff_filepair *p = q->queue[i]; |
1071 | 0 | struct module_cb *temp; |
1072 | |
|
1073 | 0 | if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode)) |
1074 | 0 | continue; |
1075 | 0 | temp = (struct module_cb*)malloc(sizeof(struct module_cb)); |
1076 | 0 | temp->mod_src = p->one->mode; |
1077 | 0 | temp->mod_dst = p->two->mode; |
1078 | 0 | temp->oid_src = p->one->oid; |
1079 | 0 | temp->oid_dst = p->two->oid; |
1080 | 0 | temp->status = p->status; |
1081 | 0 | temp->sm_path = xstrdup(p->one->path); |
1082 | |
|
1083 | 0 | ALLOC_GROW(list->entries, list->nr + 1, list->alloc); |
1084 | 0 | list->entries[list->nr++] = temp; |
1085 | 0 | } |
1086 | 0 | } |
1087 | | |
1088 | | static const char *get_diff_cmd(enum diff_cmd diff_cmd) |
1089 | 0 | { |
1090 | 0 | switch (diff_cmd) { |
1091 | 0 | case DIFF_INDEX: return "diff-index"; |
1092 | 0 | case DIFF_FILES: return "diff-files"; |
1093 | 0 | default: BUG("bad diff_cmd value %d", diff_cmd); |
1094 | 0 | } |
1095 | 0 | } |
1096 | | |
1097 | | static int compute_summary_module_list(struct object_id *head_oid, |
1098 | | struct summary_cb *info, |
1099 | | enum diff_cmd diff_cmd) |
1100 | 0 | { |
1101 | 0 | struct strvec diff_args = STRVEC_INIT; |
1102 | 0 | struct rev_info rev; |
1103 | 0 | struct setup_revision_opt opt = { |
1104 | 0 | .free_removed_argv_elements = 1, |
1105 | 0 | }; |
1106 | 0 | struct module_cb_list list = MODULE_CB_LIST_INIT; |
1107 | 0 | int ret = 0; |
1108 | |
|
1109 | 0 | strvec_push(&diff_args, get_diff_cmd(diff_cmd)); |
1110 | 0 | if (info->cached) |
1111 | 0 | strvec_push(&diff_args, "--cached"); |
1112 | 0 | strvec_pushl(&diff_args, "--ignore-submodules=dirty", "--raw", NULL); |
1113 | 0 | if (head_oid) |
1114 | 0 | strvec_push(&diff_args, oid_to_hex(head_oid)); |
1115 | 0 | strvec_push(&diff_args, "--"); |
1116 | 0 | if (info->argc) |
1117 | 0 | strvec_pushv(&diff_args, info->argv); |
1118 | |
|
1119 | 0 | git_config(git_diff_basic_config, NULL); |
1120 | 0 | repo_init_revisions(the_repository, &rev, info->prefix); |
1121 | 0 | rev.abbrev = 0; |
1122 | 0 | precompose_argv_prefix(diff_args.nr, diff_args.v, NULL); |
1123 | 0 | setup_revisions(diff_args.nr, diff_args.v, &rev, &opt); |
1124 | 0 | rev.diffopt.output_format = DIFF_FORMAT_NO_OUTPUT | DIFF_FORMAT_CALLBACK; |
1125 | 0 | rev.diffopt.format_callback = submodule_summary_callback; |
1126 | 0 | rev.diffopt.format_callback_data = &list; |
1127 | |
|
1128 | 0 | if (!info->cached) { |
1129 | 0 | if (diff_cmd == DIFF_INDEX) |
1130 | 0 | setup_work_tree(); |
1131 | 0 | if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) { |
1132 | 0 | perror("repo_read_index_preload"); |
1133 | 0 | ret = -1; |
1134 | 0 | goto cleanup; |
1135 | 0 | } |
1136 | 0 | } else if (repo_read_index(the_repository) < 0) { |
1137 | 0 | perror("repo_read_cache"); |
1138 | 0 | ret = -1; |
1139 | 0 | goto cleanup; |
1140 | 0 | } |
1141 | | |
1142 | 0 | if (diff_cmd == DIFF_INDEX) |
1143 | 0 | run_diff_index(&rev, info->cached ? DIFF_INDEX_CACHED : 0); |
1144 | 0 | else |
1145 | 0 | run_diff_files(&rev, 0); |
1146 | 0 | prepare_submodule_summary(info, &list); |
1147 | 0 | cleanup: |
1148 | 0 | strvec_clear(&diff_args); |
1149 | 0 | release_revisions(&rev); |
1150 | 0 | module_cb_list_release(&list); |
1151 | 0 | return ret; |
1152 | 0 | } |
1153 | | |
1154 | | static int module_summary(int argc, const char **argv, const char *prefix) |
1155 | 0 | { |
1156 | 0 | struct summary_cb info = SUMMARY_CB_INIT; |
1157 | 0 | int cached = 0; |
1158 | 0 | int for_status = 0; |
1159 | 0 | int files = 0; |
1160 | 0 | int summary_limit = -1; |
1161 | 0 | enum diff_cmd diff_cmd = DIFF_INDEX; |
1162 | 0 | struct object_id head_oid; |
1163 | 0 | int ret; |
1164 | 0 | struct option module_summary_options[] = { |
1165 | 0 | OPT_BOOL(0, "cached", &cached, |
1166 | 0 | N_("use the commit stored in the index instead of the submodule HEAD")), |
1167 | 0 | OPT_BOOL(0, "files", &files, |
1168 | 0 | N_("compare the commit in the index with that in the submodule HEAD")), |
1169 | 0 | OPT_BOOL(0, "for-status", &for_status, |
1170 | 0 | N_("skip submodules with 'ignore_config' value set to 'all'")), |
1171 | 0 | OPT_INTEGER('n', "summary-limit", &summary_limit, |
1172 | 0 | N_("limit the summary size")), |
1173 | 0 | OPT_END() |
1174 | 0 | }; |
1175 | 0 | const char *const git_submodule_helper_usage[] = { |
1176 | 0 | N_("git submodule summary [<options>] [<commit>] [--] [<path>]"), |
1177 | 0 | NULL |
1178 | 0 | }; |
1179 | |
|
1180 | 0 | argc = parse_options(argc, argv, prefix, module_summary_options, |
1181 | 0 | git_submodule_helper_usage, 0); |
1182 | |
|
1183 | 0 | if (!summary_limit) |
1184 | 0 | return 0; |
1185 | | |
1186 | 0 | if (!repo_get_oid(the_repository, argc ? argv[0] : "HEAD", &head_oid)) { |
1187 | 0 | if (argc) { |
1188 | 0 | argv++; |
1189 | 0 | argc--; |
1190 | 0 | } |
1191 | 0 | } else if (!argc || !strcmp(argv[0], "HEAD")) { |
1192 | | /* before the first commit: compare with an empty tree */ |
1193 | 0 | oidcpy(&head_oid, the_hash_algo->empty_tree); |
1194 | 0 | if (argc) { |
1195 | 0 | argv++; |
1196 | 0 | argc--; |
1197 | 0 | } |
1198 | 0 | } else { |
1199 | 0 | if (repo_get_oid(the_repository, "HEAD", &head_oid)) |
1200 | 0 | die(_("could not fetch a revision for HEAD")); |
1201 | 0 | } |
1202 | | |
1203 | 0 | if (files) { |
1204 | 0 | if (cached) |
1205 | 0 | die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files"); |
1206 | 0 | diff_cmd = DIFF_FILES; |
1207 | 0 | } |
1208 | | |
1209 | 0 | info.argc = argc; |
1210 | 0 | info.argv = argv; |
1211 | 0 | info.prefix = prefix; |
1212 | 0 | info.cached = !!cached; |
1213 | 0 | info.files = !!files; |
1214 | 0 | info.for_status = !!for_status; |
1215 | 0 | info.summary_limit = summary_limit; |
1216 | |
|
1217 | 0 | ret = compute_summary_module_list((diff_cmd == DIFF_INDEX) ? &head_oid : NULL, |
1218 | 0 | &info, diff_cmd); |
1219 | 0 | return ret; |
1220 | 0 | } |
1221 | | |
1222 | | struct sync_cb { |
1223 | | const char *prefix; |
1224 | | const char *super_prefix; |
1225 | | unsigned int flags; |
1226 | | }; |
1227 | 0 | #define SYNC_CB_INIT { 0 } |
1228 | | |
1229 | | static void sync_submodule(const char *path, const char *prefix, |
1230 | | const char *super_prefix, unsigned int flags) |
1231 | 0 | { |
1232 | 0 | const struct submodule *sub; |
1233 | 0 | char *remote_key = NULL; |
1234 | 0 | char *sub_origin_url, *super_config_url, *displaypath, *default_remote; |
1235 | 0 | struct strbuf sb = STRBUF_INIT; |
1236 | 0 | char *sub_config_path = NULL; |
1237 | 0 | int code; |
1238 | |
|
1239 | 0 | if (!is_submodule_active(the_repository, path)) |
1240 | 0 | return; |
1241 | | |
1242 | 0 | sub = submodule_from_path(the_repository, null_oid(), path); |
1243 | |
|
1244 | 0 | if (sub && sub->url) { |
1245 | 0 | if (starts_with_dot_dot_slash(sub->url) || |
1246 | 0 | starts_with_dot_slash(sub->url)) { |
1247 | 0 | char *up_path = get_up_path(path); |
1248 | |
|
1249 | 0 | sub_origin_url = resolve_relative_url(sub->url, up_path, 1); |
1250 | 0 | super_config_url = resolve_relative_url(sub->url, NULL, 1); |
1251 | 0 | free(up_path); |
1252 | 0 | } else { |
1253 | 0 | sub_origin_url = xstrdup(sub->url); |
1254 | 0 | super_config_url = xstrdup(sub->url); |
1255 | 0 | } |
1256 | 0 | } else { |
1257 | 0 | sub_origin_url = xstrdup(""); |
1258 | 0 | super_config_url = xstrdup(""); |
1259 | 0 | } |
1260 | |
|
1261 | 0 | displaypath = get_submodule_displaypath(path, prefix, super_prefix); |
1262 | |
|
1263 | 0 | if (!(flags & OPT_QUIET)) |
1264 | 0 | printf(_("Synchronizing submodule url for '%s'\n"), |
1265 | 0 | displaypath); |
1266 | |
|
1267 | 0 | strbuf_reset(&sb); |
1268 | 0 | strbuf_addf(&sb, "submodule.%s.url", sub->name); |
1269 | 0 | if (git_config_set_gently(sb.buf, super_config_url)) |
1270 | 0 | die(_("failed to register url for submodule path '%s'"), |
1271 | 0 | displaypath); |
1272 | | |
1273 | 0 | if (!is_submodule_populated_gently(path, NULL)) |
1274 | 0 | goto cleanup; |
1275 | | |
1276 | 0 | strbuf_reset(&sb); |
1277 | 0 | code = get_default_remote_submodule(path, &default_remote); |
1278 | 0 | if (code) |
1279 | 0 | exit(code); |
1280 | | |
1281 | 0 | remote_key = xstrfmt("remote.%s.url", default_remote); |
1282 | 0 | free(default_remote); |
1283 | |
|
1284 | 0 | submodule_to_gitdir(&sb, path); |
1285 | 0 | strbuf_addstr(&sb, "/config"); |
1286 | |
|
1287 | 0 | if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url)) |
1288 | 0 | die(_("failed to update remote for submodule '%s'"), |
1289 | 0 | path); |
1290 | | |
1291 | 0 | if (flags & OPT_RECURSIVE) { |
1292 | 0 | struct child_process cpr = CHILD_PROCESS_INIT; |
1293 | |
|
1294 | 0 | cpr.git_cmd = 1; |
1295 | 0 | cpr.dir = path; |
1296 | 0 | prepare_submodule_repo_env(&cpr.env); |
1297 | |
|
1298 | 0 | strvec_pushl(&cpr.args, "submodule--helper", "sync", |
1299 | 0 | "--recursive", NULL); |
1300 | 0 | strvec_push(&cpr.args, "--super-prefix"); |
1301 | 0 | strvec_pushf(&cpr.args, "%s/", displaypath); |
1302 | | |
1303 | |
|
1304 | 0 | if (flags & OPT_QUIET) |
1305 | 0 | strvec_push(&cpr.args, "--quiet"); |
1306 | |
|
1307 | 0 | if (run_command(&cpr)) |
1308 | 0 | die(_("failed to recurse into submodule '%s'"), |
1309 | 0 | path); |
1310 | 0 | } |
1311 | | |
1312 | 0 | cleanup: |
1313 | 0 | free(super_config_url); |
1314 | 0 | free(sub_origin_url); |
1315 | 0 | strbuf_release(&sb); |
1316 | 0 | free(remote_key); |
1317 | 0 | free(displaypath); |
1318 | 0 | free(sub_config_path); |
1319 | 0 | } |
1320 | | |
1321 | | static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data) |
1322 | 0 | { |
1323 | 0 | struct sync_cb *info = cb_data; |
1324 | |
|
1325 | 0 | sync_submodule(list_item->name, info->prefix, info->super_prefix, |
1326 | 0 | info->flags); |
1327 | 0 | } |
1328 | | |
1329 | | static int module_sync(int argc, const char **argv, const char *prefix) |
1330 | 0 | { |
1331 | 0 | struct sync_cb info = SYNC_CB_INIT; |
1332 | 0 | struct pathspec pathspec = { 0 }; |
1333 | 0 | struct module_list list = MODULE_LIST_INIT; |
1334 | 0 | int quiet = 0; |
1335 | 0 | int recursive = 0; |
1336 | 0 | struct option module_sync_options[] = { |
1337 | 0 | OPT__SUPER_PREFIX(&info.super_prefix), |
1338 | 0 | OPT__QUIET(&quiet, N_("suppress output of synchronizing submodule url")), |
1339 | 0 | OPT_BOOL(0, "recursive", &recursive, |
1340 | 0 | N_("recurse into nested submodules")), |
1341 | 0 | OPT_END() |
1342 | 0 | }; |
1343 | 0 | const char *const git_submodule_helper_usage[] = { |
1344 | 0 | N_("git submodule sync [--quiet] [--recursive] [<path>]"), |
1345 | 0 | NULL |
1346 | 0 | }; |
1347 | 0 | int ret = 1; |
1348 | |
|
1349 | 0 | argc = parse_options(argc, argv, prefix, module_sync_options, |
1350 | 0 | git_submodule_helper_usage, 0); |
1351 | |
|
1352 | 0 | if (module_list_compute(argv, prefix, &pathspec, &list) < 0) |
1353 | 0 | goto cleanup; |
1354 | | |
1355 | 0 | info.prefix = prefix; |
1356 | 0 | if (quiet) |
1357 | 0 | info.flags |= OPT_QUIET; |
1358 | 0 | if (recursive) |
1359 | 0 | info.flags |= OPT_RECURSIVE; |
1360 | |
|
1361 | 0 | for_each_listed_submodule(&list, sync_submodule_cb, &info); |
1362 | |
|
1363 | 0 | ret = 0; |
1364 | 0 | cleanup: |
1365 | 0 | module_list_release(&list); |
1366 | 0 | clear_pathspec(&pathspec); |
1367 | 0 | return ret; |
1368 | 0 | } |
1369 | | |
1370 | | struct deinit_cb { |
1371 | | const char *prefix; |
1372 | | unsigned int flags; |
1373 | | }; |
1374 | 0 | #define DEINIT_CB_INIT { 0 } |
1375 | | |
1376 | | static void deinit_submodule(const char *path, const char *prefix, |
1377 | | unsigned int flags) |
1378 | 0 | { |
1379 | 0 | const struct submodule *sub; |
1380 | 0 | char *displaypath = NULL; |
1381 | 0 | struct child_process cp_config = CHILD_PROCESS_INIT; |
1382 | 0 | struct strbuf sb_config = STRBUF_INIT; |
1383 | 0 | char *sub_git_dir = xstrfmt("%s/.git", path); |
1384 | |
|
1385 | 0 | sub = submodule_from_path(the_repository, null_oid(), path); |
1386 | |
|
1387 | 0 | if (!sub || !sub->name) |
1388 | 0 | goto cleanup; |
1389 | | |
1390 | 0 | displaypath = get_submodule_displaypath(path, prefix, NULL); |
1391 | | |
1392 | | /* remove the submodule work tree (unless the user already did it) */ |
1393 | 0 | if (is_directory(path)) { |
1394 | 0 | struct strbuf sb_rm = STRBUF_INIT; |
1395 | 0 | const char *format; |
1396 | |
|
1397 | 0 | if (is_directory(sub_git_dir)) { |
1398 | 0 | if (!(flags & OPT_QUIET)) |
1399 | 0 | warning(_("Submodule work tree '%s' contains a .git " |
1400 | 0 | "directory. This will be replaced with a " |
1401 | 0 | ".git file by using absorbgitdirs."), |
1402 | 0 | displaypath); |
1403 | |
|
1404 | 0 | absorb_git_dir_into_superproject(path, NULL); |
1405 | |
|
1406 | 0 | } |
1407 | |
|
1408 | 0 | if (!(flags & OPT_FORCE)) { |
1409 | 0 | struct child_process cp_rm = CHILD_PROCESS_INIT; |
1410 | |
|
1411 | 0 | cp_rm.git_cmd = 1; |
1412 | 0 | strvec_pushl(&cp_rm.args, "rm", "-qn", |
1413 | 0 | path, NULL); |
1414 | |
|
1415 | 0 | if (run_command(&cp_rm)) |
1416 | 0 | die(_("Submodule work tree '%s' contains local " |
1417 | 0 | "modifications; use '-f' to discard them"), |
1418 | 0 | displaypath); |
1419 | 0 | } |
1420 | | |
1421 | 0 | strbuf_addstr(&sb_rm, path); |
1422 | |
|
1423 | 0 | if (!remove_dir_recursively(&sb_rm, 0)) |
1424 | 0 | format = _("Cleared directory '%s'\n"); |
1425 | 0 | else |
1426 | 0 | format = _("Could not remove submodule work tree '%s'\n"); |
1427 | |
|
1428 | 0 | if (!(flags & OPT_QUIET)) |
1429 | 0 | printf(format, displaypath); |
1430 | |
|
1431 | 0 | submodule_unset_core_worktree(sub); |
1432 | |
|
1433 | 0 | strbuf_release(&sb_rm); |
1434 | 0 | } |
1435 | | |
1436 | 0 | if (mkdir(path, 0777)) |
1437 | 0 | printf(_("could not create empty submodule directory %s"), |
1438 | 0 | displaypath); |
1439 | |
|
1440 | 0 | cp_config.git_cmd = 1; |
1441 | 0 | strvec_pushl(&cp_config.args, "config", "--get-regexp", NULL); |
1442 | 0 | strvec_pushf(&cp_config.args, "submodule.%s\\.", sub->name); |
1443 | | |
1444 | | /* remove the .git/config entries (unless the user already did it) */ |
1445 | 0 | if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) { |
1446 | 0 | char *sub_key = xstrfmt("submodule.%s", sub->name); |
1447 | | |
1448 | | /* |
1449 | | * remove the whole section so we have a clean state when |
1450 | | * the user later decides to init this submodule again |
1451 | | */ |
1452 | 0 | git_config_rename_section_in_file(NULL, sub_key, NULL); |
1453 | 0 | if (!(flags & OPT_QUIET)) |
1454 | 0 | printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"), |
1455 | 0 | sub->name, sub->url, displaypath); |
1456 | 0 | free(sub_key); |
1457 | 0 | } |
1458 | |
|
1459 | 0 | cleanup: |
1460 | 0 | free(displaypath); |
1461 | 0 | free(sub_git_dir); |
1462 | 0 | strbuf_release(&sb_config); |
1463 | 0 | } |
1464 | | |
1465 | | static void deinit_submodule_cb(const struct cache_entry *list_item, |
1466 | | void *cb_data) |
1467 | 0 | { |
1468 | 0 | struct deinit_cb *info = cb_data; |
1469 | 0 | deinit_submodule(list_item->name, info->prefix, info->flags); |
1470 | 0 | } |
1471 | | |
1472 | | static int module_deinit(int argc, const char **argv, const char *prefix) |
1473 | 0 | { |
1474 | 0 | struct deinit_cb info = DEINIT_CB_INIT; |
1475 | 0 | struct pathspec pathspec = { 0 }; |
1476 | 0 | struct module_list list = MODULE_LIST_INIT; |
1477 | 0 | int quiet = 0; |
1478 | 0 | int force = 0; |
1479 | 0 | int all = 0; |
1480 | 0 | struct option module_deinit_options[] = { |
1481 | 0 | OPT__QUIET(&quiet, N_("suppress submodule status output")), |
1482 | 0 | OPT__FORCE(&force, N_("remove submodule working trees even if they contain local changes"), 0), |
1483 | 0 | OPT_BOOL(0, "all", &all, N_("unregister all submodules")), |
1484 | 0 | OPT_END() |
1485 | 0 | }; |
1486 | 0 | const char *const git_submodule_helper_usage[] = { |
1487 | 0 | N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"), |
1488 | 0 | NULL |
1489 | 0 | }; |
1490 | 0 | int ret = 1; |
1491 | |
|
1492 | 0 | argc = parse_options(argc, argv, prefix, module_deinit_options, |
1493 | 0 | git_submodule_helper_usage, 0); |
1494 | |
|
1495 | 0 | if (all && argc) { |
1496 | 0 | error("pathspec and --all are incompatible"); |
1497 | 0 | usage_with_options(git_submodule_helper_usage, |
1498 | 0 | module_deinit_options); |
1499 | 0 | } |
1500 | | |
1501 | 0 | if (!argc && !all) |
1502 | 0 | die(_("Use '--all' if you really want to deinitialize all submodules")); |
1503 | | |
1504 | 0 | if (module_list_compute(argv, prefix, &pathspec, &list) < 0) |
1505 | 0 | goto cleanup; |
1506 | | |
1507 | 0 | info.prefix = prefix; |
1508 | 0 | if (quiet) |
1509 | 0 | info.flags |= OPT_QUIET; |
1510 | 0 | if (force) |
1511 | 0 | info.flags |= OPT_FORCE; |
1512 | |
|
1513 | 0 | for_each_listed_submodule(&list, deinit_submodule_cb, &info); |
1514 | |
|
1515 | 0 | ret = 0; |
1516 | 0 | cleanup: |
1517 | 0 | module_list_release(&list); |
1518 | 0 | clear_pathspec(&pathspec); |
1519 | 0 | return ret; |
1520 | 0 | } |
1521 | | |
1522 | | struct module_clone_data { |
1523 | | const char *prefix; |
1524 | | const char *path; |
1525 | | const char *name; |
1526 | | const char *url; |
1527 | | const char *depth; |
1528 | | struct list_objects_filter_options *filter_options; |
1529 | | unsigned int quiet: 1; |
1530 | | unsigned int progress: 1; |
1531 | | unsigned int dissociate: 1; |
1532 | | unsigned int require_init: 1; |
1533 | | int single_branch; |
1534 | | }; |
1535 | 0 | #define MODULE_CLONE_DATA_INIT { \ |
1536 | 0 | .single_branch = -1, \ |
1537 | 0 | } |
1538 | | |
1539 | | struct submodule_alternate_setup { |
1540 | | const char *submodule_name; |
1541 | | enum SUBMODULE_ALTERNATE_ERROR_MODE { |
1542 | | SUBMODULE_ALTERNATE_ERROR_DIE, |
1543 | | SUBMODULE_ALTERNATE_ERROR_INFO, |
1544 | | SUBMODULE_ALTERNATE_ERROR_IGNORE |
1545 | | } error_mode; |
1546 | | struct string_list *reference; |
1547 | | }; |
1548 | 0 | #define SUBMODULE_ALTERNATE_SETUP_INIT { \ |
1549 | 0 | .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \ |
1550 | 0 | } |
1551 | | |
1552 | | static const char alternate_error_advice[] = N_( |
1553 | | "An alternate computed from a superproject's alternate is invalid.\n" |
1554 | | "To allow Git to clone without an alternate in such a case, set\n" |
1555 | | "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n" |
1556 | | "'--reference-if-able' instead of '--reference'." |
1557 | | ); |
1558 | | |
1559 | | static int add_possible_reference_from_superproject( |
1560 | | struct object_directory *odb, void *sas_cb) |
1561 | 0 | { |
1562 | 0 | struct submodule_alternate_setup *sas = sas_cb; |
1563 | 0 | size_t len; |
1564 | | |
1565 | | /* |
1566 | | * If the alternate object store is another repository, try the |
1567 | | * standard layout with .git/(modules/<name>)+/objects |
1568 | | */ |
1569 | 0 | if (strip_suffix(odb->path, "/objects", &len)) { |
1570 | 0 | struct repository alternate; |
1571 | 0 | char *sm_alternate; |
1572 | 0 | struct strbuf sb = STRBUF_INIT; |
1573 | 0 | struct strbuf err = STRBUF_INIT; |
1574 | 0 | strbuf_add(&sb, odb->path, len); |
1575 | |
|
1576 | 0 | if (repo_init(&alternate, sb.buf, NULL) < 0) |
1577 | 0 | die(_("could not get a repository handle for gitdir '%s'"), |
1578 | 0 | sb.buf); |
1579 | | |
1580 | | /* |
1581 | | * We need to end the new path with '/' to mark it as a dir, |
1582 | | * otherwise a submodule name containing '/' will be broken |
1583 | | * as the last part of a missing submodule reference would |
1584 | | * be taken as a file name. |
1585 | | */ |
1586 | 0 | strbuf_reset(&sb); |
1587 | 0 | submodule_name_to_gitdir(&sb, &alternate, sas->submodule_name); |
1588 | 0 | strbuf_addch(&sb, '/'); |
1589 | 0 | repo_clear(&alternate); |
1590 | |
|
1591 | 0 | sm_alternate = compute_alternate_path(sb.buf, &err); |
1592 | 0 | if (sm_alternate) { |
1593 | 0 | char *p = strbuf_detach(&sb, NULL); |
1594 | |
|
1595 | 0 | string_list_append(sas->reference, p)->util = p; |
1596 | 0 | free(sm_alternate); |
1597 | 0 | } else { |
1598 | 0 | switch (sas->error_mode) { |
1599 | 0 | case SUBMODULE_ALTERNATE_ERROR_DIE: |
1600 | 0 | if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE)) |
1601 | 0 | advise(_(alternate_error_advice)); |
1602 | 0 | die(_("submodule '%s' cannot add alternate: %s"), |
1603 | 0 | sas->submodule_name, err.buf); |
1604 | 0 | case SUBMODULE_ALTERNATE_ERROR_INFO: |
1605 | 0 | fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"), |
1606 | 0 | sas->submodule_name, err.buf); |
1607 | 0 | case SUBMODULE_ALTERNATE_ERROR_IGNORE: |
1608 | 0 | ; /* nothing */ |
1609 | 0 | } |
1610 | 0 | } |
1611 | 0 | strbuf_release(&sb); |
1612 | 0 | } |
1613 | | |
1614 | 0 | return 0; |
1615 | 0 | } |
1616 | | |
1617 | | static void prepare_possible_alternates(const char *sm_name, |
1618 | | struct string_list *reference) |
1619 | 0 | { |
1620 | 0 | char *sm_alternate = NULL, *error_strategy = NULL; |
1621 | 0 | struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT; |
1622 | |
|
1623 | 0 | git_config_get_string("submodule.alternateLocation", &sm_alternate); |
1624 | 0 | if (!sm_alternate) |
1625 | 0 | return; |
1626 | | |
1627 | 0 | git_config_get_string("submodule.alternateErrorStrategy", &error_strategy); |
1628 | |
|
1629 | 0 | if (!error_strategy) |
1630 | 0 | error_strategy = xstrdup("die"); |
1631 | |
|
1632 | 0 | sas.submodule_name = sm_name; |
1633 | 0 | sas.reference = reference; |
1634 | 0 | if (!strcmp(error_strategy, "die")) |
1635 | 0 | sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE; |
1636 | 0 | else if (!strcmp(error_strategy, "info")) |
1637 | 0 | sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO; |
1638 | 0 | else if (!strcmp(error_strategy, "ignore")) |
1639 | 0 | sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE; |
1640 | 0 | else |
1641 | 0 | die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy); |
1642 | | |
1643 | 0 | if (!strcmp(sm_alternate, "superproject")) |
1644 | 0 | foreach_alt_odb(add_possible_reference_from_superproject, &sas); |
1645 | 0 | else if (!strcmp(sm_alternate, "no")) |
1646 | 0 | ; /* do nothing */ |
1647 | 0 | else |
1648 | 0 | die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate); |
1649 | | |
1650 | 0 | free(sm_alternate); |
1651 | 0 | free(error_strategy); |
1652 | 0 | } |
1653 | | |
1654 | | static char *clone_submodule_sm_gitdir(const char *name) |
1655 | 0 | { |
1656 | 0 | struct strbuf sb = STRBUF_INIT; |
1657 | 0 | char *sm_gitdir; |
1658 | |
|
1659 | 0 | submodule_name_to_gitdir(&sb, the_repository, name); |
1660 | 0 | sm_gitdir = absolute_pathdup(sb.buf); |
1661 | 0 | strbuf_release(&sb); |
1662 | |
|
1663 | 0 | return sm_gitdir; |
1664 | 0 | } |
1665 | | |
1666 | | static int clone_submodule(const struct module_clone_data *clone_data, |
1667 | | struct string_list *reference) |
1668 | 0 | { |
1669 | 0 | char *p; |
1670 | 0 | char *sm_gitdir = clone_submodule_sm_gitdir(clone_data->name); |
1671 | 0 | char *sm_alternate = NULL, *error_strategy = NULL; |
1672 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
1673 | 0 | const char *clone_data_path = clone_data->path; |
1674 | 0 | char *to_free = NULL; |
1675 | |
|
1676 | 0 | if (!is_absolute_path(clone_data->path)) |
1677 | 0 | clone_data_path = to_free = xstrfmt("%s/%s", get_git_work_tree(), |
1678 | 0 | clone_data->path); |
1679 | |
|
1680 | 0 | if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0) |
1681 | 0 | die(_("refusing to create/use '%s' in another submodule's " |
1682 | 0 | "git dir"), sm_gitdir); |
1683 | | |
1684 | 0 | if (!file_exists(sm_gitdir)) { |
1685 | 0 | if (safe_create_leading_directories_const(sm_gitdir) < 0) |
1686 | 0 | die(_("could not create directory '%s'"), sm_gitdir); |
1687 | | |
1688 | 0 | prepare_possible_alternates(clone_data->name, reference); |
1689 | |
|
1690 | 0 | strvec_push(&cp.args, "clone"); |
1691 | 0 | strvec_push(&cp.args, "--no-checkout"); |
1692 | 0 | if (clone_data->quiet) |
1693 | 0 | strvec_push(&cp.args, "--quiet"); |
1694 | 0 | if (clone_data->progress) |
1695 | 0 | strvec_push(&cp.args, "--progress"); |
1696 | 0 | if (clone_data->depth && *(clone_data->depth)) |
1697 | 0 | strvec_pushl(&cp.args, "--depth", clone_data->depth, NULL); |
1698 | 0 | if (reference->nr) { |
1699 | 0 | struct string_list_item *item; |
1700 | |
|
1701 | 0 | for_each_string_list_item(item, reference) |
1702 | 0 | strvec_pushl(&cp.args, "--reference", |
1703 | 0 | item->string, NULL); |
1704 | 0 | } |
1705 | 0 | if (clone_data->dissociate) |
1706 | 0 | strvec_push(&cp.args, "--dissociate"); |
1707 | 0 | if (sm_gitdir && *sm_gitdir) |
1708 | 0 | strvec_pushl(&cp.args, "--separate-git-dir", sm_gitdir, NULL); |
1709 | 0 | if (clone_data->filter_options && clone_data->filter_options->choice) |
1710 | 0 | strvec_pushf(&cp.args, "--filter=%s", |
1711 | 0 | expand_list_objects_filter_spec( |
1712 | 0 | clone_data->filter_options)); |
1713 | 0 | if (clone_data->single_branch >= 0) |
1714 | 0 | strvec_push(&cp.args, clone_data->single_branch ? |
1715 | 0 | "--single-branch" : |
1716 | 0 | "--no-single-branch"); |
1717 | |
|
1718 | 0 | strvec_push(&cp.args, "--"); |
1719 | 0 | strvec_push(&cp.args, clone_data->url); |
1720 | 0 | strvec_push(&cp.args, clone_data_path); |
1721 | |
|
1722 | 0 | cp.git_cmd = 1; |
1723 | 0 | prepare_submodule_repo_env(&cp.env); |
1724 | 0 | cp.no_stdin = 1; |
1725 | |
|
1726 | 0 | if(run_command(&cp)) |
1727 | 0 | die(_("clone of '%s' into submodule path '%s' failed"), |
1728 | 0 | clone_data->url, clone_data_path); |
1729 | 0 | } else { |
1730 | 0 | char *path; |
1731 | |
|
1732 | 0 | if (clone_data->require_init && !access(clone_data_path, X_OK) && |
1733 | 0 | !is_empty_dir(clone_data_path)) |
1734 | 0 | die(_("directory not empty: '%s'"), clone_data_path); |
1735 | 0 | if (safe_create_leading_directories_const(clone_data_path) < 0) |
1736 | 0 | die(_("could not create directory '%s'"), clone_data_path); |
1737 | 0 | path = xstrfmt("%s/index", sm_gitdir); |
1738 | 0 | unlink_or_warn(path); |
1739 | 0 | free(path); |
1740 | 0 | } |
1741 | | |
1742 | 0 | connect_work_tree_and_git_dir(clone_data_path, sm_gitdir, 0); |
1743 | |
|
1744 | 0 | p = git_pathdup_submodule(clone_data_path, "config"); |
1745 | 0 | if (!p) |
1746 | 0 | die(_("could not get submodule directory for '%s'"), clone_data_path); |
1747 | | |
1748 | | /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */ |
1749 | 0 | git_config_get_string("submodule.alternateLocation", &sm_alternate); |
1750 | 0 | if (sm_alternate) |
1751 | 0 | git_config_set_in_file(p, "submodule.alternateLocation", |
1752 | 0 | sm_alternate); |
1753 | 0 | git_config_get_string("submodule.alternateErrorStrategy", &error_strategy); |
1754 | 0 | if (error_strategy) |
1755 | 0 | git_config_set_in_file(p, "submodule.alternateErrorStrategy", |
1756 | 0 | error_strategy); |
1757 | |
|
1758 | 0 | free(sm_alternate); |
1759 | 0 | free(error_strategy); |
1760 | |
|
1761 | 0 | free(sm_gitdir); |
1762 | 0 | free(p); |
1763 | 0 | free(to_free); |
1764 | 0 | return 0; |
1765 | 0 | } |
1766 | | |
1767 | | static int module_clone(int argc, const char **argv, const char *prefix) |
1768 | 0 | { |
1769 | 0 | int dissociate = 0, quiet = 0, progress = 0, require_init = 0; |
1770 | 0 | struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT; |
1771 | 0 | struct string_list reference = STRING_LIST_INIT_NODUP; |
1772 | 0 | struct list_objects_filter_options filter_options = |
1773 | 0 | LIST_OBJECTS_FILTER_INIT; |
1774 | |
|
1775 | 0 | struct option module_clone_options[] = { |
1776 | 0 | OPT_STRING(0, "prefix", &clone_data.prefix, |
1777 | 0 | N_("path"), |
1778 | 0 | N_("alternative anchor for relative paths")), |
1779 | 0 | OPT_STRING(0, "path", &clone_data.path, |
1780 | 0 | N_("path"), |
1781 | 0 | N_("where the new submodule will be cloned to")), |
1782 | 0 | OPT_STRING(0, "name", &clone_data.name, |
1783 | 0 | N_("string"), |
1784 | 0 | N_("name of the new submodule")), |
1785 | 0 | OPT_STRING(0, "url", &clone_data.url, |
1786 | 0 | N_("string"), |
1787 | 0 | N_("url where to clone the submodule from")), |
1788 | 0 | OPT_STRING_LIST(0, "reference", &reference, |
1789 | 0 | N_("repo"), |
1790 | 0 | N_("reference repository")), |
1791 | 0 | OPT_BOOL(0, "dissociate", &dissociate, |
1792 | 0 | N_("use --reference only while cloning")), |
1793 | 0 | OPT_STRING(0, "depth", &clone_data.depth, |
1794 | 0 | N_("string"), |
1795 | 0 | N_("depth for shallow clones")), |
1796 | 0 | OPT__QUIET(&quiet, "suppress output for cloning a submodule"), |
1797 | 0 | OPT_BOOL(0, "progress", &progress, |
1798 | 0 | N_("force cloning progress")), |
1799 | 0 | OPT_BOOL(0, "require-init", &require_init, |
1800 | 0 | N_("disallow cloning into non-empty directory")), |
1801 | 0 | OPT_BOOL(0, "single-branch", &clone_data.single_branch, |
1802 | 0 | N_("clone only one branch, HEAD or --branch")), |
1803 | 0 | OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options), |
1804 | 0 | OPT_END() |
1805 | 0 | }; |
1806 | 0 | const char *const git_submodule_helper_usage[] = { |
1807 | 0 | N_("git submodule--helper clone [--prefix=<path>] [--quiet] " |
1808 | 0 | "[--reference <repository>] [--name <name>] [--depth <depth>] " |
1809 | 0 | "[--single-branch] [--filter <filter-spec>] " |
1810 | 0 | "--url <url> --path <path>"), |
1811 | 0 | NULL |
1812 | 0 | }; |
1813 | |
|
1814 | 0 | argc = parse_options(argc, argv, prefix, module_clone_options, |
1815 | 0 | git_submodule_helper_usage, 0); |
1816 | |
|
1817 | 0 | clone_data.dissociate = !!dissociate; |
1818 | 0 | clone_data.quiet = !!quiet; |
1819 | 0 | clone_data.progress = !!progress; |
1820 | 0 | clone_data.require_init = !!require_init; |
1821 | 0 | clone_data.filter_options = &filter_options; |
1822 | |
|
1823 | 0 | if (argc || !clone_data.url || !clone_data.path || !*(clone_data.path)) |
1824 | 0 | usage_with_options(git_submodule_helper_usage, |
1825 | 0 | module_clone_options); |
1826 | | |
1827 | 0 | clone_submodule(&clone_data, &reference); |
1828 | 0 | list_objects_filter_release(&filter_options); |
1829 | 0 | string_list_clear(&reference, 1); |
1830 | 0 | return 0; |
1831 | 0 | } |
1832 | | |
1833 | | static int determine_submodule_update_strategy(struct repository *r, |
1834 | | int just_cloned, |
1835 | | const char *path, |
1836 | | enum submodule_update_type update, |
1837 | | struct submodule_update_strategy *out) |
1838 | 0 | { |
1839 | 0 | const struct submodule *sub = submodule_from_path(r, null_oid(), path); |
1840 | 0 | char *key; |
1841 | 0 | const char *val; |
1842 | 0 | int ret; |
1843 | |
|
1844 | 0 | key = xstrfmt("submodule.%s.update", sub->name); |
1845 | |
|
1846 | 0 | if (update) { |
1847 | 0 | out->type = update; |
1848 | 0 | } else if (!repo_config_get_string_tmp(r, key, &val)) { |
1849 | 0 | if (parse_submodule_update_strategy(val, out) < 0) { |
1850 | 0 | ret = die_message(_("Invalid update mode '%s' configured for submodule path '%s'"), |
1851 | 0 | val, path); |
1852 | 0 | goto cleanup; |
1853 | 0 | } |
1854 | 0 | } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) { |
1855 | 0 | if (sub->update_strategy.type == SM_UPDATE_COMMAND) |
1856 | 0 | BUG("how did we read update = !command from .gitmodules?"); |
1857 | 0 | out->type = sub->update_strategy.type; |
1858 | 0 | out->command = sub->update_strategy.command; |
1859 | 0 | } else |
1860 | 0 | out->type = SM_UPDATE_CHECKOUT; |
1861 | | |
1862 | 0 | if (just_cloned && |
1863 | 0 | (out->type == SM_UPDATE_MERGE || |
1864 | 0 | out->type == SM_UPDATE_REBASE || |
1865 | 0 | out->type == SM_UPDATE_NONE)) |
1866 | 0 | out->type = SM_UPDATE_CHECKOUT; |
1867 | |
|
1868 | 0 | ret = 0; |
1869 | 0 | cleanup: |
1870 | 0 | free(key); |
1871 | 0 | return ret; |
1872 | 0 | } |
1873 | | |
1874 | | struct update_clone_data { |
1875 | | const struct submodule *sub; |
1876 | | struct object_id oid; |
1877 | | unsigned just_cloned; |
1878 | | }; |
1879 | | |
1880 | | struct submodule_update_clone { |
1881 | | /* index into 'update_data.list', the list of submodules to look into for cloning */ |
1882 | | int current; |
1883 | | |
1884 | | /* configuration parameters which are passed on to the children */ |
1885 | | const struct update_data *update_data; |
1886 | | |
1887 | | /* to be consumed by update_submodule() */ |
1888 | | struct update_clone_data *update_clone; |
1889 | | int update_clone_nr; int update_clone_alloc; |
1890 | | |
1891 | | /* If we want to stop as fast as possible and return an error */ |
1892 | | unsigned quickstop : 1; |
1893 | | |
1894 | | /* failed clones to be retried again */ |
1895 | | const struct cache_entry **failed_clones; |
1896 | | int failed_clones_nr, failed_clones_alloc; |
1897 | | }; |
1898 | 0 | #define SUBMODULE_UPDATE_CLONE_INIT { 0 } |
1899 | | |
1900 | | static void submodule_update_clone_release(struct submodule_update_clone *suc) |
1901 | 0 | { |
1902 | 0 | free(suc->update_clone); |
1903 | 0 | free(suc->failed_clones); |
1904 | 0 | } |
1905 | | |
1906 | | struct update_data { |
1907 | | const char *prefix; |
1908 | | const char *super_prefix; |
1909 | | char *displaypath; |
1910 | | enum submodule_update_type update_default; |
1911 | | struct object_id suboid; |
1912 | | struct string_list references; |
1913 | | struct submodule_update_strategy update_strategy; |
1914 | | struct list_objects_filter_options *filter_options; |
1915 | | struct module_list list; |
1916 | | int depth; |
1917 | | int max_jobs; |
1918 | | int single_branch; |
1919 | | int recommend_shallow; |
1920 | | unsigned int require_init; |
1921 | | unsigned int force; |
1922 | | unsigned int quiet; |
1923 | | unsigned int nofetch; |
1924 | | unsigned int remote; |
1925 | | unsigned int progress; |
1926 | | unsigned int dissociate; |
1927 | | unsigned int init; |
1928 | | unsigned int warn_if_uninitialized; |
1929 | | unsigned int recursive; |
1930 | | |
1931 | | /* copied over from update_clone_data */ |
1932 | | struct object_id oid; |
1933 | | unsigned int just_cloned; |
1934 | | const char *sm_path; |
1935 | | }; |
1936 | 0 | #define UPDATE_DATA_INIT { \ |
1937 | 0 | .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \ |
1938 | 0 | .list = MODULE_LIST_INIT, \ |
1939 | 0 | .recommend_shallow = -1, \ |
1940 | 0 | .references = STRING_LIST_INIT_DUP, \ |
1941 | 0 | .single_branch = -1, \ |
1942 | 0 | .max_jobs = 1, \ |
1943 | 0 | } |
1944 | | |
1945 | | static void update_data_release(struct update_data *ud) |
1946 | 0 | { |
1947 | 0 | free(ud->displaypath); |
1948 | 0 | module_list_release(&ud->list); |
1949 | 0 | } |
1950 | | |
1951 | | static void next_submodule_warn_missing(struct submodule_update_clone *suc, |
1952 | | struct strbuf *out, const char *displaypath) |
1953 | 0 | { |
1954 | | /* |
1955 | | * Only mention uninitialized submodules when their |
1956 | | * paths have been specified. |
1957 | | */ |
1958 | 0 | if (suc->update_data->warn_if_uninitialized) { |
1959 | 0 | strbuf_addf(out, |
1960 | 0 | _("Submodule path '%s' not initialized"), |
1961 | 0 | displaypath); |
1962 | 0 | strbuf_addch(out, '\n'); |
1963 | 0 | strbuf_addstr(out, |
1964 | 0 | _("Maybe you want to use 'update --init'?")); |
1965 | 0 | strbuf_addch(out, '\n'); |
1966 | 0 | } |
1967 | 0 | } |
1968 | | |
1969 | | /** |
1970 | | * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to |
1971 | | * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise. |
1972 | | */ |
1973 | | static int prepare_to_clone_next_submodule(const struct cache_entry *ce, |
1974 | | struct child_process *child, |
1975 | | struct submodule_update_clone *suc, |
1976 | | struct strbuf *out) |
1977 | 0 | { |
1978 | 0 | const struct submodule *sub = NULL; |
1979 | 0 | const char *url = NULL; |
1980 | 0 | const char *update_string; |
1981 | 0 | enum submodule_update_type update_type; |
1982 | 0 | char *key; |
1983 | 0 | const struct update_data *ud = suc->update_data; |
1984 | 0 | char *displaypath = get_submodule_displaypath(ce->name, ud->prefix, |
1985 | 0 | ud->super_prefix); |
1986 | 0 | struct strbuf sb = STRBUF_INIT; |
1987 | 0 | int needs_cloning = 0; |
1988 | 0 | int need_free_url = 0; |
1989 | |
|
1990 | 0 | if (ce_stage(ce)) { |
1991 | 0 | strbuf_addf(out, _("Skipping unmerged submodule %s"), displaypath); |
1992 | 0 | strbuf_addch(out, '\n'); |
1993 | 0 | goto cleanup; |
1994 | 0 | } |
1995 | | |
1996 | 0 | sub = submodule_from_path(the_repository, null_oid(), ce->name); |
1997 | |
|
1998 | 0 | if (!sub) { |
1999 | 0 | next_submodule_warn_missing(suc, out, displaypath); |
2000 | 0 | goto cleanup; |
2001 | 0 | } |
2002 | | |
2003 | 0 | key = xstrfmt("submodule.%s.update", sub->name); |
2004 | 0 | if (!repo_config_get_string_tmp(the_repository, key, &update_string)) { |
2005 | 0 | update_type = parse_submodule_update_type(update_string); |
2006 | 0 | } else { |
2007 | 0 | update_type = sub->update_strategy.type; |
2008 | 0 | } |
2009 | 0 | free(key); |
2010 | |
|
2011 | 0 | if (suc->update_data->update_strategy.type == SM_UPDATE_NONE |
2012 | 0 | || (suc->update_data->update_strategy.type == SM_UPDATE_UNSPECIFIED |
2013 | 0 | && update_type == SM_UPDATE_NONE)) { |
2014 | 0 | strbuf_addf(out, _("Skipping submodule '%s'"), displaypath); |
2015 | 0 | strbuf_addch(out, '\n'); |
2016 | 0 | goto cleanup; |
2017 | 0 | } |
2018 | | |
2019 | | /* Check if the submodule has been initialized. */ |
2020 | 0 | if (!is_submodule_active(the_repository, ce->name)) { |
2021 | 0 | next_submodule_warn_missing(suc, out, displaypath); |
2022 | 0 | goto cleanup; |
2023 | 0 | } |
2024 | | |
2025 | 0 | strbuf_reset(&sb); |
2026 | 0 | strbuf_addf(&sb, "submodule.%s.url", sub->name); |
2027 | 0 | if (repo_config_get_string_tmp(the_repository, sb.buf, &url)) { |
2028 | 0 | if (sub->url && (starts_with_dot_slash(sub->url) || |
2029 | 0 | starts_with_dot_dot_slash(sub->url))) { |
2030 | 0 | url = resolve_relative_url(sub->url, NULL, 0); |
2031 | 0 | need_free_url = 1; |
2032 | 0 | } else |
2033 | 0 | url = sub->url; |
2034 | 0 | } |
2035 | |
|
2036 | 0 | if (!url) |
2037 | 0 | die(_("cannot clone submodule '%s' without a URL"), sub->name); |
2038 | | |
2039 | 0 | strbuf_reset(&sb); |
2040 | 0 | strbuf_addf(&sb, "%s/.git", ce->name); |
2041 | 0 | needs_cloning = !file_exists(sb.buf); |
2042 | |
|
2043 | 0 | ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1, |
2044 | 0 | suc->update_clone_alloc); |
2045 | 0 | oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid); |
2046 | 0 | suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning; |
2047 | 0 | suc->update_clone[suc->update_clone_nr].sub = sub; |
2048 | 0 | suc->update_clone_nr++; |
2049 | |
|
2050 | 0 | if (!needs_cloning) |
2051 | 0 | goto cleanup; |
2052 | | |
2053 | 0 | child->git_cmd = 1; |
2054 | 0 | child->no_stdin = 1; |
2055 | 0 | child->stdout_to_stderr = 1; |
2056 | 0 | child->err = -1; |
2057 | 0 | strvec_push(&child->args, "submodule--helper"); |
2058 | 0 | strvec_push(&child->args, "clone"); |
2059 | 0 | if (suc->update_data->progress) |
2060 | 0 | strvec_push(&child->args, "--progress"); |
2061 | 0 | if (suc->update_data->quiet) |
2062 | 0 | strvec_push(&child->args, "--quiet"); |
2063 | 0 | if (suc->update_data->prefix) |
2064 | 0 | strvec_pushl(&child->args, "--prefix", suc->update_data->prefix, NULL); |
2065 | 0 | if (suc->update_data->recommend_shallow && sub->recommend_shallow == 1) |
2066 | 0 | strvec_push(&child->args, "--depth=1"); |
2067 | 0 | else if (suc->update_data->depth) |
2068 | 0 | strvec_pushf(&child->args, "--depth=%d", suc->update_data->depth); |
2069 | 0 | if (suc->update_data->filter_options && suc->update_data->filter_options->choice) |
2070 | 0 | strvec_pushf(&child->args, "--filter=%s", |
2071 | 0 | expand_list_objects_filter_spec(suc->update_data->filter_options)); |
2072 | 0 | if (suc->update_data->require_init) |
2073 | 0 | strvec_push(&child->args, "--require-init"); |
2074 | 0 | strvec_pushl(&child->args, "--path", sub->path, NULL); |
2075 | 0 | strvec_pushl(&child->args, "--name", sub->name, NULL); |
2076 | 0 | strvec_pushl(&child->args, "--url", url, NULL); |
2077 | 0 | if (suc->update_data->references.nr) { |
2078 | 0 | struct string_list_item *item; |
2079 | |
|
2080 | 0 | for_each_string_list_item(item, &suc->update_data->references) |
2081 | 0 | strvec_pushl(&child->args, "--reference", item->string, NULL); |
2082 | 0 | } |
2083 | 0 | if (suc->update_data->dissociate) |
2084 | 0 | strvec_push(&child->args, "--dissociate"); |
2085 | 0 | if (suc->update_data->single_branch >= 0) |
2086 | 0 | strvec_push(&child->args, suc->update_data->single_branch ? |
2087 | 0 | "--single-branch" : |
2088 | 0 | "--no-single-branch"); |
2089 | |
|
2090 | 0 | cleanup: |
2091 | 0 | free(displaypath); |
2092 | 0 | strbuf_release(&sb); |
2093 | 0 | if (need_free_url) |
2094 | 0 | free((void*)url); |
2095 | |
|
2096 | 0 | return needs_cloning; |
2097 | 0 | } |
2098 | | |
2099 | | static int update_clone_get_next_task(struct child_process *child, |
2100 | | struct strbuf *err, |
2101 | | void *suc_cb, |
2102 | | void **idx_task_cb) |
2103 | 0 | { |
2104 | 0 | struct submodule_update_clone *suc = suc_cb; |
2105 | 0 | const struct cache_entry *ce; |
2106 | 0 | int index; |
2107 | |
|
2108 | 0 | for (; suc->current < suc->update_data->list.nr; suc->current++) { |
2109 | 0 | ce = suc->update_data->list.entries[suc->current]; |
2110 | 0 | if (prepare_to_clone_next_submodule(ce, child, suc, err)) { |
2111 | 0 | int *p = xmalloc(sizeof(*p)); |
2112 | |
|
2113 | 0 | *p = suc->current; |
2114 | 0 | *idx_task_cb = p; |
2115 | 0 | suc->current++; |
2116 | 0 | return 1; |
2117 | 0 | } |
2118 | 0 | } |
2119 | | |
2120 | | /* |
2121 | | * The loop above tried cloning each submodule once, now try the |
2122 | | * stragglers again, which we can imagine as an extension of the |
2123 | | * entry list. |
2124 | | */ |
2125 | 0 | index = suc->current - suc->update_data->list.nr; |
2126 | 0 | if (index < suc->failed_clones_nr) { |
2127 | 0 | int *p; |
2128 | |
|
2129 | 0 | ce = suc->failed_clones[index]; |
2130 | 0 | if (!prepare_to_clone_next_submodule(ce, child, suc, err)) { |
2131 | 0 | suc->current ++; |
2132 | 0 | strbuf_addstr(err, "BUG: submodule considered for " |
2133 | 0 | "cloning, doesn't need cloning " |
2134 | 0 | "any more?\n"); |
2135 | 0 | return 0; |
2136 | 0 | } |
2137 | 0 | p = xmalloc(sizeof(*p)); |
2138 | 0 | *p = suc->current; |
2139 | 0 | *idx_task_cb = p; |
2140 | 0 | suc->current ++; |
2141 | 0 | return 1; |
2142 | 0 | } |
2143 | | |
2144 | 0 | return 0; |
2145 | 0 | } |
2146 | | |
2147 | | static int update_clone_start_failure(struct strbuf *err UNUSED, |
2148 | | void *suc_cb, |
2149 | | void *idx_task_cb UNUSED) |
2150 | 0 | { |
2151 | 0 | struct submodule_update_clone *suc = suc_cb; |
2152 | |
|
2153 | 0 | suc->quickstop = 1; |
2154 | 0 | return 1; |
2155 | 0 | } |
2156 | | |
2157 | | static int update_clone_task_finished(int result, |
2158 | | struct strbuf *err, |
2159 | | void *suc_cb, |
2160 | | void *idx_task_cb) |
2161 | 0 | { |
2162 | 0 | const struct cache_entry *ce; |
2163 | 0 | struct submodule_update_clone *suc = suc_cb; |
2164 | 0 | int *idxP = idx_task_cb; |
2165 | 0 | int idx = *idxP; |
2166 | |
|
2167 | 0 | free(idxP); |
2168 | |
|
2169 | 0 | if (!result) |
2170 | 0 | return 0; |
2171 | | |
2172 | 0 | if (idx < suc->update_data->list.nr) { |
2173 | 0 | ce = suc->update_data->list.entries[idx]; |
2174 | 0 | strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"), |
2175 | 0 | ce->name); |
2176 | 0 | strbuf_addch(err, '\n'); |
2177 | 0 | ALLOC_GROW(suc->failed_clones, |
2178 | 0 | suc->failed_clones_nr + 1, |
2179 | 0 | suc->failed_clones_alloc); |
2180 | 0 | suc->failed_clones[suc->failed_clones_nr++] = ce; |
2181 | 0 | return 0; |
2182 | 0 | } else { |
2183 | 0 | idx -= suc->update_data->list.nr; |
2184 | 0 | ce = suc->failed_clones[idx]; |
2185 | 0 | strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"), |
2186 | 0 | ce->name); |
2187 | 0 | strbuf_addch(err, '\n'); |
2188 | 0 | suc->quickstop = 1; |
2189 | 0 | return 1; |
2190 | 0 | } |
2191 | | |
2192 | 0 | return 0; |
2193 | 0 | } |
2194 | | |
2195 | | static int git_update_clone_config(const char *var, const char *value, |
2196 | | const struct config_context *ctx, |
2197 | | void *cb) |
2198 | 0 | { |
2199 | 0 | int *max_jobs = cb; |
2200 | |
|
2201 | 0 | if (!strcmp(var, "submodule.fetchjobs")) |
2202 | 0 | *max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi); |
2203 | 0 | return 0; |
2204 | 0 | } |
2205 | | |
2206 | | static int is_tip_reachable(const char *path, const struct object_id *oid) |
2207 | 0 | { |
2208 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
2209 | 0 | struct strbuf rev = STRBUF_INIT; |
2210 | 0 | char *hex = oid_to_hex(oid); |
2211 | |
|
2212 | 0 | cp.git_cmd = 1; |
2213 | 0 | cp.dir = path; |
2214 | 0 | cp.no_stderr = 1; |
2215 | 0 | strvec_pushl(&cp.args, "rev-list", "-n", "1", hex, "--not", "--all", NULL); |
2216 | |
|
2217 | 0 | prepare_submodule_repo_env(&cp.env); |
2218 | |
|
2219 | 0 | if (capture_command(&cp, &rev, GIT_MAX_HEXSZ + 1) || rev.len) |
2220 | 0 | return 0; |
2221 | | |
2222 | 0 | return 1; |
2223 | 0 | } |
2224 | | |
2225 | | static int fetch_in_submodule(const char *module_path, int depth, int quiet, |
2226 | | const struct object_id *oid) |
2227 | 0 | { |
2228 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
2229 | |
|
2230 | 0 | prepare_submodule_repo_env(&cp.env); |
2231 | 0 | cp.git_cmd = 1; |
2232 | 0 | cp.dir = module_path; |
2233 | |
|
2234 | 0 | strvec_push(&cp.args, "fetch"); |
2235 | 0 | if (quiet) |
2236 | 0 | strvec_push(&cp.args, "--quiet"); |
2237 | 0 | if (depth) |
2238 | 0 | strvec_pushf(&cp.args, "--depth=%d", depth); |
2239 | 0 | if (oid) { |
2240 | 0 | char *hex = oid_to_hex(oid); |
2241 | 0 | char *remote = get_default_remote(); |
2242 | |
|
2243 | 0 | strvec_pushl(&cp.args, remote, hex, NULL); |
2244 | 0 | free(remote); |
2245 | 0 | } |
2246 | |
|
2247 | 0 | return run_command(&cp); |
2248 | 0 | } |
2249 | | |
2250 | | static int run_update_command(const struct update_data *ud, int subforce) |
2251 | 0 | { |
2252 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
2253 | 0 | char *oid = oid_to_hex(&ud->oid); |
2254 | 0 | int ret; |
2255 | |
|
2256 | 0 | switch (ud->update_strategy.type) { |
2257 | 0 | case SM_UPDATE_CHECKOUT: |
2258 | 0 | cp.git_cmd = 1; |
2259 | 0 | strvec_pushl(&cp.args, "checkout", "-q", NULL); |
2260 | 0 | if (subforce) |
2261 | 0 | strvec_push(&cp.args, "-f"); |
2262 | 0 | break; |
2263 | 0 | case SM_UPDATE_REBASE: |
2264 | 0 | cp.git_cmd = 1; |
2265 | 0 | strvec_push(&cp.args, "rebase"); |
2266 | 0 | if (ud->quiet) |
2267 | 0 | strvec_push(&cp.args, "--quiet"); |
2268 | 0 | break; |
2269 | 0 | case SM_UPDATE_MERGE: |
2270 | 0 | cp.git_cmd = 1; |
2271 | 0 | strvec_push(&cp.args, "merge"); |
2272 | 0 | if (ud->quiet) |
2273 | 0 | strvec_push(&cp.args, "--quiet"); |
2274 | 0 | break; |
2275 | 0 | case SM_UPDATE_COMMAND: |
2276 | 0 | cp.use_shell = 1; |
2277 | 0 | strvec_push(&cp.args, ud->update_strategy.command); |
2278 | 0 | break; |
2279 | 0 | default: |
2280 | 0 | BUG("unexpected update strategy type: %d", |
2281 | 0 | ud->update_strategy.type); |
2282 | 0 | } |
2283 | 0 | strvec_push(&cp.args, oid); |
2284 | |
|
2285 | 0 | cp.dir = ud->sm_path; |
2286 | 0 | prepare_submodule_repo_env(&cp.env); |
2287 | 0 | if ((ret = run_command(&cp))) { |
2288 | 0 | switch (ud->update_strategy.type) { |
2289 | 0 | case SM_UPDATE_CHECKOUT: |
2290 | 0 | die_message(_("Unable to checkout '%s' in submodule path '%s'"), |
2291 | 0 | oid, ud->displaypath); |
2292 | | /* No "ret" assignment, use "git checkout"'s */ |
2293 | 0 | break; |
2294 | 0 | case SM_UPDATE_REBASE: |
2295 | 0 | ret = die_message(_("Unable to rebase '%s' in submodule path '%s'"), |
2296 | 0 | oid, ud->displaypath); |
2297 | 0 | break; |
2298 | 0 | case SM_UPDATE_MERGE: |
2299 | 0 | ret = die_message(_("Unable to merge '%s' in submodule path '%s'"), |
2300 | 0 | oid, ud->displaypath); |
2301 | 0 | break; |
2302 | 0 | case SM_UPDATE_COMMAND: |
2303 | 0 | ret = die_message(_("Execution of '%s %s' failed in submodule path '%s'"), |
2304 | 0 | ud->update_strategy.command, oid, ud->displaypath); |
2305 | 0 | break; |
2306 | 0 | default: |
2307 | 0 | BUG("unexpected update strategy type: %d", |
2308 | 0 | ud->update_strategy.type); |
2309 | 0 | } |
2310 | | |
2311 | 0 | return ret; |
2312 | 0 | } |
2313 | | |
2314 | 0 | if (ud->quiet) |
2315 | 0 | return 0; |
2316 | | |
2317 | 0 | switch (ud->update_strategy.type) { |
2318 | 0 | case SM_UPDATE_CHECKOUT: |
2319 | 0 | printf(_("Submodule path '%s': checked out '%s'\n"), |
2320 | 0 | ud->displaypath, oid); |
2321 | 0 | break; |
2322 | 0 | case SM_UPDATE_REBASE: |
2323 | 0 | printf(_("Submodule path '%s': rebased into '%s'\n"), |
2324 | 0 | ud->displaypath, oid); |
2325 | 0 | break; |
2326 | 0 | case SM_UPDATE_MERGE: |
2327 | 0 | printf(_("Submodule path '%s': merged in '%s'\n"), |
2328 | 0 | ud->displaypath, oid); |
2329 | 0 | break; |
2330 | 0 | case SM_UPDATE_COMMAND: |
2331 | 0 | printf(_("Submodule path '%s': '%s %s'\n"), |
2332 | 0 | ud->displaypath, ud->update_strategy.command, oid); |
2333 | 0 | break; |
2334 | 0 | default: |
2335 | 0 | BUG("unexpected update strategy type: %d", |
2336 | 0 | ud->update_strategy.type); |
2337 | 0 | } |
2338 | | |
2339 | 0 | return 0; |
2340 | 0 | } |
2341 | | |
2342 | | static int run_update_procedure(const struct update_data *ud) |
2343 | 0 | { |
2344 | 0 | int subforce = is_null_oid(&ud->suboid) || ud->force; |
2345 | |
|
2346 | 0 | if (!ud->nofetch) { |
2347 | | /* |
2348 | | * Run fetch only if `oid` isn't present or it |
2349 | | * is not reachable from a ref. |
2350 | | */ |
2351 | 0 | if (!is_tip_reachable(ud->sm_path, &ud->oid) && |
2352 | 0 | fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, NULL) && |
2353 | 0 | !ud->quiet) |
2354 | 0 | fprintf_ln(stderr, |
2355 | 0 | _("Unable to fetch in submodule path '%s'; " |
2356 | 0 | "trying to directly fetch %s:"), |
2357 | 0 | ud->displaypath, oid_to_hex(&ud->oid)); |
2358 | | /* |
2359 | | * Now we tried the usual fetch, but `oid` may |
2360 | | * not be reachable from any of the refs. |
2361 | | */ |
2362 | 0 | if (!is_tip_reachable(ud->sm_path, &ud->oid) && |
2363 | 0 | fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, &ud->oid)) |
2364 | 0 | return die_message(_("Fetched in submodule path '%s', but it did not " |
2365 | 0 | "contain %s. Direct fetching of that commit failed."), |
2366 | 0 | ud->displaypath, oid_to_hex(&ud->oid)); |
2367 | 0 | } |
2368 | | |
2369 | 0 | return run_update_command(ud, subforce); |
2370 | 0 | } |
2371 | | |
2372 | | static int remote_submodule_branch(const char *path, const char **branch) |
2373 | 0 | { |
2374 | 0 | const struct submodule *sub; |
2375 | 0 | char *key; |
2376 | 0 | *branch = NULL; |
2377 | |
|
2378 | 0 | sub = submodule_from_path(the_repository, null_oid(), path); |
2379 | 0 | if (!sub) |
2380 | 0 | return die_message(_("could not initialize submodule at path '%s'"), |
2381 | 0 | path); |
2382 | | |
2383 | 0 | key = xstrfmt("submodule.%s.branch", sub->name); |
2384 | 0 | if (repo_config_get_string_tmp(the_repository, key, branch)) |
2385 | 0 | *branch = sub->branch; |
2386 | 0 | free(key); |
2387 | |
|
2388 | 0 | if (!*branch) { |
2389 | 0 | *branch = "HEAD"; |
2390 | 0 | return 0; |
2391 | 0 | } |
2392 | | |
2393 | 0 | if (!strcmp(*branch, ".")) { |
2394 | 0 | const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL); |
2395 | |
|
2396 | 0 | if (!refname) |
2397 | 0 | return die_message(_("No such ref: %s"), "HEAD"); |
2398 | | |
2399 | | /* detached HEAD */ |
2400 | 0 | if (!strcmp(refname, "HEAD")) |
2401 | 0 | return die_message(_("Submodule (%s) branch configured to inherit " |
2402 | 0 | "branch from superproject, but the superproject " |
2403 | 0 | "is not on any branch"), sub->name); |
2404 | | |
2405 | 0 | if (!skip_prefix(refname, "refs/heads/", &refname)) |
2406 | 0 | return die_message(_("Expecting a full ref name, got %s"), |
2407 | 0 | refname); |
2408 | | |
2409 | 0 | *branch = refname; |
2410 | 0 | return 0; |
2411 | 0 | } |
2412 | | |
2413 | | /* Our "branch" is coming from repo_config_get_string_tmp() */ |
2414 | 0 | return 0; |
2415 | 0 | } |
2416 | | |
2417 | | static int ensure_core_worktree(const char *path) |
2418 | 0 | { |
2419 | 0 | const char *cw; |
2420 | 0 | struct repository subrepo; |
2421 | |
|
2422 | 0 | if (repo_submodule_init(&subrepo, the_repository, path, null_oid())) |
2423 | 0 | return die_message(_("could not get a repository handle for submodule '%s'"), |
2424 | 0 | path); |
2425 | | |
2426 | 0 | if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) { |
2427 | 0 | char *cfg_file, *abs_path; |
2428 | 0 | const char *rel_path; |
2429 | 0 | struct strbuf sb = STRBUF_INIT; |
2430 | |
|
2431 | 0 | cfg_file = repo_git_path(&subrepo, "config"); |
2432 | |
|
2433 | 0 | abs_path = absolute_pathdup(path); |
2434 | 0 | rel_path = relative_path(abs_path, subrepo.gitdir, &sb); |
2435 | |
|
2436 | 0 | git_config_set_in_file(cfg_file, "core.worktree", rel_path); |
2437 | |
|
2438 | 0 | free(cfg_file); |
2439 | 0 | free(abs_path); |
2440 | 0 | strbuf_release(&sb); |
2441 | 0 | } |
2442 | |
|
2443 | 0 | repo_clear(&subrepo); |
2444 | 0 | return 0; |
2445 | 0 | } |
2446 | | |
2447 | | static const char *submodule_update_type_to_label(enum submodule_update_type type) |
2448 | 0 | { |
2449 | 0 | switch (type) { |
2450 | 0 | case SM_UPDATE_CHECKOUT: |
2451 | 0 | return "checkout"; |
2452 | 0 | case SM_UPDATE_MERGE: |
2453 | 0 | return "merge"; |
2454 | 0 | case SM_UPDATE_REBASE: |
2455 | 0 | return "rebase"; |
2456 | 0 | case SM_UPDATE_UNSPECIFIED: |
2457 | 0 | case SM_UPDATE_NONE: |
2458 | 0 | case SM_UPDATE_COMMAND: |
2459 | 0 | break; |
2460 | 0 | } |
2461 | 0 | BUG("unreachable with type %d", type); |
2462 | 0 | } |
2463 | | |
2464 | | static void update_data_to_args(const struct update_data *update_data, |
2465 | | struct strvec *args) |
2466 | 0 | { |
2467 | 0 | enum submodule_update_type update_type = update_data->update_default; |
2468 | |
|
2469 | 0 | strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL); |
2470 | 0 | if (update_data->displaypath) { |
2471 | 0 | strvec_push(args, "--super-prefix"); |
2472 | 0 | strvec_pushf(args, "%s/", update_data->displaypath); |
2473 | 0 | } |
2474 | 0 | strvec_pushf(args, "--jobs=%d", update_data->max_jobs); |
2475 | 0 | if (update_data->quiet) |
2476 | 0 | strvec_push(args, "--quiet"); |
2477 | 0 | if (update_data->force) |
2478 | 0 | strvec_push(args, "--force"); |
2479 | 0 | if (update_data->init) |
2480 | 0 | strvec_push(args, "--init"); |
2481 | 0 | if (update_data->remote) |
2482 | 0 | strvec_push(args, "--remote"); |
2483 | 0 | if (update_data->nofetch) |
2484 | 0 | strvec_push(args, "--no-fetch"); |
2485 | 0 | if (update_data->dissociate) |
2486 | 0 | strvec_push(args, "--dissociate"); |
2487 | 0 | if (update_data->progress) |
2488 | 0 | strvec_push(args, "--progress"); |
2489 | 0 | if (update_data->require_init) |
2490 | 0 | strvec_push(args, "--require-init"); |
2491 | 0 | if (update_data->depth) |
2492 | 0 | strvec_pushf(args, "--depth=%d", update_data->depth); |
2493 | 0 | if (update_type != SM_UPDATE_UNSPECIFIED) |
2494 | 0 | strvec_pushf(args, "--%s", |
2495 | 0 | submodule_update_type_to_label(update_type)); |
2496 | |
|
2497 | 0 | if (update_data->references.nr) { |
2498 | 0 | struct string_list_item *item; |
2499 | |
|
2500 | 0 | for_each_string_list_item(item, &update_data->references) |
2501 | 0 | strvec_pushl(args, "--reference", item->string, NULL); |
2502 | 0 | } |
2503 | 0 | if (update_data->filter_options && update_data->filter_options->choice) |
2504 | 0 | strvec_pushf(args, "--filter=%s", |
2505 | 0 | expand_list_objects_filter_spec( |
2506 | 0 | update_data->filter_options)); |
2507 | 0 | if (update_data->recommend_shallow == 0) |
2508 | 0 | strvec_push(args, "--no-recommend-shallow"); |
2509 | 0 | else if (update_data->recommend_shallow == 1) |
2510 | 0 | strvec_push(args, "--recommend-shallow"); |
2511 | 0 | if (update_data->single_branch >= 0) |
2512 | 0 | strvec_push(args, update_data->single_branch ? |
2513 | 0 | "--single-branch" : |
2514 | 0 | "--no-single-branch"); |
2515 | 0 | } |
2516 | | |
2517 | | static int update_submodule(struct update_data *update_data) |
2518 | 0 | { |
2519 | 0 | int ret; |
2520 | |
|
2521 | 0 | ret = determine_submodule_update_strategy(the_repository, |
2522 | 0 | update_data->just_cloned, |
2523 | 0 | update_data->sm_path, |
2524 | 0 | update_data->update_default, |
2525 | 0 | &update_data->update_strategy); |
2526 | 0 | if (ret) |
2527 | 0 | return ret; |
2528 | | |
2529 | 0 | if (update_data->just_cloned) |
2530 | 0 | oidcpy(&update_data->suboid, null_oid()); |
2531 | 0 | else if (resolve_gitlink_ref(update_data->sm_path, "HEAD", &update_data->suboid)) |
2532 | 0 | return die_message(_("Unable to find current revision in submodule path '%s'"), |
2533 | 0 | update_data->displaypath); |
2534 | | |
2535 | 0 | if (update_data->remote) { |
2536 | 0 | char *remote_name; |
2537 | 0 | const char *branch; |
2538 | 0 | char *remote_ref; |
2539 | 0 | int code; |
2540 | |
|
2541 | 0 | code = get_default_remote_submodule(update_data->sm_path, &remote_name); |
2542 | 0 | if (code) |
2543 | 0 | return code; |
2544 | 0 | code = remote_submodule_branch(update_data->sm_path, &branch); |
2545 | 0 | if (code) |
2546 | 0 | return code; |
2547 | 0 | remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch); |
2548 | |
|
2549 | 0 | free(remote_name); |
2550 | |
|
2551 | 0 | if (!update_data->nofetch) { |
2552 | 0 | if (fetch_in_submodule(update_data->sm_path, update_data->depth, |
2553 | 0 | 0, NULL)) |
2554 | 0 | return die_message(_("Unable to fetch in submodule path '%s'"), |
2555 | 0 | update_data->sm_path); |
2556 | 0 | } |
2557 | | |
2558 | 0 | if (resolve_gitlink_ref(update_data->sm_path, remote_ref, &update_data->oid)) |
2559 | 0 | return die_message(_("Unable to find %s revision in submodule path '%s'"), |
2560 | 0 | remote_ref, update_data->sm_path); |
2561 | | |
2562 | 0 | free(remote_ref); |
2563 | 0 | } |
2564 | | |
2565 | 0 | if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force) { |
2566 | 0 | ret = run_update_procedure(update_data); |
2567 | 0 | if (ret) |
2568 | 0 | return ret; |
2569 | 0 | } |
2570 | | |
2571 | 0 | if (update_data->recursive) { |
2572 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
2573 | 0 | struct update_data next = *update_data; |
2574 | |
|
2575 | 0 | next.prefix = NULL; |
2576 | 0 | oidcpy(&next.oid, null_oid()); |
2577 | 0 | oidcpy(&next.suboid, null_oid()); |
2578 | |
|
2579 | 0 | cp.dir = update_data->sm_path; |
2580 | 0 | cp.git_cmd = 1; |
2581 | 0 | prepare_submodule_repo_env(&cp.env); |
2582 | 0 | update_data_to_args(&next, &cp.args); |
2583 | |
|
2584 | 0 | ret = run_command(&cp); |
2585 | 0 | if (ret) |
2586 | 0 | die_message(_("Failed to recurse into submodule path '%s'"), |
2587 | 0 | update_data->displaypath); |
2588 | 0 | return ret; |
2589 | 0 | } |
2590 | | |
2591 | 0 | return 0; |
2592 | 0 | } |
2593 | | |
2594 | | static int update_submodules(struct update_data *update_data) |
2595 | 0 | { |
2596 | 0 | int i, ret = 0; |
2597 | 0 | struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT; |
2598 | 0 | const struct run_process_parallel_opts opts = { |
2599 | 0 | .tr2_category = "submodule", |
2600 | 0 | .tr2_label = "parallel/update", |
2601 | |
|
2602 | 0 | .processes = update_data->max_jobs, |
2603 | |
|
2604 | 0 | .get_next_task = update_clone_get_next_task, |
2605 | 0 | .start_failure = update_clone_start_failure, |
2606 | 0 | .task_finished = update_clone_task_finished, |
2607 | 0 | .data = &suc, |
2608 | 0 | }; |
2609 | |
|
2610 | 0 | suc.update_data = update_data; |
2611 | 0 | run_processes_parallel(&opts); |
2612 | | |
2613 | | /* |
2614 | | * We saved the output and put it out all at once now. |
2615 | | * That means: |
2616 | | * - the listener does not have to interleave their (checkout) |
2617 | | * work with our fetching. The writes involved in a |
2618 | | * checkout involve more straightforward sequential I/O. |
2619 | | * - the listener can avoid doing any work if fetching failed. |
2620 | | */ |
2621 | 0 | if (suc.quickstop) { |
2622 | 0 | ret = 1; |
2623 | 0 | goto cleanup; |
2624 | 0 | } |
2625 | | |
2626 | 0 | for (i = 0; i < suc.update_clone_nr; i++) { |
2627 | 0 | struct update_clone_data ucd = suc.update_clone[i]; |
2628 | 0 | int code; |
2629 | |
|
2630 | 0 | oidcpy(&update_data->oid, &ucd.oid); |
2631 | 0 | update_data->just_cloned = ucd.just_cloned; |
2632 | 0 | update_data->sm_path = ucd.sub->path; |
2633 | |
|
2634 | 0 | code = ensure_core_worktree(update_data->sm_path); |
2635 | 0 | if (code) |
2636 | 0 | goto fail; |
2637 | | |
2638 | 0 | update_data->displaypath = get_submodule_displaypath( |
2639 | 0 | update_data->sm_path, update_data->prefix, |
2640 | 0 | update_data->super_prefix); |
2641 | 0 | code = update_submodule(update_data); |
2642 | 0 | FREE_AND_NULL(update_data->displaypath); |
2643 | 0 | fail: |
2644 | 0 | if (!code) |
2645 | 0 | continue; |
2646 | 0 | ret = code; |
2647 | 0 | if (ret == 128) |
2648 | 0 | goto cleanup; |
2649 | 0 | } |
2650 | | |
2651 | 0 | cleanup: |
2652 | 0 | submodule_update_clone_release(&suc); |
2653 | 0 | string_list_clear(&update_data->references, 0); |
2654 | 0 | return ret; |
2655 | 0 | } |
2656 | | |
2657 | | static int module_update(int argc, const char **argv, const char *prefix) |
2658 | 0 | { |
2659 | 0 | struct pathspec pathspec = { 0 }; |
2660 | 0 | struct pathspec pathspec2 = { 0 }; |
2661 | 0 | struct update_data opt = UPDATE_DATA_INIT; |
2662 | 0 | struct list_objects_filter_options filter_options = |
2663 | 0 | LIST_OBJECTS_FILTER_INIT; |
2664 | 0 | int ret; |
2665 | 0 | struct option module_update_options[] = { |
2666 | 0 | OPT__SUPER_PREFIX(&opt.super_prefix), |
2667 | 0 | OPT__FORCE(&opt.force, N_("force checkout updates"), 0), |
2668 | 0 | OPT_BOOL(0, "init", &opt.init, |
2669 | 0 | N_("initialize uninitialized submodules before update")), |
2670 | 0 | OPT_BOOL(0, "remote", &opt.remote, |
2671 | 0 | N_("use SHA-1 of submodule's remote tracking branch")), |
2672 | 0 | OPT_BOOL(0, "recursive", &opt.recursive, |
2673 | 0 | N_("traverse submodules recursively")), |
2674 | 0 | OPT_BOOL('N', "no-fetch", &opt.nofetch, |
2675 | 0 | N_("don't fetch new objects from the remote site")), |
2676 | 0 | OPT_SET_INT(0, "checkout", &opt.update_default, |
2677 | 0 | N_("use the 'checkout' update strategy (default)"), |
2678 | 0 | SM_UPDATE_CHECKOUT), |
2679 | 0 | OPT_SET_INT('m', "merge", &opt.update_default, |
2680 | 0 | N_("use the 'merge' update strategy"), |
2681 | 0 | SM_UPDATE_MERGE), |
2682 | 0 | OPT_SET_INT('r', "rebase", &opt.update_default, |
2683 | 0 | N_("use the 'rebase' update strategy"), |
2684 | 0 | SM_UPDATE_REBASE), |
2685 | 0 | OPT_STRING_LIST(0, "reference", &opt.references, N_("repo"), |
2686 | 0 | N_("reference repository")), |
2687 | 0 | OPT_BOOL(0, "dissociate", &opt.dissociate, |
2688 | 0 | N_("use --reference only while cloning")), |
2689 | 0 | OPT_INTEGER(0, "depth", &opt.depth, |
2690 | 0 | N_("create a shallow clone truncated to the " |
2691 | 0 | "specified number of revisions")), |
2692 | 0 | OPT_INTEGER('j', "jobs", &opt.max_jobs, |
2693 | 0 | N_("parallel jobs")), |
2694 | 0 | OPT_BOOL(0, "recommend-shallow", &opt.recommend_shallow, |
2695 | 0 | N_("whether the initial clone should follow the shallow recommendation")), |
2696 | 0 | OPT__QUIET(&opt.quiet, N_("don't print cloning progress")), |
2697 | 0 | OPT_BOOL(0, "progress", &opt.progress, |
2698 | 0 | N_("force cloning progress")), |
2699 | 0 | OPT_BOOL(0, "require-init", &opt.require_init, |
2700 | 0 | N_("disallow cloning into non-empty directory, implies --init")), |
2701 | 0 | OPT_BOOL(0, "single-branch", &opt.single_branch, |
2702 | 0 | N_("clone only one branch, HEAD or --branch")), |
2703 | 0 | OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options), |
2704 | 0 | OPT_END() |
2705 | 0 | }; |
2706 | 0 | const char *const git_submodule_helper_usage[] = { |
2707 | 0 | N_("git submodule [--quiet] update" |
2708 | 0 | " [--init [--filter=<filter-spec>]] [--remote]" |
2709 | 0 | " [-N|--no-fetch] [-f|--force]" |
2710 | 0 | " [--checkout|--merge|--rebase]" |
2711 | 0 | " [--[no-]recommend-shallow] [--reference <repository>]" |
2712 | 0 | " [--recursive] [--[no-]single-branch] [--] [<path>...]"), |
2713 | 0 | NULL |
2714 | 0 | }; |
2715 | |
|
2716 | 0 | update_clone_config_from_gitmodules(&opt.max_jobs); |
2717 | 0 | git_config(git_update_clone_config, &opt.max_jobs); |
2718 | |
|
2719 | 0 | argc = parse_options(argc, argv, prefix, module_update_options, |
2720 | 0 | git_submodule_helper_usage, 0); |
2721 | |
|
2722 | 0 | if (opt.require_init) |
2723 | 0 | opt.init = 1; |
2724 | |
|
2725 | 0 | if (filter_options.choice && !opt.init) { |
2726 | 0 | usage_with_options(git_submodule_helper_usage, |
2727 | 0 | module_update_options); |
2728 | 0 | } |
2729 | | |
2730 | 0 | opt.filter_options = &filter_options; |
2731 | 0 | opt.prefix = prefix; |
2732 | |
|
2733 | 0 | if (opt.update_default) |
2734 | 0 | opt.update_strategy.type = opt.update_default; |
2735 | |
|
2736 | 0 | if (module_list_compute(argv, prefix, &pathspec, &opt.list) < 0) { |
2737 | 0 | ret = 1; |
2738 | 0 | goto cleanup; |
2739 | 0 | } |
2740 | | |
2741 | 0 | if (pathspec.nr) |
2742 | 0 | opt.warn_if_uninitialized = 1; |
2743 | |
|
2744 | 0 | if (opt.init) { |
2745 | 0 | struct module_list list = MODULE_LIST_INIT; |
2746 | 0 | struct init_cb info = INIT_CB_INIT; |
2747 | |
|
2748 | 0 | if (module_list_compute(argv, opt.prefix, |
2749 | 0 | &pathspec2, &list) < 0) { |
2750 | 0 | module_list_release(&list); |
2751 | 0 | ret = 1; |
2752 | 0 | goto cleanup; |
2753 | 0 | } |
2754 | | |
2755 | | /* |
2756 | | * If there are no path args and submodule.active is set then, |
2757 | | * by default, only initialize 'active' modules. |
2758 | | */ |
2759 | 0 | if (!argc && !git_config_get("submodule.active")) |
2760 | 0 | module_list_active(&list); |
2761 | |
|
2762 | 0 | info.prefix = opt.prefix; |
2763 | 0 | info.super_prefix = opt.super_prefix; |
2764 | 0 | if (opt.quiet) |
2765 | 0 | info.flags |= OPT_QUIET; |
2766 | |
|
2767 | 0 | for_each_listed_submodule(&list, init_submodule_cb, &info); |
2768 | 0 | module_list_release(&list); |
2769 | 0 | } |
2770 | | |
2771 | 0 | ret = update_submodules(&opt); |
2772 | 0 | cleanup: |
2773 | 0 | update_data_release(&opt); |
2774 | 0 | list_objects_filter_release(&filter_options); |
2775 | 0 | clear_pathspec(&pathspec); |
2776 | 0 | clear_pathspec(&pathspec2); |
2777 | 0 | return ret; |
2778 | 0 | } |
2779 | | |
2780 | | static int push_check(int argc, const char **argv, const char *prefix UNUSED) |
2781 | 0 | { |
2782 | 0 | struct remote *remote; |
2783 | 0 | const char *superproject_head; |
2784 | 0 | char *head; |
2785 | 0 | int detached_head = 0; |
2786 | 0 | struct object_id head_oid; |
2787 | |
|
2788 | 0 | if (argc < 3) |
2789 | 0 | die("submodule--helper push-check requires at least 2 arguments"); |
2790 | | |
2791 | | /* |
2792 | | * superproject's resolved head ref. |
2793 | | * if HEAD then the superproject is in a detached head state, otherwise |
2794 | | * it will be the resolved head ref. |
2795 | | */ |
2796 | 0 | superproject_head = argv[1]; |
2797 | 0 | argv++; |
2798 | 0 | argc--; |
2799 | | /* Get the submodule's head ref and determine if it is detached */ |
2800 | 0 | head = resolve_refdup("HEAD", 0, &head_oid, NULL); |
2801 | 0 | if (!head) |
2802 | 0 | die(_("Failed to resolve HEAD as a valid ref.")); |
2803 | 0 | if (!strcmp(head, "HEAD")) |
2804 | 0 | detached_head = 1; |
2805 | | |
2806 | | /* |
2807 | | * The remote must be configured. |
2808 | | * This is to avoid pushing to the exact same URL as the parent. |
2809 | | */ |
2810 | 0 | remote = pushremote_get(argv[1]); |
2811 | 0 | if (!remote || remote->origin == REMOTE_UNCONFIGURED) |
2812 | 0 | die("remote '%s' not configured", argv[1]); |
2813 | | |
2814 | | /* Check the refspec */ |
2815 | 0 | if (argc > 2) { |
2816 | 0 | int i; |
2817 | 0 | struct ref *local_refs = get_local_heads(); |
2818 | 0 | struct refspec refspec = REFSPEC_INIT_PUSH; |
2819 | |
|
2820 | 0 | refspec_appendn(&refspec, argv + 2, argc - 2); |
2821 | |
|
2822 | 0 | for (i = 0; i < refspec.nr; i++) { |
2823 | 0 | const struct refspec_item *rs = &refspec.items[i]; |
2824 | |
|
2825 | 0 | if (rs->pattern || rs->matching) |
2826 | 0 | continue; |
2827 | | |
2828 | | /* LHS must match a single ref */ |
2829 | 0 | switch (count_refspec_match(rs->src, local_refs, NULL)) { |
2830 | 0 | case 1: |
2831 | 0 | break; |
2832 | 0 | case 0: |
2833 | | /* |
2834 | | * If LHS matches 'HEAD' then we need to ensure |
2835 | | * that it matches the same named branch |
2836 | | * checked out in the superproject. |
2837 | | */ |
2838 | 0 | if (!strcmp(rs->src, "HEAD")) { |
2839 | 0 | if (!detached_head && |
2840 | 0 | !strcmp(head, superproject_head)) |
2841 | 0 | break; |
2842 | 0 | die("HEAD does not match the named branch in the superproject"); |
2843 | 0 | } |
2844 | | /* fallthrough */ |
2845 | 0 | default: |
2846 | 0 | die("src refspec '%s' must name a ref", |
2847 | 0 | rs->src); |
2848 | 0 | } |
2849 | 0 | } |
2850 | 0 | refspec_clear(&refspec); |
2851 | 0 | } |
2852 | 0 | free(head); |
2853 | |
|
2854 | 0 | return 0; |
2855 | 0 | } |
2856 | | |
2857 | | static int absorb_git_dirs(int argc, const char **argv, const char *prefix) |
2858 | 0 | { |
2859 | 0 | int i; |
2860 | 0 | struct pathspec pathspec = { 0 }; |
2861 | 0 | struct module_list list = MODULE_LIST_INIT; |
2862 | 0 | const char *super_prefix = NULL; |
2863 | 0 | struct option embed_gitdir_options[] = { |
2864 | 0 | OPT__SUPER_PREFIX(&super_prefix), |
2865 | 0 | OPT_END() |
2866 | 0 | }; |
2867 | 0 | const char *const git_submodule_helper_usage[] = { |
2868 | 0 | N_("git submodule absorbgitdirs [<options>] [<path>...]"), |
2869 | 0 | NULL |
2870 | 0 | }; |
2871 | 0 | int ret = 1; |
2872 | |
|
2873 | 0 | argc = parse_options(argc, argv, prefix, embed_gitdir_options, |
2874 | 0 | git_submodule_helper_usage, 0); |
2875 | |
|
2876 | 0 | if (module_list_compute(argv, prefix, &pathspec, &list) < 0) |
2877 | 0 | goto cleanup; |
2878 | | |
2879 | 0 | for (i = 0; i < list.nr; i++) |
2880 | 0 | absorb_git_dir_into_superproject(list.entries[i]->name, |
2881 | 0 | super_prefix); |
2882 | |
|
2883 | 0 | ret = 0; |
2884 | 0 | cleanup: |
2885 | 0 | clear_pathspec(&pathspec); |
2886 | 0 | module_list_release(&list); |
2887 | 0 | return ret; |
2888 | 0 | } |
2889 | | |
2890 | | static int module_set_url(int argc, const char **argv, const char *prefix) |
2891 | 0 | { |
2892 | 0 | int quiet = 0, ret; |
2893 | 0 | const char *newurl; |
2894 | 0 | const char *path; |
2895 | 0 | char *config_name; |
2896 | 0 | struct option options[] = { |
2897 | 0 | OPT__QUIET(&quiet, N_("suppress output for setting url of a submodule")), |
2898 | 0 | OPT_END() |
2899 | 0 | }; |
2900 | 0 | const char *const usage[] = { |
2901 | 0 | N_("git submodule set-url [--quiet] <path> <newurl>"), |
2902 | 0 | NULL |
2903 | 0 | }; |
2904 | 0 | const struct submodule *sub; |
2905 | |
|
2906 | 0 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
2907 | |
|
2908 | 0 | if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1])) |
2909 | 0 | usage_with_options(usage, options); |
2910 | | |
2911 | 0 | sub = submodule_from_path(the_repository, null_oid(), path); |
2912 | |
|
2913 | 0 | if (!sub) |
2914 | 0 | die(_("no submodule mapping found in .gitmodules for path '%s'"), |
2915 | 0 | path); |
2916 | | |
2917 | 0 | config_name = xstrfmt("submodule.%s.url", sub->name); |
2918 | 0 | ret = config_set_in_gitmodules_file_gently(config_name, newurl); |
2919 | |
|
2920 | 0 | if (!ret) { |
2921 | 0 | repo_read_gitmodules(the_repository, 0); |
2922 | 0 | sync_submodule(sub->path, prefix, NULL, quiet ? OPT_QUIET : 0); |
2923 | 0 | } |
2924 | |
|
2925 | 0 | free(config_name); |
2926 | 0 | return !!ret; |
2927 | 0 | } |
2928 | | |
2929 | | static int module_set_branch(int argc, const char **argv, const char *prefix) |
2930 | 0 | { |
2931 | 0 | int opt_default = 0, ret; |
2932 | 0 | const char *opt_branch = NULL; |
2933 | 0 | const char *path; |
2934 | 0 | char *config_name; |
2935 | 0 | struct option options[] = { |
2936 | | /* |
2937 | | * We accept the `quiet` option for uniformity across subcommands, |
2938 | | * though there is nothing to make less verbose in this subcommand. |
2939 | | */ |
2940 | 0 | OPT_NOOP_NOARG('q', "quiet"), |
2941 | |
|
2942 | 0 | OPT_BOOL('d', "default", &opt_default, |
2943 | 0 | N_("set the default tracking branch to master")), |
2944 | 0 | OPT_STRING('b', "branch", &opt_branch, N_("branch"), |
2945 | 0 | N_("set the default tracking branch")), |
2946 | 0 | OPT_END() |
2947 | 0 | }; |
2948 | 0 | const char *const usage[] = { |
2949 | 0 | N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"), |
2950 | 0 | N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"), |
2951 | 0 | NULL |
2952 | 0 | }; |
2953 | 0 | const struct submodule *sub; |
2954 | |
|
2955 | 0 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
2956 | |
|
2957 | 0 | if (!opt_branch && !opt_default) |
2958 | 0 | die(_("--branch or --default required")); |
2959 | | |
2960 | 0 | if (opt_branch && opt_default) |
2961 | 0 | die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default"); |
2962 | | |
2963 | 0 | if (argc != 1 || !(path = argv[0])) |
2964 | 0 | usage_with_options(usage, options); |
2965 | | |
2966 | 0 | sub = submodule_from_path(the_repository, null_oid(), path); |
2967 | |
|
2968 | 0 | if (!sub) |
2969 | 0 | die(_("no submodule mapping found in .gitmodules for path '%s'"), |
2970 | 0 | path); |
2971 | | |
2972 | 0 | config_name = xstrfmt("submodule.%s.branch", sub->name); |
2973 | 0 | ret = config_set_in_gitmodules_file_gently(config_name, opt_branch); |
2974 | |
|
2975 | 0 | free(config_name); |
2976 | 0 | return !!ret; |
2977 | 0 | } |
2978 | | |
2979 | | static int module_create_branch(int argc, const char **argv, const char *prefix) |
2980 | 0 | { |
2981 | 0 | enum branch_track track; |
2982 | 0 | int quiet = 0, force = 0, reflog = 0, dry_run = 0; |
2983 | 0 | struct option options[] = { |
2984 | 0 | OPT__QUIET(&quiet, N_("print only error messages")), |
2985 | 0 | OPT__FORCE(&force, N_("force creation"), 0), |
2986 | 0 | OPT_BOOL(0, "create-reflog", &reflog, |
2987 | 0 | N_("create the branch's reflog")), |
2988 | 0 | OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)", |
2989 | 0 | N_("set branch tracking configuration"), |
2990 | 0 | PARSE_OPT_OPTARG, |
2991 | 0 | parse_opt_tracking_mode), |
2992 | 0 | OPT__DRY_RUN(&dry_run, |
2993 | 0 | N_("show whether the branch would be created")), |
2994 | 0 | OPT_END() |
2995 | 0 | }; |
2996 | 0 | const char *const usage[] = { |
2997 | 0 | N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"), |
2998 | 0 | NULL |
2999 | 0 | }; |
3000 | |
|
3001 | 0 | git_config(git_default_config, NULL); |
3002 | 0 | track = git_branch_track; |
3003 | 0 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
3004 | |
|
3005 | 0 | if (argc != 3) |
3006 | 0 | usage_with_options(usage, options); |
3007 | | |
3008 | 0 | if (!quiet && !dry_run) |
3009 | 0 | printf_ln(_("creating branch '%s'"), argv[0]); |
3010 | |
|
3011 | 0 | create_branches_recursively(the_repository, argv[0], argv[1], argv[2], |
3012 | 0 | force, reflog, quiet, track, dry_run); |
3013 | 0 | return 0; |
3014 | 0 | } |
3015 | | |
3016 | | struct add_data { |
3017 | | const char *prefix; |
3018 | | const char *branch; |
3019 | | const char *reference_path; |
3020 | | char *sm_path; |
3021 | | const char *sm_name; |
3022 | | const char *repo; |
3023 | | const char *realrepo; |
3024 | | int depth; |
3025 | | unsigned int force: 1; |
3026 | | unsigned int quiet: 1; |
3027 | | unsigned int progress: 1; |
3028 | | unsigned int dissociate: 1; |
3029 | | }; |
3030 | 0 | #define ADD_DATA_INIT { .depth = -1 } |
3031 | | |
3032 | | static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path) |
3033 | 0 | { |
3034 | 0 | struct child_process cp_remote = CHILD_PROCESS_INIT; |
3035 | 0 | struct strbuf sb_remote_out = STRBUF_INIT; |
3036 | |
|
3037 | 0 | cp_remote.git_cmd = 1; |
3038 | 0 | strvec_pushf(&cp_remote.env, |
3039 | 0 | "GIT_DIR=%s", git_dir_path); |
3040 | 0 | strvec_push(&cp_remote.env, "GIT_WORK_TREE=."); |
3041 | 0 | strvec_pushl(&cp_remote.args, "remote", "-v", NULL); |
3042 | 0 | if (!capture_command(&cp_remote, &sb_remote_out, 0)) { |
3043 | 0 | char *next_line; |
3044 | 0 | char *line = sb_remote_out.buf; |
3045 | |
|
3046 | 0 | while ((next_line = strchr(line, '\n')) != NULL) { |
3047 | 0 | size_t len = next_line - line; |
3048 | |
|
3049 | 0 | if (strip_suffix_mem(line, &len, " (fetch)")) |
3050 | 0 | strbuf_addf(msg, " %.*s\n", (int)len, line); |
3051 | 0 | line = next_line + 1; |
3052 | 0 | } |
3053 | 0 | } |
3054 | |
|
3055 | 0 | strbuf_release(&sb_remote_out); |
3056 | 0 | } |
3057 | | |
3058 | | static int add_submodule(const struct add_data *add_data) |
3059 | 0 | { |
3060 | 0 | char *submod_gitdir_path; |
3061 | 0 | struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT; |
3062 | 0 | struct string_list reference = STRING_LIST_INIT_NODUP; |
3063 | 0 | int ret = -1; |
3064 | | |
3065 | | /* perhaps the path already exists and is already a git repo, else clone it */ |
3066 | 0 | if (is_directory(add_data->sm_path)) { |
3067 | 0 | struct strbuf sm_path = STRBUF_INIT; |
3068 | 0 | strbuf_addstr(&sm_path, add_data->sm_path); |
3069 | 0 | submod_gitdir_path = xstrfmt("%s/.git", add_data->sm_path); |
3070 | 0 | if (is_nonbare_repository_dir(&sm_path)) |
3071 | 0 | printf(_("Adding existing repo at '%s' to the index\n"), |
3072 | 0 | add_data->sm_path); |
3073 | 0 | else |
3074 | 0 | die(_("'%s' already exists and is not a valid git repo"), |
3075 | 0 | add_data->sm_path); |
3076 | 0 | strbuf_release(&sm_path); |
3077 | 0 | free(submod_gitdir_path); |
3078 | 0 | } else { |
3079 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
3080 | |
|
3081 | 0 | submod_gitdir_path = xstrfmt(".git/modules/%s", add_data->sm_name); |
3082 | |
|
3083 | 0 | if (is_directory(submod_gitdir_path)) { |
3084 | 0 | if (!add_data->force) { |
3085 | 0 | struct strbuf msg = STRBUF_INIT; |
3086 | 0 | char *die_msg; |
3087 | |
|
3088 | 0 | strbuf_addf(&msg, _("A git directory for '%s' is found " |
3089 | 0 | "locally with remote(s):\n"), |
3090 | 0 | add_data->sm_name); |
3091 | |
|
3092 | 0 | append_fetch_remotes(&msg, submod_gitdir_path); |
3093 | 0 | free(submod_gitdir_path); |
3094 | |
|
3095 | 0 | strbuf_addf(&msg, _("If you want to reuse this local git " |
3096 | 0 | "directory instead of cloning again from\n" |
3097 | 0 | " %s\n" |
3098 | 0 | "use the '--force' option. If the local git " |
3099 | 0 | "directory is not the correct repo\n" |
3100 | 0 | "or you are unsure what this means choose " |
3101 | 0 | "another name with the '--name' option."), |
3102 | 0 | add_data->realrepo); |
3103 | |
|
3104 | 0 | die_msg = strbuf_detach(&msg, NULL); |
3105 | 0 | die("%s", die_msg); |
3106 | 0 | } else { |
3107 | 0 | printf(_("Reactivating local git directory for " |
3108 | 0 | "submodule '%s'\n"), add_data->sm_name); |
3109 | 0 | } |
3110 | 0 | } |
3111 | 0 | free(submod_gitdir_path); |
3112 | |
|
3113 | 0 | clone_data.prefix = add_data->prefix; |
3114 | 0 | clone_data.path = add_data->sm_path; |
3115 | 0 | clone_data.name = add_data->sm_name; |
3116 | 0 | clone_data.url = add_data->realrepo; |
3117 | 0 | clone_data.quiet = add_data->quiet; |
3118 | 0 | clone_data.progress = add_data->progress; |
3119 | 0 | if (add_data->reference_path) { |
3120 | 0 | char *p = xstrdup(add_data->reference_path); |
3121 | |
|
3122 | 0 | string_list_append(&reference, p)->util = p; |
3123 | 0 | } |
3124 | 0 | clone_data.dissociate = add_data->dissociate; |
3125 | 0 | if (add_data->depth >= 0) |
3126 | 0 | clone_data.depth = xstrfmt("%d", add_data->depth); |
3127 | |
|
3128 | 0 | if (clone_submodule(&clone_data, &reference)) |
3129 | 0 | goto cleanup; |
3130 | | |
3131 | 0 | prepare_submodule_repo_env(&cp.env); |
3132 | 0 | cp.git_cmd = 1; |
3133 | 0 | cp.dir = add_data->sm_path; |
3134 | | /* |
3135 | | * NOTE: we only get here if add_data->force is true, so |
3136 | | * passing --force to checkout is reasonable. |
3137 | | */ |
3138 | 0 | strvec_pushl(&cp.args, "checkout", "-f", "-q", NULL); |
3139 | |
|
3140 | 0 | if (add_data->branch) { |
3141 | 0 | strvec_pushl(&cp.args, "-B", add_data->branch, NULL); |
3142 | 0 | strvec_pushf(&cp.args, "origin/%s", add_data->branch); |
3143 | 0 | } |
3144 | |
|
3145 | 0 | if (run_command(&cp)) |
3146 | 0 | die(_("unable to checkout submodule '%s'"), add_data->sm_path); |
3147 | 0 | } |
3148 | 0 | ret = 0; |
3149 | 0 | cleanup: |
3150 | 0 | string_list_clear(&reference, 1); |
3151 | 0 | return ret; |
3152 | 0 | } |
3153 | | |
3154 | | static int config_submodule_in_gitmodules(const char *name, const char *var, const char *value) |
3155 | 0 | { |
3156 | 0 | char *key; |
3157 | 0 | int ret; |
3158 | |
|
3159 | 0 | if (!is_writing_gitmodules_ok()) |
3160 | 0 | die(_("please make sure that the .gitmodules file is in the working tree")); |
3161 | | |
3162 | 0 | key = xstrfmt("submodule.%s.%s", name, var); |
3163 | 0 | ret = config_set_in_gitmodules_file_gently(key, value); |
3164 | 0 | free(key); |
3165 | |
|
3166 | 0 | return ret; |
3167 | 0 | } |
3168 | | |
3169 | | static void configure_added_submodule(struct add_data *add_data) |
3170 | 0 | { |
3171 | 0 | char *key; |
3172 | 0 | struct child_process add_submod = CHILD_PROCESS_INIT; |
3173 | 0 | struct child_process add_gitmodules = CHILD_PROCESS_INIT; |
3174 | |
|
3175 | 0 | key = xstrfmt("submodule.%s.url", add_data->sm_name); |
3176 | 0 | git_config_set_gently(key, add_data->realrepo); |
3177 | 0 | free(key); |
3178 | |
|
3179 | 0 | add_submod.git_cmd = 1; |
3180 | 0 | strvec_pushl(&add_submod.args, "add", |
3181 | 0 | "--no-warn-embedded-repo", NULL); |
3182 | 0 | if (add_data->force) |
3183 | 0 | strvec_push(&add_submod.args, "--force"); |
3184 | 0 | strvec_pushl(&add_submod.args, "--", add_data->sm_path, NULL); |
3185 | |
|
3186 | 0 | if (run_command(&add_submod)) |
3187 | 0 | die(_("Failed to add submodule '%s'"), add_data->sm_path); |
3188 | | |
3189 | 0 | if (config_submodule_in_gitmodules(add_data->sm_name, "path", add_data->sm_path) || |
3190 | 0 | config_submodule_in_gitmodules(add_data->sm_name, "url", add_data->repo)) |
3191 | 0 | die(_("Failed to register submodule '%s'"), add_data->sm_path); |
3192 | | |
3193 | 0 | if (add_data->branch) { |
3194 | 0 | if (config_submodule_in_gitmodules(add_data->sm_name, |
3195 | 0 | "branch", add_data->branch)) |
3196 | 0 | die(_("Failed to register submodule '%s'"), add_data->sm_path); |
3197 | 0 | } |
3198 | | |
3199 | 0 | add_gitmodules.git_cmd = 1; |
3200 | 0 | strvec_pushl(&add_gitmodules.args, |
3201 | 0 | "add", "--force", "--", ".gitmodules", NULL); |
3202 | |
|
3203 | 0 | if (run_command(&add_gitmodules)) |
3204 | 0 | die(_("Failed to register submodule '%s'"), add_data->sm_path); |
3205 | | |
3206 | | /* |
3207 | | * NEEDSWORK: In a multi-working-tree world this needs to be |
3208 | | * set in the per-worktree config. |
3209 | | */ |
3210 | | /* |
3211 | | * NEEDSWORK: In the longer run, we need to get rid of this |
3212 | | * pattern of querying "submodule.active" before calling |
3213 | | * is_submodule_active(), since that function needs to find |
3214 | | * out the value of "submodule.active" again anyway. |
3215 | | */ |
3216 | 0 | if (!git_config_get("submodule.active")) { |
3217 | | /* |
3218 | | * If the submodule being added isn't already covered by the |
3219 | | * current configured pathspec, set the submodule's active flag |
3220 | | */ |
3221 | 0 | if (!is_submodule_active(the_repository, add_data->sm_path)) { |
3222 | 0 | key = xstrfmt("submodule.%s.active", add_data->sm_name); |
3223 | 0 | git_config_set_gently(key, "true"); |
3224 | 0 | free(key); |
3225 | 0 | } |
3226 | 0 | } else { |
3227 | 0 | key = xstrfmt("submodule.%s.active", add_data->sm_name); |
3228 | 0 | git_config_set_gently(key, "true"); |
3229 | 0 | free(key); |
3230 | 0 | } |
3231 | 0 | } |
3232 | | |
3233 | | static void die_on_index_match(const char *path, int force) |
3234 | 0 | { |
3235 | 0 | struct pathspec ps; |
3236 | 0 | const char *args[] = { path, NULL }; |
3237 | 0 | parse_pathspec(&ps, 0, PATHSPEC_PREFER_CWD, NULL, args); |
3238 | |
|
3239 | 0 | if (repo_read_index_preload(the_repository, NULL, 0) < 0) |
3240 | 0 | die(_("index file corrupt")); |
3241 | | |
3242 | 0 | if (ps.nr) { |
3243 | 0 | int i; |
3244 | 0 | char *ps_matched = xcalloc(ps.nr, 1); |
3245 | | |
3246 | | /* TODO: audit for interaction with sparse-index. */ |
3247 | 0 | ensure_full_index(&the_index); |
3248 | | |
3249 | | /* |
3250 | | * Since there is only one pathspec, we just need to |
3251 | | * check ps_matched[0] to know if a cache entry matched. |
3252 | | */ |
3253 | 0 | for (i = 0; i < the_index.cache_nr; i++) { |
3254 | 0 | ce_path_match(&the_index, the_index.cache[i], &ps, |
3255 | 0 | ps_matched); |
3256 | |
|
3257 | 0 | if (ps_matched[0]) { |
3258 | 0 | if (!force) |
3259 | 0 | die(_("'%s' already exists in the index"), |
3260 | 0 | path); |
3261 | 0 | if (!S_ISGITLINK(the_index.cache[i]->ce_mode)) |
3262 | 0 | die(_("'%s' already exists in the index " |
3263 | 0 | "and is not a submodule"), path); |
3264 | 0 | break; |
3265 | 0 | } |
3266 | 0 | } |
3267 | 0 | free(ps_matched); |
3268 | 0 | } |
3269 | 0 | clear_pathspec(&ps); |
3270 | 0 | } |
3271 | | |
3272 | | static void die_on_repo_without_commits(const char *path) |
3273 | 0 | { |
3274 | 0 | struct strbuf sb = STRBUF_INIT; |
3275 | 0 | strbuf_addstr(&sb, path); |
3276 | 0 | if (is_nonbare_repository_dir(&sb)) { |
3277 | 0 | struct object_id oid; |
3278 | 0 | if (resolve_gitlink_ref(path, "HEAD", &oid) < 0) |
3279 | 0 | die(_("'%s' does not have a commit checked out"), path); |
3280 | 0 | } |
3281 | 0 | strbuf_release(&sb); |
3282 | 0 | } |
3283 | | |
3284 | | static int module_add(int argc, const char **argv, const char *prefix) |
3285 | 0 | { |
3286 | 0 | int force = 0, quiet = 0, progress = 0, dissociate = 0; |
3287 | 0 | struct add_data add_data = ADD_DATA_INIT; |
3288 | 0 | char *to_free = NULL; |
3289 | 0 | struct option options[] = { |
3290 | 0 | OPT_STRING('b', "branch", &add_data.branch, N_("branch"), |
3291 | 0 | N_("branch of repository to add as submodule")), |
3292 | 0 | OPT__FORCE(&force, N_("allow adding an otherwise ignored submodule path"), |
3293 | 0 | PARSE_OPT_NOCOMPLETE), |
3294 | 0 | OPT__QUIET(&quiet, N_("print only error messages")), |
3295 | 0 | OPT_BOOL(0, "progress", &progress, N_("force cloning progress")), |
3296 | 0 | OPT_STRING(0, "reference", &add_data.reference_path, N_("repository"), |
3297 | 0 | N_("reference repository")), |
3298 | 0 | OPT_BOOL(0, "dissociate", &dissociate, N_("borrow the objects from reference repositories")), |
3299 | 0 | OPT_STRING(0, "name", &add_data.sm_name, N_("name"), |
3300 | 0 | N_("sets the submodule's name to the given string " |
3301 | 0 | "instead of defaulting to its path")), |
3302 | 0 | OPT_INTEGER(0, "depth", &add_data.depth, N_("depth for shallow clones")), |
3303 | 0 | OPT_END() |
3304 | 0 | }; |
3305 | 0 | const char *const usage[] = { |
3306 | 0 | N_("git submodule add [<options>] [--] <repository> [<path>]"), |
3307 | 0 | NULL |
3308 | 0 | }; |
3309 | 0 | struct strbuf sb = STRBUF_INIT; |
3310 | 0 | int ret = 1; |
3311 | |
|
3312 | 0 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
3313 | |
|
3314 | 0 | if (!is_writing_gitmodules_ok()) |
3315 | 0 | die(_("please make sure that the .gitmodules file is in the working tree")); |
3316 | | |
3317 | 0 | if (prefix && *prefix && |
3318 | 0 | add_data.reference_path && !is_absolute_path(add_data.reference_path)) |
3319 | 0 | add_data.reference_path = xstrfmt("%s%s", prefix, add_data.reference_path); |
3320 | |
|
3321 | 0 | if (argc == 0 || argc > 2) |
3322 | 0 | usage_with_options(usage, options); |
3323 | | |
3324 | 0 | add_data.repo = argv[0]; |
3325 | 0 | if (argc == 1) |
3326 | 0 | add_data.sm_path = git_url_basename(add_data.repo, 0, 0); |
3327 | 0 | else |
3328 | 0 | add_data.sm_path = xstrdup(argv[1]); |
3329 | |
|
3330 | 0 | if (prefix && *prefix && !is_absolute_path(add_data.sm_path)) { |
3331 | 0 | char *sm_path = add_data.sm_path; |
3332 | |
|
3333 | 0 | add_data.sm_path = xstrfmt("%s%s", prefix, sm_path); |
3334 | 0 | free(sm_path); |
3335 | 0 | } |
3336 | |
|
3337 | 0 | if (starts_with_dot_dot_slash(add_data.repo) || |
3338 | 0 | starts_with_dot_slash(add_data.repo)) { |
3339 | 0 | if (prefix) |
3340 | 0 | die(_("Relative path can only be used from the toplevel " |
3341 | 0 | "of the working tree")); |
3342 | | |
3343 | | /* dereference source url relative to parent's url */ |
3344 | 0 | to_free = resolve_relative_url(add_data.repo, NULL, 1); |
3345 | 0 | add_data.realrepo = to_free; |
3346 | 0 | } else if (is_dir_sep(add_data.repo[0]) || strchr(add_data.repo, ':')) { |
3347 | 0 | add_data.realrepo = add_data.repo; |
3348 | 0 | } else { |
3349 | 0 | die(_("repo URL: '%s' must be absolute or begin with ./|../"), |
3350 | 0 | add_data.repo); |
3351 | 0 | } |
3352 | | |
3353 | | /* |
3354 | | * normalize path: |
3355 | | * multiple //; leading ./; /./; /../; |
3356 | | */ |
3357 | 0 | normalize_path_copy(add_data.sm_path, add_data.sm_path); |
3358 | 0 | strip_dir_trailing_slashes(add_data.sm_path); |
3359 | |
|
3360 | 0 | die_on_index_match(add_data.sm_path, force); |
3361 | 0 | die_on_repo_without_commits(add_data.sm_path); |
3362 | |
|
3363 | 0 | if (!force) { |
3364 | 0 | struct child_process cp = CHILD_PROCESS_INIT; |
3365 | |
|
3366 | 0 | cp.git_cmd = 1; |
3367 | 0 | cp.no_stdout = 1; |
3368 | 0 | strvec_pushl(&cp.args, "add", "--dry-run", "--ignore-missing", |
3369 | 0 | "--no-warn-embedded-repo", add_data.sm_path, NULL); |
3370 | 0 | if ((ret = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) { |
3371 | 0 | strbuf_complete_line(&sb); |
3372 | 0 | fputs(sb.buf, stderr); |
3373 | 0 | goto cleanup; |
3374 | 0 | } |
3375 | 0 | } |
3376 | | |
3377 | 0 | if(!add_data.sm_name) |
3378 | 0 | add_data.sm_name = add_data.sm_path; |
3379 | |
|
3380 | 0 | if (check_submodule_name(add_data.sm_name)) |
3381 | 0 | die(_("'%s' is not a valid submodule name"), add_data.sm_name); |
3382 | | |
3383 | 0 | add_data.prefix = prefix; |
3384 | 0 | add_data.force = !!force; |
3385 | 0 | add_data.quiet = !!quiet; |
3386 | 0 | add_data.progress = !!progress; |
3387 | 0 | add_data.dissociate = !!dissociate; |
3388 | |
|
3389 | 0 | if (add_submodule(&add_data)) |
3390 | 0 | goto cleanup; |
3391 | 0 | configure_added_submodule(&add_data); |
3392 | |
|
3393 | 0 | ret = 0; |
3394 | 0 | cleanup: |
3395 | 0 | free(add_data.sm_path); |
3396 | 0 | free(to_free); |
3397 | 0 | strbuf_release(&sb); |
3398 | |
|
3399 | 0 | return ret; |
3400 | 0 | } |
3401 | | |
3402 | | int cmd_submodule__helper(int argc, const char **argv, const char *prefix) |
3403 | 0 | { |
3404 | 0 | parse_opt_subcommand_fn *fn = NULL; |
3405 | 0 | const char *const usage[] = { |
3406 | 0 | N_("git submodule--helper <command>"), |
3407 | 0 | NULL |
3408 | 0 | }; |
3409 | 0 | struct option options[] = { |
3410 | 0 | OPT_SUBCOMMAND("clone", &fn, module_clone), |
3411 | 0 | OPT_SUBCOMMAND("add", &fn, module_add), |
3412 | 0 | OPT_SUBCOMMAND("update", &fn, module_update), |
3413 | 0 | OPT_SUBCOMMAND("foreach", &fn, module_foreach), |
3414 | 0 | OPT_SUBCOMMAND("init", &fn, module_init), |
3415 | 0 | OPT_SUBCOMMAND("status", &fn, module_status), |
3416 | 0 | OPT_SUBCOMMAND("sync", &fn, module_sync), |
3417 | 0 | OPT_SUBCOMMAND("deinit", &fn, module_deinit), |
3418 | 0 | OPT_SUBCOMMAND("summary", &fn, module_summary), |
3419 | 0 | OPT_SUBCOMMAND("push-check", &fn, push_check), |
3420 | 0 | OPT_SUBCOMMAND("absorbgitdirs", &fn, absorb_git_dirs), |
3421 | 0 | OPT_SUBCOMMAND("set-url", &fn, module_set_url), |
3422 | 0 | OPT_SUBCOMMAND("set-branch", &fn, module_set_branch), |
3423 | 0 | OPT_SUBCOMMAND("create-branch", &fn, module_create_branch), |
3424 | 0 | OPT_END() |
3425 | 0 | }; |
3426 | 0 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
3427 | |
|
3428 | 0 | return fn(argc, argv, prefix); |
3429 | 0 | } |