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,686 @@
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
+
27
+ extern VALUE rb_mRugged;
28
+ VALUE rb_cRuggedDiff;
29
+
30
+ VALUE rugged_diff_new(VALUE klass, VALUE owner, git_diff *diff)
31
+ {
32
+ VALUE rb_diff = Data_Wrap_Struct(klass, NULL, git_diff_free, diff);
33
+ rugged_set_owner(rb_diff, owner);
34
+ return rb_diff;
35
+ }
36
+
37
+ /**
38
+ * The caller has to free the returned git_diff_options pathspec strings array.
39
+ */
40
+ void rugged_parse_diff_options(git_diff_options *opts, VALUE rb_options)
41
+ {
42
+ if (!NIL_P(rb_options)) {
43
+ VALUE rb_value;
44
+ Check_Type(rb_options, T_HASH);
45
+
46
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("reverse")))) {
47
+ opts->flags |= GIT_DIFF_REVERSE;
48
+ }
49
+
50
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("include_ignored")))) {
51
+ opts->flags |= GIT_DIFF_INCLUDE_IGNORED;
52
+ }
53
+
54
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("recurse_ignored_dirs")))) {
55
+ opts->flags |= GIT_DIFF_RECURSE_IGNORED_DIRS;
56
+ }
57
+
58
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("include_untracked")))) {
59
+ opts->flags |= GIT_DIFF_INCLUDE_UNTRACKED;
60
+ }
61
+
62
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("recurse_untracked_dirs")))) {
63
+ opts->flags |= GIT_DIFF_RECURSE_UNTRACKED_DIRS;
64
+ }
65
+
66
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("include_unmodified")))) {
67
+ opts->flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
68
+ }
69
+
70
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("include_typechange")))) {
71
+ opts->flags |= GIT_DIFF_INCLUDE_TYPECHANGE;
72
+ }
73
+
74
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("include_typechange_trees")))) {
75
+ opts->flags |= GIT_DIFF_INCLUDE_TYPECHANGE_TREES;
76
+ }
77
+
78
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("ignore_filemode")))) {
79
+ opts->flags |= GIT_DIFF_IGNORE_FILEMODE;
80
+ }
81
+
82
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("ignore_submodules")))) {
83
+ opts->flags |= GIT_DIFF_IGNORE_SUBMODULES;
84
+ }
85
+
86
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("ignore_case")))) {
87
+ opts->flags |= GIT_DIFF_IGNORE_CASE;
88
+ }
89
+
90
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("disable_pathspec_match")))) {
91
+ opts->flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
92
+ }
93
+
94
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("skip_binary_check")))) {
95
+ opts->flags |= GIT_DIFF_SKIP_BINARY_CHECK;
96
+ }
97
+
98
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("fast_untracked_dirs")))) {
99
+ opts->flags |= GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS;
100
+ }
101
+
102
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("force_text")))) {
103
+ opts->flags |= GIT_DIFF_FORCE_TEXT;
104
+ }
105
+
106
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("force_binary")))) {
107
+ opts->flags |= GIT_DIFF_FORCE_BINARY;
108
+ }
109
+
110
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("ignore_whitespace")))) {
111
+ opts->flags |= GIT_DIFF_IGNORE_WHITESPACE;
112
+ }
113
+
114
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("ignore_whitespace_change")))) {
115
+ opts->flags |= GIT_DIFF_IGNORE_WHITESPACE_CHANGE;
116
+ }
117
+
118
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("ignore_whitespace_eol")))) {
119
+ opts->flags |= GIT_DIFF_IGNORE_WHITESPACE_EOL;
120
+ }
121
+
122
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("show_untracked_content")))) {
123
+ opts->flags |= GIT_DIFF_SHOW_UNTRACKED_CONTENT;
124
+ }
125
+
126
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("show_unmodified")))) {
127
+ opts->flags |= GIT_DIFF_SHOW_UNMODIFIED;
128
+ }
129
+
130
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("show_binary")))) {
131
+ opts->flags |= GIT_DIFF_SHOW_BINARY;
132
+ }
133
+
134
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("patience")))) {
135
+ opts->flags |= GIT_DIFF_PATIENCE;
136
+ }
137
+
138
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("minimal")))) {
139
+ opts->flags |= GIT_DIFF_MINIMAL;
140
+ }
141
+
142
+ rb_value = rb_hash_aref(rb_options, CSTR2SYM("paths"));
143
+ if (!NIL_P(rb_value)) {
144
+ int i;
145
+ Check_Type(rb_value, T_ARRAY);
146
+
147
+ for (i = 0; i < RARRAY_LEN(rb_value); ++i)
148
+ Check_Type(rb_ary_entry(rb_value, i), T_STRING);
149
+
150
+ opts->pathspec.count = RARRAY_LEN(rb_value);
151
+ opts->pathspec.strings = xmalloc(opts->pathspec.count * sizeof(char *));
152
+
153
+ for (i = 0; i < RARRAY_LEN(rb_value); ++i) {
154
+ VALUE rb_path = rb_ary_entry(rb_value, i);
155
+ opts->pathspec.strings[i] = StringValueCStr(rb_path);
156
+ }
157
+ }
158
+
159
+ rb_value = rb_hash_aref(rb_options, CSTR2SYM("context_lines"));
160
+ if (!NIL_P(rb_value)) {
161
+ Check_Type(rb_value, T_FIXNUM);
162
+ opts->context_lines = FIX2INT(rb_value);
163
+ }
164
+
165
+ rb_value = rb_hash_aref(rb_options, CSTR2SYM("interhunk_lines"));
166
+ if (!NIL_P(rb_value)) {
167
+ Check_Type(rb_value, T_FIXNUM);
168
+ opts->interhunk_lines = FIX2INT(rb_value);
169
+ }
170
+
171
+ rb_value = rb_hash_aref(rb_options, CSTR2SYM("id_abbrev"));
172
+ if (!NIL_P(rb_value)) {
173
+ Check_Type(rb_value, T_FIXNUM);
174
+ opts->id_abbrev = FIX2INT(rb_value);
175
+ }
176
+
177
+ rb_value = rb_hash_aref(rb_options, CSTR2SYM("max_size"));
178
+ if (!NIL_P(rb_value)) {
179
+ Check_Type(rb_value, T_FIXNUM);
180
+ opts->max_size = FIX2INT(rb_value);
181
+ }
182
+
183
+ rb_value = rb_hash_aref(rb_options, CSTR2SYM("old_prefix"));
184
+ if (!NIL_P(rb_value)) {
185
+ Check_Type(rb_value, T_STRING);
186
+ opts->old_prefix = StringValueCStr(rb_value);
187
+ }
188
+
189
+ rb_value = rb_hash_aref(rb_options, CSTR2SYM("new_prefix"));
190
+ if (!NIL_P(rb_value)) {
191
+ Check_Type(rb_value, T_STRING);
192
+ opts->new_prefix = StringValueCStr(rb_value);
193
+ }
194
+ }
195
+ }
196
+
197
+ static int diff_print_cb(
198
+ const git_diff_delta *delta,
199
+ const git_diff_hunk *hunk,
200
+ const git_diff_line *line,
201
+ void *payload)
202
+ {
203
+ VALUE rb_str = (VALUE)payload;
204
+
205
+ switch (line->origin) {
206
+ case GIT_DIFF_LINE_CONTEXT:
207
+ case GIT_DIFF_LINE_ADDITION:
208
+ case GIT_DIFF_LINE_DELETION:
209
+ rb_str_cat(rb_str, &line->origin, 1);
210
+ }
211
+
212
+ rb_str_cat(rb_str, line->content, line->content_len);
213
+
214
+ return GIT_OK;
215
+ }
216
+
217
+ /*
218
+ * call-seq:
219
+ * diff.patch -> patch
220
+ * diff.patch(:compact => true) -> compact_patch
221
+ *
222
+ * Return a string containing the diff in patch form.
223
+ */
224
+ static VALUE rb_git_diff_patch(int argc, VALUE *argv, VALUE self)
225
+ {
226
+ git_diff *diff;
227
+ VALUE rb_str = rb_str_new(NULL, 0);
228
+ VALUE rb_opts;
229
+
230
+ rb_scan_args(argc, argv, "00:", &rb_opts);
231
+
232
+ Data_Get_Struct(self, git_diff, diff);
233
+
234
+ if (!NIL_P(rb_opts)) {
235
+ if (rb_hash_aref(rb_opts, CSTR2SYM("compact")) == Qtrue)
236
+ git_diff_print(diff, GIT_DIFF_FORMAT_NAME_STATUS, diff_print_cb, (void*)rb_str);
237
+ else
238
+ git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, diff_print_cb, (void*)rb_str);
239
+ } else {
240
+ git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, diff_print_cb, (void*)rb_str);
241
+ }
242
+
243
+ return rb_str;
244
+ }
245
+
246
+ static int diff_write_cb(
247
+ const git_diff_delta *delta,
248
+ const git_diff_hunk *hunk,
249
+ const git_diff_line *line,
250
+ void *payload)
251
+ {
252
+ VALUE rb_io = (VALUE)payload, str = rb_str_new(line->content, line->content_len);
253
+
254
+ rb_io_write(rb_io, str);
255
+
256
+ return GIT_OK;
257
+ }
258
+
259
+ /*
260
+ * call-seq:
261
+ * diff.write_patch(io) -> nil
262
+ * diff.write_patch(io, :compact => true) -> nil
263
+ *
264
+ * Write a patch directly to an object which responds to "write".
265
+ */
266
+ static VALUE rb_git_diff_write_patch(int argc, VALUE *argv, VALUE self)
267
+ {
268
+ git_diff *diff;
269
+ VALUE rb_io, rb_opts;
270
+
271
+ rb_scan_args(argc, argv, "10:", &rb_io, &rb_opts);
272
+
273
+ if (!rb_respond_to(rb_io, rb_intern("write")))
274
+ rb_raise(rb_eArgError, "Expected io to respond to \"write\"");
275
+
276
+ Data_Get_Struct(self, git_diff, diff);
277
+
278
+ if (!NIL_P(rb_opts)) {
279
+ if (rb_hash_aref(rb_opts, CSTR2SYM("compact")) == Qtrue)
280
+ git_diff_print(diff, GIT_DIFF_FORMAT_NAME_STATUS, diff_write_cb, (void*)rb_io);
281
+ else
282
+ git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, diff_write_cb, (void*)rb_io);
283
+ } else {
284
+ git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, diff_write_cb, (void*)rb_io);
285
+ }
286
+
287
+ return Qnil;
288
+ }
289
+
290
+ /*
291
+ * call-seq:
292
+ * diff.merge!(other_diff) -> self
293
+ *
294
+ * Merges all diff information from +other_diff+.
295
+ */
296
+ static VALUE rb_git_diff_merge(VALUE self, VALUE rb_other)
297
+ {
298
+ git_diff *diff;
299
+ git_diff *other;
300
+ int error;
301
+
302
+ if (!rb_obj_is_kind_of(rb_other, rb_cRuggedDiff))
303
+ rb_raise(rb_eTypeError, "A Rugged::Diff instance is required");
304
+
305
+ Data_Get_Struct(self, git_diff, diff);
306
+ Data_Get_Struct(rb_other, git_diff, other);
307
+
308
+ error = git_diff_merge(diff, other);
309
+ rugged_exception_check(error);
310
+
311
+ return self;
312
+ }
313
+
314
+ /*
315
+ * call-seq:
316
+ * diff.find_similar!([options]) -> self
317
+ *
318
+ * Detects entries in the diff that look like renames or copies (based on the
319
+ * given options) and replaces them with actual rename or copy entries.
320
+ *
321
+ * Additionally, modified files can be broken into add/delete pairs if the
322
+ * amount of changes are above a specific threshold (see +:break_rewrite_threshold+).
323
+ *
324
+ * By default, similarity will be measured without leading whitespace. You
325
+ * you can use the +:dont_ignore_whitespace+ to disable this.
326
+ *
327
+ * The following options can be passed in the +options+ Hash:
328
+ *
329
+ * :rename_threshold ::
330
+ * An integer specifying the similarity to consider a file renamed (default 50).
331
+ *
332
+ * :rename_from_rewrite_threshold ::
333
+ * An integer specifying the similarity of modified to be eligible
334
+ * rename source (default 50).
335
+ *
336
+ * :copy_threshold ::
337
+ * An integer specifying the similarity to consider a file a copy (default 50).
338
+ *
339
+ * :break_rewrite_threshold ::
340
+ * An integer specifying the similarity to split modify into delete/add pair (default 60).
341
+ *
342
+ * :rename_limit ::
343
+ * An integer specifying the maximum amount of similarity sources to examine
344
+ * (a la diff's +-l+ option or the +diff.renameLimit+ config) (default 200).
345
+ *
346
+ * :renames ::
347
+ * If true, looking for renames will be enabled (+--find-renames+).
348
+ *
349
+ * :renames_from_rewrites ::
350
+ * If true, the "old side" of modified files will be considered for renames (+--break-rewrites=N+).
351
+ *
352
+ * :copies ::
353
+ * If true, looking for copies will be enabled (+--find-copies+).
354
+ *
355
+ * :copies_from_unmodified ::
356
+ * If true, unmodified files will be considered as copy sources (+--find-copies-harder+).
357
+ *
358
+ * :break_rewrites ::
359
+ * If true, larger rewrites will be split into delete/add pairs (+--break-rewrites=/M+).
360
+ *
361
+ * :all ::
362
+ * If true, enables all finding features.
363
+ *
364
+ * :ignore_whitespace ::
365
+ * If true, similarity will be measured with all whitespace ignored.
366
+ *
367
+ * :dont_ignore_whitespace ::
368
+ * If true, similarity will be measured without ignoring any whitespace.
369
+ *
370
+ */
371
+ static VALUE rb_git_diff_find_similar(int argc, VALUE *argv, VALUE self)
372
+ {
373
+ git_diff *diff;
374
+ git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
375
+ VALUE rb_options;
376
+ int error;
377
+
378
+ Data_Get_Struct(self, git_diff, diff);
379
+
380
+ rb_scan_args(argc, argv, "00:", &rb_options);
381
+
382
+ if (!NIL_P(rb_options)) {
383
+ VALUE rb_value = rb_hash_aref(rb_options, CSTR2SYM("rename_threshold"));
384
+ if (!NIL_P(rb_value)) {
385
+ Check_Type(rb_value, T_FIXNUM);
386
+ opts.rename_threshold = FIX2INT(rb_value);
387
+ }
388
+
389
+ rb_value = rb_hash_aref(rb_options, CSTR2SYM("rename_from_rewrite_threshold"));
390
+ if (!NIL_P(rb_value)) {
391
+ Check_Type(rb_value, T_FIXNUM);
392
+ opts.rename_from_rewrite_threshold = FIX2INT(rb_value);
393
+ }
394
+
395
+ rb_value = rb_hash_aref(rb_options, CSTR2SYM("copy_threshold"));
396
+ if (!NIL_P(rb_value)) {
397
+ Check_Type(rb_value, T_FIXNUM);
398
+ opts.copy_threshold = FIX2INT(rb_value);
399
+ }
400
+
401
+ rb_value = rb_hash_aref(rb_options, CSTR2SYM("break_rewrite_threshold"));
402
+ if (!NIL_P(rb_value)) {
403
+ Check_Type(rb_value, T_FIXNUM);
404
+ opts.break_rewrite_threshold = FIX2INT(rb_value);
405
+ }
406
+
407
+ rb_value = rb_hash_aref(rb_options, CSTR2SYM("rename_limit"));
408
+ if (!NIL_P(rb_value)) {
409
+ Check_Type(rb_value, T_FIXNUM);
410
+ opts.rename_limit = FIX2INT(rb_value);
411
+ }
412
+
413
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("renames")))) {
414
+ opts.flags |= GIT_DIFF_FIND_RENAMES;
415
+ }
416
+
417
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("renames_from_rewrites")))) {
418
+ opts.flags |= GIT_DIFF_FIND_RENAMES_FROM_REWRITES;
419
+ }
420
+
421
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("copies")))) {
422
+ opts.flags |= GIT_DIFF_FIND_COPIES;
423
+ }
424
+
425
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("copies_from_unmodified")))) {
426
+ opts.flags |= GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED;
427
+ }
428
+
429
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("break_rewrites")))) {
430
+ opts.flags |= GIT_DIFF_FIND_AND_BREAK_REWRITES;
431
+ }
432
+
433
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("all")))) {
434
+ opts.flags |= GIT_DIFF_FIND_ALL;
435
+ }
436
+
437
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("ignore_whitespace")))) {
438
+ opts.flags |= GIT_DIFF_FIND_IGNORE_WHITESPACE;
439
+ }
440
+
441
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("dont_ignore_whitespace")))) {
442
+ opts.flags |= GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE;
443
+ }
444
+ }
445
+
446
+ error = git_diff_find_similar(diff, &opts);
447
+ rugged_exception_check(error);
448
+
449
+ return self;
450
+ }
451
+
452
+ /*
453
+ * call-seq:
454
+ * diff.each_patch { |patch| } -> self
455
+ * diff.each_patch -> enumerator
456
+ *
457
+ * If given a block, yields each patch that is part of the diff.
458
+ * If no block is given, an enumerator will be returned.
459
+ */
460
+ static VALUE rb_git_diff_each_patch(VALUE self)
461
+ {
462
+ git_diff *diff;
463
+ git_patch *patch;
464
+ int error = 0;
465
+ size_t d, delta_count;
466
+
467
+ if (!rb_block_given_p()) {
468
+ return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_patch"), self);
469
+ }
470
+
471
+ Data_Get_Struct(self, git_diff, diff);
472
+
473
+ delta_count = git_diff_num_deltas(diff);
474
+ for (d = 0; d < delta_count; ++d) {
475
+ error = git_patch_from_diff(&patch, diff, d);
476
+ if (error) break;
477
+
478
+ rb_yield(rugged_patch_new(self, patch));
479
+ }
480
+
481
+ rugged_exception_check(error);
482
+
483
+ return self;
484
+ }
485
+
486
+ /*
487
+ * call-seq:
488
+ * diff.each_delta { |delta| } -> self
489
+ * diff.each_delta -> enumerator
490
+ *
491
+ * If given a block, yields each delta that is part of the diff.
492
+ * If no block is given, an enumerator will be returned.
493
+ *
494
+ * This method should be preferred over #each_patch if you're not interested
495
+ * in the actual line-by-line changes of the diff.
496
+ */
497
+ static VALUE rb_git_diff_each_delta(VALUE self)
498
+ {
499
+ git_diff *diff;
500
+ const git_diff_delta *delta;
501
+ int error = 0;
502
+ size_t d, delta_count;
503
+
504
+ if (!rb_block_given_p()) {
505
+ return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_delta"), self);
506
+ }
507
+
508
+ Data_Get_Struct(self, git_diff, diff);
509
+
510
+ delta_count = git_diff_num_deltas(diff);
511
+ for (d = 0; d < delta_count; ++d) {
512
+ delta = git_diff_get_delta(diff, d);
513
+ rb_yield(rugged_diff_delta_new(self, delta));
514
+ }
515
+
516
+ rugged_exception_check(error);
517
+
518
+ return self;
519
+ }
520
+
521
+ static int each_line_cb(
522
+ const git_diff_delta *delta,
523
+ const git_diff_hunk *hunk,
524
+ const git_diff_line *line,
525
+ void *payload)
526
+ {
527
+ int *exception = (int *)payload;
528
+ rb_protect(rb_yield, rugged_diff_line_new(line), exception);
529
+ return *exception ? GIT_ERROR : GIT_OK;
530
+ }
531
+
532
+ /*
533
+ * call-seq:
534
+ * diff.each_line([format = :patch]) { |line| } -> self
535
+ * diff.each_line([format = :patch]) -> enumerator
536
+ */
537
+ static VALUE rb_git_diff_each_line(int argc, VALUE *argv, VALUE self)
538
+ {
539
+ VALUE rb_format;
540
+ git_diff *diff;
541
+ git_diff_format_t format;
542
+ int exception = 0, error;
543
+
544
+ Data_Get_Struct(self, git_diff, diff);
545
+
546
+ if (rb_scan_args(argc, argv, "01", &rb_format) == 1) {
547
+ Check_Type(rb_format, T_SYMBOL);
548
+ } else {
549
+ rb_format = CSTR2SYM("patch");
550
+ }
551
+
552
+ if (!rb_block_given_p())
553
+ return rb_funcall(self, rb_intern("to_enum"), 2, CSTR2SYM("each_line"), rb_format);
554
+
555
+ if (SYM2ID(rb_format) == rb_intern("patch")) {
556
+ format = GIT_DIFF_FORMAT_PATCH;
557
+ } else if (SYM2ID(rb_format) == rb_intern("patch_header")) {
558
+ format = GIT_DIFF_FORMAT_PATCH_HEADER;
559
+ } else if (SYM2ID(rb_format) == rb_intern("raw")) {
560
+ format = GIT_DIFF_FORMAT_RAW;
561
+ } else if (SYM2ID(rb_format) == rb_intern("name_only")) {
562
+ format = GIT_DIFF_FORMAT_NAME_ONLY;
563
+ } else if (SYM2ID(rb_format) == rb_intern("name_status")) {
564
+ format = GIT_DIFF_FORMAT_NAME_STATUS;
565
+ } else {
566
+ rb_raise(rb_eArgError, "unknown :format");
567
+ }
568
+
569
+ error = git_diff_print(diff, format, each_line_cb, &exception);
570
+
571
+ if (exception)
572
+ rb_jump_tag(exception);
573
+ rugged_exception_check(error);
574
+
575
+ return self;
576
+ }
577
+
578
+
579
+ /*
580
+ * call-seq: diff.size -> int
581
+ *
582
+ * Returns the number of deltas/patches in this diff.
583
+ */
584
+ static VALUE rb_git_diff_size(VALUE self)
585
+ {
586
+ git_diff *diff;
587
+
588
+ Data_Get_Struct(self, git_diff, diff);
589
+
590
+ return INT2FIX(git_diff_num_deltas(diff));
591
+ }
592
+
593
+ struct diff_stats {
594
+ size_t files, adds, dels;
595
+ };
596
+
597
+ static int diff_file_stats_cb(
598
+ const git_diff_delta *delta,
599
+ float progress,
600
+ void *payload)
601
+ {
602
+ struct diff_stats *stats = payload;
603
+
604
+ switch (delta->status) {
605
+ case GIT_DELTA_ADDED:
606
+ case GIT_DELTA_DELETED:
607
+ case GIT_DELTA_MODIFIED:
608
+ case GIT_DELTA_RENAMED:
609
+ case GIT_DELTA_COPIED:
610
+ case GIT_DELTA_TYPECHANGE:
611
+ stats->files++;
612
+ break;
613
+ default:
614
+ /* unmodified, ignored, and untracked files don't count */
615
+ break;
616
+ }
617
+ return GIT_OK;
618
+ }
619
+
620
+ static int diff_line_stats_cb(
621
+ const git_diff_delta *delta,
622
+ const git_diff_hunk *hunk,
623
+ const git_diff_line *line,
624
+ void *payload)
625
+ {
626
+ struct diff_stats *stats = payload;
627
+
628
+ switch (line->origin) {
629
+ case GIT_DIFF_LINE_ADDITION: stats->adds++; break;
630
+ case GIT_DIFF_LINE_DELETION: stats->dels++; break;
631
+ default: break;
632
+ }
633
+
634
+ return GIT_OK;
635
+ }
636
+
637
+ /*
638
+ * call-seq: diff.stat -> int, int, int
639
+ *
640
+ * Returns the number of files/additions/deletions in this diff.
641
+ */
642
+ static VALUE rb_git_diff_stat(VALUE self)
643
+ {
644
+ git_diff *diff;
645
+ struct diff_stats stats = { 0, 0, 0 };
646
+
647
+ Data_Get_Struct(self, git_diff, diff);
648
+
649
+ git_diff_foreach(
650
+ diff, diff_file_stats_cb, NULL, NULL, diff_line_stats_cb, &stats);
651
+
652
+ return rb_ary_new3(
653
+ 3, INT2FIX(stats.files), INT2FIX(stats.adds), INT2FIX(stats.dels));
654
+ }
655
+
656
+ /*
657
+ * call-seq: diff.sorted_icase?
658
+ *
659
+ * Returns true when deltas are sorted case insensitively.
660
+ */
661
+ static VALUE rb_git_diff_sorted_icase_p(VALUE self)
662
+ {
663
+ git_diff *diff;
664
+ Data_Get_Struct(self, git_diff, diff);
665
+ return git_diff_is_sorted_icase(diff) ? Qtrue : Qfalse;
666
+ }
667
+
668
+ void Init_rugged_diff(void)
669
+ {
670
+ rb_cRuggedDiff = rb_define_class_under(rb_mRugged, "Diff", rb_cObject);
671
+
672
+ rb_define_method(rb_cRuggedDiff, "patch", rb_git_diff_patch, -1);
673
+ rb_define_method(rb_cRuggedDiff, "write_patch", rb_git_diff_write_patch, -1);
674
+
675
+ rb_define_method(rb_cRuggedDiff, "find_similar!", rb_git_diff_find_similar, -1);
676
+ rb_define_method(rb_cRuggedDiff, "merge!", rb_git_diff_merge, 1);
677
+
678
+ rb_define_method(rb_cRuggedDiff, "size", rb_git_diff_size, 0);
679
+ rb_define_method(rb_cRuggedDiff, "stat", rb_git_diff_stat, 0);
680
+
681
+ rb_define_method(rb_cRuggedDiff, "sorted_icase?", rb_git_diff_sorted_icase_p, 0);
682
+
683
+ rb_define_method(rb_cRuggedDiff, "each_patch", rb_git_diff_each_patch, 0);
684
+ rb_define_method(rb_cRuggedDiff, "each_delta", rb_git_diff_each_delta, 0);
685
+ rb_define_method(rb_cRuggedDiff, "each_line", rb_git_diff_each_line, -1);
686
+ }