diff mbox series

[08/11] midx-write.c: avoid directly managed temporary strbuf

Message ID 8e32755c492d20eec02c81351d249ce34cc6d7b9.1711387439.git.me@ttaylorr.com (mailing list archive)
State Superseded
Headers show
Series midx: split MIDX writing routines into midx-write.c, cleanup | expand

Commit Message

Taylor Blau March 25, 2024, 5:24 p.m. UTC
In midx-write.c::midx_repack(), we construct the command-line arguments
for a pack-objects invocation which will combine objects from the packs
below our `--batch-size` option.

To construct the base name of the output pack, we use a temporary
strbuf, and then push the result of that onto the strvec which holds the
command-line arguments, after which point we release the strbuf.

We could replace this by doing something like:

    struct strbuf buf = STRBUF_INIT;
    strbuf_addf(&buf, "%s/pack/pack", object_dir);
    strvec_push_nodup(&cmd.args, strbuf_detach(&buf));

(combining the two separate `strbuf_addstr()` calls into a single
`strbuf_addf()`). But that is more or less an open-coded version of
strvec_pushf(), which we could use directly instead.

(Note that at the time this code was written back in ce1e4a105b4 (midx:
implement midx_repack(), 2019-06-10), strvec did not yet exist, so the
above example would have replaced the last line with:

    argv_array_push_nodup(&cmd.args, strbuf_detach(&buf));

, but the code is otherwise unchanged).

Avoid directly managing the temporary strbuf used to construct the
base name for pack-object's command-line arguments, and instead use the
purpose-built `strvec_pushf()` instead.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 midx-write.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

Comments

Junio C Hamano March 25, 2024, 8:33 p.m. UTC | #1
Taylor Blau <me@ttaylorr.com> writes:

> In midx-write.c::midx_repack(), we construct the command-line arguments
> for a pack-objects invocation which will combine objects from the packs
> below our `--batch-size` option.
>
> To construct the base name of the output pack, we use a temporary
> strbuf, and then push the result of that onto the strvec which holds the
> command-line arguments, after which point we release the strbuf.
>
> We could replace this by doing something like:
>
>     struct strbuf buf = STRBUF_INIT;
>     strbuf_addf(&buf, "%s/pack/pack", object_dir);
>     strvec_push_nodup(&cmd.args, strbuf_detach(&buf));

Hmph, I thought I saw another patch recently that uses
strvec_pushf() to simplify such a sequence.  Does the technique
apply here as well?

Ah, yes, exactly.  See <9483038c-9529-4243-9b9a-97254fac29c1@web.de>

> @@ -1473,10 +1472,6 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
>  
>  	strvec_push(&cmd.args, "pack-objects");
>  
> -	strbuf_addstr(&base_name, object_dir);
> -	strbuf_addstr(&base_name, "/pack/pack");
> -	strvec_push(&cmd.args, base_name.buf);
> -
>  	if (delta_base_offset)
>  		strvec_push(&cmd.args, "--delta-base-offset");
>  	if (use_delta_islands)
> @@ -1487,7 +1482,7 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
>  	else
>  		strvec_push(&cmd.args, "-q");
>  
> -	strbuf_release(&base_name);
> +	strvec_pushf(&cmd.args, "%s/pack/pack", object_dir);
>  
>  	cmd.git_cmd = 1;
>  	cmd.in = cmd.out = -1;
Taylor Blau March 25, 2024, 10:11 p.m. UTC | #2
On Mon, Mar 25, 2024 at 01:33:11PM -0700, Junio C Hamano wrote:
> Taylor Blau <me@ttaylorr.com> writes:
>
> > In midx-write.c::midx_repack(), we construct the command-line arguments
> > for a pack-objects invocation which will combine objects from the packs
> > below our `--batch-size` option.
> >
> > To construct the base name of the output pack, we use a temporary
> > strbuf, and then push the result of that onto the strvec which holds the
> > command-line arguments, after which point we release the strbuf.
> >
> > We could replace this by doing something like:
> >
> >     struct strbuf buf = STRBUF_INIT;
> >     strbuf_addf(&buf, "%s/pack/pack", object_dir);
> >     strvec_push_nodup(&cmd.args, strbuf_detach(&buf));
>
> Hmph, I thought I saw another patch recently that uses
> strvec_pushf() to simplify such a sequence.  Does the technique
> apply here as well?
>
> Ah, yes, exactly.  See <9483038c-9529-4243-9b9a-97254fac29c1@web.de>

Hah. I wrote this patch on Saturday but didn't read the list before
sending it on Monday. Serves me right ;-). Feel free to drop this one,
or I can reroll based on René's patch.

Either is fine, just let me know :-).

Thanks,
Taylor
diff mbox series

Patch

diff --git a/midx-write.c b/midx-write.c
index c812156cbd..89e325d08e 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -1446,7 +1446,6 @@  int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
 	unsigned char *include_pack;
 	struct child_process cmd = CHILD_PROCESS_INIT;
 	FILE *cmd_in;
-	struct strbuf base_name = STRBUF_INIT;
 	struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
 
 	/*
@@ -1473,10 +1472,6 @@  int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
 
 	strvec_push(&cmd.args, "pack-objects");
 
-	strbuf_addstr(&base_name, object_dir);
-	strbuf_addstr(&base_name, "/pack/pack");
-	strvec_push(&cmd.args, base_name.buf);
-
 	if (delta_base_offset)
 		strvec_push(&cmd.args, "--delta-base-offset");
 	if (use_delta_islands)
@@ -1487,7 +1482,7 @@  int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
 	else
 		strvec_push(&cmd.args, "-q");
 
-	strbuf_release(&base_name);
+	strvec_pushf(&cmd.args, "%s/pack/pack", object_dir);
 
 	cmd.git_cmd = 1;
 	cmd.in = cmd.out = -1;