Skip to content

Commit 9716389

Browse files
Merge pull request #562 from mastahyeti/commit_header_field
Add Commit#header_field method
2 parents 06330b8 + 158278c commit 9716389

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

ext/rugged/rugged_commit.c

Lines changed: 49 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;
@@ -556,6 +557,51 @@ static VALUE rb_git_commit_to_mbox(int argc, VALUE *argv, VALUE self)
556557
return rb_email_patch;
557558
}
558559

560+
/*
561+
* call-seq:
562+
* commit.header_field(field_name) -> str
563+
*
564+
* Returns +commit+'s header field value.
565+
*/
566+
static VALUE rb_git_commit_header_field(VALUE self, VALUE rb_field) {
567+
git_buf header_field = { 0 };
568+
VALUE rb_result;
569+
git_commit *commit;
570+
571+
Check_Type(rb_field, T_STRING);
572+
573+
Data_Get_Struct(self, git_commit, commit);
574+
575+
rugged_exception_check(
576+
git_commit_header_field(&header_field, commit, StringValueCStr(rb_field))
577+
);
578+
579+
rb_result = rb_enc_str_new(header_field.ptr, header_field.size, rb_utf8_encoding());
580+
581+
git_buf_free(&header_field);
582+
583+
return rb_result;
584+
}
585+
586+
/*
587+
* call-seq:
588+
* commit.header -> str
589+
*
590+
* Returns +commit+'s entire raw header.
591+
*/
592+
static VALUE rb_git_commit_header(VALUE self) {
593+
VALUE rb_result;
594+
git_commit *commit;
595+
const char *raw_header;
596+
597+
Data_Get_Struct(self, git_commit, commit);
598+
599+
raw_header = git_commit_raw_header(commit);
600+
601+
rb_result = rb_enc_str_new(raw_header, strlen(raw_header), rb_utf8_encoding());
602+
603+
return rb_result;
604+
}
559605

560606
void Init_rugged_commit(void)
561607
{
@@ -579,4 +625,7 @@ void Init_rugged_commit(void)
579625
rb_define_method(rb_cRuggedCommit, "amend", rb_git_commit_amend, 1);
580626

581627
rb_define_method(rb_cRuggedCommit, "to_mbox", rb_git_commit_to_mbox, -1);
628+
629+
rb_define_method(rb_cRuggedCommit, "header_field", rb_git_commit_header_field, 1);
630+
rb_define_method(rb_cRuggedCommit, "header", rb_git_commit_header, 0);
582631
}

test/commit_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,27 @@ 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_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
197+
end
177198
end
178199

179200
class CommitWriteTest < Rugged::TestCase

0 commit comments

Comments
 (0)