Subject: Re: [PATCH 09/11] builtin/diff: explicitly set hash algo when there is no repo

From: Junio C Hamano <[email-masked]>
Date: Mon, 22 Apr 2024 11:41:41 -0700

Patrick Steinhardt <[email-masked]> writes:

> The git-diff(1) command can be used outside repositories to diff two
> files with each other. But even if there is no repository we will end up
> hashing the files that we are diffing so that we can print the "index"
> line:
>
> ```
> diff --git a/a b/b
> index 7898192..6178079 100644
> --- a/a
> +++ b/b
> @@ -1 +1 @@
> -a
> +b
> ```

This will break "git am"; it is customary to indent such sample
patch block in a log message.

> We implicitly use SHA1 to calculate the hash here, which is because
> `the_repository` gets initialized with SHA1 during the startup routine.
> We are about to stop doing this though such that `the_repository` only
> ever has a hash function when it was properly initialized via a repo's
> configuration.
>
> To give full control to our users, we would ideally add a new switch to
> git-diff(1) that allows them to specify the hash function when executed
> outside of a repository. But for now, we only convert the code to make
> this explicit such that we can stop setting the default hash algorithm
> for `the_repository`.
>
> Signed-off-by: Patrick Steinhardt <[email-masked]>
> ---
>  builtin/diff.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
>
> diff --git a/builtin/diff.c b/builtin/diff.c
> index 6e196e0c7d..58ec7e5da2 100644
> --- a/builtin/diff.c
> +++ b/builtin/diff.c
> @@ -465,6 +465,15 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
>  			no_index = DIFF_NO_INDEX_IMPLICIT;
>  	}
>  
> +	/*
> +	 * When operating outside of a Git repository we need to have a hash
> +	 * algorithm at hand so that we can generate the blob hashes. We
> +	 * default to SHA1 here, but may eventually want to change this to be
> +	 * configurable via a command line option.
> +	 */
> +	if (nongit)
> +		repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
> +
>  	init_diff_ui_defaults();
>  	git_config(git_diff_ui_config, NULL);
>  	prefix = precompose_argv_prefix(argc, argv, prefix);