1919#include "git2/email.h"
2020
2121struct patch_id_args {
22+ git_diff * diff ;
2223 git_hash_ctx ctx ;
2324 git_oid result ;
25+ git_oid_t oid_type ;
2426 int first_file ;
2527};
2628
@@ -280,17 +282,19 @@ int git_diff_find_options_init(
280282 return 0 ;
281283}
282284
283- static int flush_hunk (git_oid * result , git_hash_ctx * ctx )
285+ static int flush_hunk (git_oid * result , struct patch_id_args * args )
284286{
287+ git_hash_ctx * ctx = & args -> ctx ;
285288 git_oid hash ;
286289 unsigned short carry = 0 ;
287- int error , i ;
290+ size_t i ;
291+ int error ;
288292
289293 if ((error = git_hash_final (hash .id , ctx )) < 0 ||
290294 (error = git_hash_init (ctx )) < 0 )
291295 return error ;
292296
293- for (i = 0 ; i < GIT_OID_SHA1_SIZE ; i ++ ) {
297+ for (i = 0 ; i < git_oid_size ( args -> oid_type ) ; i ++ ) {
294298 carry += result -> id [i ] + hash .id [i ];
295299 result -> id [i ] = (unsigned char )carry ;
296300 carry >>= 8 ;
@@ -338,7 +342,7 @@ static int diff_patchid_print_callback_to_buf(
338342
339343 if (line -> origin == GIT_DIFF_LINE_FILE_HDR &&
340344 !args -> first_file &&
341- (error = flush_hunk (& args -> result , & args -> ctx ) < 0 ))
345+ (error = flush_hunk (& args -> result , args ) < 0 ))
342346 goto out ;
343347
344348 if ((error = git_hash_update (& args -> ctx , buf .ptr , buf .size )) < 0 )
@@ -362,14 +366,19 @@ int git_diff_patchid_options_init(git_diff_patchid_options *opts, unsigned int v
362366int git_diff_patchid (git_oid * out , git_diff * diff , git_diff_patchid_options * opts )
363367{
364368 struct patch_id_args args ;
369+ git_hash_algorithm_t algorithm ;
365370 int error ;
366371
367372 GIT_ERROR_CHECK_VERSION (
368373 opts , GIT_DIFF_PATCHID_OPTIONS_VERSION , "git_diff_patchid_options" );
369374
375+ algorithm = git_oid_algorithm (diff -> opts .oid_type );
376+
370377 memset (& args , 0 , sizeof (args ));
378+ args .diff = diff ;
371379 args .first_file = 1 ;
372- if ((error = git_hash_ctx_init (& args .ctx , GIT_HASH_ALGORITHM_SHA1 )) < 0 )
380+ args .oid_type = diff -> opts .oid_type ;
381+ if ((error = git_hash_ctx_init (& args .ctx , algorithm )) < 0 )
373382 goto out ;
374383
375384 if ((error = git_diff_print (diff ,
@@ -378,11 +387,11 @@ int git_diff_patchid(git_oid *out, git_diff *diff, git_diff_patchid_options *opt
378387 & args )) < 0 )
379388 goto out ;
380389
381- if ((error = (flush_hunk (& args .result , & args . ctx ))) < 0 )
390+ if ((error = (flush_hunk (& args .result , & args ))) < 0 )
382391 goto out ;
383392
384393#ifdef GIT_EXPERIMENTAL_SHA256
385- args .result .type = GIT_OID_SHA1 ;
394+ args .result .type = diff -> opts . oid_type ;
386395#endif
387396
388397 git_oid_cpy (out , & args .result );
0 commit comments