Skip to content

Commit ab07bf2

Browse files
committed
add Commit#header_field? for checking header existence
1 parent 926a208 commit ab07bf2

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

ext/rugged/rugged_commit.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,33 @@ static VALUE rb_git_commit_header_field(VALUE self, VALUE rb_field) {
583583
return rb_result;
584584
}
585585

586+
/*
587+
* call-seq:
588+
* commit.header_field?(field_name) -> bool
589+
*
590+
* Returns true if header field is present, false otherwise.
591+
*/
592+
static VALUE rb_git_commit_header_field_present(VALUE self, VALUE rb_field) {
593+
git_buf header_field = { 0 };
594+
git_commit *commit;
595+
int err;
596+
597+
Check_Type(rb_field, T_STRING);
598+
599+
Data_Get_Struct(self, git_commit, commit);
600+
601+
err = git_commit_header_field(&header_field, commit, StringValueCStr(rb_field));
602+
603+
git_buf_free(&header_field);
604+
605+
if (err == GIT_ENOTFOUND)
606+
return Qfalse;
607+
608+
rugged_exception_check(err);
609+
610+
return Qtrue;
611+
}
612+
586613
/*
587614
* call-seq:
588615
* commit.header -> str
@@ -627,5 +654,6 @@ void Init_rugged_commit(void)
627654
rb_define_method(rb_cRuggedCommit, "to_mbox", rb_git_commit_to_mbox, -1);
628655

629656
rb_define_method(rb_cRuggedCommit, "header_field", rb_git_commit_header_field, 1);
657+
rb_define_method(rb_cRuggedCommit, "header_field?", rb_git_commit_header_field_present, 1);
630658
rb_define_method(rb_cRuggedCommit, "header", rb_git_commit_header, 0);
631659
}

test/commit_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ def test_header_field
183183
assert_equal expected_header_field, obj.header_field("author")
184184
end
185185

186+
def test_header_field?
187+
oid = "8496071c1b46c854b31185ea97743be6a8774479"
188+
obj = @repo.lookup(oid)
189+
190+
assert_equal true, obj.header_field?("author")
191+
assert_equal false, obj.header_field?("foobar")
192+
end
193+
186194
def test_header
187195
oid = "8496071c1b46c854b31185ea97743be6a8774479"
188196
obj = @repo.lookup(oid)

0 commit comments

Comments
 (0)