|
7 | 7 |
|
8 | 8 | #include "rugged.h"
|
9 | 9 | #include "git2/commit.h"
|
| 10 | +#include "git2/message.h" |
10 | 11 |
|
11 | 12 | extern VALUE rb_mRugged;
|
12 | 13 | extern VALUE rb_cRuggedObject;
|
@@ -44,6 +45,69 @@ static VALUE rb_git_commit_message_GET(VALUE self)
|
44 | 45 | return rb_enc_str_new(message, strlen(message), encoding);
|
45 | 46 | }
|
46 | 47 |
|
| 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 | + |
47 | 111 | /*
|
48 | 112 | * call-seq:
|
49 | 113 | * commit.summary -> summary
|
@@ -819,6 +883,7 @@ void Init_rugged_commit(void)
|
819 | 883 | rb_define_singleton_method(rb_cRuggedCommit, "extract_signature", rb_git_commit_extract_signature, -1);
|
820 | 884 |
|
821 | 885 | 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); |
822 | 887 | rb_define_method(rb_cRuggedCommit, "summary", rb_git_commit_summary_GET, 0);
|
823 | 888 | rb_define_method(rb_cRuggedCommit, "epoch_time", rb_git_commit_epoch_time_GET, 0);
|
824 | 889 | rb_define_method(rb_cRuggedCommit, "committer", rb_git_commit_committer_GET, 0);
|
|
0 commit comments