Merge branch 'jk/submodule-c-credential' into js/http-custom-headers
[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 "utf8.h"
8 #include "submodule.h"
9 #include "submodule-config.h"
10 #include "string-list.h"
11 #include "run-command.h"
12
13 struct module_list {
14         const struct cache_entry **entries;
15         int alloc, nr;
16 };
17 #define MODULE_LIST_INIT { NULL, 0, 0 }
18
19 static int module_list_compute(int argc, const char **argv,
20                                const char *prefix,
21                                struct pathspec *pathspec,
22                                struct module_list *list)
23 {
24         int i, result = 0;
25         char *ps_matched = NULL;
26         parse_pathspec(pathspec, 0,
27                        PATHSPEC_PREFER_FULL |
28                        PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
29                        prefix, argv);
30
31         if (pathspec->nr)
32                 ps_matched = xcalloc(pathspec->nr, 1);
33
34         if (read_cache() < 0)
35                 die(_("index file corrupt"));
36
37         for (i = 0; i < active_nr; i++) {
38                 const struct cache_entry *ce = active_cache[i];
39
40                 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce),
41                                     0, ps_matched, 1) ||
42                     !S_ISGITLINK(ce->ce_mode))
43                         continue;
44
45                 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
46                 list->entries[list->nr++] = ce;
47                 while (i + 1 < active_nr &&
48                        !strcmp(ce->name, active_cache[i + 1]->name))
49                         /*
50                          * Skip entries with the same name in different stages
51                          * to make sure an entry is returned only once.
52                          */
53                         i++;
54         }
55
56         if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
57                 result = -1;
58
59         free(ps_matched);
60
61         return result;
62 }
63
64 static int module_list(int argc, const char **argv, const char *prefix)
65 {
66         int i;
67         struct pathspec pathspec;
68         struct module_list list = MODULE_LIST_INIT;
69
70         struct option module_list_options[] = {
71                 OPT_STRING(0, "prefix", &prefix,
72                            N_("path"),
73                            N_("alternative anchor for relative paths")),
74                 OPT_END()
75         };
76
77         const char *const git_submodule_helper_usage[] = {
78                 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
79                 NULL
80         };
81
82         argc = parse_options(argc, argv, prefix, module_list_options,
83                              git_submodule_helper_usage, 0);
84
85         if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) {
86                 printf("#unmatched\n");
87                 return 1;
88         }
89
90         for (i = 0; i < list.nr; i++) {
91                 const struct cache_entry *ce = list.entries[i];
92
93                 if (ce_stage(ce))
94                         printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
95                 else
96                         printf("%06o %s %d\t", ce->ce_mode, sha1_to_hex(ce->sha1), ce_stage(ce));
97
98                 utf8_fprintf(stdout, "%s\n", ce->name);
99         }
100         return 0;
101 }
102
103 static int module_name(int argc, const char **argv, const char *prefix)
104 {
105         const struct submodule *sub;
106
107         if (argc != 2)
108                 usage(_("git submodule--helper name <path>"));
109
110         gitmodules_config();
111         sub = submodule_from_path(null_sha1, argv[1]);
112
113         if (!sub)
114                 die(_("no submodule mapping found in .gitmodules for path '%s'"),
115                     argv[1]);
116
117         printf("%s\n", sub->name);
118
119         return 0;
120 }
121
122 static int clone_submodule(const char *path, const char *gitdir, const char *url,
123                            const char *depth, const char *reference, int quiet)
124 {
125         struct child_process cp;
126         child_process_init(&cp);
127
128         argv_array_push(&cp.args, "clone");
129         argv_array_push(&cp.args, "--no-checkout");
130         if (quiet)
131                 argv_array_push(&cp.args, "--quiet");
132         if (depth && *depth)
133                 argv_array_pushl(&cp.args, "--depth", depth, NULL);
134         if (reference && *reference)
135                 argv_array_pushl(&cp.args, "--reference", reference, NULL);
136         if (gitdir && *gitdir)
137                 argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
138
139         argv_array_push(&cp.args, url);
140         argv_array_push(&cp.args, path);
141
142         cp.git_cmd = 1;
143         prepare_submodule_repo_env(&cp.env_array);
144         cp.no_stdin = 1;
145
146         return run_command(&cp);
147 }
148
149 static int module_clone(int argc, const char **argv, const char *prefix)
150 {
151         const char *path = NULL, *name = NULL, *url = NULL;
152         const char *reference = NULL, *depth = NULL;
153         int quiet = 0;
154         FILE *submodule_dot_git;
155         char *sm_gitdir, *cwd, *p;
156         struct strbuf rel_path = STRBUF_INIT;
157         struct strbuf sb = STRBUF_INIT;
158
159         struct option module_clone_options[] = {
160                 OPT_STRING(0, "prefix", &prefix,
161                            N_("path"),
162                            N_("alternative anchor for relative paths")),
163                 OPT_STRING(0, "path", &path,
164                            N_("path"),
165                            N_("where the new submodule will be cloned to")),
166                 OPT_STRING(0, "name", &name,
167                            N_("string"),
168                            N_("name of the new submodule")),
169                 OPT_STRING(0, "url", &url,
170                            N_("string"),
171                            N_("url where to clone the submodule from")),
172                 OPT_STRING(0, "reference", &reference,
173                            N_("string"),
174                            N_("reference repository")),
175                 OPT_STRING(0, "depth", &depth,
176                            N_("string"),
177                            N_("depth for shallow clones")),
178                 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
179                 OPT_END()
180         };
181
182         const char *const git_submodule_helper_usage[] = {
183                 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
184                    "[--reference <repository>] [--name <name>] [--depth <depth>] "
185                    "--url <url> --path <path>"),
186                 NULL
187         };
188
189         argc = parse_options(argc, argv, prefix, module_clone_options,
190                              git_submodule_helper_usage, 0);
191
192         if (argc || !url || !path)
193                 usage_with_options(git_submodule_helper_usage,
194                                    module_clone_options);
195
196         strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
197         sm_gitdir = strbuf_detach(&sb, NULL);
198
199         if (!file_exists(sm_gitdir)) {
200                 if (safe_create_leading_directories_const(sm_gitdir) < 0)
201                         die(_("could not create directory '%s'"), sm_gitdir);
202                 if (clone_submodule(path, sm_gitdir, url, depth, reference, quiet))
203                         die(_("clone of '%s' into submodule path '%s' failed"),
204                             url, path);
205         } else {
206                 if (safe_create_leading_directories_const(path) < 0)
207                         die(_("could not create directory '%s'"), path);
208                 strbuf_addf(&sb, "%s/index", sm_gitdir);
209                 unlink_or_warn(sb.buf);
210                 strbuf_reset(&sb);
211         }
212
213         /* Write a .git file in the submodule to redirect to the superproject. */
214         if (safe_create_leading_directories_const(path) < 0)
215                 die(_("could not create directory '%s'"), path);
216
217         if (path && *path)
218                 strbuf_addf(&sb, "%s/.git", path);
219         else
220                 strbuf_addstr(&sb, ".git");
221
222         if (safe_create_leading_directories_const(sb.buf) < 0)
223                 die(_("could not create leading directories of '%s'"), sb.buf);
224         submodule_dot_git = fopen(sb.buf, "w");
225         if (!submodule_dot_git)
226                 die_errno(_("cannot open file '%s'"), sb.buf);
227
228         fprintf(submodule_dot_git, "gitdir: %s\n",
229                 relative_path(sm_gitdir, path, &rel_path));
230         if (fclose(submodule_dot_git))
231                 die(_("could not close file %s"), sb.buf);
232         strbuf_reset(&sb);
233         strbuf_reset(&rel_path);
234
235         cwd = xgetcwd();
236         /* Redirect the worktree of the submodule in the superproject's config */
237         if (!is_absolute_path(sm_gitdir)) {
238                 strbuf_addf(&sb, "%s/%s", cwd, sm_gitdir);
239                 free(sm_gitdir);
240                 sm_gitdir = strbuf_detach(&sb, NULL);
241         }
242
243         strbuf_addf(&sb, "%s/%s", cwd, path);
244         p = git_pathdup_submodule(path, "config");
245         if (!p)
246                 die(_("could not get submodule directory for '%s'"), path);
247         git_config_set_in_file(p, "core.worktree",
248                                relative_path(sb.buf, sm_gitdir, &rel_path));
249         strbuf_release(&sb);
250         strbuf_release(&rel_path);
251         free(sm_gitdir);
252         free(cwd);
253         free(p);
254         return 0;
255 }
256
257 struct cmd_struct {
258         const char *cmd;
259         int (*fn)(int, const char **, const char *);
260 };
261
262 static struct cmd_struct commands[] = {
263         {"list", module_list},
264         {"name", module_name},
265         {"clone", module_clone},
266 };
267
268 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
269 {
270         int i;
271         if (argc < 2)
272                 die(_("fatal: submodule--helper subcommand must be "
273                       "called with a subcommand"));
274
275         for (i = 0; i < ARRAY_SIZE(commands); i++)
276                 if (!strcmp(argv[1], commands[i].cmd))
277                         return commands[i].fn(argc - 1, argv + 1, prefix);
278
279         die(_("fatal: '%s' is not a valid submodule--helper "
280               "subcommand"), argv[1]);
281 }