Skip to content

Commit 435f5f0

Browse files
authored
Merge pull request #734 from libgit2/brianmario/trailers-api
Wrap trailer parsing API
2 parents d909d3f + 987bacd commit 435f5f0

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

ext/rugged/rugged_commit.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "rugged.h"
99
#include "git2/commit.h"
10+
#include "git2/message.h"
1011

1112
extern VALUE rb_mRugged;
1213
extern VALUE rb_cRuggedObject;
@@ -44,6 +45,60 @@ static VALUE rb_git_commit_message_GET(VALUE self)
4445
return rb_enc_str_new(message, strlen(message), encoding);
4546
}
4647

48+
/*
49+
* call-seq:
50+
* commit.trailers -> [["Trailer-name", "trailer value"], ...]
51+
*
52+
* Return an array of arrays, each of which is a key/value pair representing a
53+
* commit message trailer. Both the keys and values will be strings. An array
54+
* is used to preserve the order the trailers were found.
55+
*
56+
* In Ruby 1.9+, the returned strings will be encoded with the encoding
57+
* specified in the +Encoding+ header of the commit, if available.
58+
*
59+
*/
60+
static VALUE rb_git_commit_trailers_GET(VALUE self)
61+
{
62+
git_commit *commit;
63+
const char *message;
64+
rb_encoding *encoding = rb_utf8_encoding();
65+
const char *encoding_name;
66+
git_message_trailer_array arr;
67+
VALUE trailers = rb_ary_new();
68+
int error;
69+
size_t i;
70+
71+
Data_Get_Struct(self, git_commit, commit);
72+
73+
encoding_name = git_commit_message_encoding(commit);
74+
if (encoding_name != NULL)
75+
encoding = rb_enc_find(encoding_name);
76+
77+
message = git_commit_message(commit);
78+
79+
error = git_message_trailers(&arr, message);
80+
rugged_exception_check(error);
81+
82+
for(i = 0; i < arr.count; i++) {
83+
VALUE pair = rb_ary_new();
84+
const char *key = arr.trailers[i].key;
85+
const char *value = arr.trailers[i].value;
86+
87+
// trailer key
88+
rb_ary_push(pair, rb_enc_str_new(key, strlen(key), encoding));
89+
90+
// trailer value
91+
rb_ary_push(pair, rb_enc_str_new(value, strlen(value), encoding));
92+
93+
// add it to the list
94+
rb_ary_push(trailers, pair);
95+
}
96+
97+
git_message_trailer_array_free(&arr);
98+
99+
return trailers;
100+
}
101+
47102
/*
48103
* call-seq:
49104
* commit.summary -> summary
@@ -819,6 +874,7 @@ void Init_rugged_commit(void)
819874
rb_define_singleton_method(rb_cRuggedCommit, "extract_signature", rb_git_commit_extract_signature, -1);
820875

821876
rb_define_method(rb_cRuggedCommit, "message", rb_git_commit_message_GET, 0);
877+
rb_define_method(rb_cRuggedCommit, "trailers", rb_git_commit_trailers_GET, 0);
822878
rb_define_method(rb_cRuggedCommit, "summary", rb_git_commit_summary_GET, 0);
823879
rb_define_method(rb_cRuggedCommit, "epoch_time", rb_git_commit_epoch_time_GET, 0);
824880
rb_define_method(rb_cRuggedCommit, "committer", rb_git_commit_committer_GET, 0);

test/commit_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,4 +699,31 @@ def test_format_to_mbox_diff_options
699699
EOS
700700
end
701701

702+
class TrailersTest < Rugged::TestCase
703+
def setup
704+
@source_repo = FixtureRepo.from_rugged("testrepo.git")
705+
@repo = FixtureRepo.clone(@source_repo)
706+
@repo.config['core.abbrev'] = 7
707+
end
708+
709+
def test_can_parse_trailers
710+
person = {:name => 'Brian', :email => '[email protected]', :time => Time.now }
711+
712+
commit_oid = Rugged::Commit.create(@repo,
713+
:message => "This is the commit message\n\nCo-authored-by: Charles <[email protected]>\nSigned-off-by: Arthur Schreiber <[email protected]>",
714+
:committer => person,
715+
:author => person,
716+
:parents => [@repo.head.target],
717+
:tree => @repo.head.target.tree_oid)
718+
719+
commit = @repo.lookup(commit_oid)
720+
721+
expected = [
722+
["Co-authored-by", "Charles <[email protected]>"],
723+
["Signed-off-by", "Arthur Schreiber <[email protected]>"]
724+
]
725+
726+
assert_equal expected, commit.trailers
727+
end
728+
end
702729
end

0 commit comments

Comments
 (0)