Skip to content

Commit 79f1a0d

Browse files
author
Vicent Marti
committed
Merge pull request #567 from libgit2/vmg/commit-header-clean
commit: Cleanup header extraction APIs
2 parents a8ec107 + 82f729f commit 79f1a0d

File tree

2 files changed

+22
-43
lines changed

2 files changed

+22
-43
lines changed

ext/rugged/rugged_commit.c

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -563,77 +563,53 @@ static VALUE rb_git_commit_to_mbox(int argc, VALUE *argv, VALUE self)
563563
*
564564
* Returns +commit+'s header field value.
565565
*/
566-
static VALUE rb_git_commit_header_field(VALUE self, VALUE rb_field) {
566+
static VALUE rb_git_commit_header_field(VALUE self, VALUE rb_field)
567+
{
567568
git_buf header_field = { 0 };
569+
git_commit *commit = NULL;
570+
571+
const char *encoding_name;
572+
rb_encoding *encoding = rb_utf8_encoding();
568573
VALUE rb_result;
569-
git_commit *commit;
574+
570575
int error;
571576

572577
Check_Type(rb_field, T_STRING);
573-
574578
Data_Get_Struct(self, git_commit, commit);
575579

576580
error = git_commit_header_field(&header_field, commit, StringValueCStr(rb_field));
577581

578-
if (error == GIT_ENOTFOUND) {
582+
if (error < 0) {
579583
git_buf_free(&header_field);
580-
return Qnil;
584+
if (error == GIT_ENOTFOUND)
585+
return Qnil;
586+
rugged_exception_check(error);
581587
}
582588

583-
rugged_exception_check(error);
584-
585-
rb_result = rb_enc_str_new(header_field.ptr, header_field.size, rb_utf8_encoding());
589+
encoding_name = git_commit_message_encoding(commit);
590+
if (encoding_name != NULL)
591+
encoding = rb_enc_find(encoding_name);
586592

593+
rb_result = rb_enc_str_new(header_field.ptr, header_field.size, encoding);
587594
git_buf_free(&header_field);
588-
589595
return rb_result;
590596
}
591597

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-
619598
/*
620599
* call-seq:
621600
* commit.header -> str
622601
*
623602
* Returns +commit+'s entire raw header.
624603
*/
625-
static VALUE rb_git_commit_header(VALUE self) {
626-
VALUE rb_result;
604+
static VALUE rb_git_commit_header(VALUE self)
605+
{
627606
git_commit *commit;
628607
const char *raw_header;
629608

630609
Data_Get_Struct(self, git_commit, commit);
631610

632611
raw_header = git_commit_raw_header(commit);
633-
634-
rb_result = rb_enc_str_new(raw_header, strlen(raw_header), rb_utf8_encoding());
635-
636-
return rb_result;
612+
return rb_str_new_utf8(raw_header);
637613
}
638614

639615
void Init_rugged_commit(void)
@@ -660,6 +636,5 @@ void Init_rugged_commit(void)
660636
rb_define_method(rb_cRuggedCommit, "to_mbox", rb_git_commit_to_mbox, -1);
661637

662638
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);
664639
rb_define_method(rb_cRuggedCommit, "header", rb_git_commit_header, 0);
665640
}

lib/rugged/commit.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ def inspect
99
"#<Rugged::Commit:#{object_id} {message: #{message.inspect}, tree: #{tree.inspect}, parents: #{parent_oids}}>"
1010
end
1111

12+
def header_field?(field)
13+
!!header_field(field)
14+
end
15+
1216
# Return a diff between this commit and its first parent or another commit or tree.
1317
#
1418
# See Rugged::Tree#diff for more details.

0 commit comments

Comments
 (0)