]> git.ipfire.org Git - thirdparty/git.git/commitdiff
cat-file: declare loop counter inside for()
authorEric Ju <eric.peijian@gmail.com>
Fri, 24 Jul 2026 10:54:13 +0000 (12:54 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 24 Jul 2026 15:46:58 +0000 (08:46 -0700)
Declare loop counters in the for statement when they are only used
within the loop body, limiting their scope and improving readability.

While updating the loop counters, use size_t instead of int for counters
that iterate over object counts.

Update the 'nr' parameter of dispatch_calls() to size_t as all callers
already pass a value of that type.

Helped-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Eric Ju <eric.peijian@gmail.com>
Signed-off-by: Pablo Sabater <pabloosabaterr@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/cat-file.c
fetch-pack.c

index b4b99a73da96965d1a5a655a620a68e1fbfd51cc..03afc44c5ed4824bf747ef338b88a2d6358fc580 100644 (file)
@@ -721,14 +721,12 @@ static void dispatch_calls(struct batch_options *opt,
                struct strbuf *output,
                struct expand_data *data,
                struct queued_cmd *cmd,
-               int nr)
+               size_t nr)
 {
-       int i;
-
        if (!opt->buffer_output)
                die(_("flush is only for --buffer mode"));
 
-       for (i = 0; i < nr; i++)
+       for (size_t i = 0; i < nr; i++)
                cmd[i].fn(opt, cmd[i].line, output, data);
 
        fflush(stdout);
@@ -736,9 +734,7 @@ static void dispatch_calls(struct batch_options *opt,
 
 static void free_cmds(struct queued_cmd *cmd, size_t *nr)
 {
-       size_t i;
-
-       for (i = 0; i < *nr; i++)
+       for (size_t i = 0; i < *nr; i++)
                FREE_AND_NULL(cmd[i].line);
 
        *nr = 0;
@@ -765,7 +761,6 @@ static void batch_objects_command(struct batch_options *opt,
        size_t alloc = 0, nr = 0;
 
        while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) {
-               int i;
                const struct parse_cmd *cmd = NULL;
                const char *p = NULL, *cmd_end;
                struct queued_cmd call = {0};
@@ -775,7 +770,7 @@ static void batch_objects_command(struct batch_options *opt,
                if (isspace(*input.buf))
                        die(_("whitespace before command: '%s'"), input.buf);
 
-               for (i = 0; i < ARRAY_SIZE(commands); i++) {
+               for (size_t i = 0; i < ARRAY_SIZE(commands); i++) {
                        if (!skip_prefix(input.buf, commands[i].name, &cmd_end))
                                continue;
 
index 29c41132ee04957d582c899146cc7afbfe626ee9..9eb8fc539986b7bd6e1fd03ad3e3291fbdaf373c 100644 (file)
@@ -1388,9 +1388,8 @@ static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
        if (advertise_sid && server_supports_v2("session-id"))
                packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
        if (server_options && server_options->nr) {
-               int i;
                ensure_server_supports_v2("server-option");
-               for (i = 0; i < server_options->nr; i++)
+               for (size_t i = 0; i < server_options->nr; i++)
                        packet_buf_write(req_buf, "server-option=%s",
                                         server_options->items[i].string);
        }