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

Re: [GSoC] [PATCH v2 1/2] submodule--helper: introduce add-clone subcommand

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 9, 2021, 04:24 UTC
Message-ID
<xmqqh7i7ll6h.fsf@gitster.g>
In-Reply-To
<20210608095655.47324-2-raykar.ath@gmail.com>
Just a bit of random comments, leaving the full review to mentors.
Show 27 quoted lines
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index d55f6262e9..c9cb535312 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> @@ -2745,6 +2745,204 @@ static int module_set_branch(int argc, const char **argv, const char *prefix)
>  	return !!ret;
>  }
>  
> +struct add_data {
> +	const char *prefix;
> +	const char *branch;
> +	const char *reference_path;
> +	const char *sm_path;
> +	const char *sm_name;
> +	const char *repo;
> +	const char *realrepo;
> +	int depth;
> +	unsigned int force: 1;
> +	unsigned int quiet: 1;
> +	unsigned int progress: 1;
> +	unsigned int dissociate: 1;
> +};
> +#define ADD_DATA_INIT { .depth = -1 }
> +
> +static char *parse_token(char **begin, const char *end, int *tok_len)
> +{
> +	char *tok_start, *pos = *begin;

Make it a habit to have a blank line between the initial block of declarations and the first statement.

Show 11 quoted lines
> +	while (pos != end && (*pos != ' ' && *pos != '\t' && *pos != '\n'))
> +		pos++;
> +	tok_start = *begin;
> +	*tok_len = pos - *begin;
> +	*begin = pos + 1;
> +	return tok_start;
> +}
> +static char *get_next_line(char *const begin, const char *const end)
> +{
> +	char *pos = begin;
> +	while (pos != end && *pos++ != '\n');
Write an empty loop on two lines, like this:
	while (... condition ...)
		; /* keep scanning */

If there is a NUL byte between begin and end, this keeps going and the resulting string will contain one. Is that a problem?

> +	return pos;
> +}

In general, this project is mature enough that we should question ourselves if there is already a suitable line parser we can reuse when tempted to write another one.

Show 15 quoted lines
> +static void show_fetch_remotes(FILE *output, const char *sm_name, const char *git_dir_path)
> +{
> +	struct child_process cp_remote = CHILD_PROCESS_INIT;
> +	struct strbuf sb_remote_out = STRBUF_INIT;
> +
> +	cp_remote.git_cmd = 1;
> +	strvec_pushf(&cp_remote.env_array,
> +		     "GIT_DIR=%s", git_dir_path);
> +	strvec_push(&cp_remote.env_array, "GIT_WORK_TREE=.");
> +	strvec_pushl(&cp_remote.args, "remote", "-v", NULL);
> +	if (!capture_command(&cp_remote, &sb_remote_out, 0)) {
> +		char *line;
> +		char *begin = sb_remote_out.buf;
> +		char *end = sb_remote_out.buf + sb_remote_out.len;
> +		while (begin != end && (line = get_next_line(begin, end))) {

OK, so this tries to parse output from "git remote -v", so NUL will not be an issue at all. We will get a string that is NUL terminated and has zero or more lines, terminated with LFs.

If that is the case, I think it is far easier to read without a custom get-next-line wrapper, e.g.

	for (this_line = begin;
	     *this_line;
	     this_line = next_line) {
		next_line = strchrnul(this_line, '\n');
		... process bytes between this_line..next_line ...
	}                
> +			int namelen = 0, urllen = 0, taillen = 0;
> +			char *name = parse_token(&begin, line, &namelen);

Similarly, consider if strcspn() is useful in implementing parse_token(). See how existing code uses the standard system function with

	$ git grep strcspn \*.c
> +			char *url = parse_token(&begin, line, &urllen);
> +			char *tail = parse_token(&begin, line, &taillen);
> +			if (!memcmp(tail, "(fetch)", 7))

At this point do we know there are enough number of bytes after tail[0] to allow us to do this comparison safely? Otherwise,

			if (starts_with(tail, "(fetch)")
may be preferrable.
Previous: Atharva RaykarNext: Atharva Raykar
Message 14 of 40 in “submodule--helper: introduce subcommands for sh to C conversion”
  1. Atharva RaykarJun 5, 2021
  2. [GSoC] [PATCH 2/2] submodule--helper: introduce add-config subcommandAtharva Raykar, Jun 5, 2021
  3. Christian CouderJun 7, 2021
  4. Atharva RaykarJun 7, 2021
  5. [GSoC] [PATCH 1/2] submodule--helper: introduce add-clone subcommandAtharva Raykar, Jun 5, 2021
  6. Bagas SanjayaJun 6, 2021
  7. Christian CouderJun 6, 2021
  8. [GSoC] [PATCH v2 0/2] submodule--helper: introduce subcommands for sh to C conversionAtharva Raykar, Jun 8, 2021
  9. [GSoC] [PATCH v2 1/2] submodule--helper: introduce add-clone subcommandAtharva Raykar, Jun 8, 2021
  10. Đoàn Trần Công DanhJun 8, 2021
  11. Atharva RaykarJun 9, 2021
  12. Đoàn Trần Công DanhJun 9, 2021
  13. Atharva RaykarJun 9, 2021
  14. Junio C HamanoJun 9, 2021
  15. Atharva RaykarJun 9, 2021
  16. [GSoC] [PATCH v2 2/2] submodule--helper: introduce add-config subcommandAtharva Raykar, Jun 8, 2021
  17. [GSoC] [PATCH v3 0/2] submodule--helper: introduce subcommands for sh to C conversionAtharva Raykar, Jun 10, 2021
  18. 1/2 submodule--helper: introduce add-clone subcommandAtharva Raykar, Jun 10, 2021
  19. Junio C HamanoJun 11, 2021
  20. Atharva RaykarJun 11, 2021
  21. Junio C HamanoJun 11, 2021
  22. 2/2 submodule--helper: introduce add-config subcommandAtharva Raykar, Jun 10, 2021
  23. [GSoC] [PATCH v4 0/3] submodule--helper: introduce subcommands for sh to C conversionAtharva Raykar, Jun 14, 2021
  24. 1/3 submodule--helper: refactor module_clone()Atharva Raykar, Jun 14, 2021
  25. Junio C HamanoJun 15, 2021
  26. Atharva RaykarJun 15, 2021
  27. 3/3 submodule--helper: introduce add-config subcommandAtharva Raykar, Jun 14, 2021
  28. Rafael SilvaJun 14, 2021
  29. Eric SunshineJun 14, 2021
  30. Rafael SilvaJun 15, 2021
  31. Atharva RaykarJun 15, 2021
  32. 2/3 submodule--helper: introduce add-clone subcommandAtharva Raykar, Jun 14, 2021
  33. [GSoC] [PATCH v5 0/3] submodule--helper: introduce subcommands for sh to C conversionAtharva Raykar, Jun 15, 2021
  34. 1/3 submodule--helper: refactor module_clone()Atharva Raykar, Jun 15, 2021
  35. 2/3 submodule--helper: introduce add-clone subcommandAtharva Raykar, Jun 15, 2021
  36. 3/3 submodule--helper: introduce add-config subcommandAtharva Raykar, Jun 15, 2021
  37. [GSoC] [PATCH v6 0/3] submodule--helper: introduce subcommands for sh to C conversionAtharva Raykar, Jun 15, 2021
  38. 1/3 submodule--helper: refactor module_clone()Atharva Raykar, Jun 15, 2021
  39. 2/3 submodule--helper: introduce add-clone subcommandAtharva Raykar, Jun 15, 2021
  40. 3/3 submodule--helper: introduce add-config subcommandAtharva Raykar, Jun 15, 2021

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.