git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 4/7] refs: consolidate remote name validation

From
Sean Barag via GitGitGadget <gitgitgadget@gmail.com>
Date
Oct 1, 2020, 03:46 UTC
Message-ID
<cb686b4129902335ef98c83c8cfbcac1c88da856.1601523977.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.727.v3.git.1601523977.gitgitgadget@gmail.com>
From: Sean Barag <sean@barag.org>

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>
---
 builtin/remote.c |  7 ++-----
 refspec.c        | 10 ++++++++++
 refspec.h        |  1 +
 3 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/builtin/remote.c b/builtin/remote.c
index 64b4b551eb..63f2b46c3d 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -194,8 +194,7 @@ static int add(int argc, const char **argv)
 	if (remote_is_configured(remote, 1))
 		die(_("remote %s already exists."), name);
 
-	strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
-	if (!valid_fetch_refspec(buf2.buf))
+	if (!valid_remote_name(name))
 		die(_("'%s' is not a valid remote name"), name);
 
 	strbuf_addf(&buf, "remote.%s.url", name);
@@ -696,11 +695,9 @@ static int mv(int argc, const char **argv)
 	if (remote_is_configured(newremote, 1))
 		die(_("remote %s already exists."), rename.new_name);
 
-	strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new_name);
-	if (!valid_fetch_refspec(buf.buf))
+	if (!valid_remote_name(rename.new_name))
 		die(_("'%s' is not a valid remote name"), rename.new_name);
 
-	strbuf_reset(&buf);
 	strbuf_addf(&buf, "remote.%s", rename.old_name);
 	strbuf_addf(&buf2, "remote.%s", rename.new_name);
 	if (git_config_rename_section(buf.buf, buf2.buf) < 1)
diff --git a/refspec.c b/refspec.c
index 8d0affc34a..3056ffdfb8 100644
--- a/refspec.c
+++ b/refspec.c
@@ -215,6 +215,16 @@ int valid_fetch_refspec(const char *fetch_refspec_str)
 	return ret;
 }
 
+int valid_remote_name(const char *name)
+{
+	int result;
+	struct strbuf refspec = STRBUF_INIT;
+	strbuf_addf(&refspec, "refs/heads/test:refs/remotes/%s/test", name);
+	result = valid_fetch_refspec(refspec.buf);
+	strbuf_release(&refspec);
+	return result;
+}
+
 void refspec_ref_prefixes(const struct refspec *rs,
 			  struct strvec *ref_prefixes)
 {
diff --git a/refspec.h b/refspec.h
index 7569248d11..bbb25968a8 100644
--- a/refspec.h
+++ b/refspec.h
@@ -62,6 +62,7 @@ void refspec_appendn(struct refspec *rs, const char **refspecs, int nr);
 void refspec_clear(struct refspec *rs);
 
 int valid_fetch_refspec(const char *refspec);
+int valid_remote_name(const char *name);
 
 struct strvec;
 /*
-- 
gitgitgadget
Previous: Sean Barag via GitGitGadgetNext: Derrick Stolee
Message 48 of 49 in “clone: allow configurable default for -o/--origin”
  1. 0/4 clone: allow configurable default for -o/--originSean Barag via GitGitGadget, Sep 11, 2020
  2. 2/4 clone: call git_config before parse_optionsSean Barag via GitGitGadget, Sep 11, 2020
  3. Derrick StoleeSep 11, 2020
  4. Junio C HamanoSep 11, 2020
  5. Sean BaragSep 16, 2020
  6. 1/4 clone: add tests for --template and some disallowed option pairsSean Barag via GitGitGadget, Sep 11, 2020
  7. Derrick StoleeSep 11, 2020
  8. Jeff KingSep 11, 2020
  9. Eric SunshineSep 11, 2020
  10. Sean BaragSep 16, 2020
  11. Taylor BlauSep 12, 2020
  12. Sean BaragSep 15, 2020
  13. Jeff KingSep 16, 2020
  14. Junio C HamanoSep 11, 2020
  15. Derrick StoleeSep 12, 2020
  16. 3/4 clone: validate --origin option before useSean Barag via GitGitGadget, Sep 11, 2020
  17. Derrick StoleeSep 11, 2020
  18. Sean BaragSep 16, 2020
  19. Junio C HamanoSep 11, 2020
  20. Sean BaragSep 16, 2020
  21. Sean BaragSep 21, 2020
  22. 4/4 clone: allow configurable default for `-o`/`--origin`Sean Barag via GitGitGadget, Sep 11, 2020
  23. Derrick StoleeSep 11, 2020
  24. Sean BaragSep 28, 2020
  25. Junio C HamanoSep 11, 2020
  26. Sean BaragSep 28, 2020
  27. Andrei RybakSep 17, 2020
  28. Derrick StoleeSep 11, 2020
  29. Junio C HamanoSep 11, 2020
  30. 0/7 clone: allow configurable default for -o/--originSean Barag via GitGitGadget, Sep 29, 2020
  31. 1/7 clone: add tests for --template and some disallowed option pairsSean Barag via GitGitGadget, Sep 29, 2020
  32. 2/7 clone: use more conventional config/option layeringSean Barag via GitGitGadget, Sep 29, 2020
  33. 3/7 remote: add tests for add and rename with invalid namesSean Barag via GitGitGadget, Sep 29, 2020
  34. 6/7 clone: read new remote name from remote_name instead of option_originSean Barag via GitGitGadget, Sep 29, 2020
  35. 5/7 clone: validate --origin option before useSean Barag via GitGitGadget, Sep 29, 2020
  36. 4/7 refs: consolidate remote name validationSean Barag via GitGitGadget, Sep 29, 2020
  37. 7/7 clone: allow configurable default for `-o`/`--origin`Sean Barag via GitGitGadget, Sep 29, 2020
  38. Junio C HamanoSep 29, 2020
  39. Sean BaragSep 29, 2020
  40. 0/7 clone: allow configurable default for -o/--originSean Barag, Sep 29, 2020
  41. 0/7 clone: allow configurable default for -o/--originSean Barag via GitGitGadget, Oct 1, 2020
  42. 1/7 clone: add tests for --template and some disallowed option pairsSean Barag via GitGitGadget, Oct 1, 2020
  43. 5/7 clone: validate --origin option before useSean Barag via GitGitGadget, Oct 1, 2020
  44. 7/7 clone: allow configurable default for `-o`/`--origin`Sean Barag via GitGitGadget, Oct 1, 2020
  45. 2/7 clone: use more conventional config/option layeringSean Barag via GitGitGadget, Oct 1, 2020
  46. 3/7 remote: add tests for add and rename with invalid namesSean Barag via GitGitGadget, Oct 1, 2020
  47. 6/7 clone: read new remote name from remote_name instead of option_originSean Barag via GitGitGadget, Oct 1, 2020
  48. 4/7 refs: consolidate remote name validationSean Barag via GitGitGadget, Oct 1, 2020
  49. Derrick StoleeOct 2, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.