Skip to content

Commit 1405f96

Browse files
Add Rugged::Patch#header to generate a patch containin only header information.
1 parent 98cd180 commit 1405f96

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

ext/rugged/rugged_patch.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,22 @@ static int patch_print_cb(
346346
return GIT_OK;
347347
}
348348

349+
static int patch_print_header_cb(
350+
const git_diff_delta *delta,
351+
const git_diff_hunk *hunk,
352+
const git_diff_line *line,
353+
void *payload)
354+
{
355+
VALUE rb_str = (VALUE)payload;
356+
357+
if (line->origin == GIT_DIFF_LINE_FILE_HDR) {
358+
rb_str_cat(rb_str, line->content, line->content_len);
359+
}
360+
361+
return GIT_OK;
362+
}
363+
364+
349365
/*
350366
* call-seq:
351367
* patch.to_s -> str
@@ -363,6 +379,24 @@ static VALUE rb_git_diff_patch_to_s(VALUE self)
363379
return rb_str;
364380
}
365381

382+
/*
383+
* call-seq:
384+
* patch.header -> str
385+
*
386+
* Returns only the header of the patch as a string.
387+
*/
388+
static VALUE rb_git_diff_patch_header(VALUE self)
389+
{
390+
git_patch *patch;
391+
VALUE rb_str = rb_str_new(NULL, 0);
392+
Data_Get_Struct(self, git_patch, patch);
393+
394+
rugged_exception_check(git_patch_print(patch, patch_print_header_cb, (void*)rb_str));
395+
396+
return rb_str;
397+
}
398+
399+
366400
void Init_rugged_patch(void)
367401
{
368402
rb_cRuggedPatch = rb_define_class_under(rb_mRugged, "Patch", rb_cObject);
@@ -375,6 +409,7 @@ void Init_rugged_patch(void)
375409

376410
rb_define_method(rb_cRuggedPatch, "delta", rb_git_diff_patch_delta, 0);
377411

412+
rb_define_method(rb_cRuggedPatch, "header", rb_git_diff_patch_header, 0);
378413
rb_define_method(rb_cRuggedPatch, "to_s", rb_git_diff_patch_to_s, 0);
379414

380415
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)