rdavila-rugged 0.24.0b13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +619 -0
  4. data/ext/rugged/extconf.rb +105 -0
  5. data/ext/rugged/rugged.c +527 -0
  6. data/ext/rugged/rugged.h +185 -0
  7. data/ext/rugged/rugged_backend.c +34 -0
  8. data/ext/rugged/rugged_blame.c +292 -0
  9. data/ext/rugged/rugged_blob.c +638 -0
  10. data/ext/rugged/rugged_branch.c +195 -0
  11. data/ext/rugged/rugged_branch_collection.c +408 -0
  12. data/ext/rugged/rugged_commit.c +691 -0
  13. data/ext/rugged/rugged_config.c +404 -0
  14. data/ext/rugged/rugged_cred.c +148 -0
  15. data/ext/rugged/rugged_diff.c +686 -0
  16. data/ext/rugged/rugged_diff_delta.c +105 -0
  17. data/ext/rugged/rugged_diff_hunk.c +103 -0
  18. data/ext/rugged/rugged_diff_line.c +83 -0
  19. data/ext/rugged/rugged_index.c +1255 -0
  20. data/ext/rugged/rugged_note.c +376 -0
  21. data/ext/rugged/rugged_object.c +383 -0
  22. data/ext/rugged/rugged_patch.c +245 -0
  23. data/ext/rugged/rugged_reference.c +396 -0
  24. data/ext/rugged/rugged_reference_collection.c +446 -0
  25. data/ext/rugged/rugged_remote.c +691 -0
  26. data/ext/rugged/rugged_remote_collection.c +457 -0
  27. data/ext/rugged/rugged_repo.c +2669 -0
  28. data/ext/rugged/rugged_revwalk.c +495 -0
  29. data/ext/rugged/rugged_settings.c +155 -0
  30. data/ext/rugged/rugged_signature.c +106 -0
  31. data/ext/rugged/rugged_submodule.c +852 -0
  32. data/ext/rugged/rugged_submodule_collection.c +384 -0
  33. data/ext/rugged/rugged_tag.c +251 -0
  34. data/ext/rugged/rugged_tag_collection.c +347 -0
  35. data/ext/rugged/rugged_tree.c +919 -0
  36. data/lib/rugged.rb +23 -0
  37. data/lib/rugged/attributes.rb +41 -0
  38. data/lib/rugged/blob.rb +28 -0
  39. data/lib/rugged/branch.rb +19 -0
  40. data/lib/rugged/commit.rb +54 -0
  41. data/lib/rugged/console.rb +9 -0
  42. data/lib/rugged/credentials.rb +43 -0
  43. data/lib/rugged/diff.rb +20 -0
  44. data/lib/rugged/diff/delta.rb +53 -0
  45. data/lib/rugged/diff/hunk.rb +18 -0
  46. data/lib/rugged/diff/line.rb +47 -0
  47. data/lib/rugged/index.rb +13 -0
  48. data/lib/rugged/object.rb +7 -0
  49. data/lib/rugged/patch.rb +36 -0
  50. data/lib/rugged/reference.rb +7 -0
  51. data/lib/rugged/remote.rb +4 -0
  52. data/lib/rugged/repository.rb +227 -0
  53. data/lib/rugged/submodule_collection.rb +48 -0
  54. data/lib/rugged/tag.rb +50 -0
  55. data/lib/rugged/tree.rb +38 -0
  56. data/lib/rugged/version.rb +3 -0
  57. data/lib/rugged/walker.rb +5 -0
  58. metadata +146 -0
@@ -0,0 +1,691 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2014 GitHub, Inc
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ * THE SOFTWARE.
23
+ */
24
+
25
+ #include "rugged.h"
26
+ #include "git2/commit.h"
27
+
28
+ extern VALUE rb_mRugged;
29
+ extern VALUE rb_cRuggedObject;
30
+ extern VALUE rb_cRuggedRepo;
31
+ extern VALUE rb_cRuggedSignature;
32
+ VALUE rb_cRuggedCommit;
33
+
34
+ /*
35
+ * call-seq:
36
+ * commit.message -> msg
37
+ *
38
+ * Return the message of this commit. This includes the full body of the
39
+ * message, with the short description, detailed descritpion, and any
40
+ * optional footers or signatures after it.
41
+ *
42
+ * In Ruby 1.9+, the returned string will be encoded with the encoding
43
+ * specified in the +Encoding+ header of the commit, if available.
44
+ *
45
+ * commit.message #=> "add a lot of RDoc docs\n\nthis includes docs for commit and blob"
46
+ */
47
+ static VALUE rb_git_commit_message_GET(VALUE self)
48
+ {
49
+ git_commit *commit;
50
+ rb_encoding *encoding = rb_utf8_encoding();
51
+ const char *encoding_name;
52
+ const char *message;
53
+
54
+ Data_Get_Struct(self, git_commit, commit);
55
+
56
+ message = git_commit_message(commit);
57
+ encoding_name = git_commit_message_encoding(commit);
58
+ if (encoding_name != NULL)
59
+ encoding = rb_enc_find(encoding_name);
60
+
61
+ return rb_enc_str_new(message, strlen(message), encoding);
62
+ }
63
+
64
+ /*
65
+ * call-seq:
66
+ * commit.committer -> signature
67
+ *
68
+ * Return the signature for the committer of this +commit+. The signature
69
+ * is returned as a +Hash+ containing +:name+, +:email+ of the author
70
+ * and +:time+ of the change.
71
+ *
72
+ * The committer of a commit is the person who actually applied the changes
73
+ * of the commit; in most cases it's the same as the author.
74
+ *
75
+ * In Ruby 1.9+, the returned string will be encoded with the encoding
76
+ * specified in the +Encoding+ header of the commit, if available.
77
+ *
78
+ * commit.committer #=> {:email=>"tanoku@gmail.com", :time=>Tue Jan 24 05:42:45 UTC 2012, :name=>"Vicent Mart\303\255"}
79
+ */
80
+ static VALUE rb_git_commit_committer_GET(VALUE self)
81
+ {
82
+ git_commit *commit;
83
+ Data_Get_Struct(self, git_commit, commit);
84
+
85
+ return rugged_signature_new(
86
+ git_commit_committer(commit),
87
+ git_commit_message_encoding(commit));
88
+ }
89
+
90
+ /*
91
+ * call-seq:
92
+ * commit.author -> signature
93
+ *
94
+ * Return the signature for the author of this +commit+. The signature
95
+ * is returned as a +Hash+ containing +:name+, +:email+ of the author
96
+ * and +:time+ of the change.
97
+ *
98
+ * The author of the commit is the person who intially created the changes.
99
+ *
100
+ * In Ruby 1.9+, the returned string will be encoded with the encoding
101
+ * specified in the +Encoding+ header of the commit, if available.
102
+ *
103
+ * commit.author #=> {:email=>"tanoku@gmail.com", :time=>Tue Jan 24 05:42:45 UTC 2012, :name=>"Vicent Mart\303\255"}
104
+ */
105
+ static VALUE rb_git_commit_author_GET(VALUE self)
106
+ {
107
+ git_commit *commit;
108
+ Data_Get_Struct(self, git_commit, commit);
109
+
110
+ return rugged_signature_new(
111
+ git_commit_author(commit),
112
+ git_commit_message_encoding(commit));
113
+ }
114
+
115
+ /*
116
+ * call-seq:
117
+ * commit.epoch_time -> int
118
+ *
119
+ * Return the time when this commit was made effective. This is the same value
120
+ * as the +:time+ attribute for +commit.committer+, but represented as an +Integer+
121
+ * value in seconds since the Epoch.
122
+ *
123
+ * commit.time #=> 1327383765
124
+ */
125
+ static VALUE rb_git_commit_epoch_time_GET(VALUE self)
126
+ {
127
+ git_commit *commit;
128
+ Data_Get_Struct(self, git_commit, commit);
129
+
130
+ return ULONG2NUM(git_commit_time(commit));
131
+ }
132
+
133
+ /*
134
+ * call-seq:
135
+ * commit.tree -> tree
136
+ *
137
+ * Return the tree pointed at by this +commit+. The tree is
138
+ * returned as a +Rugged::Tree+ object.
139
+ *
140
+ * commit.tree #=> #<Rugged::Tree:0x10882c680>
141
+ */
142
+ static VALUE rb_git_commit_tree_GET(VALUE self)
143
+ {
144
+ git_commit *commit;
145
+ git_tree *tree;
146
+ VALUE owner;
147
+ int error;
148
+
149
+ Data_Get_Struct(self, git_commit, commit);
150
+ owner = rugged_owner(self);
151
+
152
+ error = git_commit_tree(&tree, commit);
153
+ rugged_exception_check(error);
154
+
155
+ return rugged_object_new(owner, (git_object *)tree);
156
+ }
157
+
158
+ /*
159
+ * call-seq:
160
+ * commit.tree_id -> oid
161
+ *
162
+ * Return the tree oid pointed at by this +commit+. The tree is
163
+ * returned as a String object.
164
+ *
165
+ * commit.tree_id #=> "f148106ca58764adc93ad4e2d6b1d168422b9796"
166
+ */
167
+ static VALUE rb_git_commit_tree_id_GET(VALUE self)
168
+ {
169
+ git_commit *commit;
170
+ const git_oid *tree_id;
171
+
172
+ Data_Get_Struct(self, git_commit, commit);
173
+
174
+ tree_id = git_commit_tree_id(commit);
175
+
176
+ return rugged_create_oid(tree_id);
177
+ }
178
+
179
+ /*
180
+ * call-seq:
181
+ * commit.parents -> [commit, ...]
182
+ *
183
+ * Return the parent(s) of this commit as an array of +Rugged::Commit+
184
+ * objects. An array is always returned even when the commit has only
185
+ * one or zero parents.
186
+ *
187
+ * commit.parents #=> => [#<Rugged::Commit:0x108828918>]
188
+ * root.parents #=> []
189
+ */
190
+ static VALUE rb_git_commit_parents_GET(VALUE self)
191
+ {
192
+ git_commit *commit;
193
+ git_commit *parent;
194
+ unsigned int n, parent_count;
195
+ VALUE ret_arr, owner;
196
+ int error;
197
+
198
+ Data_Get_Struct(self, git_commit, commit);
199
+ owner = rugged_owner(self);
200
+
201
+ parent_count = git_commit_parentcount(commit);
202
+ ret_arr = rb_ary_new2((long)parent_count);
203
+
204
+ for (n = 0; n < parent_count; n++) {
205
+ error = git_commit_parent(&parent, commit, n);
206
+ rugged_exception_check(error);
207
+ rb_ary_push(ret_arr, rugged_object_new(owner, (git_object *)parent));
208
+ }
209
+
210
+ return ret_arr;
211
+ }
212
+
213
+ /*
214
+ * call-seq:
215
+ * commit.parent_ids -> [oid, ...]
216
+ *
217
+ * Return the parent oid(s) of this commit as an array of oid String
218
+ * objects. An array is always returned even when the commit has only
219
+ * one or zero parents.
220
+ *
221
+ * commit.parent_ids #=> => ["2cb831a8aea28b2c1b9c63385585b864e4d3bad1", ...]
222
+ * root.parent_ids #=> []
223
+ */
224
+ static VALUE rb_git_commit_parent_ids_GET(VALUE self)
225
+ {
226
+ git_commit *commit;
227
+ const git_oid *parent_id;
228
+ unsigned int n, parent_count;
229
+ VALUE ret_arr;
230
+
231
+ Data_Get_Struct(self, git_commit, commit);
232
+
233
+ parent_count = git_commit_parentcount(commit);
234
+ ret_arr = rb_ary_new2((long)parent_count);
235
+
236
+ for (n = 0; n < parent_count; n++) {
237
+ parent_id = git_commit_parent_id(commit, n);
238
+ if (parent_id) {
239
+ rb_ary_push(ret_arr, rugged_create_oid(parent_id));
240
+ }
241
+ }
242
+
243
+ return ret_arr;
244
+ }
245
+
246
+ /*
247
+ * call-seq:
248
+ * commit.amend(data = {}) -> oid
249
+ *
250
+ * Amend a commit object, with the given +data+
251
+ * arguments, passed as a +Hash+:
252
+ *
253
+ * - +:message+: a string with the full text for the commit's message
254
+ * - +:committer+ (optional): a hash with the signature for the committer,
255
+ * defaults to the signature from the configuration
256
+ * - +:author+ (optional): a hash with the signature for the author,
257
+ * defaults to the signature from the configuration
258
+ * - +:tree+: the tree for this amended commit, represented as a <tt>Rugged::Tree</tt>
259
+ * instance or an OID +String+.
260
+ * - +:update_ref+ (optional): a +String+ with the name of a reference in the
261
+ * repository which should be updated to point to this amended commit (e.g. "HEAD")
262
+ *
263
+ * When the amended commit is successfully written to disk, its +oid+ will be
264
+ * returned as a hex +String+.
265
+ *
266
+ * author = {:email=>"tanoku@gmail.com", :time=>Time.now, :name=>"Vicent Mart\303\255"}
267
+ *
268
+ * commit.amend(
269
+ * :author => author,
270
+ * :message => "Updated Hello world\n\n",
271
+ * :committer => author,
272
+ * :tree => some_tree) #=> "f148106ca58764adc93ad4e2d6b1d168422b9796"
273
+ */
274
+ static VALUE rb_git_commit_amend(VALUE self, VALUE rb_data)
275
+ {
276
+ VALUE rb_message, rb_tree, rb_ref, owner;
277
+ int error = 0;
278
+ git_commit *commit_to_amend;
279
+ char *message = NULL;
280
+ git_tree *tree = NULL;
281
+ git_signature *author = NULL, *committer = NULL;
282
+ git_oid commit_oid;
283
+ git_repository *repo;
284
+ const char *update_ref = NULL;
285
+
286
+ Check_Type(rb_data, T_HASH);
287
+
288
+ Data_Get_Struct(self, git_commit, commit_to_amend);
289
+
290
+ owner = rugged_owner(self);
291
+ Data_Get_Struct(owner, git_repository, repo);
292
+
293
+ rb_ref = rb_hash_aref(rb_data, CSTR2SYM("update_ref"));
294
+ if (!NIL_P(rb_ref)) {
295
+ Check_Type(rb_ref, T_STRING);
296
+ update_ref = StringValueCStr(rb_ref);
297
+ }
298
+
299
+ rb_message = rb_hash_aref(rb_data, CSTR2SYM("message"));
300
+ if (!NIL_P(rb_message)) {
301
+ Check_Type(rb_message, T_STRING);
302
+ message = StringValueCStr(rb_message);
303
+ }
304
+
305
+ rb_tree = rb_hash_aref(rb_data, CSTR2SYM("tree"));
306
+ if (!NIL_P(rb_tree))
307
+ tree = (git_tree *)rugged_object_get(repo, rb_tree, GIT_OBJ_TREE);
308
+
309
+ if (!NIL_P(rb_hash_aref(rb_data, CSTR2SYM("committer")))) {
310
+ committer = rugged_signature_get(
311
+ rb_hash_aref(rb_data, CSTR2SYM("committer")), repo
312
+ );
313
+ }
314
+
315
+ if (!NIL_P(rb_hash_aref(rb_data, CSTR2SYM("author")))) {
316
+ author = rugged_signature_get(
317
+ rb_hash_aref(rb_data, CSTR2SYM("author")), repo
318
+ );
319
+ }
320
+
321
+ error = git_commit_amend(
322
+ &commit_oid,
323
+ commit_to_amend,
324
+ update_ref,
325
+ author,
326
+ committer,
327
+ NULL,
328
+ message,
329
+ tree);
330
+
331
+ git_signature_free(author);
332
+ git_signature_free(committer);
333
+
334
+ git_object_free((git_object *)tree);
335
+
336
+ rugged_exception_check(error);
337
+
338
+ return rugged_create_oid(&commit_oid);
339
+ }
340
+
341
+ /*
342
+ * call-seq:
343
+ * Commit.create(repository, data = {}) -> oid
344
+ *
345
+ * Write a new +Commit+ object to +repository+, with the given +data+
346
+ * arguments, passed as a +Hash+:
347
+ *
348
+ * - +:message+: a string with the full text for the commit's message
349
+ * - +:committer+ (optional): a hash with the signature for the committer,
350
+ * defaults to the signature from the configuration
351
+ * - +:author+ (optional): a hash with the signature for the author,
352
+ * defaults to the signature from the configuration
353
+ * - +:parents+: an +Array+ with zero or more parents for this commit,
354
+ * represented as <tt>Rugged::Commit</tt> instances, or OID +String+.
355
+ * - +:tree+: the tree for this commit, represented as a <tt>Rugged::Tree</tt>
356
+ * instance or an OID +String+.
357
+ * - +:update_ref+ (optional): a +String+ with the name of a reference in the
358
+ * repository which should be updated to point to this commit (e.g. "HEAD")
359
+ *
360
+ * When the commit is successfully written to disk, its +oid+ will be
361
+ * returned as a hex +String+.
362
+ *
363
+ * author = {:email=>"tanoku@gmail.com", :time=>Time.now, :name=>"Vicent Mart\303\255"}
364
+ *
365
+ * Rugged::Commit.create(r,
366
+ * :author => author,
367
+ * :message => "Hello world\n\n",
368
+ * :committer => author,
369
+ * :parents => ["2cb831a8aea28b2c1b9c63385585b864e4d3bad1"],
370
+ * :tree => some_tree) #=> "f148106ca58764adc93ad4e2d6b1d168422b9796"
371
+ */
372
+ static VALUE rb_git_commit_create(VALUE self, VALUE rb_repo, VALUE rb_data)
373
+ {
374
+ VALUE rb_message, rb_tree, rb_parents, rb_ref;
375
+ VALUE rb_err_obj = Qnil;
376
+ int parent_count, i, error = 0;
377
+ const git_commit **parents = NULL;
378
+ git_commit **free_list = NULL;
379
+ git_tree *tree;
380
+ git_signature *author, *committer;
381
+ git_oid commit_oid;
382
+ git_repository *repo;
383
+ const char *update_ref = NULL;
384
+
385
+ Check_Type(rb_data, T_HASH);
386
+
387
+ rugged_check_repo(rb_repo);
388
+ Data_Get_Struct(rb_repo, git_repository, repo);
389
+
390
+ rb_ref = rb_hash_aref(rb_data, CSTR2SYM("update_ref"));
391
+ if (!NIL_P(rb_ref)) {
392
+ Check_Type(rb_ref, T_STRING);
393
+ update_ref = StringValueCStr(rb_ref);
394
+ }
395
+
396
+ rb_message = rb_hash_aref(rb_data, CSTR2SYM("message"));
397
+ Check_Type(rb_message, T_STRING);
398
+
399
+ committer = rugged_signature_get(
400
+ rb_hash_aref(rb_data, CSTR2SYM("committer")), repo
401
+ );
402
+
403
+ author = rugged_signature_get(
404
+ rb_hash_aref(rb_data, CSTR2SYM("author")), repo
405
+ );
406
+
407
+ rb_parents = rb_hash_aref(rb_data, CSTR2SYM("parents"));
408
+ Check_Type(rb_parents, T_ARRAY);
409
+
410
+ rb_tree = rb_hash_aref(rb_data, CSTR2SYM("tree"));
411
+ tree = (git_tree *)rugged_object_get(repo, rb_tree, GIT_OBJ_TREE);
412
+
413
+ parents = alloca(RARRAY_LEN(rb_parents) * sizeof(void *));
414
+ free_list = alloca(RARRAY_LEN(rb_parents) * sizeof(void *));
415
+ parent_count = 0;
416
+
417
+ for (i = 0; i < (int)RARRAY_LEN(rb_parents); ++i) {
418
+ VALUE p = rb_ary_entry(rb_parents, i);
419
+ git_commit *parent = NULL;
420
+ git_commit *free_ptr = NULL;
421
+
422
+ if (NIL_P(p))
423
+ continue;
424
+
425
+ if (TYPE(p) == T_STRING) {
426
+ git_oid oid;
427
+
428
+ error = git_oid_fromstr(&oid, StringValueCStr(p));
429
+ if (error < GIT_OK)
430
+ goto cleanup;
431
+
432
+ error = git_commit_lookup(&parent, repo, &oid);
433
+ if (error < GIT_OK)
434
+ goto cleanup;
435
+
436
+ free_ptr = parent;
437
+
438
+ } else if (rb_obj_is_kind_of(p, rb_cRuggedCommit)) {
439
+ Data_Get_Struct(p, git_commit, parent);
440
+ } else {
441
+ rb_err_obj = rb_exc_new2(rb_eTypeError, "Invalid type for parent object");
442
+ goto cleanup;
443
+ }
444
+
445
+ parents[parent_count] = parent;
446
+ free_list[parent_count] = free_ptr;
447
+ parent_count++;
448
+ }
449
+
450
+ error = git_commit_create(
451
+ &commit_oid,
452
+ repo,
453
+ update_ref,
454
+ author,
455
+ committer,
456
+ NULL,
457
+ StringValueCStr(rb_message),
458
+ tree,
459
+ parent_count,
460
+ parents);
461
+
462
+ cleanup:
463
+ git_signature_free(author);
464
+ git_signature_free(committer);
465
+
466
+ git_object_free((git_object *)tree);
467
+
468
+ for (i = 0; i < parent_count; ++i)
469
+ git_object_free((git_object *)free_list[i]);
470
+
471
+ if (!NIL_P(rb_err_obj))
472
+ rb_exc_raise(rb_err_obj);
473
+
474
+ rugged_exception_check(error);
475
+
476
+ return rugged_create_oid(&commit_oid);
477
+ }
478
+
479
+ /*
480
+ * call-seq:
481
+ * commit.to_mbox(options = {}) -> str
482
+ *
483
+ * Returns +commit+'s contents formatted to resemble UNIX mailbox format.
484
+ *
485
+ * Does not (yet) support merge commits.
486
+ *
487
+ * The following options can be passed in the +options+ Hash:
488
+ *
489
+ * :patch_no ::
490
+ * Number for this patch in the series. Defaults to +1+.
491
+ *
492
+ * :total_patches ::
493
+ * Total number of patches in the series. Defaults to +1+.
494
+ *
495
+ * :exclude_subject_patch_marker ::
496
+ * If set to true, no "[PATCH]" marker will be
497
+ * added to the beginning of the subject line.
498
+ *
499
+ * Additionally, you can also pass the same options as for Rugged::Tree#diff.
500
+ */
501
+ static VALUE rb_git_commit_to_mbox(int argc, VALUE *argv, VALUE self)
502
+ {
503
+ git_buf email_patch = { NULL };
504
+ git_repository *repo;
505
+ git_commit *commit;
506
+ git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
507
+ git_diff_format_email_flags_t flags = GIT_DIFF_FORMAT_EMAIL_NONE;
508
+
509
+ VALUE rb_repo = rugged_owner(self), rb_email_patch = Qnil, rb_val, rb_options;
510
+
511
+ int error;
512
+ size_t patch_no = 1, total_patches = 1;
513
+
514
+ rb_scan_args(argc, argv, ":", &rb_options);
515
+
516
+ rugged_check_repo(rb_repo);
517
+ Data_Get_Struct(rb_repo, git_repository, repo);
518
+
519
+ Data_Get_Struct(self, git_commit, commit);
520
+
521
+ if (!NIL_P(rb_options)) {
522
+ Check_Type(rb_options, T_HASH);
523
+
524
+ rb_val = rb_hash_aref(rb_options, CSTR2SYM("patch_no"));
525
+ if (!NIL_P(rb_val))
526
+ patch_no = NUM2INT(rb_val);
527
+
528
+ rb_val = rb_hash_aref(rb_options, CSTR2SYM("total_patches"));
529
+ if (!NIL_P(rb_val))
530
+ total_patches = NUM2INT(rb_val);
531
+
532
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("exclude_subject_patch_marker"))))
533
+ flags |= GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER;
534
+
535
+ rugged_parse_diff_options(&opts, rb_options);
536
+ }
537
+
538
+ error = git_diff_commit_as_email(
539
+ &email_patch,
540
+ repo,
541
+ commit,
542
+ patch_no,
543
+ total_patches,
544
+ flags,
545
+ &opts);
546
+
547
+ if (error) goto cleanup;
548
+
549
+ rb_email_patch = rb_enc_str_new(email_patch.ptr, email_patch.size, rb_utf8_encoding());
550
+
551
+ cleanup:
552
+
553
+ xfree(opts.pathspec.strings);
554
+ git_buf_free(&email_patch);
555
+ rugged_exception_check(error);
556
+
557
+ return rb_email_patch;
558
+ }
559
+
560
+ /*
561
+ * call-seq:
562
+ * commit.header_field(field_name) -> str
563
+ *
564
+ * Returns +commit+'s header field value.
565
+ */
566
+ static VALUE rb_git_commit_header_field(VALUE self, VALUE rb_field)
567
+ {
568
+ git_buf header_field = { 0 };
569
+ git_commit *commit = NULL;
570
+
571
+ const char *encoding_name;
572
+ rb_encoding *encoding = rb_utf8_encoding();
573
+ VALUE rb_result;
574
+
575
+ int error;
576
+
577
+ Check_Type(rb_field, T_STRING);
578
+ Data_Get_Struct(self, git_commit, commit);
579
+
580
+ error = git_commit_header_field(&header_field, commit, StringValueCStr(rb_field));
581
+
582
+ if (error < 0) {
583
+ git_buf_free(&header_field);
584
+ if (error == GIT_ENOTFOUND)
585
+ return Qnil;
586
+ rugged_exception_check(error);
587
+ }
588
+
589
+ encoding_name = git_commit_message_encoding(commit);
590
+ if (encoding_name != NULL)
591
+ encoding = rb_enc_find(encoding_name);
592
+
593
+ rb_result = rb_enc_str_new(header_field.ptr, header_field.size, encoding);
594
+ git_buf_free(&header_field);
595
+ return rb_result;
596
+ }
597
+
598
+ /*
599
+ * call-seq:
600
+ * commit.header -> str
601
+ *
602
+ * Returns +commit+'s entire raw header.
603
+ */
604
+ static VALUE rb_git_commit_header(VALUE self)
605
+ {
606
+ git_commit *commit;
607
+ const char *raw_header;
608
+
609
+ Data_Get_Struct(self, git_commit, commit);
610
+
611
+ raw_header = git_commit_raw_header(commit);
612
+ return rb_str_new_utf8(raw_header);
613
+ }
614
+
615
+ /*
616
+ * call-seq:
617
+ * Rugged::Commit.extract_signature(repo, commit, field_name) -> [str, str]
618
+ *
619
+ * Returns +commit+'s signature in 'field' and the signed data
620
+ *
621
+ * The signature is done over the contents of the commit without the
622
+ * signature block in the header, which is the data in the second
623
+ * element in the return array.
624
+ */
625
+ static VALUE rb_git_commit_extract_signature(int argc, VALUE *argv, VALUE self)
626
+ {
627
+ int error;
628
+ VALUE ret_arr;
629
+ git_oid commit_id;
630
+ const char *field;
631
+ git_repository *repo;
632
+ git_buf signature = {0}, signed_data = {0};
633
+ VALUE rb_repo, rb_commit, rb_field = Qnil;
634
+
635
+ rb_scan_args(argc, argv, "21", &rb_repo, &rb_commit, &rb_field);
636
+
637
+ rugged_check_repo(rb_repo);
638
+ Data_Get_Struct(rb_repo, git_repository, repo);
639
+
640
+ error = git_oid_fromstr(&commit_id, StringValueCStr(rb_commit));
641
+ rugged_exception_check(error);
642
+
643
+ field = NIL_P(rb_field) ? NULL : StringValueCStr(rb_field);
644
+ error = git_commit_extract_signature(&signature, &signed_data, repo, &commit_id, field);
645
+ if (error < 0) {
646
+ git_buf_free(&signature);
647
+ git_buf_free(&signed_data);
648
+ }
649
+
650
+ if (error == GIT_ENOTFOUND) {
651
+ ret_arr = rb_ary_new3(2, Qnil, Qnil);
652
+ } else {
653
+ rugged_exception_check(error);
654
+
655
+ ret_arr = rb_ary_new3(2, rb_str_new(signature.ptr, signature.size),
656
+ rb_str_new(signed_data.ptr, signed_data.size));
657
+ }
658
+
659
+ git_buf_free(&signature);
660
+ git_buf_free(&signed_data);
661
+
662
+ return ret_arr;
663
+ }
664
+
665
+ void Init_rugged_commit(void)
666
+ {
667
+ rb_cRuggedCommit = rb_define_class_under(rb_mRugged, "Commit", rb_cRuggedObject);
668
+
669
+ rb_define_singleton_method(rb_cRuggedCommit, "create", rb_git_commit_create, 2);
670
+ rb_define_singleton_method(rb_cRuggedCommit, "extract_signature", rb_git_commit_extract_signature, -1);
671
+
672
+ rb_define_method(rb_cRuggedCommit, "message", rb_git_commit_message_GET, 0);
673
+ rb_define_method(rb_cRuggedCommit, "epoch_time", rb_git_commit_epoch_time_GET, 0);
674
+ rb_define_method(rb_cRuggedCommit, "committer", rb_git_commit_committer_GET, 0);
675
+ rb_define_method(rb_cRuggedCommit, "author", rb_git_commit_author_GET, 0);
676
+ rb_define_method(rb_cRuggedCommit, "tree", rb_git_commit_tree_GET, 0);
677
+
678
+ rb_define_method(rb_cRuggedCommit, "tree_id", rb_git_commit_tree_id_GET, 0);
679
+ rb_define_method(rb_cRuggedCommit, "tree_oid", rb_git_commit_tree_id_GET, 0);
680
+
681
+ rb_define_method(rb_cRuggedCommit, "parents", rb_git_commit_parents_GET, 0);
682
+ rb_define_method(rb_cRuggedCommit, "parent_ids", rb_git_commit_parent_ids_GET, 0);
683
+ rb_define_method(rb_cRuggedCommit, "parent_oids", rb_git_commit_parent_ids_GET, 0);
684
+
685
+ rb_define_method(rb_cRuggedCommit, "amend", rb_git_commit_amend, 1);
686
+
687
+ rb_define_method(rb_cRuggedCommit, "to_mbox", rb_git_commit_to_mbox, -1);
688
+
689
+ rb_define_method(rb_cRuggedCommit, "header_field", rb_git_commit_header_field, 1);
690
+ rb_define_method(rb_cRuggedCommit, "header", rb_git_commit_header, 0);
691
+ }