submodule: port init from shell to C

By having the `submodule init` functionality in C, we can reference it easier from other parts in the code in later patches. The code is split up to have one function to initialize one submodule and a calling function that takes care of the rest, such as argument handling and translating the arguments to the paths of the submodules. This is the first submodule subcommand that is fully converted to C except for the usage string, so this is actually removing a call to the `submodule--helper list` function, which is supposed to be used in this transition. Instead we'll make a direct call to `module_list_compute`. An explanation why we need to edit the prefixes in cmd_update in git-submodule.sh in this patch: By having no processing in the shell part, we need to convey the notion of wt_prefix and prefix to the C parts, which former patches punted on and did the processing of displaying path in the shell. `wt_prefix` used to hold the path from the repository root to the current directory, e.g. wt_prefix would be t/ if the user invoked the `git submodule` command in ~/repo/t and ~repo is the GIT_DIR. `prefix` used to hold the relative path from the repository root to the operation, e.g. if you have recursive submodules, the shell script would modify the `prefix` in each recursive step by adding the submodule path. We will pass `wt_prefix` into the C helper via `git -C <dir>` as that will setup git in the directory the user actually called git-submodule.sh from. The `prefix` will be passed in via the `--prefix` option. Having `prefix` and `wt_prefix` relative to the GIT_DIR of the calling superproject is unfortunate with this patch as the C code doesn't know about a possible recursion from a superproject via `submodule update --init --recursive`. To fix this, we change the meaning of `wt_prefix` to point to the current project instead of the superproject and `prefix` to include any relative paths issues in the superproject. That way `prefix` will become the leading part for displaying paths and `wt_prefix` will be empty in recursive calls for now. The new notion of `wt_prefix` and `prefix` still allows us to reconstruct the calling directory in the superproject by just traveling reverse of `prefix`. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Apr 15, 2016 at 17:50 UTC 3604242f080a813d6f20a7394def422d1e55b30e
4 files changed +140 -45
builtin/submodule--helper.c
+115
@@ -305,6 +305,120 @@ static int module_list(int argc, const char **argv, const char *prefix)
305 return 0;
306 }
307
308 +static void init_submodule(const char *path, const char *prefix, int quiet)
309 +{
310 + const struct submodule *sub;
311 + struct strbuf sb = STRBUF_INIT;
312 + char *upd = NULL, *url = NULL, *displaypath;
313 +
314 + /* Only loads from .gitmodules, no overlay with .git/config */
315 + gitmodules_config();
316 +
317 + sub = submodule_from_path(null_sha1, path);
318 +
319 + if (prefix) {
320 + strbuf_addf(&sb, "%s%s", prefix, path);
321 + displaypath = strbuf_detach(&sb, NULL);
322 + } else
323 + displaypath = xstrdup(sub->path);
324 +
325 + /*
326 + * Copy url setting when it is not set yet.
327 + * To look up the url in .git/config, we must not fall back to
328 + * .gitmodules, so look it up directly.
329 + */
330 + strbuf_reset(&sb);
331 + strbuf_addf(&sb, "submodule.%s.url", sub->name);
332 + if (git_config_get_string(sb.buf, &url)) {
333 + url = xstrdup(sub->url);
334 +
335 + if (!url)
336 + die(_("No url found for submodule path '%s' in .gitmodules"),
337 + displaypath);
338 +
339 + /* Possibly a url relative to parent */
340 + if (starts_with_dot_dot_slash(url) ||
341 + starts_with_dot_slash(url)) {
342 + char *remoteurl, *relurl;
343 + char *remote = get_default_remote();
344 + struct strbuf remotesb = STRBUF_INIT;
345 + strbuf_addf(&remotesb, "remote.%s.url", remote);
346 + free(remote);
347 +
348 + if (git_config_get_string(remotesb.buf, &remoteurl))
349 + /*
350 + * The repository is its own
351 + * authoritative upstream
352 + */
353 + remoteurl = xgetcwd();
354 + relurl = relative_url(remoteurl, url, NULL);
355 + strbuf_release(&remotesb);
356 + free(remoteurl);
357 + free(url);
358 + url = relurl;
359 + }
360 +
361 + if (git_config_set_gently(sb.buf, url))
362 + die(_("Failed to register url for submodule path '%s'"),
363 + displaypath);
364 + if (!quiet)
365 + printf(_("Submodule '%s' (%s) registered for path '%s'\n"),
366 + sub->name, url, displaypath);
367 + }
368 +
369 + /* Copy "update" setting when it is not set yet */
370 + strbuf_reset(&sb);
371 + strbuf_addf(&sb, "submodule.%s.update", sub->name);
372 + if (git_config_get_string(sb.buf, &upd) &&
373 + sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
374 + if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
375 + fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
376 + sub->name);
377 + upd = xstrdup("none");
378 + } else
379 + upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
380 +
381 + if (git_config_set_gently(sb.buf, upd))
382 + die(_("Failed to register update mode for submodule path '%s'"), displaypath);
383 + }
384 + strbuf_release(&sb);
385 + free(displaypath);
386 + free(url);
387 + free(upd);
388 +}
389 +
390 +static int module_init(int argc, const char **argv, const char *prefix)
391 +{
392 + struct pathspec pathspec;
393 + struct module_list list = MODULE_LIST_INIT;
394 + int quiet = 0;
395 + int i;
396 +
397 + struct option module_init_options[] = {
398 + OPT_STRING(0, "prefix", &prefix,
399 + N_("path"),
400 + N_("alternative anchor for relative paths")),
401 + OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
402 + OPT_END()
403 + };
404 +
405 + const char *const git_submodule_helper_usage[] = {
406 + N_("git submodule--helper init [<path>]"),
407 + NULL
408 + };
409 +
410 + argc = parse_options(argc, argv, prefix, module_init_options,
411 + git_submodule_helper_usage, 0);
412 +
413 + if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
414 + return 1;
415 +
416 + for (i = 0; i < list.nr; i++)
417 + init_submodule(list.entries[i]->name, prefix, quiet);
418 +
419 + return 0;
420 +}
421 +
422 static int module_name(int argc, const char **argv, const char *prefix)
423 {
424 const struct submodule *sub;
@@ -712,6 +826,7 @@ static struct cmd_struct commands[] = {
826 {"update-clone", update_clone},
827 {"resolve-relative-url", resolve_relative_url},
828 {"resolve-relative-url-test", resolve_relative_url_test},
829 + {"init", module_init}
830 };
831
832 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
git-submodule.sh
+3 -45
@@ -394,50 +394,7 @@ cmd_init()
394 shift
395 done
396
397 - git submodule--helper list --prefix "$wt_prefix" "$@" |
398 - while read mode sha1 stage sm_path
399 - do
400 - die_if_unmatched "$mode"
401 - name=$(git submodule--helper name "$sm_path") || exit
402 -
403 - displaypath=$(relative_path "$prefix$sm_path")
404 -
405 - # Copy url setting when it is not set yet
406 - if test -z "$(git config "submodule.$name.url")"
407 - then
408 - url=$(git config -f .gitmodules submodule."$name".url)
409 - test -z "$url" &&
410 - die "$(eval_gettext "No url found for submodule path '\$displaypath' in .gitmodules")"
411 -
412 - # Possibly a url relative to parent
413 - case "$url" in
414 - ./*|../*)
415 - url=$(git submodule--helper resolve-relative-url "$url") || exit
416 - ;;
417 - esac
418 - git config submodule."$name".url "$url" ||
419 - die "$(eval_gettext "Failed to register url for submodule path '\$displaypath'")"
420 -
421 - say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$displaypath'")"
422 - fi
423 -
424 - # Copy "update" setting when it is not set yet
425 - if upd="$(git config -f .gitmodules submodule."$name".update)" &&
426 - test -n "$upd" &&
427 - test -z "$(git config submodule."$name".update)"
428 - then
429 - case "$upd" in
430 - checkout | rebase | merge | none)
431 - ;; # known modes of updating
432 - *)
433 - echo >&2 "warning: unknown update mode '$upd' suggested for submodule '$name'"
434 - upd=none
435 - ;;
436 - esac
437 - git config submodule."$name".update "$upd" ||
438 - die "$(eval_gettext "Failed to register update mode for submodule path '\$displaypath'")"
439 - fi
440 - done
397 + git ${wt_prefix:+-C "$wt_prefix"} submodule--helper init ${GIT_QUIET:+--quiet} ${prefix:+--prefix "$prefix"} "$@"
398 }
399
400 #
@@ -740,7 +697,8 @@ cmd_update()
697 if test -n "$recursive"
698 then
699 (
743 - prefix="$prefix$sm_path/"
700 + prefix=$(relative_path "$prefix$sm_path/")
701 + wt_prefix=
702 clear_local_git_env
703 cd "$sm_path" &&
704 eval cmd_update
submodule.c
+21
@@ -237,6 +237,27 @@ int parse_submodule_update_strategy(const char *value,
237 return 0;
238 }
239
240 +const char *submodule_strategy_to_string(const struct submodule_update_strategy *s)
241 +{
242 + struct strbuf sb = STRBUF_INIT;
243 + switch (s->type) {
244 + case SM_UPDATE_CHECKOUT:
245 + return "checkout";
246 + case SM_UPDATE_MERGE:
247 + return "merge";
248 + case SM_UPDATE_REBASE:
249 + return "rebase";
250 + case SM_UPDATE_NONE:
251 + return "none";
252 + case SM_UPDATE_UNSPECIFIED:
253 + return NULL;
254 + case SM_UPDATE_COMMAND:
255 + strbuf_addf(&sb, "!%s", s->command);
256 + return strbuf_detach(&sb, NULL);
257 + }
258 + return NULL;
259 +}
260 +
261 void handle_ignore_submodules_arg(struct diff_options *diffopt,
262 const char *arg)
263 {
submodule.h
+1
@@ -39,6 +39,7 @@ int submodule_config(const char *var, const char *value, void *cb);
39 void gitmodules_config(void);
40 int parse_submodule_update_strategy(const char *value,
41 struct submodule_update_strategy *dst);
42 +const char *submodule_strategy_to_string(const struct submodule_update_strategy *s);
43 void handle_ignore_submodules_arg(struct diff_options *diffopt, const char *);
44 void show_submodule_summary(FILE *f, const char *path,
45 const char *line_prefix,