]> git.ipfire.org Git - thirdparty/git.git/commitdiff
use strvec_pushv() to add another strvec
authorJunio C Hamano <gitster@pobox.com>
Tue, 24 Mar 2026 19:26:58 +0000 (12:26 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 24 Mar 2026 19:26:58 +0000 (12:26 -0700)
Add and apply a semantic patch that simplifies the code by letting
strvec_pushv() append the items of a second strvec instead of pushing
them one by one.

Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rebase.c
fetch-pack.c
git.c
submodule.c
tools/coccinelle/strvec.cocci [new file with mode: 0644]

index a1c7d78196750e96aaae3bb46775a0694ca3fd18..fa4f5d9306b856336970bf77b037cd5870aae4de 100644 (file)
@@ -182,8 +182,7 @@ static struct replay_opts get_replay_opts(const struct rebase_options *opts)
 
        replay.signoff = opts->signoff;
 
-       for (size_t i = 0; i < opts->trailer_args.nr; i++)
-               strvec_push(&replay.trailer_args, opts->trailer_args.v[i]);
+       strvec_pushv(&replay.trailer_args, opts->trailer_args.v);
 
        replay.allow_ff = !(opts->flags & REBASE_FORCE);
        if (opts->allow_rerere_autoupdate)
index 6ecd468ef766a8923af879f336f282baf65a5d86..a32224ed02659233419796679915620aa2dd2f2c 100644 (file)
@@ -1024,12 +1024,8 @@ static int get_pack(struct fetch_pack_args *args,
                                     fsck_msg_types.buf);
        }
 
-       if (index_pack_args) {
-               int i;
-
-               for (i = 0; i < cmd.args.nr; i++)
-                       strvec_push(index_pack_args, cmd.args.v[i]);
-       }
+       if (index_pack_args)
+               strvec_pushv(index_pack_args, cmd.args.v);
 
        sigchain_push(SIGPIPE, SIG_IGN);
 
diff --git a/git.c b/git.c
index 2b212e6675d9260eab0055dfd85cfc5e212c93aa..5a40eab8a26a66276444586b5898409f718515b3 100644 (file)
--- a/git.c
+++ b/git.c
@@ -877,8 +877,7 @@ static int run_argv(struct strvec *args)
                        commit_pager_choice();
 
                        strvec_push(&cmd.args, "git");
-                       for (size_t i = 0; i < args->nr; i++)
-                               strvec_push(&cmd.args, args->v[i]);
+                       strvec_pushv(&cmd.args, args->v);
 
                        trace_argv_printf(cmd.args.v, "trace: exec:");
 
index cd879a5cfe73ec886e1a37428085a35346f7770e..4c8c674aa4b0477082567d7dc2cd02364838253c 100644 (file)
@@ -1815,7 +1815,6 @@ int fetch_submodules(struct repository *r,
                     int default_option,
                     int quiet, int max_parallel_jobs)
 {
-       int i;
        struct submodule_parallel_fetch spf = SPF_INIT;
        const struct run_process_parallel_opts opts = {
                .tr2_category = "submodule",
@@ -1842,8 +1841,7 @@ int fetch_submodules(struct repository *r,
                die(_("index file corrupt"));
 
        strvec_push(&spf.args, "fetch");
-       for (i = 0; i < options->nr; i++)
-               strvec_push(&spf.args, options->v[i]);
+       strvec_pushv(&spf.args, options->v);
        strvec_push(&spf.args, "--recurse-submodules-default");
        /* default value, "--submodule-prefix" and its value are added later */
 
diff --git a/tools/coccinelle/strvec.cocci b/tools/coccinelle/strvec.cocci
new file mode 100644 (file)
index 0000000..64edb09
--- /dev/null
@@ -0,0 +1,46 @@
+@@
+type T;
+identifier i;
+expression dst;
+struct strvec *src_ptr;
+struct strvec src_arr;
+@@
+(
+- for (T i = 0; i < src_ptr->nr; i++) { strvec_push(dst, src_ptr->v[i]); }
++ strvec_pushv(dst, src_ptr->v);
+|
+- for (T i = 0; i < src_arr.nr; i++) { strvec_push(dst, src_arr.v[i]); }
++ strvec_pushv(dst, src_arr.v);
+)
+
+@ separate_loop_index @
+type T;
+identifier i;
+expression dst;
+struct strvec *src_ptr;
+struct strvec src_arr;
+@@
+  T i;
+  ...
+(
+- for (i = 0; i < src_ptr->nr; i++) { strvec_push(dst, src_ptr->v[i]); }
++ strvec_pushv(dst, src_ptr->v);
+|
+- for (i = 0; i < src_arr.nr; i++) { strvec_push(dst, src_arr.v[i]); }
++ strvec_pushv(dst, src_arr.v);
+)
+
+@ unused_loop_index extends separate_loop_index @
+@@
+  {
+  ...
+- T i;
+  ... when != i
+  }
+
+@ depends on unused_loop_index @
+@@
+  if (...)
+- {
+  strvec_pushv(...);
+- }