Skip to content

Commit 491c49e

Browse files
committed
Change Rugged::Patch#lines to accept count exclusion options
1 parent 23fa664 commit 491c49e

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

ext/rugged/rugged_patch.c

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,56 @@ static VALUE rb_git_diff_patch_stat(VALUE self)
175175

176176
/*
177177
* call-seq:
178-
* patch.lines -> int
178+
* patch.lines(options = {}) -> int
179+
*
180+
* The following options can be passed in the +options+ Hash:
181+
*
182+
* :exclude_context ::
183+
* Boolean value specifying that context line counts should be excluded from
184+
* the returned total.
185+
*
186+
* :exclude_additions ::
187+
* Boolean value specifying that addition line counts should be excluded from
188+
* the returned total.
189+
*
190+
* :exclude_deletions ::
191+
* Boolean value specifying that deletion line counts should be excluded from
192+
* the returned total.
179193
*
180194
* Returns the total number of lines in the patch.
181195
*/
182-
static VALUE rb_git_diff_patch_lines(VALUE self)
196+
static VALUE rb_git_diff_patch_lines(int argc, VALUE *argv, VALUE self)
183197
{
184198
git_patch *patch;
185-
size_t context, adds, dels;
199+
size_t context_lines, additions, deletions;
200+
size_t total_out;
201+
VALUE rb_options;
186202
Data_Get_Struct(self, git_patch, patch);
187203

188-
git_patch_line_stats(&context, &adds, &dels, patch);
204+
context_lines = 0;
205+
additions = 0;
206+
deletions = 0;
207+
208+
git_patch_line_stats(&context_lines, &additions, &deletions, patch);
209+
210+
total_out = context_lines + additions + deletions;
211+
212+
rb_scan_args(argc, argv, "0:", &rb_options);
213+
if (!NIL_P(rb_options)) {
214+
if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("exclude_context")))) {
215+
total_out -= context_lines;
216+
}
217+
218+
if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("exclude_additions")))) {
219+
total_out -= additions;
220+
}
221+
222+
if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("exclude_deletions")))) {
223+
total_out -= deletions;
224+
}
225+
}
189226

190-
return INT2FIX(context + adds + dels);
227+
return INT2FIX(total_out);
191228
}
192229

193230
static VALUE rb_git_diff_patch_bytesize(int argc, VALUE *argv, VALUE self)
@@ -264,7 +301,7 @@ void Init_rugged_patch(void)
264301
rb_define_singleton_method(rb_cRuggedPatch, "from_strings", rb_git_patch_from_strings, -1);
265302

266303
rb_define_method(rb_cRuggedPatch, "stat", rb_git_diff_patch_stat, 0);
267-
rb_define_method(rb_cRuggedPatch, "lines", rb_git_diff_patch_lines, 0);
304+
rb_define_method(rb_cRuggedPatch, "lines", rb_git_diff_patch_lines, -1);
268305
rb_define_method(rb_cRuggedPatch, "bytesize", rb_git_diff_patch_bytesize, -1);
269306

270307
rb_define_method(rb_cRuggedPatch, "delta", rb_git_diff_patch_delta, 0);

test/diff_test.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,15 +373,15 @@ def test_stats
373373

374374
# expected per-file values from the diff --stat output plus total lines
375375
expected_patch_stat = [
376-
[ 0, 1, 1 ], [ 1, 0, 2 ], [ 1, 0, 2 ], [ 0, 1, 1 ], [ 2, 0, 3 ],
377-
[ 0, 1, 1 ], [ 0, 1, 1 ], [ 1, 0, 1 ], [ 2, 0, 2 ], [ 0, 1, 1 ],
378-
[ 1, 0, 2 ]
376+
[ 0, 1, 1, 1 ], [ 1, 0, 2, 1 ], [ 1, 0, 2, 1 ], [ 0, 1, 1, 1 ], [ 2, 0, 3, 2 ],
377+
[ 0, 1, 1, 1 ], [ 0, 1, 1, 1 ], [ 1, 0, 1, 1 ], [ 2, 0, 2, 2 ], [ 0, 1, 1, 1 ],
378+
[ 1, 0, 2, 1 ]
379379
]
380380

381381
diff.each_patch do |patch|
382382
next if [:unmodified, :ignored, :untracked].include? patch.delta.status
383383

384-
expected_adds, expected_dels, expected_lines = expected_patch_stat.shift
384+
expected_adds, expected_dels, expected_lines, lines_wo_context = expected_patch_stat.shift
385385

386386
actual_adds, actual_dels = patch.stat
387387

@@ -390,6 +390,7 @@ def test_stats
390390
assert_equal expected_adds + expected_dels, patch.changes
391391

392392
assert_equal expected_lines, patch.lines
393+
assert_equal lines_wo_context, patch.lines(exclude_context: true)
393394
end
394395
end
395396
end

0 commit comments

Comments
 (0)