Our "git diff" command supports running as a standalone tool. In this
code path, we try to hash the file content but after
18e2588e11 (sha1_file: switch uses of SHA-1 to the_hash_algo -
2018-02-01), there is a chance that the_hash_algo (required by
index_path) may still be uninitialized if no repository is found.

Executing index_path() when the_hash_algo is NULL (or points to unknown
algo) either crashes or dies. Let's make it a bit safer by explicitly
falling back to SHA-1 (so that the diff output remains the same as
before, compared to the alternative that we simply do not hash).

Signed-off-by: Nguyễn Thái Ngọc Duy <pclo...@gmail.com>
---
 diff.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/diff.c b/diff.c
index 21c3838b25..5f28d84b87 100644
--- a/diff.c
+++ b/diff.c
@@ -3995,6 +3995,18 @@ static void run_diff(struct diff_filepair *p, struct 
diff_options *o)
                return;
        }
 
+       /*
+        * NEEDSWORK: When running in no-index mode (and no repo is
+        * found, thus no hash algo configured), fall back to SHA-1
+        * hashing (which is used by diff_fill_oid_info below) to
+        * avoid regression in diff output.
+        *
+        * In future, perhaps we can allow the user to specify their
+        * hash algorithm from command line in this mode.
+        */
+       if (o->flags.no_index && !repo_has_valid_hash_algo(the_repository))
+               repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
+
        diff_fill_oid_info(one);
        diff_fill_oid_info(two);
 
-- 
2.16.1.435.g8f24da2e1a

Reply via email to