Skip to content

Commit a8ec107

Browse files
committed
Merge pull request #565 from mastahyeti/header_field_present
Add Commit#header_field? for checking header existence
2 parents fbee82a + ed9bdde commit a8ec107

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
@@ -589,6 +589,33 @@ static VALUE rb_git_commit_header_field(VALUE self, VALUE rb_field) {
589589
return rb_result;
590590
}
591591

592+
/*
593+
* call-seq:
594+
* commit.header_field?(field_name) -> bool
595+
*
596+
* Returns true if header field is present, false otherwise.
597+
*/
598+
static VALUE rb_git_commit_header_field_present(VALUE self, VALUE rb_field) {
599+
git_buf header_field = { 0 };
600+
git_commit *commit;
601+
int error;
602+
603+
Check_Type(rb_field, T_STRING);
604+
605+
Data_Get_Struct(self, git_commit, commit);
606+
607+
error = git_commit_header_field(&header_field, commit, StringValueCStr(rb_field));
608+
609+
git_buf_free(&header_field);
610+
611+
if (error == GIT_ENOTFOUND)
612+
return Qfalse;
613+
614+
rugged_exception_check(error);
615+
616+
return Qtrue;
617+
}
618+
592619
/*
593620
* call-seq:
594621
* commit.header -> str
@@ -633,5 +660,6 @@ void Init_rugged_commit(void)
633660
rb_define_method(rb_cRuggedCommit, "to_mbox", rb_git_commit_to_mbox, -1);
634661

635662
rb_define_method(rb_cRuggedCommit, "header_field", rb_git_commit_header_field, 1);
663+
rb_define_method(rb_cRuggedCommit, "header_field?", rb_git_commit_header_field_present, 1);
636664
rb_define_method(rb_cRuggedCommit, "header", rb_git_commit_header, 0);
637665
}

test/commit_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ def test_header_field
184184
assert_equal nil, obj.header_field("foobar")
185185
end
186186

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

0 commit comments

Comments
 (0)