From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: Jeff King <peff@peff.net>
Cc: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>,
Johannes Schindelin <Johannes.Schindelin@gmx.de>,
Andrei Rybak <rybak.a.v@gmail.com>
Subject: Re: [PATCH v2 3/3] bundle: remove "ref_list" in favor of string-list.c API
Date: Thu, 24 Jun 2021 21:31:02 +0200 [thread overview]
Message-ID: <87sg17ys82.fsf@evledraar.gmail.com> (raw)
In-Reply-To: <YNS8vzPc1KyRaSR5@coredump.intra.peff.net>
On Thu, Jun 24 2021, Jeff King wrote:
> On Mon, Jun 21, 2021 at 05:16:14PM +0200, Ævar Arnfjörð Bjarmason wrote:
>
>> 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:
>
>> @@ -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.
That sounds simpler indeed, thanks.
>> - 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.
Hah, that's actually the exact code I wrote to begin with, before
thinking "hrm, someone will probably say I should just use a ternary
here". I'll change it back :)
next prev parent reply other threads:[~2021-06-24 19:31 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-06-17 11:21 [PATCH 0/3] bundle.c: remove "ref_list" in favor of string-list.c API Ævar Arnfjörð Bjarmason
2021-06-17 11:21 ` [PATCH 1/3] bundle cmd: stop leaking memory from parse_options_cmd_bundle() Ævar Arnfjörð Bjarmason
2021-06-17 11:21 ` [PATCH 2/3] bundle.c: use a temporary variable for OIDs and names Ævar Arnfjörð Bjarmason
2021-06-17 11:21 ` [PATCH 3/3] bundle: remove "ref_list" in favor of string-list.c API Ævar Arnfjörð Bjarmason
2021-06-19 2:12 ` Andrei Rybak
2021-06-21 15:16 ` [PATCH v2 0/3] bundle.c: " Ævar Arnfjörð Bjarmason
2021-06-21 15:16 ` [PATCH v2 1/3] bundle cmd: stop leaking memory from parse_options_cmd_bundle() Ævar Arnfjörð Bjarmason
2021-06-24 16:54 ` Jeff King
2021-06-24 19:28 ` Ævar Arnfjörð Bjarmason
2021-06-21 15:16 ` [PATCH v2 2/3] bundle.c: use a temporary variable for OIDs and names Ævar Arnfjörð Bjarmason
2021-06-21 15:16 ` [PATCH v2 3/3] bundle: remove "ref_list" in favor of string-list.c API Ævar Arnfjörð Bjarmason
2021-06-24 17:11 ` Jeff King
2021-06-24 19:31 ` Ævar Arnfjörð Bjarmason [this message]
2021-06-29 1:02 ` Junio C Hamano
2021-06-24 17:14 ` [PATCH v2 0/3] bundle.c: " Jeff King
2021-06-30 14:06 ` [PATCH v3 0/3] Ævar Arnfjörð Bjarmason
2021-06-30 14:06 ` [PATCH v3 1/3] bundle cmd: stop leaking memory from parse_options_cmd_bundle() Ævar Arnfjörð Bjarmason
2021-06-30 17:26 ` Jeff King
2021-06-30 18:00 ` Ævar Arnfjörð Bjarmason
2021-07-01 15:41 ` Jeff King
2021-06-30 14:06 ` [PATCH v3 2/3] bundle.c: use a temporary variable for OIDs and names Ævar Arnfjörð Bjarmason
2021-06-30 14:06 ` [PATCH v3 3/3] bundle: remove "ref_list" in favor of string-list.c API Ævar Arnfjörð Bjarmason
2021-06-30 21:18 ` Junio C Hamano
2021-06-30 17:34 ` [PATCH v3 0/3] Jeff King
2021-06-30 17:45 ` Jeff King
2021-06-30 18:00 ` Ævar Arnfjörð Bjarmason
2021-07-01 10:53 ` Ævar Arnfjörð Bjarmason
2021-07-02 9:57 ` [PATCH v4 0/3] bundle.c: remove "ref_list" in favor of string-list.c API Ævar Arnfjörð Bjarmason
2021-07-02 9:57 ` [PATCH v4 1/3] bundle cmd: stop leaking memory from parse_options_cmd_bundle() Ævar Arnfjörð Bjarmason
2021-07-02 9:57 ` [PATCH v4 2/3] bundle.c: use a temporary variable for OIDs and names Ævar Arnfjörð Bjarmason
2021-07-02 9:57 ` [PATCH v4 3/3] bundle: remove "ref_list" in favor of string-list.c API Ævar Arnfjörð Bjarmason
2021-07-03 10:52 ` [PATCH v4 0/3] bundle.c: " Jeff King
2021-07-03 11:28 ` Ævar Arnfjörð Bjarmason
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87sg17ys82.fsf@evledraar.gmail.com \
--to=avarab@gmail.com \
--cc=Johannes.Schindelin@gmx.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=peff@peff.net \
--cc=rybak.a.v@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).