Skip to content

Commit a8d447f

Browse files
authored
Merge pull request libgit2#4837 from pks-t/cmn/reject-option-submodule-url-path
submodule: ignore path and url attributes if they look like options
2 parents ce8803a + c8ca3ca commit a8d447f

File tree

2 files changed

+103
-8
lines changed

2 files changed

+103
-8
lines changed

src/submodule.c

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,6 +1865,14 @@ static int get_value(const char **out, git_config *cfg, git_buf *buf, const char
18651865
return error;
18661866
}
18671867

1868+
static bool looks_like_command_line_option(const char *s)
1869+
{
1870+
if (s && s[0] == '-')
1871+
return true;
1872+
1873+
return false;
1874+
}
1875+
18681876
static int submodule_read_config(git_submodule *sm, git_config *cfg)
18691877
{
18701878
git_buf key = GIT_BUF_INIT;
@@ -1878,24 +1886,31 @@ static int submodule_read_config(git_submodule *sm, git_config *cfg)
18781886

18791887
if ((error = get_value(&value, cfg, &key, sm->name, "path")) == 0) {
18801888
in_config = 1;
1889+
/* We would warn here if we had that API */
1890+
if (!looks_like_command_line_option(value)) {
18811891
/*
18821892
* TODO: if case insensitive filesystem, then the following strcmp
18831893
* should be strcasecmp
18841894
*/
1885-
if (strcmp(sm->name, value) != 0) {
1886-
if (sm->path != sm->name)
1887-
git__free(sm->path);
1888-
sm->path = git__strdup(value);
1889-
GITERR_CHECK_ALLOC(sm->path);
1895+
if (strcmp(sm->name, value) != 0) {
1896+
if (sm->path != sm->name)
1897+
git__free(sm->path);
1898+
sm->path = git__strdup(value);
1899+
GITERR_CHECK_ALLOC(sm->path);
1900+
}
1901+
18901902
}
18911903
} else if (error != GIT_ENOTFOUND) {
18921904
goto cleanup;
18931905
}
18941906

18951907
if ((error = get_value(&value, cfg, &key, sm->name, "url")) == 0) {
1896-
in_config = 1;
1897-
sm->url = git__strdup(value);
1898-
GITERR_CHECK_ALLOC(sm->url);
1908+
/* We would warn here if we had that API */
1909+
if (!looks_like_command_line_option(value)) {
1910+
in_config = 1;
1911+
sm->url = git__strdup(value);
1912+
GITERR_CHECK_ALLOC(sm->url);
1913+
}
18991914
} else if (error != GIT_ENOTFOUND) {
19001915
goto cleanup;
19011916
}

tests/submodule/inject_option.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "clar_libgit2.h"
2+
#include "posix.h"
3+
#include "path.h"
4+
#include "submodule_helpers.h"
5+
#include "fileops.h"
6+
#include "repository.h"
7+
8+
static git_repository *g_repo = NULL;
9+
10+
void test_submodule_inject_option__initialize(void)
11+
{
12+
g_repo = setup_fixture_submodule_simple();
13+
}
14+
15+
void test_submodule_inject_option__cleanup(void)
16+
{
17+
cl_git_sandbox_cleanup();
18+
}
19+
20+
static int find_naughty(git_submodule *sm, const char *name, void *payload)
21+
{
22+
int *foundit = (int *) payload;
23+
24+
GIT_UNUSED(sm);
25+
26+
if (!git__strcmp("naughty", name))
27+
*foundit = true;
28+
29+
return 0;
30+
}
31+
32+
void test_submodule_inject_option__url(void)
33+
{
34+
int foundit;
35+
git_submodule *sm;
36+
git_buf buf = GIT_BUF_INIT;
37+
38+
cl_git_pass(git_buf_joinpath(&buf, git_repository_workdir(g_repo), ".gitmodules"));
39+
cl_git_rewritefile(buf.ptr,
40+
"[submodule \"naughty\"]\n"
41+
" path = testrepo\n"
42+
" url = -u./payload\n");
43+
git_buf_dispose(&buf);
44+
45+
/* We do want to find it, but with the appropriate field empty */
46+
foundit = 0;
47+
cl_git_pass(git_submodule_foreach(g_repo, find_naughty, &foundit));
48+
cl_assert_equal_i(1, foundit);
49+
50+
cl_git_pass(git_submodule_lookup(&sm, g_repo, "naughty"));
51+
cl_assert_equal_s("testrepo", git_submodule_path(sm));
52+
cl_assert_equal_p(NULL, git_submodule_url(sm));
53+
54+
git_submodule_free(sm);
55+
}
56+
57+
void test_submodule_inject_option__path(void)
58+
{
59+
int foundit;
60+
git_submodule *sm;
61+
git_buf buf = GIT_BUF_INIT;
62+
63+
cl_git_pass(git_buf_joinpath(&buf, git_repository_workdir(g_repo), ".gitmodules"));
64+
cl_git_rewritefile(buf.ptr,
65+
"[submodule \"naughty\"]\n"
66+
" path = --something\n"
67+
" url = blah.git\n");
68+
git_buf_dispose(&buf);
69+
70+
/* We do want to find it, but with the appropriate field empty */
71+
foundit = 0;
72+
cl_git_pass(git_submodule_foreach(g_repo, find_naughty, &foundit));
73+
cl_assert_equal_i(1, foundit);
74+
75+
cl_git_pass(git_submodule_lookup(&sm, g_repo, "naughty"));
76+
cl_assert_equal_s("naughty", git_submodule_path(sm));
77+
cl_assert_equal_s("blah.git", git_submodule_url(sm));
78+
79+
git_submodule_free(sm);
80+
}

0 commit comments

Comments
 (0)