diff mbox series

[v2,1/7] remote-curl: pass "struct strvec *" instead of int/char ** pair

Message ID patch-v2-1.7-2ef48d734e8-20210912T001420Z-avarab@gmail.com (mailing list archive)
State New, archived
Headers show
Series strvec: use size_t to store nr and alloc | expand

Commit Message

Ævar Arnfjörð Bjarmason Sept. 12, 2021, 12:15 a.m. UTC
We'll be changing the "int nr" in the "struct strvec" to "int size_t"
soon, i.e. from signed to unsigned. It will then make sense to use the
"size_t" type for things that mirror that "nr" member.

This sets up that change in remote-curl.c, we'll change the "int i"
here to a "size_t i" in a later commit (let's also split and "err" up
to make that change smaller).

In this case the push() function can just pass the "struct strvec" it
has down to push_dav() and push_git(), in addition make use of
strvec_pushv() in push_dav() instead of looping over the the specs
ourselves. So we can get rid of much of the need for tracking the
"nr", which in the case of push_dav() was already a "size_t".

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 remote-curl.c | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

Comments

Carlo Marcelo Arenas Belón Sept. 12, 2021, 12:36 a.m. UTC | #1
On Sat, Sep 11, 2021 at 5:16 PM Ævar Arnfjörð Bjarmason
<avarab@gmail.com> wrote:
>
> We'll be changing the "int nr" in the "struct strvec" to "int size_t"

Did you mean "size_t nr"?

> -static int push_git(struct discovery *heads, int nr_spec, const char **specs)
> +static int push_git(struct discovery *heads, struct strvec *specs)
>  {
>         struct rpc_state rpc;
> -       int i, err;
> +       int i;

you are already splitting them, why not then change 'i' also to be a size_t?

Carlo
Ævar Arnfjörð Bjarmason Sept. 13, 2021, 3:56 a.m. UTC | #2
On Sat, Sep 11 2021, Carlo Arenas wrote:

> On Sat, Sep 11, 2021 at 5:16 PM Ævar Arnfjörð Bjarmason
> <avarab@gmail.com> wrote:
>>
>> We'll be changing the "int nr" in the "struct strvec" to "int size_t"
>
> Did you mean "size_t nr"?

Yes, will fix.

>> -static int push_git(struct discovery *heads, int nr_spec, const char **specs)
>> +static int push_git(struct discovery *heads, struct strvec *specs)
>>  {
>>         struct rpc_state rpc;
>> -       int i, err;
>> +       int i;
>
> you are already splitting them, why not then change 'i' also to be a size_t?

The conversion to size_t comes later in this series, this is setting
that diff up to be smaller. I.e. we'll then to s/int i/size_t /g.
diff mbox series

Patch

diff --git a/remote-curl.c b/remote-curl.c
index 598cff7cde6..1f177e86f11 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -1266,10 +1266,9 @@  static void parse_fetch(struct strbuf *buf)
 	strbuf_reset(buf);
 }
 
-static int push_dav(int nr_spec, const char **specs)
+static int push_dav(struct strvec *specs)
 {
 	struct child_process child = CHILD_PROCESS_INIT;
-	size_t i;
 
 	child.git_cmd = 1;
 	strvec_push(&child.args, "http-push");
@@ -1279,18 +1278,18 @@  static int push_dav(int nr_spec, const char **specs)
 	if (options.verbosity > 1)
 		strvec_push(&child.args, "--verbose");
 	strvec_push(&child.args, url.buf);
-	for (i = 0; i < nr_spec; i++)
-		strvec_push(&child.args, specs[i]);
+	strvec_pushv(&child.args, specs->v);
 
 	if (run_command(&child))
 		die(_("git-http-push failed"));
 	return 0;
 }
 
-static int push_git(struct discovery *heads, int nr_spec, const char **specs)
+static int push_git(struct discovery *heads, struct strvec *specs)
 {
 	struct rpc_state rpc;
-	int i, err;
+	int i;
+	int err;
 	struct strvec args;
 	struct string_list_item *cas_option;
 	struct strbuf preamble = STRBUF_INIT;
@@ -1326,8 +1325,8 @@  static int push_git(struct discovery *heads, int nr_spec, const char **specs)
 		strvec_push(&args, "--force-if-includes");
 
 	strvec_push(&args, "--stdin");
-	for (i = 0; i < nr_spec; i++)
-		packet_buf_write(&preamble, "%s\n", specs[i]);
+	for (i = 0; i < specs->nr; i++)
+		packet_buf_write(&preamble, "%s\n", specs->v[i]);
 	packet_buf_flush(&preamble);
 
 	memset(&rpc, 0, sizeof(rpc));
@@ -1342,15 +1341,15 @@  static int push_git(struct discovery *heads, int nr_spec, const char **specs)
 	return err;
 }
 
-static int push(int nr_spec, const char **specs)
+static int push(struct strvec *specs)
 {
 	struct discovery *heads = discover_refs("git-receive-pack", 1);
 	int ret;
 
 	if (heads->proto_git)
-		ret = push_git(heads, nr_spec, specs);
+		ret = push_git(heads, specs);
 	else
-		ret = push_dav(nr_spec, specs);
+		ret = push_dav(specs);
 	free_discovery(heads);
 	return ret;
 }
@@ -1374,7 +1373,7 @@  static void parse_push(struct strbuf *buf)
 			break;
 	} while (1);
 
-	ret = push(specs.nr, specs.v);
+	ret = push(&specs);
 	printf("\n");
 	fflush(stdout);