Class: Rugged::Patch

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rugged/patch.rb,
ext/rugged/rugged_patch.c

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ownerObject Also known as: diff

Returns the value of attribute owner.



9
10
11
# File 'lib/rugged/patch.rb', line 9

def owner
  @owner
end

Class Method Details

.from_strings(old_content = nil, new_content = nil, options = {}) ⇒ Object

Directly generate a Rugged::Patch from the difference between the content of the two strings ‘old_content` and `new_content`.

The following options can be passed in the options Hash:

:old_path

An optional string to treat blob as if it had this filename.

:new_path

An optional string to treat other as if it had this filename.

Additionally, ‘options` can also contain all other valid diff options (see Rugged::Tree#diff for a complete list).



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'ext/rugged/rugged_patch.c', line 49

VALUE rb_git_patch_from_strings(int argc, VALUE *argv, VALUE self)
{
	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
	git_patch *patch;
	char * old_path = NULL, * new_path = NULL;
	VALUE rb_old_buffer, rb_new_buffer, rb_options;

	rb_scan_args(argc, argv, "02:", &rb_old_buffer, &rb_new_buffer, &rb_options);

	if (!NIL_P(rb_options)) {
		VALUE rb_value;

		rb_value = rb_hash_aref(rb_options, CSTR2SYM("old_path"));
		if (!NIL_P(rb_value)) {
			Check_Type(rb_value, T_STRING);
			old_path = StringValueCStr(rb_value);
		}

		rb_value = rb_hash_aref(rb_options, CSTR2SYM("new_path"));
		if (!NIL_P(rb_value)) {
			Check_Type(rb_value, T_STRING);
			new_path = StringValueCStr(rb_value);
		}

		rugged_parse_diff_options(&opts, rb_options);
	}

	rugged_exception_check(git_patch_from_buffers(&patch,
		NIL_P(rb_old_buffer) ? NULL : StringValuePtr(rb_old_buffer),
		NIL_P(rb_old_buffer) ? 0 : RSTRING_LEN(rb_old_buffer),
		old_path,
		NIL_P(rb_new_buffer) ? NULL : StringValuePtr(rb_new_buffer),
		NIL_P(rb_new_buffer) ? 0 : RSTRING_LEN(rb_new_buffer),
		new_path,
		&opts
	));

	return rugged_patch_new(self, patch);
}

Instance Method Details

#changesObject

Returns the number of changes in the patch.



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

def changes
  stat.reduce { |t,v| t + v }
end

#deltaObject

Returns the delta object associated with the patch.



151
152
153
154
155
156
157
# File 'ext/rugged/rugged_patch.c', line 151

static VALUE rb_git_diff_patch_delta(VALUE self)
{
	git_patch *patch;
	Data_Get_Struct(self, git_patch, patch);

	return rugged_diff_delta_new(rugged_owner(self), git_patch_get_delta(patch));
}

#each_hunk {|hunk| ... } ⇒ self #each_hunkObject Also known as: each

If given a block, yields each hunk that is part of the patch.

If no block is given, an enumerator is returned instead.

Overloads:

  • #each_hunk {|hunk| ... } ⇒ self

    Yields:

    • (hunk)

    Returns:

    • (self)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'ext/rugged/rugged_patch.c', line 105

static VALUE rb_git_diff_patch_each_hunk(VALUE self)
{
	git_patch *patch;
	const git_diff_hunk *hunk;
	size_t lines_in_hunk;
	int error = 0;
	size_t hunks_count, h;

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

	Data_Get_Struct(self, git_patch, patch);

	hunks_count = git_patch_num_hunks(patch);
	for (h = 0; h < hunks_count; ++h) {
		error = git_patch_get_hunk(&hunk, &lines_in_hunk, patch, h);
		if (error) break;

		rb_yield(rugged_diff_hunk_new(self, h, hunk, lines_in_hunk));
	}
	rugged_exception_check(error);

	return self;
}

#hunk_countInteger Also known as: size, count

Returns the number of hunks in the patch.

Returns:

  • (Integer)


137
138
139
140
141
142
143
# File 'ext/rugged/rugged_patch.c', line 137

static VALUE rb_git_diff_patch_hunk_count(VALUE self)
{
	git_patch *patch;
	Data_Get_Struct(self, git_patch, patch);

	return INT2FIX(git_patch_num_hunks(patch));
}

#hunksObject

Returns an Array containing all hunks of the patch.



22
23
24
# File 'lib/rugged/patch.rb', line 22

def hunks
  each_hunk.to_a
end

#inspectObject



12
13
14
# File 'lib/rugged/patch.rb', line 12

def inspect
  "#<#{self.class.name}:#{object_id}>"
end

#linesInteger

Returns the total number of lines in the patch.

Returns:

  • (Integer)


182
183
184
185
186
187
188
189
190
191
# File 'ext/rugged/rugged_patch.c', line 182

static VALUE rb_git_diff_patch_lines(VALUE self)
{
	git_patch *patch;
	size_t context, adds, dels;
	Data_Get_Struct(self, git_patch, patch);

	git_patch_line_stats(&context, &adds, &dels, patch);

	return INT2FIX(context + adds + dels);
}

#statInteger

Returns the number of additions and deletions in the patch.

Returns:

  • (Integer, Integer)


165
166
167
168
169
170
171
172
173
174
# File 'ext/rugged/rugged_patch.c', line 165

static VALUE rb_git_diff_patch_stat(VALUE self)
{
	git_patch *patch;
	size_t additions, deletions;
	Data_Get_Struct(self, git_patch, patch);

	git_patch_line_stats(NULL, &additions, &deletions, patch);

	return rb_ary_new3(2, INT2FIX(additions), INT2FIX(deletions));
}

#to_sString

Returns the contents of the patch as a single diff string.

Returns:

  • (String)


219
220
221
222
223
224
225
226
227
228
# File 'ext/rugged/rugged_patch.c', line 219

static VALUE rb_git_diff_patch_to_s(VALUE self)
{
	git_patch *patch;
	VALUE rb_str = rb_str_new(NULL, 0);
	Data_Get_Struct(self, git_patch, patch);

	rugged_exception_check(git_patch_print(patch, patch_print_cb, (void*)rb_str));

	return rb_str;
}