@@ -175,47 +175,107 @@ static VALUE rb_git_diff_patch_stat(VALUE self)
175
175
176
176
/*
177
177
* call-seq:
178
- * patch.lines -> int
178
+ * patch.lines(options = {}) -> int
179
179
*
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.
181
196
*/
182
- static VALUE rb_git_diff_patch_lines (VALUE self )
197
+ static VALUE rb_git_diff_patch_lines (int argc , VALUE * argv , VALUE self )
183
198
{
184
199
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 ;
186
203
Data_Get_Struct (self , git_patch , patch );
187
204
188
- git_patch_line_stats (& context , & adds , & dels , patch );
205
+ context_lines = 0 ;
206
+ additions = 0 ;
207
+ deletions = 0 ;
189
208
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
+ }
192
222
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
+ */
193
251
static VALUE rb_git_diff_patch_bytesize (int argc , VALUE * argv , VALUE self )
194
252
{
195
253
git_patch * patch ;
196
254
size_t bytesize ;
197
255
VALUE rb_options ;
198
- int options [ 3 ] ;
256
+ int include_context , include_hunk_headers , include_file_headers ;
199
257
Data_Get_Struct (self , git_patch , patch );
200
258
201
- memset (options , 0 , sizeof (options ));
259
+ include_context = 1 ;
260
+ include_hunk_headers = 1 ;
261
+ include_file_headers = 1 ;
202
262
203
263
rb_scan_args (argc , argv , "0:" , & rb_options );
204
264
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 ;
207
267
}
208
268
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 ;
211
271
}
212
272
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 ;
215
275
}
216
276
}
217
277
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 );
219
279
220
280
return INT2FIX (bytesize );
221
281
}
@@ -264,7 +324,7 @@ void Init_rugged_patch(void)
264
324
rb_define_singleton_method (rb_cRuggedPatch , "from_strings" , rb_git_patch_from_strings , -1 );
265
325
266
326
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 );
268
328
rb_define_method (rb_cRuggedPatch , "bytesize" , rb_git_diff_patch_bytesize , -1 );
269
329
270
330
rb_define_method (rb_cRuggedPatch , "delta" , rb_git_diff_patch_delta , 0 );
0 commit comments