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