Imported Upstream version 2.13.7
[platform/upstream/git.git] / builtin / submodule--helper.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "parse-options.h"
4 #include "quote.h"
5 #include "pathspec.h"
6 #include "dir.h"
7 #include "submodule.h"
8 #include "submodule-config.h"
9 #include "string-list.h"
10 #include "run-command.h"
11 #include "remote.h"
12 #include "refs.h"
13 #include "connect.h"
14
15 static char *get_default_remote(void)
16 {
17         char *dest = NULL, *ret;
18         unsigned char sha1[20];
19         struct strbuf sb = STRBUF_INIT;
20         const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, NULL);
21
22         if (!refname)
23                 die(_("No such ref: %s"), "HEAD");
24
25         /* detached HEAD */
26         if (!strcmp(refname, "HEAD"))
27                 return xstrdup("origin");
28
29         if (!skip_prefix(refname, "refs/heads/", &refname))
30                 die(_("Expecting a full ref name, got %s"), refname);
31
32         strbuf_addf(&sb, "branch.%s.remote", refname);
33         if (git_config_get_string(sb.buf, &dest))
34                 ret = xstrdup("origin");
35         else
36                 ret = dest;
37
38         strbuf_release(&sb);
39         return ret;
40 }
41
42 static int starts_with_dot_slash(const char *str)
43 {
44         return str[0] == '.' && is_dir_sep(str[1]);
45 }
46
47 static int starts_with_dot_dot_slash(const char *str)
48 {
49         return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]);
50 }
51
52 /*
53  * Returns 1 if it was the last chop before ':'.
54  */
55 static int chop_last_dir(char **remoteurl, int is_relative)
56 {
57         char *rfind = find_last_dir_sep(*remoteurl);
58         if (rfind) {
59                 *rfind = '\0';
60                 return 0;
61         }
62
63         rfind = strrchr(*remoteurl, ':');
64         if (rfind) {
65                 *rfind = '\0';
66                 return 1;
67         }
68
69         if (is_relative || !strcmp(".", *remoteurl))
70                 die(_("cannot strip one component off url '%s'"),
71                         *remoteurl);
72
73         free(*remoteurl);
74         *remoteurl = xstrdup(".");
75         return 0;
76 }
77
78 /*
79  * The `url` argument is the URL that navigates to the submodule origin
80  * repo. When relative, this URL is relative to the superproject origin
81  * URL repo. The `up_path` argument, if specified, is the relative
82  * path that navigates from the submodule working tree to the superproject
83  * working tree. Returns the origin URL of the submodule.
84  *
85  * Return either an absolute URL or filesystem path (if the superproject
86  * origin URL is an absolute URL or filesystem path, respectively) or a
87  * relative file system path (if the superproject origin URL is a relative
88  * file system path).
89  *
90  * When the output is a relative file system path, the path is either
91  * relative to the submodule working tree, if up_path is specified, or to
92  * the superproject working tree otherwise.
93  *
94  * NEEDSWORK: This works incorrectly on the domain and protocol part.
95  * remote_url      url              outcome          expectation
96  * http://a.com/b  ../c             http://a.com/c   as is
97  * http://a.com/b/ ../c             http://a.com/c   same as previous line, but
98  *                                                   ignore trailing slash in url
99  * http://a.com/b  ../../c          http://c         error out
100  * http://a.com/b  ../../../c       http:/c          error out
101  * http://a.com/b  ../../../../c    http:c           error out
102  * http://a.com/b  ../../../../../c    .:c           error out
103  * NEEDSWORK: Given how chop_last_dir() works, this function is broken
104  * when a local part has a colon in its path component, too.
105  */
106 static char *relative_url(const char *remote_url,
107                                 const char *url,
108                                 const char *up_path)
109 {
110         int is_relative = 0;
111         int colonsep = 0;
112         char *out;
113         char *remoteurl = xstrdup(remote_url);
114         struct strbuf sb = STRBUF_INIT;
115         size_t len = strlen(remoteurl);
116
117         if (is_dir_sep(remoteurl[len-1]))
118                 remoteurl[len-1] = '\0';
119
120         if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl))
121                 is_relative = 0;
122         else {
123                 is_relative = 1;
124                 /*
125                  * Prepend a './' to ensure all relative
126                  * remoteurls start with './' or '../'
127                  */
128                 if (!starts_with_dot_slash(remoteurl) &&
129                     !starts_with_dot_dot_slash(remoteurl)) {
130                         strbuf_reset(&sb);
131                         strbuf_addf(&sb, "./%s", remoteurl);
132                         free(remoteurl);
133                         remoteurl = strbuf_detach(&sb, NULL);
134                 }
135         }
136         /*
137          * When the url starts with '../', remove that and the
138          * last directory in remoteurl.
139          */
140         while (url) {
141                 if (starts_with_dot_dot_slash(url)) {
142                         url += 3;
143                         colonsep |= chop_last_dir(&remoteurl, is_relative);
144                 } else if (starts_with_dot_slash(url))
145                         url += 2;
146                 else
147                         break;
148         }
149         strbuf_reset(&sb);
150         strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url);
151         if (ends_with(url, "/"))
152                 strbuf_setlen(&sb, sb.len - 1);
153         free(remoteurl);
154
155         if (starts_with_dot_slash(sb.buf))
156                 out = xstrdup(sb.buf + 2);
157         else
158                 out = xstrdup(sb.buf);
159         strbuf_reset(&sb);
160
161         if (!up_path || !is_relative)
162                 return out;
163
164         strbuf_addf(&sb, "%s%s", up_path, out);
165         free(out);
166         return strbuf_detach(&sb, NULL);
167 }
168
169 static int resolve_relative_url(int argc, const char **argv, const char *prefix)
170 {
171         char *remoteurl = NULL;
172         char *remote = get_default_remote();
173         const char *up_path = NULL;
174         char *res;
175         const char *url;
176         struct strbuf sb = STRBUF_INIT;
177
178         if (argc != 2 && argc != 3)
179                 die("resolve-relative-url only accepts one or two arguments");
180
181         url = argv[1];
182         strbuf_addf(&sb, "remote.%s.url", remote);
183         free(remote);
184
185         if (git_config_get_string(sb.buf, &remoteurl))
186                 /* the repository is its own authoritative upstream */
187                 remoteurl = xgetcwd();
188
189         if (argc == 3)
190                 up_path = argv[2];
191
192         res = relative_url(remoteurl, url, up_path);
193         puts(res);
194         free(res);
195         free(remoteurl);
196         return 0;
197 }
198
199 static int resolve_relative_url_test(int argc, const char **argv, const char *prefix)
200 {
201         char *remoteurl, *res;
202         const char *up_path, *url;
203
204         if (argc != 4)
205                 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
206
207         up_path = argv[1];
208         remoteurl = xstrdup(argv[2]);
209         url = argv[3];
210
211         if (!strcmp(up_path, "(null)"))
212                 up_path = NULL;
213
214         res = relative_url(remoteurl, url, up_path);
215         puts(res);
216         free(res);
217         free(remoteurl);
218         return 0;
219 }
220
221 struct module_list {
222         const struct cache_entry **entries;
223         int alloc, nr;
224 };
225 #define MODULE_LIST_INIT { NULL, 0, 0 }
226
227 static int module_list_compute(int argc, const char **argv,
228                                const char *prefix,
229                                struct pathspec *pathspec,
230                                struct module_list *list)
231 {
232         int i, result = 0;
233         char *ps_matched = NULL;
234         parse_pathspec(pathspec, 0,
235                        PATHSPEC_PREFER_FULL |
236                        PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
237                        prefix, argv);
238
239         if (pathspec->nr)
240                 ps_matched = xcalloc(pathspec->nr, 1);
241
242         if (read_cache() < 0)
243                 die(_("index file corrupt"));
244
245         for (i = 0; i < active_nr; i++) {
246                 const struct cache_entry *ce = active_cache[i];
247
248                 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce),
249                                     0, ps_matched, 1) ||
250                     !S_ISGITLINK(ce->ce_mode))
251                         continue;
252
253                 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
254                 list->entries[list->nr++] = ce;
255                 while (i + 1 < active_nr &&
256                        !strcmp(ce->name, active_cache[i + 1]->name))
257                         /*
258                          * Skip entries with the same name in different stages
259                          * to make sure an entry is returned only once.
260                          */
261                         i++;
262         }
263
264         if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
265                 result = -1;
266
267         free(ps_matched);
268
269         return result;
270 }
271
272 static void module_list_active(struct module_list *list)
273 {
274         int i;
275         struct module_list active_modules = MODULE_LIST_INIT;
276
277         gitmodules_config();
278
279         for (i = 0; i < list->nr; i++) {
280                 const struct cache_entry *ce = list->entries[i];
281
282                 if (!is_submodule_initialized(ce->name))
283                         continue;
284
285                 ALLOC_GROW(active_modules.entries,
286                            active_modules.nr + 1,
287                            active_modules.alloc);
288                 active_modules.entries[active_modules.nr++] = ce;
289         }
290
291         free(list->entries);
292         *list = active_modules;
293 }
294
295 static int module_list(int argc, const char **argv, const char *prefix)
296 {
297         int i;
298         struct pathspec pathspec;
299         struct module_list list = MODULE_LIST_INIT;
300
301         struct option module_list_options[] = {
302                 OPT_STRING(0, "prefix", &prefix,
303                            N_("path"),
304                            N_("alternative anchor for relative paths")),
305                 OPT_END()
306         };
307
308         const char *const git_submodule_helper_usage[] = {
309                 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
310                 NULL
311         };
312
313         argc = parse_options(argc, argv, prefix, module_list_options,
314                              git_submodule_helper_usage, 0);
315
316         if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
317                 return 1;
318
319         for (i = 0; i < list.nr; i++) {
320                 const struct cache_entry *ce = list.entries[i];
321
322                 if (ce_stage(ce))
323                         printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
324                 else
325                         printf("%06o %s %d\t", ce->ce_mode,
326                                oid_to_hex(&ce->oid), ce_stage(ce));
327
328                 fprintf(stdout, "%s\n", ce->name);
329         }
330         return 0;
331 }
332
333 static void init_submodule(const char *path, const char *prefix, int quiet)
334 {
335         const struct submodule *sub;
336         struct strbuf sb = STRBUF_INIT;
337         char *upd = NULL, *url = NULL, *displaypath;
338
339         /* Only loads from .gitmodules, no overlay with .git/config */
340         gitmodules_config();
341
342         if (prefix && get_super_prefix())
343                 die("BUG: cannot have prefix and superprefix");
344         else if (prefix)
345                 displaypath = xstrdup(relative_path(path, prefix, &sb));
346         else if (get_super_prefix()) {
347                 strbuf_addf(&sb, "%s%s", get_super_prefix(), path);
348                 displaypath = strbuf_detach(&sb, NULL);
349         } else
350                 displaypath = xstrdup(path);
351
352         sub = submodule_from_path(null_sha1, path);
353
354         if (!sub)
355                 die(_("No url found for submodule path '%s' in .gitmodules"),
356                         displaypath);
357
358         /*
359          * NEEDSWORK: In a multi-working-tree world, this needs to be
360          * set in the per-worktree config.
361          *
362          * Set active flag for the submodule being initialized
363          */
364         if (!is_submodule_initialized(path)) {
365                 strbuf_reset(&sb);
366                 strbuf_addf(&sb, "submodule.%s.active", sub->name);
367                 git_config_set_gently(sb.buf, "true");
368         }
369
370         /*
371          * Copy url setting when it is not set yet.
372          * To look up the url in .git/config, we must not fall back to
373          * .gitmodules, so look it up directly.
374          */
375         strbuf_reset(&sb);
376         strbuf_addf(&sb, "submodule.%s.url", sub->name);
377         if (git_config_get_string(sb.buf, &url)) {
378                 if (!sub->url)
379                         die(_("No url found for submodule path '%s' in .gitmodules"),
380                                 displaypath);
381
382                 url = xstrdup(sub->url);
383
384                 /* Possibly a url relative to parent */
385                 if (starts_with_dot_dot_slash(url) ||
386                     starts_with_dot_slash(url)) {
387                         char *remoteurl, *relurl;
388                         char *remote = get_default_remote();
389                         struct strbuf remotesb = STRBUF_INIT;
390                         strbuf_addf(&remotesb, "remote.%s.url", remote);
391                         free(remote);
392
393                         if (git_config_get_string(remotesb.buf, &remoteurl)) {
394                                 warning(_("could not lookup configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
395                                 remoteurl = xgetcwd();
396                         }
397                         relurl = relative_url(remoteurl, url, NULL);
398                         strbuf_release(&remotesb);
399                         free(remoteurl);
400                         free(url);
401                         url = relurl;
402                 }
403
404                 if (git_config_set_gently(sb.buf, url))
405                         die(_("Failed to register url for submodule path '%s'"),
406                             displaypath);
407                 if (!quiet)
408                         fprintf(stderr,
409                                 _("Submodule '%s' (%s) registered for path '%s'\n"),
410                                 sub->name, url, displaypath);
411         }
412
413         /* Copy "update" setting when it is not set yet */
414         strbuf_reset(&sb);
415         strbuf_addf(&sb, "submodule.%s.update", sub->name);
416         if (git_config_get_string(sb.buf, &upd) &&
417             sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
418                 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
419                         fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
420                                 sub->name);
421                         upd = xstrdup("none");
422                 } else
423                         upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
424
425                 if (git_config_set_gently(sb.buf, upd))
426                         die(_("Failed to register update mode for submodule path '%s'"), displaypath);
427         }
428         strbuf_release(&sb);
429         free(displaypath);
430         free(url);
431         free(upd);
432 }
433
434 static int module_init(int argc, const char **argv, const char *prefix)
435 {
436         struct pathspec pathspec;
437         struct module_list list = MODULE_LIST_INIT;
438         int quiet = 0;
439         int i;
440
441         struct option module_init_options[] = {
442                 OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
443                 OPT_END()
444         };
445
446         const char *const git_submodule_helper_usage[] = {
447                 N_("git submodule--helper init [<path>]"),
448                 NULL
449         };
450
451         argc = parse_options(argc, argv, prefix, module_init_options,
452                              git_submodule_helper_usage, 0);
453
454         if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
455                 return 1;
456
457         /*
458          * If there are no path args and submodule.active is set then,
459          * by default, only initialize 'active' modules.
460          */
461         if (!argc && git_config_get_value_multi("submodule.active"))
462                 module_list_active(&list);
463
464         for (i = 0; i < list.nr; i++)
465                 init_submodule(list.entries[i]->name, prefix, quiet);
466
467         return 0;
468 }
469
470 static int module_name(int argc, const char **argv, const char *prefix)
471 {
472         const struct submodule *sub;
473
474         if (argc != 2)
475                 usage(_("git submodule--helper name <path>"));
476
477         gitmodules_config();
478         sub = submodule_from_path(null_sha1, argv[1]);
479
480         if (!sub)
481                 die(_("no submodule mapping found in .gitmodules for path '%s'"),
482                     argv[1]);
483
484         printf("%s\n", sub->name);
485
486         return 0;
487 }
488
489 static int clone_submodule(const char *path, const char *gitdir, const char *url,
490                            const char *depth, struct string_list *reference,
491                            int quiet, int progress)
492 {
493         struct child_process cp = CHILD_PROCESS_INIT;
494
495         argv_array_push(&cp.args, "clone");
496         argv_array_push(&cp.args, "--no-checkout");
497         if (quiet)
498                 argv_array_push(&cp.args, "--quiet");
499         if (progress)
500                 argv_array_push(&cp.args, "--progress");
501         if (depth && *depth)
502                 argv_array_pushl(&cp.args, "--depth", depth, NULL);
503         if (reference->nr) {
504                 struct string_list_item *item;
505                 for_each_string_list_item(item, reference)
506                         argv_array_pushl(&cp.args, "--reference",
507                                          item->string, NULL);
508         }
509         if (gitdir && *gitdir)
510                 argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
511
512         argv_array_push(&cp.args, url);
513         argv_array_push(&cp.args, path);
514
515         cp.git_cmd = 1;
516         prepare_submodule_repo_env(&cp.env_array);
517         cp.no_stdin = 1;
518
519         return run_command(&cp);
520 }
521
522 struct submodule_alternate_setup {
523         const char *submodule_name;
524         enum SUBMODULE_ALTERNATE_ERROR_MODE {
525                 SUBMODULE_ALTERNATE_ERROR_DIE,
526                 SUBMODULE_ALTERNATE_ERROR_INFO,
527                 SUBMODULE_ALTERNATE_ERROR_IGNORE
528         } error_mode;
529         struct string_list *reference;
530 };
531 #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
532         SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
533
534 static int add_possible_reference_from_superproject(
535                 struct alternate_object_database *alt, void *sas_cb)
536 {
537         struct submodule_alternate_setup *sas = sas_cb;
538
539         /*
540          * If the alternate object store is another repository, try the
541          * standard layout with .git/(modules/<name>)+/objects
542          */
543         if (ends_with(alt->path, "/objects")) {
544                 char *sm_alternate;
545                 struct strbuf sb = STRBUF_INIT;
546                 struct strbuf err = STRBUF_INIT;
547                 strbuf_add(&sb, alt->path, strlen(alt->path) - strlen("objects"));
548
549                 /*
550                  * We need to end the new path with '/' to mark it as a dir,
551                  * otherwise a submodule name containing '/' will be broken
552                  * as the last part of a missing submodule reference would
553                  * be taken as a file name.
554                  */
555                 strbuf_addf(&sb, "modules/%s/", sas->submodule_name);
556
557                 sm_alternate = compute_alternate_path(sb.buf, &err);
558                 if (sm_alternate) {
559                         string_list_append(sas->reference, xstrdup(sb.buf));
560                         free(sm_alternate);
561                 } else {
562                         switch (sas->error_mode) {
563                         case SUBMODULE_ALTERNATE_ERROR_DIE:
564                                 die(_("submodule '%s' cannot add alternate: %s"),
565                                     sas->submodule_name, err.buf);
566                         case SUBMODULE_ALTERNATE_ERROR_INFO:
567                                 fprintf(stderr, _("submodule '%s' cannot add alternate: %s"),
568                                         sas->submodule_name, err.buf);
569                         case SUBMODULE_ALTERNATE_ERROR_IGNORE:
570                                 ; /* nothing */
571                         }
572                 }
573                 strbuf_release(&sb);
574         }
575
576         return 0;
577 }
578
579 static void prepare_possible_alternates(const char *sm_name,
580                 struct string_list *reference)
581 {
582         char *sm_alternate = NULL, *error_strategy = NULL;
583         struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
584
585         git_config_get_string("submodule.alternateLocation", &sm_alternate);
586         if (!sm_alternate)
587                 return;
588
589         git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
590
591         if (!error_strategy)
592                 error_strategy = xstrdup("die");
593
594         sas.submodule_name = sm_name;
595         sas.reference = reference;
596         if (!strcmp(error_strategy, "die"))
597                 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
598         else if (!strcmp(error_strategy, "info"))
599                 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
600         else if (!strcmp(error_strategy, "ignore"))
601                 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
602         else
603                 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
604
605         if (!strcmp(sm_alternate, "superproject"))
606                 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
607         else if (!strcmp(sm_alternate, "no"))
608                 ; /* do nothing */
609         else
610                 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
611
612         free(sm_alternate);
613         free(error_strategy);
614 }
615
616 static int module_clone(int argc, const char **argv, const char *prefix)
617 {
618         const char *name = NULL, *url = NULL, *depth = NULL;
619         int quiet = 0;
620         int progress = 0;
621         char *p, *path = NULL, *sm_gitdir;
622         struct strbuf sb = STRBUF_INIT;
623         struct string_list reference = STRING_LIST_INIT_NODUP;
624         char *sm_alternate = NULL, *error_strategy = NULL;
625
626         struct option module_clone_options[] = {
627                 OPT_STRING(0, "prefix", &prefix,
628                            N_("path"),
629                            N_("alternative anchor for relative paths")),
630                 OPT_STRING(0, "path", &path,
631                            N_("path"),
632                            N_("where the new submodule will be cloned to")),
633                 OPT_STRING(0, "name", &name,
634                            N_("string"),
635                            N_("name of the new submodule")),
636                 OPT_STRING(0, "url", &url,
637                            N_("string"),
638                            N_("url where to clone the submodule from")),
639                 OPT_STRING_LIST(0, "reference", &reference,
640                            N_("repo"),
641                            N_("reference repository")),
642                 OPT_STRING(0, "depth", &depth,
643                            N_("string"),
644                            N_("depth for shallow clones")),
645                 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
646                 OPT_BOOL(0, "progress", &progress,
647                            N_("force cloning progress")),
648                 OPT_END()
649         };
650
651         const char *const git_submodule_helper_usage[] = {
652                 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
653                    "[--reference <repository>] [--name <name>] [--depth <depth>] "
654                    "--url <url> --path <path>"),
655                 NULL
656         };
657
658         argc = parse_options(argc, argv, prefix, module_clone_options,
659                              git_submodule_helper_usage, 0);
660
661         if (argc || !url || !path || !*path)
662                 usage_with_options(git_submodule_helper_usage,
663                                    module_clone_options);
664
665         strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
666         sm_gitdir = absolute_pathdup(sb.buf);
667         strbuf_reset(&sb);
668
669         if (!is_absolute_path(path)) {
670                 strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
671                 path = strbuf_detach(&sb, NULL);
672         } else
673                 path = xstrdup(path);
674
675         if (!file_exists(sm_gitdir)) {
676                 if (safe_create_leading_directories_const(sm_gitdir) < 0)
677                         die(_("could not create directory '%s'"), sm_gitdir);
678
679                 prepare_possible_alternates(name, &reference);
680
681                 if (clone_submodule(path, sm_gitdir, url, depth, &reference,
682                                     quiet, progress))
683                         die(_("clone of '%s' into submodule path '%s' failed"),
684                             url, path);
685         } else {
686                 if (safe_create_leading_directories_const(path) < 0)
687                         die(_("could not create directory '%s'"), path);
688                 strbuf_addf(&sb, "%s/index", sm_gitdir);
689                 unlink_or_warn(sb.buf);
690                 strbuf_reset(&sb);
691         }
692
693         /* Connect module worktree and git dir */
694         connect_work_tree_and_git_dir(path, sm_gitdir);
695
696         p = git_pathdup_submodule(path, "config");
697         if (!p)
698                 die(_("could not get submodule directory for '%s'"), path);
699
700         /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
701         git_config_get_string("submodule.alternateLocation", &sm_alternate);
702         if (sm_alternate)
703                 git_config_set_in_file(p, "submodule.alternateLocation",
704                                            sm_alternate);
705         git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
706         if (error_strategy)
707                 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
708                                            error_strategy);
709
710         free(sm_alternate);
711         free(error_strategy);
712
713         strbuf_release(&sb);
714         free(sm_gitdir);
715         free(path);
716         free(p);
717         return 0;
718 }
719
720 struct submodule_update_clone {
721         /* index into 'list', the list of submodules to look into for cloning */
722         int current;
723         struct module_list list;
724         unsigned warn_if_uninitialized : 1;
725
726         /* update parameter passed via commandline */
727         struct submodule_update_strategy update;
728
729         /* configuration parameters which are passed on to the children */
730         int progress;
731         int quiet;
732         int recommend_shallow;
733         struct string_list references;
734         const char *depth;
735         const char *recursive_prefix;
736         const char *prefix;
737
738         /* Machine-readable status lines to be consumed by git-submodule.sh */
739         struct string_list projectlines;
740
741         /* If we want to stop as fast as possible and return an error */
742         unsigned quickstop : 1;
743
744         /* failed clones to be retried again */
745         const struct cache_entry **failed_clones;
746         int failed_clones_nr, failed_clones_alloc;
747 };
748 #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
749         SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, \
750         NULL, NULL, NULL, \
751         STRING_LIST_INIT_DUP, 0, NULL, 0, 0}
752
753
754 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
755                 struct strbuf *out, const char *displaypath)
756 {
757         /*
758          * Only mention uninitialized submodules when their
759          * paths have been specified.
760          */
761         if (suc->warn_if_uninitialized) {
762                 strbuf_addf(out,
763                         _("Submodule path '%s' not initialized"),
764                         displaypath);
765                 strbuf_addch(out, '\n');
766                 strbuf_addstr(out,
767                         _("Maybe you want to use 'update --init'?"));
768                 strbuf_addch(out, '\n');
769         }
770 }
771
772 /**
773  * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
774  * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
775  */
776 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
777                                            struct child_process *child,
778                                            struct submodule_update_clone *suc,
779                                            struct strbuf *out)
780 {
781         const struct submodule *sub = NULL;
782         struct strbuf displaypath_sb = STRBUF_INIT;
783         struct strbuf sb = STRBUF_INIT;
784         const char *displaypath = NULL;
785         int needs_cloning = 0;
786
787         if (ce_stage(ce)) {
788                 if (suc->recursive_prefix)
789                         strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
790                 else
791                         strbuf_addstr(&sb, ce->name);
792                 strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
793                 strbuf_addch(out, '\n');
794                 goto cleanup;
795         }
796
797         sub = submodule_from_path(null_sha1, ce->name);
798
799         if (suc->recursive_prefix)
800                 displaypath = relative_path(suc->recursive_prefix,
801                                             ce->name, &displaypath_sb);
802         else
803                 displaypath = ce->name;
804
805         if (!sub) {
806                 next_submodule_warn_missing(suc, out, displaypath);
807                 goto cleanup;
808         }
809
810         if (suc->update.type == SM_UPDATE_NONE
811             || (suc->update.type == SM_UPDATE_UNSPECIFIED
812                 && sub->update_strategy.type == SM_UPDATE_NONE)) {
813                 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
814                 strbuf_addch(out, '\n');
815                 goto cleanup;
816         }
817
818         /* Check if the submodule has been initialized. */
819         if (!is_submodule_initialized(ce->name)) {
820                 next_submodule_warn_missing(suc, out, displaypath);
821                 goto cleanup;
822         }
823
824         strbuf_reset(&sb);
825         strbuf_addf(&sb, "%s/.git", ce->name);
826         needs_cloning = !file_exists(sb.buf);
827
828         strbuf_reset(&sb);
829         strbuf_addf(&sb, "%06o %s %d %d\t%s\n", ce->ce_mode,
830                         oid_to_hex(&ce->oid), ce_stage(ce),
831                         needs_cloning, ce->name);
832         string_list_append(&suc->projectlines, sb.buf);
833
834         if (!needs_cloning)
835                 goto cleanup;
836
837         child->git_cmd = 1;
838         child->no_stdin = 1;
839         child->stdout_to_stderr = 1;
840         child->err = -1;
841         argv_array_push(&child->args, "submodule--helper");
842         argv_array_push(&child->args, "clone");
843         if (suc->progress)
844                 argv_array_push(&child->args, "--progress");
845         if (suc->quiet)
846                 argv_array_push(&child->args, "--quiet");
847         if (suc->prefix)
848                 argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL);
849         if (suc->recommend_shallow && sub->recommend_shallow == 1)
850                 argv_array_push(&child->args, "--depth=1");
851         argv_array_pushl(&child->args, "--path", sub->path, NULL);
852         argv_array_pushl(&child->args, "--name", sub->name, NULL);
853         argv_array_pushl(&child->args, "--url", sub->url, NULL);
854         if (suc->references.nr) {
855                 struct string_list_item *item;
856                 for_each_string_list_item(item, &suc->references)
857                         argv_array_pushl(&child->args, "--reference", item->string, NULL);
858         }
859         if (suc->depth)
860                 argv_array_push(&child->args, suc->depth);
861
862 cleanup:
863         strbuf_reset(&displaypath_sb);
864         strbuf_reset(&sb);
865
866         return needs_cloning;
867 }
868
869 static int update_clone_get_next_task(struct child_process *child,
870                                       struct strbuf *err,
871                                       void *suc_cb,
872                                       void **idx_task_cb)
873 {
874         struct submodule_update_clone *suc = suc_cb;
875         const struct cache_entry *ce;
876         int index;
877
878         for (; suc->current < suc->list.nr; suc->current++) {
879                 ce = suc->list.entries[suc->current];
880                 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
881                         int *p = xmalloc(sizeof(*p));
882                         *p = suc->current;
883                         *idx_task_cb = p;
884                         suc->current++;
885                         return 1;
886                 }
887         }
888
889         /*
890          * The loop above tried cloning each submodule once, now try the
891          * stragglers again, which we can imagine as an extension of the
892          * entry list.
893          */
894         index = suc->current - suc->list.nr;
895         if (index < suc->failed_clones_nr) {
896                 int *p;
897                 ce = suc->failed_clones[index];
898                 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
899                         suc->current ++;
900                         strbuf_addstr(err, "BUG: submodule considered for "
901                                            "cloning, doesn't need cloning "
902                                            "any more?\n");
903                         return 0;
904                 }
905                 p = xmalloc(sizeof(*p));
906                 *p = suc->current;
907                 *idx_task_cb = p;
908                 suc->current ++;
909                 return 1;
910         }
911
912         return 0;
913 }
914
915 static int update_clone_start_failure(struct strbuf *err,
916                                       void *suc_cb,
917                                       void *idx_task_cb)
918 {
919         struct submodule_update_clone *suc = suc_cb;
920         suc->quickstop = 1;
921         return 1;
922 }
923
924 static int update_clone_task_finished(int result,
925                                       struct strbuf *err,
926                                       void *suc_cb,
927                                       void *idx_task_cb)
928 {
929         const struct cache_entry *ce;
930         struct submodule_update_clone *suc = suc_cb;
931
932         int *idxP = *(int**)idx_task_cb;
933         int idx = *idxP;
934         free(idxP);
935
936         if (!result)
937                 return 0;
938
939         if (idx < suc->list.nr) {
940                 ce  = suc->list.entries[idx];
941                 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
942                             ce->name);
943                 strbuf_addch(err, '\n');
944                 ALLOC_GROW(suc->failed_clones,
945                            suc->failed_clones_nr + 1,
946                            suc->failed_clones_alloc);
947                 suc->failed_clones[suc->failed_clones_nr++] = ce;
948                 return 0;
949         } else {
950                 idx -= suc->list.nr;
951                 ce  = suc->failed_clones[idx];
952                 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
953                             ce->name);
954                 strbuf_addch(err, '\n');
955                 suc->quickstop = 1;
956                 return 1;
957         }
958
959         return 0;
960 }
961
962 static int update_clone(int argc, const char **argv, const char *prefix)
963 {
964         const char *update = NULL;
965         int max_jobs = -1;
966         struct string_list_item *item;
967         struct pathspec pathspec;
968         struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
969
970         struct option module_update_clone_options[] = {
971                 OPT_STRING(0, "prefix", &prefix,
972                            N_("path"),
973                            N_("path into the working tree")),
974                 OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
975                            N_("path"),
976                            N_("path into the working tree, across nested "
977                               "submodule boundaries")),
978                 OPT_STRING(0, "update", &update,
979                            N_("string"),
980                            N_("rebase, merge, checkout or none")),
981                 OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"),
982                            N_("reference repository")),
983                 OPT_STRING(0, "depth", &suc.depth, "<depth>",
984                            N_("Create a shallow clone truncated to the "
985                               "specified number of revisions")),
986                 OPT_INTEGER('j', "jobs", &max_jobs,
987                             N_("parallel jobs")),
988                 OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
989                             N_("whether the initial clone should follow the shallow recommendation")),
990                 OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
991                 OPT_BOOL(0, "progress", &suc.progress,
992                             N_("force cloning progress")),
993                 OPT_END()
994         };
995
996         const char *const git_submodule_helper_usage[] = {
997                 N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
998                 NULL
999         };
1000         suc.prefix = prefix;
1001
1002         argc = parse_options(argc, argv, prefix, module_update_clone_options,
1003                              git_submodule_helper_usage, 0);
1004
1005         if (update)
1006                 if (parse_submodule_update_strategy(update, &suc.update) < 0)
1007                         die(_("bad value for update parameter"));
1008
1009         if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
1010                 return 1;
1011
1012         if (pathspec.nr)
1013                 suc.warn_if_uninitialized = 1;
1014
1015         /* Overlay the parsed .gitmodules file with .git/config */
1016         gitmodules_config();
1017         git_config(submodule_config, NULL);
1018
1019         if (max_jobs < 0)
1020                 max_jobs = parallel_submodules();
1021
1022         run_processes_parallel(max_jobs,
1023                                update_clone_get_next_task,
1024                                update_clone_start_failure,
1025                                update_clone_task_finished,
1026                                &suc);
1027
1028         /*
1029          * We saved the output and put it out all at once now.
1030          * That means:
1031          * - the listener does not have to interleave their (checkout)
1032          *   work with our fetching.  The writes involved in a
1033          *   checkout involve more straightforward sequential I/O.
1034          * - the listener can avoid doing any work if fetching failed.
1035          */
1036         if (suc.quickstop)
1037                 return 1;
1038
1039         for_each_string_list_item(item, &suc.projectlines)
1040                 fprintf(stdout, "%s", item->string);
1041
1042         return 0;
1043 }
1044
1045 static int resolve_relative_path(int argc, const char **argv, const char *prefix)
1046 {
1047         struct strbuf sb = STRBUF_INIT;
1048         if (argc != 3)
1049                 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc);
1050
1051         printf("%s", relative_path(argv[1], argv[2], &sb));
1052         strbuf_release(&sb);
1053         return 0;
1054 }
1055
1056 static const char *remote_submodule_branch(const char *path)
1057 {
1058         const struct submodule *sub;
1059         gitmodules_config();
1060         git_config(submodule_config, NULL);
1061
1062         sub = submodule_from_path(null_sha1, path);
1063         if (!sub)
1064                 return NULL;
1065
1066         if (!sub->branch)
1067                 return "master";
1068
1069         if (!strcmp(sub->branch, ".")) {
1070                 unsigned char sha1[20];
1071                 const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, NULL);
1072
1073                 if (!refname)
1074                         die(_("No such ref: %s"), "HEAD");
1075
1076                 /* detached HEAD */
1077                 if (!strcmp(refname, "HEAD"))
1078                         die(_("Submodule (%s) branch configured to inherit "
1079                               "branch from superproject, but the superproject "
1080                               "is not on any branch"), sub->name);
1081
1082                 if (!skip_prefix(refname, "refs/heads/", &refname))
1083                         die(_("Expecting a full ref name, got %s"), refname);
1084                 return refname;
1085         }
1086
1087         return sub->branch;
1088 }
1089
1090 static int resolve_remote_submodule_branch(int argc, const char **argv,
1091                 const char *prefix)
1092 {
1093         const char *ret;
1094         struct strbuf sb = STRBUF_INIT;
1095         if (argc != 2)
1096                 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
1097
1098         ret = remote_submodule_branch(argv[1]);
1099         if (!ret)
1100                 die("submodule %s doesn't exist", argv[1]);
1101
1102         printf("%s", ret);
1103         strbuf_release(&sb);
1104         return 0;
1105 }
1106
1107 static int push_check(int argc, const char **argv, const char *prefix)
1108 {
1109         struct remote *remote;
1110
1111         if (argc < 2)
1112                 die("submodule--helper push-check requires at least 1 argument");
1113
1114         /*
1115          * The remote must be configured.
1116          * This is to avoid pushing to the exact same URL as the parent.
1117          */
1118         remote = pushremote_get(argv[1]);
1119         if (!remote || remote->origin == REMOTE_UNCONFIGURED)
1120                 die("remote '%s' not configured", argv[1]);
1121
1122         /* Check the refspec */
1123         if (argc > 2) {
1124                 int i, refspec_nr = argc - 2;
1125                 struct ref *local_refs = get_local_heads();
1126                 struct refspec *refspec = parse_push_refspec(refspec_nr,
1127                                                              argv + 2);
1128
1129                 for (i = 0; i < refspec_nr; i++) {
1130                         struct refspec *rs = refspec + i;
1131
1132                         if (rs->pattern || rs->matching)
1133                                 continue;
1134
1135                         /*
1136                          * LHS must match a single ref
1137                          * NEEDSWORK: add logic to special case 'HEAD' once
1138                          * working with submodules in a detached head state
1139                          * ceases to be the norm.
1140                          */
1141                         if (count_refspec_match(rs->src, local_refs, NULL) != 1)
1142                                 die("src refspec '%s' must name a ref",
1143                                     rs->src);
1144                 }
1145                 free_refspec(refspec_nr, refspec);
1146         }
1147
1148         return 0;
1149 }
1150
1151 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
1152 {
1153         int i;
1154         struct pathspec pathspec;
1155         struct module_list list = MODULE_LIST_INIT;
1156         unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
1157
1158         struct option embed_gitdir_options[] = {
1159                 OPT_STRING(0, "prefix", &prefix,
1160                            N_("path"),
1161                            N_("path into the working tree")),
1162                 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
1163                         ABSORB_GITDIR_RECURSE_SUBMODULES),
1164                 OPT_END()
1165         };
1166
1167         const char *const git_submodule_helper_usage[] = {
1168                 N_("git submodule--helper embed-git-dir [<path>...]"),
1169                 NULL
1170         };
1171
1172         argc = parse_options(argc, argv, prefix, embed_gitdir_options,
1173                              git_submodule_helper_usage, 0);
1174
1175         gitmodules_config();
1176         git_config(submodule_config, NULL);
1177
1178         if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1179                 return 1;
1180
1181         for (i = 0; i < list.nr; i++)
1182                 absorb_git_dir_into_superproject(prefix,
1183                                 list.entries[i]->name, flags);
1184
1185         return 0;
1186 }
1187
1188 static int is_active(int argc, const char **argv, const char *prefix)
1189 {
1190         if (argc != 2)
1191                 die("submodule--helper is-active takes exactly 1 argument");
1192
1193         gitmodules_config();
1194
1195         return !is_submodule_initialized(argv[1]);
1196 }
1197
1198 /*
1199  * Exit non-zero if any of the submodule names given on the command line is
1200  * invalid. If no names are given, filter stdin to print only valid names
1201  * (which is primarily intended for testing).
1202  */
1203 static int check_name(int argc, const char **argv, const char *prefix)
1204 {
1205         if (argc > 1) {
1206                 while (*++argv) {
1207                         if (check_submodule_name(*argv) < 0)
1208                                 return 1;
1209                 }
1210         } else {
1211                 struct strbuf buf = STRBUF_INIT;
1212                 while (strbuf_getline(&buf, stdin) != EOF) {
1213                         if (!check_submodule_name(buf.buf))
1214                                 printf("%s\n", buf.buf);
1215                 }
1216                 strbuf_release(&buf);
1217         }
1218         return 0;
1219 }
1220
1221 #define SUPPORT_SUPER_PREFIX (1<<0)
1222
1223 struct cmd_struct {
1224         const char *cmd;
1225         int (*fn)(int, const char **, const char *);
1226         unsigned option;
1227 };
1228
1229 static struct cmd_struct commands[] = {
1230         {"list", module_list, 0},
1231         {"name", module_name, 0},
1232         {"clone", module_clone, 0},
1233         {"update-clone", update_clone, 0},
1234         {"relative-path", resolve_relative_path, 0},
1235         {"resolve-relative-url", resolve_relative_url, 0},
1236         {"resolve-relative-url-test", resolve_relative_url_test, 0},
1237         {"init", module_init, SUPPORT_SUPER_PREFIX},
1238         {"remote-branch", resolve_remote_submodule_branch, 0},
1239         {"push-check", push_check, 0},
1240         {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
1241         {"is-active", is_active, 0},
1242         {"check-name", check_name, 0},
1243 };
1244
1245 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
1246 {
1247         int i;
1248         if (argc < 2)
1249                 die(_("submodule--helper subcommand must be "
1250                       "called with a subcommand"));
1251
1252         for (i = 0; i < ARRAY_SIZE(commands); i++) {
1253                 if (!strcmp(argv[1], commands[i].cmd)) {
1254                         if (get_super_prefix() &&
1255                             !(commands[i].option & SUPPORT_SUPER_PREFIX))
1256                                 die(_("%s doesn't support --super-prefix"),
1257                                     commands[i].cmd);
1258                         return commands[i].fn(argc - 1, argv + 1, prefix);
1259                 }
1260         }
1261
1262         die(_("'%s' is not a valid submodule--helper "
1263               "subcommand"), argv[1]);
1264 }