Subject: Re: [PATCH v2 2/3] builtin/commit: error out when passing untracked path with -i

From: Junio C Hamano <[email-masked]>
Date: Fri, 29 Mar 2024 14:38:24 -0700

Ghanshyam Thakkar <[email-masked]> writes:

> When we provide a pathspec which does not match any tracked path
> alongside --include, we do not error like without --include. If there
> is something staged, it will commit the staged changes and ignore the
> pathspec which does not match any tracked path. And if nothing is
> staged, it will print the status. Exit code is 0 in both cases (unlike
> without --include). This is also described in the TODO comment before
> the relevant testcase.
>
> Fix this by passing a character array to add_files_to_cache() to
> collect the pathspec matching information and error out if the given
> path is untracked. Also, amend the testcase to check for the error
> message and remove the TODO comment.
>
> Signed-off-by: Ghanshyam Thakkar <[email-masked]>
> ---
>  builtin/commit.c                      |  9 ++++++++-
>  t/t7501-commit-basic-functionality.sh | 16 +---------------
>  2 files changed, 9 insertions(+), 16 deletions(-)

Nice.

> diff --git a/builtin/commit.c b/builtin/commit.c
> index 24efeaca98..355f25ec2a 100644
> --- a/builtin/commit.c
> +++ b/builtin/commit.c
> @@ -441,10 +441,17 @@ static const char *prepare_index(const char **argv, const char *prefix,
>  	 * (B) on failure, rollback the real index.
>  	 */
>  	if (all || (also && pathspec.nr)) {
> +		char *ps_matched = xcalloc(pathspec.nr, 1);
>  		repo_hold_locked_index(the_repository, &index_lock,
>  				       LOCK_DIE_ON_ERROR);
>  		add_files_to_cache(the_repository, also ? prefix : NULL,
> -				   &pathspec, NULL, 0, 0);
> +				   &pathspec, ps_matched, 0, 0);
> +		if (!all && report_path_error(ps_matched, &pathspec)) {
> +			free(ps_matched);
> +			exit(1);
> +		}
> +		free(ps_matched);
> +

Looking simple and very nice.

This change would not have to be redone even if we decide not to add
a new parameter to run_diff_files() and instead add a new member to
the revs structure instead, because it all happens at the level or
below add_files_to_cache().