Skip to content

Commit 92409b5

Browse files
committed
wrap trailer parsing API
1 parent a2824ca commit 92409b5

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

ext/rugged/rugged_commit.c

Lines changed: 65 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,69 @@ static VALUE rb_git_commit_message_GET(VALUE self)
4445
return rb_enc_str_new(message, strlen(message), encoding);
4546
}
4647

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

821885
rb_define_method(rb_cRuggedCommit, "message", rb_git_commit_message_GET, 0);
886+
rb_define_method(rb_cRuggedCommit, "trailers", rb_git_commit_trailers_GET, 0);
822887
rb_define_method(rb_cRuggedCommit, "summary", rb_git_commit_summary_GET, 0);
823888
rb_define_method(rb_cRuggedCommit, "epoch_time", rb_git_commit_epoch_time_GET, 0);
824889
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)