submodule--helper: replace connect-gitdir-workingtree by ensure-core-worktree

e98317508c0 (submodule: ensure core.worktree is set after update, 2018-06-18) was overly aggressive in calling connect_work_tree_and_git_dir as that ensures both the 'core.worktree' configuration is set as well as setting up correct gitlink file pointing at the git directory. We do not need to check for the gitlink in this part of the cmd_update in git-submodule.sh, as the initial call to update-clone will have ensured that. So we can reduce the work to only (check and potentially) set the 'core.worktree' setting. While at it move the check from shell to C as that proves to be useful in a follow up patch, as we do not need the 'name' in shell now. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Aug 13, 2018 at 15:42 UTC 74d4731da1fd61e3705e808bcd496979ef8ddf5a
2 files changed +42 -29
builtin/submodule--helper.c
+40 -24
@@ -1964,6 +1964,45 @@ static int push_check(int argc, const char **argv, const char *prefix)
1964 return 0;
1965 }
1966
1967 +static int ensure_core_worktree(int argc, const char **argv, const char *prefix)
1968 +{
1969 + const struct submodule *sub;
1970 + const char *path;
1971 + char *cw;
1972 + struct repository subrepo;
1973 +
1974 + if (argc != 2)
1975 + BUG("submodule--helper connect-gitdir-workingtree <name> <path>");
1976 +
1977 + path = argv[1];
1978 +
1979 + sub = submodule_from_path(the_repository, &null_oid, path);
1980 + if (!sub)
1981 + BUG("We could get the submodule handle before?");
1982 +
1983 + if (repo_submodule_init(&subrepo, the_repository, path))
1984 + die(_("could not get a repository handle for submodule '%s'"), path);
1985 +
1986 + if (!repo_config_get_string(&subrepo, "core.worktree", &cw)) {
1987 + char *cfg_file, *abs_path;
1988 + const char *rel_path;
1989 + struct strbuf sb = STRBUF_INIT;
1990 +
1991 + cfg_file = repo_git_path(&subrepo, "config");
1992 +
1993 + abs_path = absolute_pathdup(path);
1994 + rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
1995 +
1996 + git_config_set_in_file(cfg_file, "core.worktree", rel_path);
1997 +
1998 + free(cfg_file);
1999 + free(abs_path);
2000 + strbuf_release(&sb);
2001 + }
2002 +
2003 + return 0;
2004 +}
2005 +
2006 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2007 {
2008 int i;
@@ -2029,29 +2068,6 @@ static int check_name(int argc, const char **argv, const char *prefix)
2068 return 0;
2069 }
2070
2032 -static int connect_gitdir_workingtree(int argc, const char **argv, const char *prefix)
2033 -{
2034 - struct strbuf sb = STRBUF_INIT;
2035 - const char *name, *path;
2036 - char *sm_gitdir;
2037 -
2038 - if (argc != 3)
2039 - BUG("submodule--helper connect-gitdir-workingtree <name> <path>");
2040 -
2041 - name = argv[1];
2042 - path = argv[2];
2043 -
2044 - strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
2045 - sm_gitdir = absolute_pathdup(sb.buf);
2046 -
2047 - connect_work_tree_and_git_dir(path, sm_gitdir, 0);
2048 -
2049 - strbuf_release(&sb);
2050 - free(sm_gitdir);
2051 -
2052 - return 0;
2053 -}
2054 -
2071 #define SUPPORT_SUPER_PREFIX (1<<0)
2072
2073 struct cmd_struct {
@@ -2065,7 +2081,7 @@ static struct cmd_struct commands[] = {
2081 {"name", module_name, 0},
2082 {"clone", module_clone, 0},
2083 {"update-clone", update_clone, 0},
2068 - {"connect-gitdir-workingtree", connect_gitdir_workingtree, 0},
2084 + {"ensure-core-worktree", ensure_core_worktree, 0},
2085 {"relative-path", resolve_relative_path, 0},
2086 {"resolve-relative-url", resolve_relative_url, 0},
2087 {"resolve-relative-url-test", resolve_relative_url_test, 0},
git-submodule.sh
+2 -5
@@ -535,6 +535,8 @@ cmd_update()
535 do
536 die_if_unmatched "$quickabort" "$sha1"
537
538 + git submodule--helper ensure-core-worktree "$sm_path"
539 +
540 name=$(git submodule--helper name "$sm_path") || exit
541 if ! test -z "$update"
542 then
@@ -577,11 +579,6 @@ cmd_update()
579 die "$(eval_gettext "Unable to find current \${remote_name}/\${branch} revision in submodule path '\$sm_path'")"
580 fi
581
580 - if ! $(git config -f "$(git rev-parse --git-common-dir)/modules/$name/config" core.worktree) 2>/dev/null
581 - then
582 - git submodule--helper connect-gitdir-workingtree "$name" "$sm_path"
583 - fi
584 -
582 if test "$subsha1" != "$sha1" || test -n "$force"
583 then
584 subforce=$force