|
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,60 @@ 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 | +/* |
| 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 | + |
47 | 102 | /*
|
48 | 103 | * call-seq:
|
49 | 104 | * commit.summary -> summary
|
@@ -819,6 +874,7 @@ void Init_rugged_commit(void)
|
819 | 874 | rb_define_singleton_method(rb_cRuggedCommit, "extract_signature", rb_git_commit_extract_signature, -1);
|
820 | 875 |
|
821 | 876 | 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); |
822 | 878 | rb_define_method(rb_cRuggedCommit, "summary", rb_git_commit_summary_GET, 0);
|
823 | 879 | rb_define_method(rb_cRuggedCommit, "epoch_time", rb_git_commit_epoch_time_GET, 0);
|
824 | 880 | rb_define_method(rb_cRuggedCommit, "committer", rb_git_commit_committer_GET, 0);
|
|
0 commit comments