Skip to content

Commit 9a6021f

Browse files
committed
add Commit#header_field method
1 parent 06330b8 commit 9a6021f

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

ext/rugged/rugged_commit.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,25 @@ static VALUE rb_git_commit_to_mbox(int argc, VALUE *argv, VALUE self)
556556
return rb_email_patch;
557557
}
558558

559+
static VALUE rb_git_commit_header_field(VALUE self, VALUE rb_field) {
560+
git_buf header_field = { 0 };
561+
VALUE rb_result;
562+
git_commit *commit;
563+
564+
Check_Type(rb_field, T_STRING);
565+
566+
Data_Get_Struct(self, git_commit, commit);
567+
568+
rugged_exception_check(
569+
git_commit_header_field(&header_field, commit, StringValueCStr(rb_field))
570+
);
571+
572+
rb_result = rb_enc_str_new(header_field.ptr, header_field.size, rb_utf8_encoding());
573+
574+
git_buf_free(&header_field);
575+
576+
return rb_result;
577+
}
559578

560579
void Init_rugged_commit(void)
561580
{
@@ -579,4 +598,6 @@ void Init_rugged_commit(void)
579598
rb_define_method(rb_cRuggedCommit, "amend", rb_git_commit_amend, 1);
580599

581600
rb_define_method(rb_cRuggedCommit, "to_mbox", rb_git_commit_to_mbox, -1);
601+
602+
rb_define_method(rb_cRuggedCommit, "header_field", rb_git_commit_header_field, 1);
582603
}

test/commit_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ def test_amend_commit_blank_message
174174
amended_commit = @repo.lookup(new_commit_oid)
175175
assert_equal tree_oid, amended_commit.tree.oid
176176
end
177+
178+
def test_header_field
179+
oid = "8496071c1b46c854b31185ea97743be6a8774479"
180+
obj = @repo.lookup(oid)
181+
182+
expected_header = "Scott Chacon <[email protected]> 1273360386 -0700"
183+
assert_equal expected_header, obj.header_field("author")
184+
end
177185
end
178186

179187
class CommitWriteTest < Rugged::TestCase

0 commit comments

Comments
 (0)