@@ -275,6 +275,7 @@ static VALUE rb_git_diff_patch_lines(int argc, VALUE *argv, VALUE self)
275
275
276
276
return INT2FIX (lines );
277
277
}
278
+
278
279
/*
279
280
* call-seq:
280
281
* patch.bytesize(options = {}) -> int
@@ -332,20 +333,36 @@ static int patch_print_cb(
332
333
const git_diff_line * line ,
333
334
void * payload )
334
335
{
335
- VALUE rb_str = (VALUE )payload ;
336
+ VALUE rb_buffer = (VALUE )payload ;
336
337
337
338
switch (line -> origin ) {
338
339
case GIT_DIFF_LINE_CONTEXT :
339
340
case GIT_DIFF_LINE_ADDITION :
340
341
case GIT_DIFF_LINE_DELETION :
341
- rb_str_cat ( rb_str , & line -> origin , 1 );
342
+ rb_ary_push ( rb_buffer , rb_str_new ( & line -> origin , 1 ) );
342
343
}
343
344
344
- rb_str_cat ( rb_str , line -> content , line -> content_len );
345
+ rb_ary_push ( rb_buffer , rb_str_new ( line -> content , line -> content_len ) );
345
346
346
347
return GIT_OK ;
347
348
}
348
349
350
+ static int patch_print_header_cb (
351
+ const git_diff_delta * delta ,
352
+ const git_diff_hunk * hunk ,
353
+ const git_diff_line * line ,
354
+ void * payload )
355
+ {
356
+ VALUE rb_buffer = (VALUE )payload ;
357
+
358
+ if (line -> origin == GIT_DIFF_LINE_FILE_HDR ) {
359
+ rb_ary_push (rb_buffer , rb_str_new (line -> content , line -> content_len ));
360
+ return GIT_OK ;
361
+ } else {
362
+ return GIT_ITEROVER ;
363
+ }
364
+ }
365
+
349
366
/*
350
367
* call-seq:
351
368
* patch.to_s -> str
@@ -355,14 +372,35 @@ static int patch_print_cb(
355
372
static VALUE rb_git_diff_patch_to_s (VALUE self )
356
373
{
357
374
git_patch * patch ;
358
- VALUE rb_str = rb_str_new (NULL , 0 );
375
+ VALUE rb_buffer = rb_ary_new ();
376
+ Data_Get_Struct (self , git_patch , patch );
377
+
378
+ rugged_exception_check (git_patch_print (patch , patch_print_cb , (void * )rb_buffer ));
379
+
380
+ return rb_ary_join (rb_buffer , Qnil );
381
+ }
382
+
383
+ /*
384
+ * call-seq:
385
+ * patch.header -> str
386
+ *
387
+ * Returns only the header of the patch as a string.
388
+ */
389
+ static VALUE rb_git_diff_patch_header (VALUE self )
390
+ {
391
+ git_patch * patch ;
392
+ int error = 0 ;
393
+ VALUE rb_buffer = rb_ary_new ();
359
394
Data_Get_Struct (self , git_patch , patch );
360
395
361
- rugged_exception_check (git_patch_print (patch , patch_print_cb , (void * )rb_str ));
396
+ error = git_patch_print (patch , patch_print_header_cb , (void * )rb_buffer );
397
+ if (error && error != GIT_ITEROVER )
398
+ rugged_exception_check (error );
362
399
363
- return rb_str ;
400
+ return rb_ary_join ( rb_buffer , Qnil ) ;
364
401
}
365
402
403
+
366
404
void Init_rugged_patch (void )
367
405
{
368
406
rb_cRuggedPatch = rb_define_class_under (rb_mRugged , "Patch" , rb_cObject );
@@ -375,6 +413,7 @@ void Init_rugged_patch(void)
375
413
376
414
rb_define_method (rb_cRuggedPatch , "delta" , rb_git_diff_patch_delta , 0 );
377
415
416
+ rb_define_method (rb_cRuggedPatch , "header" , rb_git_diff_patch_header , 0 );
378
417
rb_define_method (rb_cRuggedPatch , "to_s" , rb_git_diff_patch_to_s , 0 );
379
418
380
419
rb_define_method (rb_cRuggedPatch , "each_hunk" , rb_git_diff_patch_each_hunk , 0 );
0 commit comments