Skip to content

Commit 2bef24e

Browse files
Merge pull request #623 from libgit2/brianmario/update-patch-lines
Change Rugged::Patch#lines to accept count exclusion options
2 parents 23fa664 + 4a07e38 commit 2bef24e

File tree

2 files changed

+84
-23
lines changed

2 files changed

+84
-23
lines changed

ext/rugged/rugged_patch.c

Lines changed: 77 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -175,47 +175,107 @@ static VALUE rb_git_diff_patch_stat(VALUE self)
175175

176176
/*
177177
* call-seq:
178-
* patch.lines -> int
178+
* patch.lines(options = {}) -> int
179179
*
180-
* Returns the total number of lines in the patch.
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.
193+
*
194+
* Returns the total number of lines in the patch, depending on the options
195+
* specified.
181196
*/
182-
static VALUE rb_git_diff_patch_lines(VALUE self)
197+
static VALUE rb_git_diff_patch_lines(int argc, VALUE *argv, VALUE self)
183198
{
184199
git_patch *patch;
185-
size_t context, adds, dels;
200+
size_t context_lines, additions, deletions;
201+
size_t total_out;
202+
VALUE rb_options;
186203
Data_Get_Struct(self, git_patch, patch);
187204

188-
git_patch_line_stats(&context, &adds, &dels, patch);
205+
context_lines = 0;
206+
additions = 0;
207+
deletions = 0;
189208

190-
return INT2FIX(context + adds + dels);
191-
}
209+
git_patch_line_stats(&context_lines, &additions, &deletions, patch);
210+
211+
total_out = context_lines + additions + deletions;
212+
213+
rb_scan_args(argc, argv, "0:", &rb_options);
214+
if (!NIL_P(rb_options)) {
215+
if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("exclude_context")))) {
216+
total_out -= context_lines;
217+
}
218+
219+
if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("exclude_additions")))) {
220+
total_out -= additions;
221+
}
192222

223+
if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("exclude_deletions")))) {
224+
total_out -= deletions;
225+
}
226+
}
227+
228+
return INT2FIX(total_out);
229+
}
230+
/*
231+
* call-seq:
232+
* patch.bytesize(options = {}) -> int
233+
*
234+
* The following options can be passed in the +options+ Hash:
235+
*
236+
* :exclude_context ::
237+
* Boolean value specifying that context lines should be excluded when
238+
* counting the number of bytes in the patch.
239+
*
240+
* :exclude_hunk_headers ::
241+
* Boolean value specifying that hunk headers should be excluded when
242+
* counting the number of bytes in the patch.
243+
*
244+
* :exclude_file_headers ::
245+
* Boolean value specifying that file headers should be excluded when
246+
* counting the number of bytes in the patch.
247+
*
248+
* Returns the number of bytes in the patch, depending on which options are
249+
* specified.
250+
*/
193251
static VALUE rb_git_diff_patch_bytesize(int argc, VALUE *argv, VALUE self)
194252
{
195253
git_patch *patch;
196254
size_t bytesize;
197255
VALUE rb_options;
198-
int options[3];
256+
int include_context, include_hunk_headers, include_file_headers;
199257
Data_Get_Struct(self, git_patch, patch);
200258

201-
memset(options, 0, sizeof(options));
259+
include_context = 1;
260+
include_hunk_headers = 1;
261+
include_file_headers = 1;
202262

203263
rb_scan_args(argc, argv, "0:", &rb_options);
204264
if (!NIL_P(rb_options)) {
205-
if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("include_context")))) {
206-
options[0] = 1;
265+
if (rb_hash_aref(rb_options, CSTR2SYM("include_context")) == Qfalse) {
266+
include_context = 0;
207267
}
208268

209-
if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("include_hunk_headers")))) {
210-
options[1] = 1;
269+
if (rb_hash_aref(rb_options, CSTR2SYM("include_hunk_headers")) == Qfalse) {
270+
include_hunk_headers = 0;
211271
}
212272

213-
if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("include_file_headers")))) {
214-
options[2] = 1;
273+
if (rb_hash_aref(rb_options, CSTR2SYM("include_file_headers")) == Qfalse) {
274+
include_file_headers = 0;
215275
}
216276
}
217277

218-
bytesize = git_patch_size(patch, options[0], options[1], options[2]);
278+
bytesize = git_patch_size(patch, include_context, include_hunk_headers, include_file_headers);
219279

220280
return INT2FIX(bytesize);
221281
}
@@ -264,7 +324,7 @@ void Init_rugged_patch(void)
264324
rb_define_singleton_method(rb_cRuggedPatch, "from_strings", rb_git_patch_from_strings, -1);
265325

266326
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);
327+
rb_define_method(rb_cRuggedPatch, "lines", rb_git_diff_patch_lines, -1);
268328
rb_define_method(rb_cRuggedPatch, "bytesize", rb_git_diff_patch_bytesize, -1);
269329

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

test/diff_test.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +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)}
76+
bytesize = patches.inject(0) {|n, p| n += p.bytesize(include_context: false)}
7777

7878
assert_equal 5, diff.size
7979
assert_equal 5, deltas.size
8080
assert_equal 5, patches.size
81-
assert_equal 975, bytesize
81+
assert_equal 1589, bytesize
8282

8383
assert_equal 2, deltas.select(&:added?).size
8484
assert_equal 1, deltas.select(&:deleted?).size
@@ -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)