3 #include "parse-options.h"
9 #include "submodule-config.h"
10 #include "string-list.h"
11 #include "run-command.h"
14 const struct cache_entry
**entries
;
17 #define MODULE_LIST_INIT { NULL, 0, 0 }
19 static int module_list_compute(int argc
, const char **argv
,
21 struct pathspec
*pathspec
,
22 struct module_list
*list
)
25 char *ps_matched
= NULL
;
26 parse_pathspec(pathspec
, 0,
27 PATHSPEC_PREFER_FULL
|
28 PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP
,
32 ps_matched
= xcalloc(pathspec
->nr
, 1);
35 die(_("index file corrupt"));
37 for (i
= 0; i
< active_nr
; i
++) {
38 const struct cache_entry
*ce
= active_cache
[i
];
40 if (!match_pathspec(pathspec
, ce
->name
, ce_namelen(ce
),
42 !S_ISGITLINK(ce
->ce_mode
))
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
))
50 * Skip entries with the same name in different stages
51 * to make sure an entry is returned only once.
56 if (ps_matched
&& report_path_error(ps_matched
, pathspec
, prefix
))
64 static int module_list(int argc
, const char **argv
, const char *prefix
)
67 struct pathspec pathspec
;
68 struct module_list list
= MODULE_LIST_INIT
;
70 struct option module_list_options
[] = {
71 OPT_STRING(0, "prefix", &prefix
,
73 N_("alternative anchor for relative paths")),
77 const char *const git_submodule_helper_usage
[] = {
78 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
82 argc
= parse_options(argc
, argv
, prefix
, module_list_options
,
83 git_submodule_helper_usage
, 0);
85 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0) {
86 printf("#unmatched\n");
90 for (i
= 0; i
< list
.nr
; i
++) {
91 const struct cache_entry
*ce
= list
.entries
[i
];
94 printf("%06o %s U\t", ce
->ce_mode
, sha1_to_hex(null_sha1
));
96 printf("%06o %s %d\t", ce
->ce_mode
, sha1_to_hex(ce
->sha1
), ce_stage(ce
));
98 utf8_fprintf(stdout
, "%s\n", ce
->name
);
103 static int module_name(int argc
, const char **argv
, const char *prefix
)
105 const struct submodule
*sub
;
108 usage(_("git submodule--helper name <path>"));
111 sub
= submodule_from_path(null_sha1
, argv
[1]);
114 die(_("no submodule mapping found in .gitmodules for path '%s'"),
117 printf("%s\n", sub
->name
);
121 static int clone_submodule(const char *path
, const char *gitdir
, const char *url
,
122 const char *depth
, const char *reference
, int quiet
)
124 struct child_process cp
;
125 child_process_init(&cp
);
127 argv_array_push(&cp
.args
, "clone");
128 argv_array_push(&cp
.args
, "--no-checkout");
130 argv_array_push(&cp
.args
, "--quiet");
132 argv_array_pushl(&cp
.args
, "--depth", depth
, NULL
);
133 if (reference
&& *reference
)
134 argv_array_pushl(&cp
.args
, "--reference", reference
, NULL
);
135 if (gitdir
&& *gitdir
)
136 argv_array_pushl(&cp
.args
, "--separate-git-dir", gitdir
, NULL
);
138 argv_array_push(&cp
.args
, url
);
139 argv_array_push(&cp
.args
, path
);
142 cp
.env
= local_repo_env
;
145 return run_command(&cp
);
148 static int module_clone(int argc
, const char **argv
, const char *prefix
)
150 const char *path
= NULL
, *name
= NULL
, *url
= NULL
;
151 const char *reference
= NULL
, *depth
= NULL
;
153 FILE *submodule_dot_git
;
154 char *sm_gitdir
, *cwd
, *p
;
155 struct strbuf rel_path
= STRBUF_INIT
;
156 struct strbuf sb
= STRBUF_INIT
;
158 struct option module_clone_options
[] = {
159 OPT_STRING(0, "prefix", &prefix
,
161 N_("alternative anchor for relative paths")),
162 OPT_STRING(0, "path", &path
,
164 N_("where the new submodule will be cloned to")),
165 OPT_STRING(0, "name", &name
,
167 N_("name of the new submodule")),
168 OPT_STRING(0, "url", &url
,
170 N_("url where to clone the submodule from")),
171 OPT_STRING(0, "reference", &reference
,
173 N_("reference repository")),
174 OPT_STRING(0, "depth", &depth
,
176 N_("depth for shallow clones")),
177 OPT__QUIET(&quiet
, "Suppress output for cloning a submodule"),
181 const char *const git_submodule_helper_usage
[] = {
182 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
183 "[--reference <repository>] [--name <name>] [--url <url>]"
184 "[--depth <depth>] [--] [<path>...]"),
188 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
189 git_submodule_helper_usage
, 0);
191 strbuf_addf(&sb
, "%s/modules/%s", get_git_dir(), name
);
192 sm_gitdir
= strbuf_detach(&sb
, NULL
);
194 if (!file_exists(sm_gitdir
)) {
195 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
196 die(_("could not create directory '%s'"), sm_gitdir
);
197 if (clone_submodule(path
, sm_gitdir
, url
, depth
, reference
, quiet
))
198 die(_("clone of '%s' into submodule path '%s' failed"),
201 if (safe_create_leading_directories_const(path
) < 0)
202 die(_("could not create directory '%s'"), path
);
203 strbuf_addf(&sb
, "%s/index", sm_gitdir
);
204 unlink_or_warn(sb
.buf
);
208 /* Write a .git file in the submodule to redirect to the superproject. */
209 if (safe_create_leading_directories_const(path
) < 0)
210 die(_("could not create directory '%s'"), path
);
213 strbuf_addf(&sb
, "%s/.git", path
);
215 strbuf_addstr(&sb
, ".git");
217 if (safe_create_leading_directories_const(sb
.buf
) < 0)
218 die(_("could not create leading directories of '%s'"), sb
.buf
);
219 submodule_dot_git
= fopen(sb
.buf
, "w");
220 if (!submodule_dot_git
)
221 die_errno(_("cannot open file '%s'"), sb
.buf
);
223 fprintf(submodule_dot_git
, "gitdir: %s\n",
224 relative_path(sm_gitdir
, path
, &rel_path
));
225 if (fclose(submodule_dot_git
))
226 die(_("could not close file %s"), sb
.buf
);
228 strbuf_reset(&rel_path
);
231 /* Redirect the worktree of the submodule in the superproject's config */
232 if (!is_absolute_path(sm_gitdir
)) {
233 strbuf_addf(&sb
, "%s/%s", cwd
, sm_gitdir
);
235 sm_gitdir
= strbuf_detach(&sb
, NULL
);
238 strbuf_addf(&sb
, "%s/%s", cwd
, path
);
239 p
= git_pathdup_submodule(path
, "config");
241 die(_("could not get submodule directory for '%s'"), path
);
242 git_config_set_in_file(p
, "core.worktree",
243 relative_path(sb
.buf
, sm_gitdir
, &rel_path
));
245 strbuf_release(&rel_path
);
254 int (*fn
)(int, const char **, const char *);
257 static struct cmd_struct commands
[] = {
258 {"list", module_list
},
259 {"name", module_name
},
260 {"clone", module_clone
},
263 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
267 die(_("fatal: submodule--helper subcommand must be "
268 "called with a subcommand"));
270 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++)
271 if (!strcmp(argv
[1], commands
[i
].cmd
))
272 return commands
[i
].fn(argc
- 1, argv
+ 1, prefix
);
274 die(_("fatal: '%s' is not a valid submodule--helper "
275 "subcommand"), argv
[1]);