Skip to content

Commit f2c6fda

Browse files
sjbaraggitster
authored andcommitted
refs: consolidate remote name validation
In preparation for a future patch, extract from remote.c a function that validates possible remote names so that its rules can be used consistently in other places. Helped-by: Derrick Stolee <[email protected]> Signed-off-by: Sean Barag <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 444825c commit f2c6fda

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

builtin/remote.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ static int add(int argc, const char **argv)
194194
if (remote_is_configured(remote, 1))
195195
die(_("remote %s already exists."), name);
196196

197-
strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
198-
if (!valid_fetch_refspec(buf2.buf))
197+
if (!valid_remote_name(name))
199198
die(_("'%s' is not a valid remote name"), name);
200199

201200
strbuf_addf(&buf, "remote.%s.url", name);
@@ -696,11 +695,9 @@ static int mv(int argc, const char **argv)
696695
if (remote_is_configured(newremote, 1))
697696
die(_("remote %s already exists."), rename.new_name);
698697

699-
strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new_name);
700-
if (!valid_fetch_refspec(buf.buf))
698+
if (!valid_remote_name(rename.new_name))
701699
die(_("'%s' is not a valid remote name"), rename.new_name);
702700

703-
strbuf_reset(&buf);
704701
strbuf_addf(&buf, "remote.%s", rename.old_name);
705702
strbuf_addf(&buf2, "remote.%s", rename.new_name);
706703
if (git_config_rename_section(buf.buf, buf2.buf) < 1)

refspec.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,16 @@ int valid_fetch_refspec(const char *fetch_refspec_str)
201201
return ret;
202202
}
203203

204+
int valid_remote_name(const char *name)
205+
{
206+
int result;
207+
struct strbuf refspec = STRBUF_INIT;
208+
strbuf_addf(&refspec, "refs/heads/test:refs/remotes/%s/test", name);
209+
result = valid_fetch_refspec(refspec.buf);
210+
strbuf_release(&refspec);
211+
return result;
212+
}
213+
204214
void refspec_ref_prefixes(const struct refspec *rs,
205215
struct strvec *ref_prefixes)
206216
{

refspec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ void refspec_appendn(struct refspec *rs, const char **refspecs, int nr);
6060
void refspec_clear(struct refspec *rs);
6161

6262
int valid_fetch_refspec(const char *refspec);
63+
int valid_remote_name(const char *name);
6364

6465
struct strvec;
6566
/*

0 commit comments

Comments
 (0)