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

Re: [PATCH v2 3/3] bundle: remove "ref_list" in favor of string-list.c API

From
Jeff King <peff@peff.net>
Date
Jun 24, 2021, 17:11 UTC
Message-ID
<YNS8vzPc1KyRaSR5@coredump.intra.peff.net>
In-Reply-To
<patch-3.3-9d9cb5aaf9e-20210621T151357Z-avarab@gmail.com>
On Mon, Jun 21, 2021 at 05:16:14PM +0200, Ævar Arnfjörð Bjarmason wrote:
Show 9 quoted lines
> Move away from the "struct ref_list" in bundle.c in favor of the
> almost identical string-list.c API.
> 
> That API fits this use-case perfectly, but did not exist in its
> current form when this code was added in 2e0afafebd (Add git-bundle:
> move objects and references by archive, 2007-02-22), with hindsight we
> could have used the path-list API, which later got renamed to
> string-list. See 8fd2cb4069 (Extract helper bits from
> c-merge-recursive work, 2006-07-25)

I think this is a good direction, and I didn't see any errors in the code. It's slightly sad that we end up with more lines than we started with, but I think that's mostly because you're actually freeing the memory now.

Two small nitpicks:
Show 16 quoted lines
> @@ -103,19 +94,22 @@ static int parse_bundle_header(int fd, struct bundle_header *header,
>  		 * Prerequisites have object name that is optionally
>  		 * followed by SP and subject line.
>  		 */
> -		if (parse_oid_hex_algop(buf.buf, &oid, &p, header->hash_algo) ||
> +		oid = xmalloc(sizeof(struct object_id));
> +		if (parse_oid_hex_algop(buf.buf, oid, &p, header->hash_algo) ||
>  		    (*p && !isspace(*p)) ||
>  		    (!is_prereq && !*p)) {
>  			if (report_path)
>  				error(_("unrecognized header: %s%s (%d)"),
>  				      (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
>  			status = -1;
> +			free(oid);
>  			break;
>  		} else {

This would be slightly simpler if you kept a local "struct object_id", and then called:

  string_list_append(list, string)->util = oiddup(&oid);

later when you know you want to save it. And then you don't have to worry about the extra cleanup here. That does require an extra oidcpy() under the hood, but I suspect that is lost in the noise.

I'm OK with it either way.
Show 9 quoted lines
> -			if (is_prereq)
> -				add_to_ref_list(&oid, "", &header->prerequisites);
> -			else
> -				add_to_ref_list(&oid, p + 1, &header->references);
> +			const char *string = is_prereq ? "" : p + 1;
> +			struct string_list *list = is_prereq
> +				? &header->prerequisites
> +				: &header->references;
> +			string_list_append(list, string)->util = oid;

I'm usually a big fan of the ternary operator, and using variable indirection to make it clear that we always call a function. But here I think it makes things more confusing. The two sides of the if/else are sufficiently simple that it's easy to see they both make the same function call. And because there are two variables, we check is_prereq twice, making it much harder to see the two cases.

I.e., I think:
  if (is_prereq)
          string_list_append(&header->prerequisites, "")->util = oid;
  else
          string_list_append(&header->references, p + 1)->util = oid;
is much more obvious.
-Peff
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 12 of 33 in “bundle.c: remove "ref_list" in favor of string-list.c API”
  1. 0/3 bundle.c: remove "ref_list" in favor of string-list.c APIÆvar Arnfjörð Bjarmason, Jun 17, 2021
  2. 1/3 bundle cmd: stop leaking memory from parse_options_cmd_bundle()Ævar Arnfjörð Bjarmason, Jun 17, 2021
  3. 3/3 bundle: remove "ref_list" in favor of string-list.c APIÆvar Arnfjörð Bjarmason, Jun 17, 2021
  4. Andrei RybakJun 19, 2021
  5. 2/3 bundle.c: use a temporary variable for OIDs and namesÆvar Arnfjörð Bjarmason, Jun 17, 2021
  6. 0/3 bundle.c: remove "ref_list" in favor of string-list.c APIÆvar Arnfjörð Bjarmason, Jun 21, 2021
  7. 2/3 bundle.c: use a temporary variable for OIDs and namesÆvar Arnfjörð Bjarmason, Jun 21, 2021
  8. 1/3 bundle cmd: stop leaking memory from parse_options_cmd_bundle()Ævar Arnfjörð Bjarmason, Jun 21, 2021
  9. Jeff KingJun 24, 2021
  10. Ævar Arnfjörð BjarmasonJun 24, 2021
  11. 3/3 bundle: remove "ref_list" in favor of string-list.c APIÆvar Arnfjörð Bjarmason, Jun 21, 2021
  12. Jeff KingJun 24, 2021
  13. Ævar Arnfjörð BjarmasonJun 24, 2021
  14. Junio C HamanoJun 29, 2021
  15. Jeff KingJun 24, 2021
  16. 0/3 Ævar Arnfjörð Bjarmason, Jun 30, 2021
  17. 1/3 bundle cmd: stop leaking memory from parse_options_cmd_bundle()Ævar Arnfjörð Bjarmason, Jun 30, 2021
  18. Jeff KingJun 30, 2021
  19. Ævar Arnfjörð BjarmasonJun 30, 2021
  20. Jeff KingJul 1, 2021
  21. 2/3 bundle.c: use a temporary variable for OIDs and namesÆvar Arnfjörð Bjarmason, Jun 30, 2021
  22. 3/3 bundle: remove "ref_list" in favor of string-list.c APIÆvar Arnfjörð Bjarmason, Jun 30, 2021
  23. Junio C HamanoJun 30, 2021
  24. Jeff KingJun 30, 2021
  25. Jeff KingJun 30, 2021
  26. Ævar Arnfjörð BjarmasonJun 30, 2021
  27. Ævar Arnfjörð BjarmasonJul 1, 2021
  28. 0/3 bundle.c: remove "ref_list" in favor of string-list.c APIÆvar Arnfjörð Bjarmason, Jul 2, 2021
  29. 1/3 bundle cmd: stop leaking memory from parse_options_cmd_bundle()Ævar Arnfjörð Bjarmason, Jul 2, 2021
  30. 2/3 bundle.c: use a temporary variable for OIDs and namesÆvar Arnfjörð Bjarmason, Jul 2, 2021
  31. 3/3 bundle: remove "ref_list" in favor of string-list.c APIÆvar Arnfjörð Bjarmason, Jul 2, 2021
  32. Jeff KingJul 3, 2021
  33. Ævar Arnfjörð BjarmasonJul 3, 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.