@@ -265,58 +265,56 @@ int git_diff_foreach(
265
265
}
266
266
267
267
typedef struct {
268
- git_xdiff_output xo ;
269
268
git_diff_patch patch ;
270
269
git_diff_delta delta ;
271
- } diff_single_info ;
270
+ } diff_patch_with_delta ;
272
271
273
- static int diff_single_generate (diff_single_info * info )
272
+ static int diff_single_generate (diff_patch_with_delta * pd , git_xdiff_output * xo )
274
273
{
275
274
int error = 0 ;
276
- git_diff_patch * patch = & info -> patch ;
275
+ git_diff_patch * patch = & pd -> patch ;
277
276
bool has_old = ((patch -> ofile .file .flags & GIT_DIFF_FLAG__NO_DATA ) == 0 );
278
277
bool has_new = ((patch -> nfile .file .flags & GIT_DIFF_FLAG__NO_DATA ) == 0 );
279
278
280
- info -> delta .status = has_new ?
279
+ pd -> delta .status = has_new ?
281
280
(has_old ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED ) :
282
281
(has_old ? GIT_DELTA_DELETED : GIT_DELTA_UNTRACKED );
283
282
284
283
if (git_oid_equal (& patch -> nfile .file .oid , & patch -> ofile .file .oid ))
285
- info -> delta .status = GIT_DELTA_UNMODIFIED ;
284
+ pd -> delta .status = GIT_DELTA_UNMODIFIED ;
286
285
287
- patch -> delta = & info -> delta ;
286
+ patch -> delta = & pd -> delta ;
288
287
289
288
diff_patch_init_common (patch );
290
289
291
- error = diff_patch_file_callback (patch , (git_diff_output * )& info -> xo );
290
+ error = diff_patch_file_callback (patch , (git_diff_output * )xo );
292
291
293
292
if (!error )
294
- error = diff_patch_generate (patch , (git_diff_output * )& info -> xo );
293
+ error = diff_patch_generate (patch , (git_diff_output * )xo );
295
294
296
295
if (error == GIT_EUSER )
297
296
giterr_clear (); /* don't leave error message set invalidly */
298
297
299
298
return error ;
300
299
}
301
300
302
- int git_diff_blobs (
301
+ static int diff_patch_from_blobs (
302
+ diff_patch_with_delta * pd ,
303
+ git_xdiff_output * xo ,
303
304
const git_blob * old_blob ,
304
305
const git_blob * new_blob ,
305
- const git_diff_options * opts ,
306
- git_diff_file_cb file_cb ,
307
- git_diff_hunk_cb hunk_cb ,
308
- git_diff_data_cb data_cb ,
309
- void * payload )
306
+ const git_diff_options * opts )
310
307
{
311
308
int error = 0 ;
312
- diff_single_info info ;
313
309
git_repository * repo =
314
310
new_blob ? git_object_owner ((const git_object * )new_blob ) :
315
311
old_blob ? git_object_owner ((const git_object * )old_blob ) : NULL ;
316
312
317
313
GITERR_CHECK_VERSION (opts , GIT_DIFF_OPTIONS_VERSION , "git_diff_options" );
318
314
319
- if (!repo ) /* Hmm, given two NULL blobs, silently do no callbacks? */
315
+ pd -> patch .delta = & pd -> delta ;
316
+
317
+ if (!repo ) /* return two NULL items as UNMODIFIED delta */
320
318
return 0 ;
321
319
322
320
if (opts && (opts -> flags & GIT_DIFF_REVERSE ) != 0 ) {
@@ -325,64 +323,163 @@ int git_diff_blobs(
325
323
new_blob = swap ;
326
324
}
327
325
328
- memset (& info , 0 , sizeof (info ));
326
+ if ((error = diff_file_content_init_from_blob (
327
+ & pd -> patch .ofile , repo , opts , old_blob )) < 0 ||
328
+ (error = diff_file_content_init_from_blob (
329
+ & pd -> patch .nfile , repo , opts , new_blob )) < 0 )
330
+ return error ;
329
331
330
- diff_output_init ((git_diff_output * )& info .xo ,
331
- opts , file_cb , hunk_cb , data_cb , payload );
332
- git_xdiff_init (& info .xo , opts );
332
+ return diff_single_generate (pd , xo );
333
+ }
333
334
334
- if (!(error = diff_file_content_init_from_blob (
335
- & info .patch .ofile , repo , opts , old_blob )) &&
336
- !(error = diff_file_content_init_from_blob (
337
- & info .patch .nfile , repo , opts , new_blob )))
338
- error = diff_single_generate (& info );
335
+ int git_diff_blobs (
336
+ const git_blob * old_blob ,
337
+ const git_blob * new_blob ,
338
+ const git_diff_options * opts ,
339
+ git_diff_file_cb file_cb ,
340
+ git_diff_hunk_cb hunk_cb ,
341
+ git_diff_data_cb data_cb ,
342
+ void * payload )
343
+ {
344
+ int error = 0 ;
345
+ diff_patch_with_delta pd ;
346
+ git_xdiff_output xo ;
339
347
340
- git_diff_patch_free (& info .patch );
348
+ memset (& pd , 0 , sizeof (pd ));
349
+ memset (& xo , 0 , sizeof (xo ));
350
+
351
+ diff_output_init (
352
+ (git_diff_output * )& xo , opts , file_cb , hunk_cb , data_cb , payload );
353
+ git_xdiff_init (& xo , opts );
354
+
355
+ error = diff_patch_from_blobs (& pd , & xo , old_blob , new_blob , opts );
356
+
357
+ git_diff_patch_free ((git_diff_patch * )& pd );
341
358
342
359
return error ;
343
360
}
344
361
345
- int git_diff_blob_to_buffer (
362
+ int git_diff_patch_from_blobs (
363
+ git_diff_patch * * out ,
364
+ const git_blob * old_blob ,
365
+ const git_blob * new_blob ,
366
+ const git_diff_options * opts )
367
+ {
368
+ int error = 0 ;
369
+ diff_patch_with_delta * pd ;
370
+ git_xdiff_output xo ;
371
+
372
+ assert (out );
373
+ * out = NULL ;
374
+
375
+ pd = git__calloc (1 , sizeof (* pd ));
376
+ GITERR_CHECK_ALLOC (pd );
377
+ pd -> patch .flags = GIT_DIFF_PATCH_ALLOCATED ;
378
+
379
+ memset (& xo , 0 , sizeof (xo ));
380
+
381
+ diff_output_to_patch ((git_diff_output * )& xo , & pd -> patch );
382
+ git_xdiff_init (& xo , opts );
383
+
384
+ if (!(error = diff_patch_from_blobs (pd , & xo , old_blob , new_blob , opts )))
385
+ * out = (git_diff_patch * )pd ;
386
+ else
387
+ git_diff_patch_free ((git_diff_patch * )pd );
388
+
389
+ return error ;
390
+ }
391
+
392
+ static int diff_patch_from_blob_and_buffer (
393
+ diff_patch_with_delta * pd ,
394
+ git_xdiff_output * xo ,
346
395
const git_blob * old_blob ,
347
396
const char * buf ,
348
397
size_t buflen ,
349
- const git_diff_options * opts ,
350
- git_diff_file_cb file_cb ,
351
- git_diff_hunk_cb hunk_cb ,
352
- git_diff_data_cb data_cb ,
353
- void * payload )
398
+ const git_diff_options * opts )
354
399
{
355
400
int error = 0 ;
356
- diff_single_info info ;
357
401
git_repository * repo =
358
402
old_blob ? git_object_owner ((const git_object * )old_blob ) : NULL ;
359
403
360
404
GITERR_CHECK_VERSION (opts , GIT_DIFF_OPTIONS_VERSION , "git_diff_options" );
361
405
362
- if (!repo && !buf ) /* Hmm, given NULLs, silently do no callbacks? */
363
- return 0 ;
364
-
365
- memset (& info , 0 , sizeof (info ));
406
+ pd -> patch .delta = & pd -> delta ;
366
407
367
- diff_output_init ((git_diff_output * )& info .xo ,
368
- opts , file_cb , hunk_cb , data_cb , payload );
369
- git_xdiff_init (& info .xo , opts );
408
+ if (!repo && !buf ) /* return two NULL items as UNMODIFIED delta */
409
+ return 0 ;
370
410
371
411
if (opts && (opts -> flags & GIT_DIFF_REVERSE ) != 0 ) {
372
412
if (!(error = diff_file_content_init_from_raw (
373
- & info . patch .ofile , repo , opts , buf , buflen )))
413
+ & pd -> patch .ofile , repo , opts , buf , buflen )))
374
414
error = diff_file_content_init_from_blob (
375
- & info . patch .nfile , repo , opts , old_blob );
415
+ & pd -> patch .nfile , repo , opts , old_blob );
376
416
} else {
377
417
if (!(error = diff_file_content_init_from_blob (
378
- & info . patch .ofile , repo , opts , old_blob )))
418
+ & pd -> patch .ofile , repo , opts , old_blob )))
379
419
error = diff_file_content_init_from_raw (
380
- & info . patch .nfile , repo , opts , buf , buflen );
420
+ & pd -> patch .nfile , repo , opts , buf , buflen );
381
421
}
382
422
383
- error = diff_single_generate (& info );
423
+ return diff_single_generate (pd , xo );
424
+ }
425
+
426
+ int git_diff_blob_to_buffer (
427
+ const git_blob * old_blob ,
428
+ const char * buf ,
429
+ size_t buflen ,
430
+ const git_diff_options * opts ,
431
+ git_diff_file_cb file_cb ,
432
+ git_diff_hunk_cb hunk_cb ,
433
+ git_diff_data_cb data_cb ,
434
+ void * payload )
435
+ {
436
+ int error = 0 ;
437
+ diff_patch_with_delta pd ;
438
+ git_xdiff_output xo ;
384
439
385
- git_diff_patch_free (& info .patch );
440
+ memset (& pd , 0 , sizeof (pd ));
441
+ memset (& xo , 0 , sizeof (xo ));
442
+
443
+ diff_output_init (
444
+ (git_diff_output * )& xo , opts , file_cb , hunk_cb , data_cb , payload );
445
+ git_xdiff_init (& xo , opts );
446
+
447
+ error = diff_patch_from_blob_and_buffer (
448
+ & pd , & xo , old_blob , buf , buflen , opts );
449
+
450
+ git_diff_patch_free ((git_diff_patch * )& pd );
451
+
452
+ return error ;
453
+ }
454
+
455
+ int git_diff_patch_from_blob_and_buffer (
456
+ git_diff_patch * * out ,
457
+ const git_blob * old_blob ,
458
+ const char * buf ,
459
+ size_t buflen ,
460
+ const git_diff_options * opts )
461
+ {
462
+ int error = 0 ;
463
+ diff_patch_with_delta * pd ;
464
+ git_xdiff_output xo ;
465
+
466
+ assert (out );
467
+ * out = NULL ;
468
+
469
+ pd = git__calloc (1 , sizeof (* pd ));
470
+ GITERR_CHECK_ALLOC (pd );
471
+ pd -> patch .flags = GIT_DIFF_PATCH_ALLOCATED ;
472
+
473
+ memset (& xo , 0 , sizeof (xo ));
474
+
475
+ diff_output_to_patch ((git_diff_output * )& xo , & pd -> patch );
476
+ git_xdiff_init (& xo , opts );
477
+
478
+ if (!(error = diff_patch_from_blob_and_buffer (
479
+ pd , & xo , old_blob , buf , buflen , opts )))
480
+ * out = (git_diff_patch * )pd ;
481
+ else
482
+ git_diff_patch_free ((git_diff_patch * )pd );
386
483
387
484
return error ;
388
485
}
@@ -599,9 +696,7 @@ static int diff_patch_file_cb(
599
696
float progress ,
600
697
void * payload )
601
698
{
602
- GIT_UNUSED (delta );
603
- GIT_UNUSED (progress );
604
- GIT_UNUSED (payload );
699
+ GIT_UNUSED (delta ); GIT_UNUSED (progress ); GIT_UNUSED (payload );
605
700
return 0 ;
606
701
}
607
702
0 commit comments