Skip to content

Commit ab27490

Browse files
pks-tgitster
authored andcommitted
builtin/diff: explicitly set hash algo when there is no repo
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 ``` 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 protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 332b56b commit ab27490

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

builtin/diff.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,15 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
465465
no_index = DIFF_NO_INDEX_IMPLICIT;
466466
}
467467

468+
/*
469+
* When operating outside of a Git repository we need to have a hash
470+
* algorithm at hand so that we can generate the blob hashes. We
471+
* default to SHA1 here, but may eventually want to change this to be
472+
* configurable via a command line option.
473+
*/
474+
if (nongit)
475+
repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
476+
468477
init_diff_ui_defaults();
469478
git_config(git_diff_ui_config, NULL);
470479
prefix = precompose_argv_prefix(argc, argv, prefix);

0 commit comments

Comments
 (0)