Subject: Re: [PATCH v2 3/4] builtin/stash: provide a way to export stashes to a ref

From: Junio C Hamano <[email-masked]>
Date: Wed, 30 Mar 2022 16:05:28 -0700

"brian m. carlson" <[email-masked]> writes:

> +static int do_export_stash(const char *ref, size_t argc, const char **argv)
> +{
> +	struct object_id base;
> +	struct object_context unused;
> +	struct commit *prev;
> +	struct object_id *items = NULL;
> +	int nitems = 0, nalloc = 0;
> +	int res = 0;
> +	struct strbuf revision;
> +	const char *author, *committer;
> +
> +	/*
> +	 * This is an arbitrary, fixed date, specifically the one used by git
> +	 * format-patch.  The goal is merely to produce reproducible output.
> +	 */
> +	prepare_fallback_ident("git stash", "git@stash");
> +	author = fmt_ident("git stash", "git@stash", WANT_BLANK_IDENT, "2001-09-17T00:00:00Z", 0);
> +	committer = fmt_ident("git stash", "git@stash", WANT_BLANK_IDENT, "2001-09-17T00:00:00Z", 0);
> +
> +	/* First, we create a single empty commit. */
> +	if (commit_tree_extended(NULL, 0, the_hash_algo->empty_tree, NULL, &base, author, committer, NULL, NULL))
> +		return error(_("unable to write base commit"));
> +
> +	prev = lookup_commit_reference(the_repository, &base);
> +
> +	if (argc) {
> +		/*
> +		 * Find each specified stash, and load data into the array.
> +		 */
> +		for (int i = 0; i < argc; i++) {
> +			ALLOC_GROW_BY(items, nitems, 1, nalloc);

Documentation/CodingGuidelines still says this

 - Declaring a variable in the for loop "for (int i = 0; i < 10; i++)"
   is still not allowed in this codebase.

We have been experimenting since we merged 44ba10d6 (revision: use
C99 declaration of variable in for() loop, 2021-11-14) at 5a4069a1
(Merge branch 'jc/c99-var-decl-in-for-loop', 2021-12-21), which is
shipped as a part of v2.35.0 just a few months ago.

Let's not add more until we are sure that we do not have to revert
44ba10d6 (revision: use C99 declaration of variable in for() loop,
2021-11-14) to limit the potential damage, especially when it is so
easy to do so.

Just declare "int i" at the beginning of the funcion and keep
reusing it would be fine here.