git: submodule honor -c credential.* from command line
[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 *max_prefix, *ps_matched = NULL;
26         int max_prefix_len;
27         parse_pathspec(pathspec, 0,
28                        PATHSPEC_PREFER_FULL |
29                        PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
30                        prefix, argv);
31
32         /* Find common prefix for all pathspec's */
33         max_prefix = common_prefix(pathspec);
34         max_prefix_len = max_prefix ? strlen(max_prefix) : 0;
35
36         if (pathspec->nr)
37                 ps_matched = xcalloc(pathspec->nr, 1);
38
39         if (read_cache() < 0)
40                 die(_("index file corrupt"));
41
42         for (i = 0; i < active_nr; i++) {
43                 const struct cache_entry *ce = active_cache[i];
44
45                 if (!S_ISGITLINK(ce->ce_mode) ||
46                     !match_pathspec(pathspec, ce->name, ce_namelen(ce),
47                                     max_prefix_len, ps_matched, 1))
48                         continue;
49
50                 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
51                 list->entries[list->nr++] = ce;
52                 while (i + 1 < active_nr &&
53                        !strcmp(ce->name, active_cache[i + 1]->name))
54                         /*
55                          * Skip entries with the same name in different stages
56                          * to make sure an entry is returned only once.
57                          */
58                         i++;
59         }
60         free(max_prefix);
61
62         if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
63                 result = -1;
64
65         free(ps_matched);
66
67         return result;
68 }
69
70 static int module_list(int argc, const char **argv, const char *prefix)
71 {
72         int i;
73         struct pathspec pathspec;
74         struct module_list list = MODULE_LIST_INIT;
75
76         struct option module_list_options[] = {
77                 OPT_STRING(0, "prefix", &prefix,
78                            N_("path"),
79                            N_("alternative anchor for relative paths")),
80                 OPT_END()
81         };
82
83         const char *const git_submodule_helper_usage[] = {
84                 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
85                 NULL
86         };
87
88         argc = parse_options(argc, argv, prefix, module_list_options,
89                              git_submodule_helper_usage, 0);
90
91         if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) {
92                 printf("#unmatched\n");
93                 return 1;
94         }
95
96         for (i = 0; i < list.nr; i++) {
97                 const struct cache_entry *ce = list.entries[i];
98
99                 if (ce_stage(ce))
100                         printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
101                 else
102                         printf("%06o %s %d\t", ce->ce_mode, sha1_to_hex(ce->sha1), ce_stage(ce));
103
104                 utf8_fprintf(stdout, "%s\n", ce->name);
105         }
106         return 0;
107 }
108
109 static int module_name(int argc, const char **argv, const char *prefix)
110 {
111         const struct submodule *sub;
112
113         if (argc != 2)
114                 usage(_("git submodule--helper name <path>"));
115
116         gitmodules_config();
117         sub = submodule_from_path(null_sha1, argv[1]);
118
119         if (!sub)
120                 die(_("no submodule mapping found in .gitmodules for path '%s'"),
121                     argv[1]);
122
123         printf("%s\n", sub->name);
124
125         return 0;
126 }
127
128 /*
129  * Rules to sanitize configuration variables that are Ok to be passed into
130  * submodule operations from the parent project using "-c". Should only
131  * include keys which are both (a) safe and (b) necessary for proper
132  * operation.
133  */
134 static int submodule_config_ok(const char *var)
135 {
136         if (starts_with(var, "credential."))
137                 return 1;
138         return 0;
139 }
140
141 static int sanitize_submodule_config(const char *var, const char *value, void *data)
142 {
143         struct strbuf *out = data;
144
145         if (submodule_config_ok(var)) {
146                 if (out->len)
147                         strbuf_addch(out, ' ');
148
149                 if (value)
150                         sq_quotef(out, "%s=%s", var, value);
151                 else
152                         sq_quote_buf(out, var);
153         }
154
155         return 0;
156 }
157
158 static void prepare_submodule_repo_env(struct argv_array *out)
159 {
160         const char * const *var;
161
162         for (var = local_repo_env; *var; var++) {
163                 if (!strcmp(*var, CONFIG_DATA_ENVIRONMENT)) {
164                         struct strbuf sanitized_config = STRBUF_INIT;
165                         git_config_from_parameters(sanitize_submodule_config,
166                                                    &sanitized_config);
167                         argv_array_pushf(out, "%s=%s", *var, sanitized_config.buf);
168                         strbuf_release(&sanitized_config);
169                 } else {
170                         argv_array_push(out, *var);
171                 }
172         }
173
174 }
175
176 static int clone_submodule(const char *path, const char *gitdir, const char *url,
177                            const char *depth, const char *reference, int quiet)
178 {
179         struct child_process cp;
180         child_process_init(&cp);
181
182         argv_array_push(&cp.args, "clone");
183         argv_array_push(&cp.args, "--no-checkout");
184         if (quiet)
185                 argv_array_push(&cp.args, "--quiet");
186         if (depth && *depth)
187                 argv_array_pushl(&cp.args, "--depth", depth, NULL);
188         if (reference && *reference)
189                 argv_array_pushl(&cp.args, "--reference", reference, NULL);
190         if (gitdir && *gitdir)
191                 argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
192
193         argv_array_push(&cp.args, url);
194         argv_array_push(&cp.args, path);
195
196         cp.git_cmd = 1;
197         prepare_submodule_repo_env(&cp.env_array);
198         cp.no_stdin = 1;
199
200         return run_command(&cp);
201 }
202
203 static int module_clone(int argc, const char **argv, const char *prefix)
204 {
205         const char *path = NULL, *name = NULL, *url = NULL;
206         const char *reference = NULL, *depth = NULL;
207         int quiet = 0;
208         FILE *submodule_dot_git;
209         char *sm_gitdir, *cwd, *p;
210         struct strbuf rel_path = STRBUF_INIT;
211         struct strbuf sb = STRBUF_INIT;
212
213         struct option module_clone_options[] = {
214                 OPT_STRING(0, "prefix", &prefix,
215                            N_("path"),
216                            N_("alternative anchor for relative paths")),
217                 OPT_STRING(0, "path", &path,
218                            N_("path"),
219                            N_("where the new submodule will be cloned to")),
220                 OPT_STRING(0, "name", &name,
221                            N_("string"),
222                            N_("name of the new submodule")),
223                 OPT_STRING(0, "url", &url,
224                            N_("string"),
225                            N_("url where to clone the submodule from")),
226                 OPT_STRING(0, "reference", &reference,
227                            N_("string"),
228                            N_("reference repository")),
229                 OPT_STRING(0, "depth", &depth,
230                            N_("string"),
231                            N_("depth for shallow clones")),
232                 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
233                 OPT_END()
234         };
235
236         const char *const git_submodule_helper_usage[] = {
237                 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
238                    "[--reference <repository>] [--name <name>] [--depth <depth>] "
239                    "--url <url> --path <path>"),
240                 NULL
241         };
242
243         argc = parse_options(argc, argv, prefix, module_clone_options,
244                              git_submodule_helper_usage, 0);
245
246         if (argc || !url || !path)
247                 usage_with_options(git_submodule_helper_usage,
248                                    module_clone_options);
249
250         strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
251         sm_gitdir = strbuf_detach(&sb, NULL);
252
253         if (!file_exists(sm_gitdir)) {
254                 if (safe_create_leading_directories_const(sm_gitdir) < 0)
255                         die(_("could not create directory '%s'"), sm_gitdir);
256                 if (clone_submodule(path, sm_gitdir, url, depth, reference, quiet))
257                         die(_("clone of '%s' into submodule path '%s' failed"),
258                             url, path);
259         } else {
260                 if (safe_create_leading_directories_const(path) < 0)
261                         die(_("could not create directory '%s'"), path);
262                 strbuf_addf(&sb, "%s/index", sm_gitdir);
263                 unlink_or_warn(sb.buf);
264                 strbuf_reset(&sb);
265         }
266
267         /* Write a .git file in the submodule to redirect to the superproject. */
268         if (safe_create_leading_directories_const(path) < 0)
269                 die(_("could not create directory '%s'"), path);
270
271         if (path && *path)
272                 strbuf_addf(&sb, "%s/.git", path);
273         else
274                 strbuf_addstr(&sb, ".git");
275
276         if (safe_create_leading_directories_const(sb.buf) < 0)
277                 die(_("could not create leading directories of '%s'"), sb.buf);
278         submodule_dot_git = fopen(sb.buf, "w");
279         if (!submodule_dot_git)
280                 die_errno(_("cannot open file '%s'"), sb.buf);
281
282         fprintf(submodule_dot_git, "gitdir: %s\n",
283                 relative_path(sm_gitdir, path, &rel_path));
284         if (fclose(submodule_dot_git))
285                 die(_("could not close file %s"), sb.buf);
286         strbuf_reset(&sb);
287         strbuf_reset(&rel_path);
288
289         cwd = xgetcwd();
290         /* Redirect the worktree of the submodule in the superproject's config */
291         if (!is_absolute_path(sm_gitdir)) {
292                 strbuf_addf(&sb, "%s/%s", cwd, sm_gitdir);
293                 free(sm_gitdir);
294                 sm_gitdir = strbuf_detach(&sb, NULL);
295         }
296
297         strbuf_addf(&sb, "%s/%s", cwd, path);
298         p = git_pathdup_submodule(path, "config");
299         if (!p)
300                 die(_("could not get submodule directory for '%s'"), path);
301         git_config_set_in_file(p, "core.worktree",
302                                relative_path(sb.buf, sm_gitdir, &rel_path));
303         strbuf_release(&sb);
304         strbuf_release(&rel_path);
305         free(sm_gitdir);
306         free(cwd);
307         free(p);
308         return 0;
309 }
310
311 static int module_sanitize_config(int argc, const char **argv, const char *prefix)
312 {
313         struct strbuf sanitized_config = STRBUF_INIT;
314
315         if (argc > 1)
316                 usage(_("git submodule--helper sanitize-config"));
317
318         git_config_from_parameters(sanitize_submodule_config, &sanitized_config);
319         if (sanitized_config.len)
320                 printf("%s\n", sanitized_config.buf);
321
322         strbuf_release(&sanitized_config);
323
324         return 0;
325 }
326
327 struct cmd_struct {
328         const char *cmd;
329         int (*fn)(int, const char **, const char *);
330 };
331
332 static struct cmd_struct commands[] = {
333         {"list", module_list},
334         {"name", module_name},
335         {"clone", module_clone},
336         {"sanitize-config", module_sanitize_config},
337 };
338
339 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
340 {
341         int i;
342         if (argc < 2)
343                 die(_("fatal: submodule--helper subcommand must be "
344                       "called with a subcommand"));
345
346         for (i = 0; i < ARRAY_SIZE(commands); i++)
347                 if (!strcmp(argv[1], commands[i].cmd))
348                         return commands[i].fn(argc - 1, argv + 1, prefix);
349
350         die(_("fatal: '%s' is not a valid submodule--helper "
351               "subcommand"), argv[1]);
352 }