Skip to content

Commit 3f5aab3

Browse files
10ne1gitster
authored andcommitted
submodule: add gitdir path config override
This adds the ability to override gitdir paths via config files (not .gitmodules) such that the encoding scheme (or plain text name if the encoding extension is disabled) can be changed via config entries. These entries are not added by default for all submodules: they should be used on an as-needed basis. A new test and a helper are added. The helper will also be used in further tests exercising gitdir encoding functionality. Based-on-patch-by: Brandon Williams <[email protected]> Signed-off-by: Adrian Ratiu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 03bd616 commit 3f5aab3

File tree

6 files changed

+63
-0
lines changed

6 files changed

+63
-0
lines changed

Documentation/config/submodule.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ submodule.<name>.active::
5252
submodule.active config option. See linkgit:gitsubmodules[7] for
5353
details.
5454

55+
submodule.<name>.gitdir::
56+
This option sets the gitdir path for submodule <name>, allowing users
57+
to override the default path or change the default path name encoding.
58+
5559
submodule.active::
5660
A repeated field which contains a pathspec used to match against a
5761
submodule's path to determine if the submodule is of interest to git

builtin/submodule--helper.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,22 @@ static int module_summary(int argc, const char **argv, const char *prefix,
12141214
return ret;
12151215
}
12161216

1217+
static int module_gitdir(int argc, const char **argv, const char *prefix UNUSED,
1218+
struct repository *repo)
1219+
{
1220+
struct strbuf gitdir = STRBUF_INIT;
1221+
1222+
if (argc != 2)
1223+
usage(_("git submodule--helper gitdir <name>"));
1224+
1225+
submodule_name_to_gitdir(&gitdir, repo, argv[1]);
1226+
1227+
printf("%s\n", gitdir.buf);
1228+
1229+
strbuf_release(&gitdir);
1230+
return 0;
1231+
}
1232+
12171233
struct sync_cb {
12181234
const char *prefix;
12191235
const char *super_prefix;
@@ -3597,6 +3613,7 @@ int cmd_submodule__helper(int argc,
35973613
NULL
35983614
};
35993615
struct option options[] = {
3616+
OPT_SUBCOMMAND("gitdir", &fn, module_gitdir),
36003617
OPT_SUBCOMMAND("clone", &fn, module_clone),
36013618
OPT_SUBCOMMAND("add", &fn, module_add),
36023619
OPT_SUBCOMMAND("update", &fn, module_update),

submodule.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,6 +2604,18 @@ void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
26042604
* administrators can explicitly set. Nothing has been decided,
26052605
* so for now, just append the name at the end of the path.
26062606
*/
2607+
char *gitdir_path, *key;
2608+
2609+
/* Allow config override. */
2610+
key = xstrfmt("submodule.%s.gitdirpath", submodule_name);
2611+
if (!repo_config_get_string(r, key, &gitdir_path)) {
2612+
strbuf_addstr(buf, gitdir_path);
2613+
free(key);
2614+
free(gitdir_path);
2615+
return;
2616+
}
2617+
free(key);
2618+
26072619
repo_git_path_append(r, buf, "modules/");
26082620
strbuf_addstr(buf, submodule_name);
26092621
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Helper to verify if repo $1 contains a submodule named $2 with gitdir path $3
2+
3+
# This does not check filesystem existence. That is done in submodule.c via the
4+
# submodule_name_to_gitdir() API which this helper ends up calling. The gitdirs
5+
# might or might not exist (e.g. when adding a new submodule), so this only
6+
# checks the expected configuration path, which might be overridden by the user.
7+
8+
verify_submodule_gitdir_path() {
9+
repo="$1" &&
10+
name="$2" &&
11+
path="$3" &&
12+
(
13+
cd "$repo" &&
14+
cat >expect <<-EOF &&
15+
$(git rev-parse --git-common-dir)/$path
16+
EOF
17+
git submodule--helper gitdir "$name" >actual &&
18+
test_cmp expect actual
19+
)
20+
}

t/t7400-submodule-basic.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
1313
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
1414

1515
. ./test-lib.sh
16+
. "$TEST_DIRECTORY"/lib-verify-submodule-gitdir-path.sh
1617

1718
test_expect_success 'setup - enable local submodules' '
1819
git config --global protocol.file.allow always
@@ -1505,4 +1506,12 @@ test_expect_success 'submodule add fails when name is reused' '
15051506
)
15061507
'
15071508

1509+
test_expect_success 'submodule helper gitdir config overrides' '
1510+
verify_submodule_gitdir_path test-submodule child modules/child &&
1511+
test_config -C test-submodule submodule.child.gitdirpath ".git/modules/custom-child" &&
1512+
verify_submodule_gitdir_path test-submodule child modules/custom-child &&
1513+
test_unconfig -C test-submodule submodule.child.gitdirpath &&
1514+
verify_submodule_gitdir_path test-submodule child modules/child
1515+
'
1516+
15081517
test_done

t/t9902-completion.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3053,6 +3053,7 @@ test_expect_success 'git config set - variable name - __git_compute_second_level
30533053
submodule.sub.fetchRecurseSubmodules Z
30543054
submodule.sub.ignore Z
30553055
submodule.sub.active Z
3056+
submodule.sub.gitdir Z
30563057
EOF
30573058
'
30583059

0 commit comments

Comments
 (0)