Skip to content

Commit 458d562

Browse files
Merge pull request #628 from libgit2/arthur/add-patch-header
Add `Rugged::Patch#header`
2 parents 98cd180 + 1d71200 commit 458d562

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

ext/rugged/rugged_patch.c

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ static VALUE rb_git_diff_patch_lines(int argc, VALUE *argv, VALUE self)
275275

276276
return INT2FIX(lines);
277277
}
278+
278279
/*
279280
* call-seq:
280281
* patch.bytesize(options = {}) -> int
@@ -332,20 +333,36 @@ static int patch_print_cb(
332333
const git_diff_line *line,
333334
void *payload)
334335
{
335-
VALUE rb_str = (VALUE)payload;
336+
VALUE rb_buffer = (VALUE)payload;
336337

337338
switch (line->origin) {
338339
case GIT_DIFF_LINE_CONTEXT:
339340
case GIT_DIFF_LINE_ADDITION:
340341
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));
342343
}
343344

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));
345346

346347
return GIT_OK;
347348
}
348349

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+
349366
/*
350367
* call-seq:
351368
* patch.to_s -> str
@@ -355,14 +372,35 @@ static int patch_print_cb(
355372
static VALUE rb_git_diff_patch_to_s(VALUE self)
356373
{
357374
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();
359394
Data_Get_Struct(self, git_patch, patch);
360395

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);
362399

363-
return rb_str;
400+
return rb_ary_join(rb_buffer, Qnil);
364401
}
365402

403+
366404
void Init_rugged_patch(void)
367405
{
368406
rb_cRuggedPatch = rb_define_class_under(rb_mRugged, "Patch", rb_cObject);
@@ -375,6 +413,7 @@ void Init_rugged_patch(void)
375413

376414
rb_define_method(rb_cRuggedPatch, "delta", rb_git_diff_patch_delta, 0);
377415

416+
rb_define_method(rb_cRuggedPatch, "header", rb_git_diff_patch_header, 0);
378417
rb_define_method(rb_cRuggedPatch, "to_s", rb_git_diff_patch_to_s, 0);
379418

380419
rb_define_method(rb_cRuggedPatch, "each_hunk", rb_git_diff_patch_each_hunk, 0);

test/patch_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,21 @@ def test_lines
7676

7777
assert_equal 0, patch.lines(exclude_eofnl: true, exclude_additions: true, exclude_deletions: true, exclude_context: true)
7878
end
79+
80+
def test_header
81+
repo = FixtureRepo.from_libgit2("diff")
82+
repo.config['core.abbrev'] = 7
83+
84+
a = repo.lookup("d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
85+
b = repo.lookup("7a9e0b02e63179929fed24f0a3e0f19168114d10")
86+
87+
diff = a.tree.diff(b.tree, :context_lines => 0)
88+
89+
assert_equal <<-DIFF, diff.patches[1].header
90+
diff --git a/readme.txt b/readme.txt
91+
index 7b808f7..29ab705 100644
92+
--- a/readme.txt
93+
+++ b/readme.txt
94+
DIFF
95+
end
7996
end

0 commit comments

Comments
 (0)