@matheusbsbs / matheus-bagliano / commits / f2c6fda886
Fork 0

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 <stolee@gmail.com> Signed-off-by: Sean Barag <sean@barag.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Sean Barag committed Oct 1, 2020 at 03:46 UTC f2c6fda88624370fda1fc706a0f2ceda7d50d6ab
3 files changed +13 -5
builtin/remote.c
+2 -5
@@ -194,8 +194,7 @@ static int add(int argc, const char **argv)
194 if (remote_is_configured(remote, 1))
195 die(_("remote %s already exists."), name);
196
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))
198 die(_("'%s' is not a valid remote name"), name);
199
200 strbuf_addf(&buf, "remote.%s.url", name);
@@ -696,11 +695,9 @@ static int mv(int argc, const char **argv)
695 if (remote_is_configured(newremote, 1))
696 die(_("remote %s already exists."), rename.new_name);
697
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))
699 die(_("'%s' is not a valid remote name"), rename.new_name);
700
703 - strbuf_reset(&buf);
701 strbuf_addf(&buf, "remote.%s", rename.old_name);
702 strbuf_addf(&buf2, "remote.%s", rename.new_name);
703 if (git_config_rename_section(buf.buf, buf2.buf) < 1)
refspec.c
+10
@@ -201,6 +201,16 @@ int valid_fetch_refspec(const char *fetch_refspec_str)
201 return ret;
202 }
203
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 +
214 void refspec_ref_prefixes(const struct refspec *rs,
215 struct strvec *ref_prefixes)
216 {
refspec.h
+1
@@ -60,6 +60,7 @@ void refspec_appendn(struct refspec *rs, const char **refspecs, int nr);
60 void refspec_clear(struct refspec *rs);
61
62 int valid_fetch_refspec(const char *refspec);
63 +int valid_remote_name(const char *name);
64
65 struct strvec;
66 /*