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

Re: [PATCH v2 7/7] clone: allow configurable default for `-o`/`--origin`

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 29, 2020, 19:59 UTC
Message-ID
<xmqqa6x8icbp.fsf@gitster.c.googlers.com>
In-Reply-To
<737f91c6244220eec196c327fcea9a6548c45310.1601350615.git.gitgitgadget@gmail.com>
"Sean Barag via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 6 quoted lines
>  static int git_clone_config(const char *k, const char *v, void *cb)
>  {
> +	if (!strcmp(k, "clone.defaultremotename")) {
> +		if (remote_name != default_remote_name)
> +			free(remote_name);
> +		remote_name = xstrdup(v);
This feels strange.  The usual arrangement is
    - initialize the variable to NULL (or any value that the code
      can tell that nobody touched it);
    - let git_config() callback to update the variable, taking care
      of freeing and strduping as needed.  Note that free(NULL) is
      kosher.
    - let parse_options() to further update the variable, taking
      care of freeing and strduping as needed.
    - finally, if the variable is still NULL, give it its default
      value.

so there is no room for the "if the variable has the value of the fallback default, do things differently" logic to be in the git_config() callback function.

> @@ -976,6 +982,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
>  	int submodule_progress;
>  
>  	struct strvec ref_prefixes = STRVEC_INIT;
Missing blank line here.
> +	remote_name = default_remote_name;

Isn't the reason why the git_config() callback we saw above has an unusual special case for default_remote_name because we have this assignment way too early? Would it make the control flow in a more natural way if we removed this, and then after parse_options() did the "-o" handling, add something like

	if (!remote_name)
		remote_name = xstrdup("origin");
instead?
Show 7 quoted lines
> @@ -1153,10 +1154,20 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
>  
>  	/*
>  	 * re-read config after init_db and write_config to pick up any config
> -	 * injected by --template and --config, respectively
> +	 * injected by --template and --config, respectively.
>  	 */

Squash this "oops, I forgot to finish the sentence" to the step the mistake was introduced, i.e. "use more conventional..."

Show 8 quoted lines
>  	git_config(git_clone_config, NULL);
>  
> +	/*
> +	 * apply the remote name provided by --origin only after this second
> +	 * call to git_config, to ensure it overrides all config-based values.
> +	 */
> +	if (option_origin)
> +		remote_name = option_origin;
And here would be where you'd fall back
	if (!remote_name)
		remote_name = xstrdup("origin");

Note that you'd need to dup the option_origin for consistency if you want to free() it at the end.

> +	if (!valid_remote_name(remote_name))
> +		die(_("'%s' is not a valid remote name"), remote_name);
> +
Previous: Sean Barag via GitGitGadgetNext: Sean Barag
Message 38 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.