Skip to content

Commit 1f3da31

Browse files
authored
Merge pull request #622 from libgit2/brianmario/add-patch-bytesize
Add Rugged::Diff::Patch#bytesize
2 parents 363edef + 88f57a8 commit 1f3da31

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

ext/rugged/rugged_patch.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,36 @@ static VALUE rb_git_diff_patch_lines(VALUE self)
190190
return INT2FIX(context + adds + dels);
191191
}
192192

193+
static VALUE rb_git_diff_patch_bytesize(int argc, VALUE *argv, VALUE self)
194+
{
195+
git_patch *patch;
196+
size_t bytesize;
197+
VALUE rb_options;
198+
int options[3];
199+
Data_Get_Struct(self, git_patch, patch);
200+
201+
memset(options, 0, sizeof(options));
202+
203+
rb_scan_args(argc, argv, "0:", &rb_options);
204+
if (!NIL_P(rb_options)) {
205+
if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("include_context")))) {
206+
options[0] = 1;
207+
}
208+
209+
if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("include_hunk_headers")))) {
210+
options[1] = 1;
211+
}
212+
213+
if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("include_file_headers")))) {
214+
options[2] = 1;
215+
}
216+
}
217+
218+
bytesize = git_patch_size(patch, options[0], options[1], options[2]);
219+
220+
return INT2FIX(bytesize);
221+
}
222+
193223
static int patch_print_cb(
194224
const git_diff_delta *delta,
195225
const git_diff_hunk *hunk,
@@ -235,6 +265,7 @@ void Init_rugged_patch(void)
235265

236266
rb_define_method(rb_cRuggedPatch, "stat", rb_git_diff_patch_stat, 0);
237267
rb_define_method(rb_cRuggedPatch, "lines", rb_git_diff_patch_lines, 0);
268+
rb_define_method(rb_cRuggedPatch, "bytesize", rb_git_diff_patch_bytesize, -1);
238269

239270
rb_define_method(rb_cRuggedPatch, "delta", rb_git_diff_patch_delta, 0);
240271

test/diff_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ def test_with_oid_string
7373
patches = diff.patches
7474
hunks = patches.map(&:hunks).flatten
7575
lines = hunks.map(&:lines).flatten
76+
bytesize = patches.inject(0) {|n, p| n += p.bytesize(include_context: true)}
7677

7778
assert_equal 5, diff.size
7879
assert_equal 5, deltas.size
7980
assert_equal 5, patches.size
81+
assert_equal 975, bytesize
8082

8183
assert_equal 2, deltas.select(&:added?).size
8284
assert_equal 1, deltas.select(&:deleted?).size

0 commit comments

Comments
 (0)