Skip to content

Commit 8454965

Browse files
committed
add Commit#header method
1 parent 9a6021f commit 8454965

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

ext/rugged/rugged_commit.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424

2525
#include "rugged.h"
26+
#include "git2/commit.h"
2627

2728
extern VALUE rb_mRugged;
2829
extern VALUE rb_cRuggedObject;
@@ -576,6 +577,20 @@ static VALUE rb_git_commit_header_field(VALUE self, VALUE rb_field) {
576577
return rb_result;
577578
}
578579

580+
static VALUE rb_git_commit_header(VALUE self) {
581+
VALUE rb_result;
582+
git_commit *commit;
583+
const char *raw_header;
584+
585+
Data_Get_Struct(self, git_commit, commit);
586+
587+
raw_header = git_commit_raw_header(commit);
588+
589+
rb_result = rb_enc_str_new(raw_header, strlen(raw_header), rb_utf8_encoding());
590+
591+
return rb_result;
592+
}
593+
579594
void Init_rugged_commit(void)
580595
{
581596
rb_cRuggedCommit = rb_define_class_under(rb_mRugged, "Commit", rb_cRuggedObject);
@@ -600,4 +615,5 @@ void Init_rugged_commit(void)
600615
rb_define_method(rb_cRuggedCommit, "to_mbox", rb_git_commit_to_mbox, -1);
601616

602617
rb_define_method(rb_cRuggedCommit, "header_field", rb_git_commit_header_field, 1);
618+
rb_define_method(rb_cRuggedCommit, "header", rb_git_commit_header, 0);
603619
}

test/commit_test.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,21 @@ def test_header_field
179179
oid = "8496071c1b46c854b31185ea97743be6a8774479"
180180
obj = @repo.lookup(oid)
181181

182-
expected_header = "Scott Chacon <[email protected]> 1273360386 -0700"
183-
assert_equal expected_header, obj.header_field("author")
182+
expected_header_field = "Scott Chacon <[email protected]> 1273360386 -0700"
183+
assert_equal expected_header_field, obj.header_field("author")
184+
end
185+
186+
def test_header
187+
oid = "8496071c1b46c854b31185ea97743be6a8774479"
188+
obj = @repo.lookup(oid)
189+
190+
expected_header = <<-HEADER
191+
tree 181037049a54a1eb5fab404658a3a250b44335d7
192+
author Scott Chacon <[email protected]> 1273360386 -0700
193+
committer Scott Chacon <[email protected]> 1273360386 -0700
194+
HEADER
195+
196+
assert_equal expected_header, obj.header
184197
end
185198
end
186199

0 commit comments

Comments
 (0)