Skip to content

Commit 5f2b826

Browse files
committed
Merge branch 'jk/remote-avoid-overlapping-names'
"git remote" now detects remote names that overlap with each other (e.g., remote nickname "outer" and "outer/inner" are used at the same time), as it will lead to overlapping remote-tracking branches. * jk/remote-avoid-overlapping-names: remote: detect collisions in remote names
2 parents 205493d + a5a727c commit 5f2b826

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

builtin/remote.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@ static int parse_mirror_opt(const struct option *opt, const char *arg, int not)
157157
return 0;
158158
}
159159

160+
static int check_remote_collision(struct remote *remote, void *data)
161+
{
162+
const char *name = data;
163+
const char *p;
164+
165+
if (skip_prefix(name, remote->name, &p) && *p == '/')
166+
die(_("remote name '%s' is a subset of existing remote '%s'"),
167+
name, remote->name);
168+
if (skip_prefix(remote->name, name, &p) && *p == '/')
169+
die(_("remote name '%s' is a superset of existing remote '%s'"),
170+
name, remote->name);
171+
172+
return 0;
173+
}
174+
160175
static int add(int argc, const char **argv, const char *prefix,
161176
struct repository *repo UNUSED)
162177
{
@@ -208,6 +223,8 @@ static int add(int argc, const char **argv, const char *prefix,
208223
if (!valid_remote_name(name))
209224
die(_("'%s' is not a valid remote name"), name);
210225

226+
for_each_remote(check_remote_collision, (void *)name);
227+
211228
strbuf_addf(&buf, "remote.%s.url", name);
212229
git_config_set(buf.buf, url);
213230

t/t5505-remote.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,4 +1644,18 @@ test_expect_success 'empty config clears remote.*.pushurl list' '
16441644
test_cmp expect actual
16451645
'
16461646

1647+
test_expect_success 'forbid adding subset of existing remote' '
1648+
test_when_finished "git remote rm outer" &&
1649+
git remote add outer url &&
1650+
test_must_fail git remote add outer/inner url 2>err &&
1651+
test_grep ".outer/inner. is a subset of existing remote .outer." err
1652+
'
1653+
1654+
test_expect_success 'forbid adding superset of existing remote' '
1655+
test_when_finished "git remote rm outer/inner" &&
1656+
git remote add outer/inner url &&
1657+
test_must_fail git remote add outer url 2>err &&
1658+
test_grep ".outer. is a superset of existing remote .outer/inner." err
1659+
'
1660+
16471661
test_done

0 commit comments

Comments
 (0)