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

Re: [PATCH 04/10] evolve: add support for parsing metacommits

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Sep 26, 2022, 13:27 UTC
Message-ID
<e7278794-428d-4aff-e91b-d2e6527f142d@gmail.com>
In-Reply-To
<2e9a4a9bd819785404e8a5343385f4fb2bc06109.1663959325.git.gitgitgadget@gmail.com>
Hi Chris
On 23/09/2022 19:55, Stefan Xenos via GitGitGadget wrote:
Show 26 quoted lines
> From: Stefan Xenos <sxenos@google.com>
> 
> This patch adds the get_metacommit_content method, which can classify
> commits as either metacommits or normal commits, determine whether they
> are abandoned, and extract the content commit's object id from the
> metacommit.
> 
> Signed-off-by: Stefan Xenos <sxenos@google.com>
> Signed-off-by: Chris Poucet <poucet@google.com>
> ---
>   Makefile            |   1 +
>   metacommit-parser.c | 110 ++++++++++++++++++++++++++++++++++++++++++++
>   metacommit-parser.h |  19 ++++++++
>   3 files changed, 130 insertions(+)
>   create mode 100644 metacommit-parser.c
>   create mode 100644 metacommit-parser.h
> 
> diff --git a/Makefile b/Makefile
> index cac3452edb9..b2bcc00c289 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -999,6 +999,7 @@ LIB_OBJS += merge-ort.o
>   LIB_OBJS += merge-ort-wrappers.o
>   LIB_OBJS += merge-recursive.o
>   LIB_OBJS += merge.o
> +LIB_OBJS += metacommit-parser.o
There seems to be a problem with the indent here
>   LIB_OBJS += midx.o
>   LIB_OBJS += name-hash.o
>   LIB_OBJS += negotiator/default.o
 > diff --git a/metacommit-parser.h b/metacommit-parser.h
 > new file mode 100644
 > index 00000000000..1c74bd6d699
 > --- /dev/null
 > +++ b/metacommit-parser.h
 > @@ -0,0 +1,19 @@
 > +#ifndef METACOMMIT_PARSER_H
 > +#define METACOMMIT_PARSER_H
 > +
 > +#include "commit.h"
 > +#include "hash.h"
 > +
 > +/* Indicates a normal commit (non-metacommit) */
 > +#define METACOMMIT_TYPE_NONE 0
 > +/* Indicates a metacommit with normal content (non-abandoned) */
 > +#define METACOMMIT_TYPE_NORMAL 1
 > +/* Indicates a metacommit with abandoned content */
 > +#define METACOMMIT_TYPE_ABANDONED 2

Is it possible to define these as an enum? It would make the signature of get_meta_commit_content() nicer.

 > +struct commit;
What's this for? We're including commit.h above.
 > +extern int get_metacommit_content(
 > +	struct commit *commit, struct object_id *content);
Show 14 quoted lines
> diff --git a/metacommit-parser.c b/metacommit-parser.c
> new file mode 100644
> index 00000000000..70c1428bfc6
> --- /dev/null
> +++ b/metacommit-parser.c
> @@ -0,0 +1,110 @@
> +#include "cache.h"
> +#include "metacommit-parser.h"
> +#include "commit.h"
> +
> +/*
> + * Search the commit buffer for a line starting with the given key. Unlike
> + * find_commit_header, this also searches the commit message body.
> + */

There is no explanation in the code or commit message as to why this function is needed. The documentation added in the first commit says that "parent-type" header is a commit header. I think the answer is that this series does not implement that header but uses the commit message instead. That's perfectly fine for a proof of concept but it is precisely the sort of detail that should be described it the commit message and probably flagged up in the cover letter.

Show 30 quoted lines
> +static const char *find_key(const char *msg, const char *key, size_t *out_len)
> +{
> +	int key_len = strlen(key);
> +	const char *line = msg;
> +
> +	while (line) {
> +		const char *eol = strchrnul(line, '\n');
> +
> +		if (eol - line > key_len && !memcmp(line, key, key_len) &&
> +		    line[key_len] == ' ') {
> +			*out_len = eol - line - key_len - 1;
> +			return line + key_len + 1;
> +		}
> +		line = *eol ? eol + 1 : NULL;
> +	}
> +	return NULL;
> +}
> +
> +static struct commit *get_commit_by_index(struct commit_list *to_search, int index)
> +{
> +	while (to_search && index) {
> +		to_search = to_search->next;
> +		index--;
> +	}
> +
> +	if (!to_search)
> +		return NULL;
> +
> +	return to_search->item;
> +}

This function is a useful utility for struct commit_list and should live in commit.c. It could be used to simplify object-name.c:get_parent() for example.

Show 5 quoted lines
> +/*
> + * Writes the index of the content parent to "result". Returns the metacommit
> + * type. See the METACOMMIT_TYPE_* constants.
> + */
> +static int index_of_content_commit(const char *buffer, int *result)

I found the signature confusing as it is returning an int but that is not the index. Switching to an enum for the metacommit types would clarify that.

Show 19 quoted lines
> +{
> +	int index = 0;
> +	int ret = METACOMMIT_TYPE_NONE;
> +	size_t parent_types_size;
> +	const char *parent_types = find_key(buffer, "parent-type",
> +		&parent_types_size);
> +	const char *end;
> +	const char *enum_start = parent_types;
> +	int enum_length = 0;
> +
> +	if (!parent_types)
> +		return METACOMMIT_TYPE_NONE;
> +
> +	end = &parent_types[parent_types_size];
> +
> +	while (1) {
> +		char next = *parent_types;
> +		if (next == ' ' || parent_types >= end) {
> +			if (enum_length == 1) {

if enum_length != 1 then there is an error in the parent-type header and we should probably bail out.

> +				char first_char_in_enum = *enum_start;

It's not just the first character, it's the only character, do we really need such a long variable name? (how about just calling it "type")

I'll try and take at look at the next couple of patches later in the week.
Best Wishes
Phillip
Previous: Stefan Xenos via GitGitGadgetNext: Chris P
Message 8 of 66 in “Add the Git Change command”
  1. 00/10 Add the Git Change commandChristophe Poucet via GitGitGadget, Sep 23, 2022
  2. 02/10 sha1-array: implement oid_array_readonly_containsChris Poucet via GitGitGadget, Sep 23, 2022
  3. Phillip WoodSep 26, 2022
  4. 03/10 ref-filter: add the metas namespace to ref-filterChris Poucet via GitGitGadget, Sep 23, 2022
  5. Phillip WoodSep 26, 2022
  6. Chris POct 4, 2022
  7. 04/10 evolve: add support for parsing metacommitsStefan Xenos via GitGitGadget, Sep 23, 2022
  8. Phillip WoodSep 26, 2022
  9. Chris POct 4, 2022
  10. Phillip WoodOct 4, 2022
  11. 01/10 technical doc: add a design doc for the evolve commandStefan Xenos via GitGitGadget, Sep 23, 2022
  12. Jerry ZhangSep 23, 2022
  13. Junio C HamanoSep 28, 2022
  14. Junio C HamanoSep 28, 2022
  15. Phillip WoodSep 29, 2022
  16. Jonathan TanSep 29, 2022
  17. 05/10 evolve: add the change-table structureStefan Xenos via GitGitGadget, Sep 23, 2022
  18. Phillip WoodSep 27, 2022
  19. Ævar Arnfjörð BjarmasonSep 27, 2022
  20. Phillip WoodSep 27, 2022
  21. Ævar Arnfjörð BjarmasonSep 27, 2022
  22. Phillip WoodSep 28, 2022
  23. Ævar Arnfjörð BjarmasonSep 28, 2022
  24. Junio C HamanoSep 28, 2022
  25. Phillip WoodSep 27, 2022
  26. Chris POct 4, 2022
  27. 06/10 evolve: add support for writing metacommitsStefan Xenos via GitGitGadget, Sep 23, 2022
  28. Phillip WoodSep 28, 2022
  29. Chris POct 5, 2022
  30. Phillip WoodOct 5, 2022
  31. 10/10 evolve: add documentation for `git change`Chris Poucet via GitGitGadget, Sep 23, 2022
  32. Phillip WoodSep 25, 2022
  33. 07/10 evolve: implement the git change commandStefan Xenos via GitGitGadget, Sep 23, 2022
  34. Phillip WoodSep 25, 2022
  35. Ævar Arnfjörð BjarmasonSep 26, 2022
  36. Ævar Arnfjörð BjarmasonSep 26, 2022
  37. Chris POct 5, 2022
  38. 09/10 evolve: add delete commandChris Poucet via GitGitGadget, Sep 23, 2022
  39. Ævar Arnfjörð BjarmasonSep 26, 2022
  40. Chris PoucetSep 26, 2022
  41. 08/10 evolve: add the git change list commandStefan Xenos via GitGitGadget, Sep 23, 2022
  42. Phillip WoodSep 25, 2022
  43. Chris POct 4, 2022
  44. Phillip WoodOct 4, 2022
  45. Chris POct 4, 2022
  46. Chris POct 4, 2022
  47. Phillip WoodOct 4, 2022
  48. Phillip WoodOct 4, 2022
  49. 00/10 RFC: Git Evolve / ChangeChristophe Poucet via GitGitGadget, Oct 5, 2022
  50. 03/10 ref-filter: add the metas namespace to ref-filterChris Poucet via GitGitGadget, Oct 5, 2022
  51. 02/10 sha1-array: implement oid_array_readonly_containsChris Poucet via GitGitGadget, Oct 5, 2022
  52. 05/10 evolve: add the change-table structureStefan Xenos via GitGitGadget, Oct 5, 2022
  53. 04/10 evolve: add support for parsing metacommitsStefan Xenos via GitGitGadget, Oct 5, 2022
  54. 06/10 evolve: add support for writing metacommitsStefan Xenos via GitGitGadget, Oct 5, 2022
  55. 01/10 technical doc: add a design doc for the evolve commandStefan Xenos via GitGitGadget, Oct 5, 2022
  56. Chris PoucetOct 5, 2022
  57. Glen ChooOct 6, 2022
  58. Victoria DyeOct 10, 2022
  59. Phillip WoodOct 11, 2022
  60. Victoria DyeOct 11, 2022
  61. Phillip WoodOct 12, 2022
  62. 07/10 evolve: implement the git change commandStefan Xenos via GitGitGadget, Oct 5, 2022
  63. 09/10 evolve: add documentation for `git change`Chris Poucet via GitGitGadget, Oct 5, 2022
  64. 08/10 evolve: add delete commandChris Poucet via GitGitGadget, Oct 5, 2022
  65. 10/10 evolve: add tests for the git-change commandChris Poucet via GitGitGadget, Oct 5, 2022
  66. Phillip WoodOct 10, 2022

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.