Class: Rugged::Diff

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rugged/diff.rb,
lib/rugged/diff/hunk.rb,
lib/rugged/diff/line.rb,
lib/rugged/diff/delta.rb,
ext/rugged/rugged_diff.c

Defined Under Namespace

Classes: Delta, Hunk, Line

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ownerObject (readonly) Also known as: tree

Returns the value of attribute owner.



10
11
12
# File 'lib/rugged/diff.rb', line 10

def owner
  @owner
end

Instance Method Details

#deltasObject



17
18
19
# File 'lib/rugged/diff.rb', line 17

def deltas
  each_delta.to_a
end

#each_delta {|delta| ... } ⇒ self #each_deltaObject

If given a block, yields each delta that is part of the diff. If no block is given, an enumerator will be returned.

This method should be preferred over #each_patch if you’re not interested in the actual line-by-line changes of the diff.

Overloads:

  • #each_delta {|delta| ... } ⇒ self

    Yields:

    • (delta)

    Returns:

    • (self)


493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'ext/rugged/rugged_diff.c', line 493

static VALUE rb_git_diff_each_delta(VALUE self)
{
	git_diff *diff;
	const git_diff_delta *delta;
	int error = 0;
	size_t d, delta_count;

	if (!rb_block_given_p()) {
		return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_delta"), self);
	}

	Data_Get_Struct(self, git_diff, diff);

	delta_count = git_diff_num_deltas(diff);
	for (d = 0; d < delta_count; ++d) {
		delta = git_diff_get_delta(diff, d);
		rb_yield(rugged_diff_delta_new(self, delta));
	}

	rugged_exception_check(error);

	return self;
}

#each_line([format = :patch]) {|line| ... } ⇒ self #each_line([format = :patch]) ⇒ Object

Overloads:

  • #each_line([format = :patch]) {|line| ... } ⇒ self

    Yields:

    • (line)

    Returns:

    • (self)


533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'ext/rugged/rugged_diff.c', line 533

static VALUE rb_git_diff_each_line(int argc, VALUE *argv, VALUE self)
{
	VALUE rb_format;
	git_diff *diff;
	git_diff_format_t format;
	int exception = 0, error;

	Data_Get_Struct(self, git_diff, diff);

	if (rb_scan_args(argc, argv, "01", &rb_format) == 1) {
		Check_Type(rb_format, T_SYMBOL);
	} else {
		rb_format = CSTR2SYM("patch");
	}

	if (!rb_block_given_p())
		return rb_funcall(self, rb_intern("to_enum"), 2, CSTR2SYM("each_line"), rb_format);

	if (SYM2ID(rb_format) == rb_intern("patch")) {
		format = GIT_DIFF_FORMAT_PATCH;
	} else if (SYM2ID(rb_format) == rb_intern("patch_header")) {
		format = GIT_DIFF_FORMAT_PATCH_HEADER;
	} else if (SYM2ID(rb_format) == rb_intern("raw")) {
		format = GIT_DIFF_FORMAT_RAW;
	} else if (SYM2ID(rb_format) == rb_intern("name_only")) {
		format = GIT_DIFF_FORMAT_NAME_ONLY;
	} else if (SYM2ID(rb_format) == rb_intern("name_status")) {
		format = GIT_DIFF_FORMAT_NAME_STATUS;
	} else {
		rb_raise(rb_eArgError, "unknown :format");
	}

	error = git_diff_print(diff, format, each_line_cb, &exception);

	if (exception)
		rb_jump_tag(exception);
	rugged_exception_check(error);

	return self;
}

#each_patch {|patch| ... } ⇒ self #each_patchObject Also known as: each

If given a block, yields each patch that is part of the diff. If no block is given, an enumerator will be returned.

Overloads:

  • #each_patch {|patch| ... } ⇒ self

    Yields:

    Returns:

    • (self)


456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'ext/rugged/rugged_diff.c', line 456

static VALUE rb_git_diff_each_patch(VALUE self)
{
	git_diff *diff;
	git_patch *patch;
	int error = 0;
	size_t d, delta_count;

	if (!rb_block_given_p()) {
		return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_patch"), self);
	}

	Data_Get_Struct(self, git_diff, diff);

	delta_count = git_diff_num_deltas(diff);
	for (d = 0; d < delta_count; ++d) {
		error = git_patch_from_diff(&patch, diff, d);
		if (error) break;

		rb_yield(rugged_patch_new(self, patch));
	}

	rugged_exception_check(error);

	return self;
}

#find_similar!([options]) ⇒ self

Detects entries in the diff that look like renames or copies (based on the given options) and replaces them with actual rename or copy entries.

Additionally, modified files can be broken into add/delete pairs if the amount of changes are above a specific threshold (see :break_rewrite_threshold).

By default, similarity will be measured without leading whitespace. You you can use the :dont_ignore_whitespace to disable this.

The following options can be passed in the options Hash:

:rename_threshold

An integer specifying the similarity to consider a file renamed (default 50).

:rename_from_rewrite_threshold

An integer specifying the similarity of modified to be eligible rename source (default 50).

:copy_threshold

An integer specifying the similarity to consider a file a copy (default 50).

:break_rewrite_threshold

An integer specifying the similarity to split modify into delete/add pair (default 60).

:rename_limit

An integer specifying the maximum amount of similarity sources to examine (a la diff’s -l option or the diff.renameLimit config) (default 200).

:renames

If true, looking for renames will be enabled (--find-renames).

:renames_from_rewrites

If true, the “old side” of modified files will be considered for renames (+–break-rewrites=N+).

:copies

If true, looking for copies will be enabled (--find-copies).

:copies_from_unmodified

If true, unmodified files will be considered as copy sources (--find-copies-harder).

:break_rewrites

If true, larger rewrites will be split into delete/add pairs (+–break-rewrites=/M+).

:all

If true, enables all finding features.

:ignore_whitespace

If true, similarity will be measured with all whitespace ignored.

:dont_ignore_whitespace

If true, similarity will be measured without ignoring any whitespace.

Returns:

  • (self)


367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'ext/rugged/rugged_diff.c', line 367

static VALUE rb_git_diff_find_similar(int argc, VALUE *argv, VALUE self)
{
	git_diff *diff;
	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
	VALUE rb_options;
	int error;

	Data_Get_Struct(self, git_diff, diff);

	rb_scan_args(argc, argv, "00:", &rb_options);

	if (!NIL_P(rb_options)) {
		VALUE rb_value = rb_hash_aref(rb_options, CSTR2SYM("rename_threshold"));
		if (!NIL_P(rb_value)) {
			Check_Type(rb_value, T_FIXNUM);
			opts.rename_threshold = FIX2INT(rb_value);
		}

		rb_value = rb_hash_aref(rb_options, CSTR2SYM("rename_from_rewrite_threshold"));
		if (!NIL_P(rb_value)) {
			Check_Type(rb_value, T_FIXNUM);
			opts.rename_from_rewrite_threshold = FIX2INT(rb_value);
		}

		rb_value = rb_hash_aref(rb_options, CSTR2SYM("copy_threshold"));
		if (!NIL_P(rb_value)) {
			Check_Type(rb_value, T_FIXNUM);
			opts.copy_threshold = FIX2INT(rb_value);
		}

		rb_value = rb_hash_aref(rb_options, CSTR2SYM("break_rewrite_threshold"));
		if (!NIL_P(rb_value)) {
			Check_Type(rb_value, T_FIXNUM);
			opts.break_rewrite_threshold = FIX2INT(rb_value);
		}

		rb_value = rb_hash_aref(rb_options, CSTR2SYM("rename_limit"));
		if (!NIL_P(rb_value)) {
			Check_Type(rb_value, T_FIXNUM);
			opts.rename_limit = FIX2INT(rb_value);
		}

		if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("renames")))) {
			opts.flags |= GIT_DIFF_FIND_RENAMES;
		}

		if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("renames_from_rewrites")))) {
			opts.flags |= GIT_DIFF_FIND_RENAMES_FROM_REWRITES;
		}

		if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("copies")))) {
			opts.flags |= GIT_DIFF_FIND_COPIES;
		}

		if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("copies_from_unmodified")))) {
			opts.flags |= GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED;
		}

		if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("break_rewrites")))) {
			opts.flags |= GIT_DIFF_FIND_AND_BREAK_REWRITES;
		}

		if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("all")))) {
			opts.flags |= GIT_DIFF_FIND_ALL;
		}

		if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("ignore_whitespace")))) {
			opts.flags |= GIT_DIFF_FIND_IGNORE_WHITESPACE;
		}

		if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("dont_ignore_whitespace")))) {
			opts.flags |= GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE;
		}
	}

	error = git_diff_find_similar(diff, &opts);
	rugged_exception_check(error);

	return self;
}

#merge!(other_diff) ⇒ self

Merges all diff information from other_diff.

Returns:

  • (self)


292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'ext/rugged/rugged_diff.c', line 292

static VALUE rb_git_diff_merge(VALUE self, VALUE rb_other)
{
	git_diff *diff;
	git_diff *other;
	int error;

	if (!rb_obj_is_kind_of(rb_other, rb_cRuggedDiff))
		rb_raise(rb_eTypeError, "A Rugged::Diff instance is required");

	Data_Get_Struct(self, git_diff, diff);
	Data_Get_Struct(rb_other, git_diff, other);

	error = git_diff_merge(diff, other);
	rugged_exception_check(error);

	return self;
}

#patchObject #patch(: compact) ⇒ Object

Return a string containing the diff in patch form.



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'ext/rugged/rugged_diff.c', line 220

static VALUE rb_git_diff_patch(int argc, VALUE *argv, VALUE self)
{
	git_diff *diff;
	VALUE rb_str = rb_str_new(NULL, 0);
	VALUE rb_opts;

	rb_scan_args(argc, argv, "00:", &rb_opts);

	Data_Get_Struct(self, git_diff, diff);

	if (!NIL_P(rb_opts)) {
		if (rb_hash_aref(rb_opts, CSTR2SYM("compact")) == Qtrue)
			git_diff_print(diff, GIT_DIFF_FORMAT_NAME_STATUS, diff_print_cb, (void*)rb_str);
		else
			git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, diff_print_cb, (void*)rb_str);
	} else {
		git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, diff_print_cb, (void*)rb_str);
	}

	return rb_str;
}

#patchesObject



13
14
15
# File 'lib/rugged/diff.rb', line 13

def patches
  each_patch.to_a
end

#sizeInteger

Returns the number of deltas/patches in this diff.

Returns:

  • (Integer)


580
581
582
583
584
585
586
587
# File 'ext/rugged/rugged_diff.c', line 580

static VALUE rb_git_diff_size(VALUE self)
{
	git_diff *diff;

	Data_Get_Struct(self, git_diff, diff);

	return INT2FIX(git_diff_num_deltas(diff));
}

#sorted_icase?Boolean

Returns true when deltas are sorted case insensitively.

Returns:

  • (Boolean)


657
658
659
660
661
662
# File 'ext/rugged/rugged_diff.c', line 657

static VALUE rb_git_diff_sorted_icase_p(VALUE self)
{
	git_diff *diff;
	Data_Get_Struct(self, git_diff, diff);
	return git_diff_is_sorted_icase(diff) ? Qtrue : Qfalse;
}

#statInteger

Returns the number of files/additions/deletions in this diff.

Returns:

  • (Integer, Integer, Integer)


638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'ext/rugged/rugged_diff.c', line 638

static VALUE rb_git_diff_stat(VALUE self)
{
	git_diff *diff;
	struct diff_stats stats = { 0, 0, 0 };

	Data_Get_Struct(self, git_diff, diff);

	git_diff_foreach(
		diff, diff_file_stats_cb, NULL, diff_line_stats_cb, &stats);

	return rb_ary_new3(
		3, INT2FIX(stats.files), INT2FIX(stats.adds), INT2FIX(stats.dels));
}

#write_patch(io) ⇒ nil #write_patch(io, : compact) ⇒ Object

Write a patch directly to an object which responds to “write”.

Overloads:

  • #write_patch(io) ⇒ nil

    Returns:

    • (nil)


262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'ext/rugged/rugged_diff.c', line 262

static VALUE rb_git_diff_write_patch(int argc, VALUE *argv, VALUE self)
{
	git_diff *diff;
	VALUE rb_io, rb_opts;

	rb_scan_args(argc, argv, "10:", &rb_io, &rb_opts);

	if (!rb_respond_to(rb_io, rb_intern("write")))
		rb_raise(rb_eArgError, "Expected io to respond to \"write\"");

	Data_Get_Struct(self, git_diff, diff);

	if (!NIL_P(rb_opts)) {
		if (rb_hash_aref(rb_opts, CSTR2SYM("compact")) == Qtrue)
			git_diff_print(diff, GIT_DIFF_FORMAT_NAME_STATUS, diff_write_cb, (void*)rb_io);
		else
			git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, diff_write_cb, (void*)rb_io);
	} else {
		git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, diff_write_cb, (void*)rb_io);
	}

	return Qnil;
}