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.



14
15
16
# File 'lib/rugged/patch.rb', line 14

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).



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'ext/rugged/rugged_patch.c', line 32

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

#additionsObject

Returns the number of additions in the patch.



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

def additions
  stat[0]
end

#bytesize(options = {}) ⇒ Integer

The following options can be passed in the options Hash:

:exclude_context

Boolean value specifying that context lines should be excluded when counting the number of bytes in the patch.

:exclude_hunk_headers

Boolean value specifying that hunk headers should be excluded when counting the number of bytes in the patch.

:exclude_file_headers

Boolean value specifying that file headers should be excluded when counting the number of bytes in the patch.

Returns the number of bytes in the patch, depending on which options are specified.

Returns:

  • (Integer)


279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'ext/rugged/rugged_patch.c', line 279

static VALUE rb_git_diff_patch_bytesize(int argc, VALUE *argv, VALUE self)
{
	git_patch *patch;
	size_t bytesize;
	VALUE rb_options;
	int include_context, include_hunk_headers, include_file_headers;
	Data_Get_Struct(self, git_patch, patch);

	include_context = include_hunk_headers = include_file_headers = 1;

	rb_scan_args(argc, argv, "0:", &rb_options);
	if (!NIL_P(rb_options)) {
		if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("exclude_context")))) {
			include_context = 0;
		}

		if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("exclude_hunk_headers")))) {
			include_hunk_headers = 0;
		}

		if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("exclude_file_headers")))) {
			include_file_headers = 0;
		}
	}

	bytesize = git_patch_size(patch, include_context, include_hunk_headers, include_file_headers);

	return INT2FIX(bytesize);
}

#changesObject

Returns the number of total changes in the patch.



32
33
34
# File 'lib/rugged/patch.rb', line 32

def changes
  additions + deletions
end

#deletionsObject

Returns the number of deletions in the patch.



27
28
29
# File 'lib/rugged/patch.rb', line 27

def deletions
  stat[1]
end

#deltaObject

Returns the delta object associated with the patch.



131
132
133
134
135
136
137
# File 'ext/rugged/rugged_patch.c', line 131

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)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'ext/rugged/rugged_patch.c', line 88

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;

	RETURN_ENUMERATOR(self, 0, 0);
	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;
}

#headerString

Returns only the header of the patch as a string.

Returns:

  • (String)


368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'ext/rugged/rugged_patch.c', line 368

static VALUE rb_git_diff_patch_header(VALUE self)
{
	git_patch *patch;
	int error = 0;
	VALUE rb_buffer = rb_ary_new();
	Data_Get_Struct(self, git_patch, patch);

	error = git_patch_print(patch, patch_print_header_cb, (void*)rb_buffer);
	if (error && error != GIT_ITEROVER)
		rugged_exception_check(error);

	return rb_ary_join(rb_buffer, Qnil);
}

#hunk_countInteger Also known as: size, count

Returns the number of hunks in the patch.

Returns:

  • (Integer)


117
118
119
120
121
122
123
# File 'ext/rugged/rugged_patch.c', line 117

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.



37
38
39
# File 'lib/rugged/patch.rb', line 37

def hunks
  each_hunk.to_a
end

#inspectObject



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

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

#lines(options = {}) ⇒ Integer

The following options can be passed in the options Hash:

:exclude_context

Boolean value specifying that context line counts should be excluded from the returned total.

:exclude_additions

Boolean value specifying that addition line counts should be excluded from the returned total.

:exclude_deletions

Boolean value specifying that deletion line counts should be excluded from the returned total.

:exclude_eofnl

Boolean value specifying that end-of-file newline change lines should be excluded from the returned total.

Returns the total number of lines in the patch, depending on the options specified.

Returns:

  • (Integer)


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'ext/rugged/rugged_patch.c', line 188

static VALUE rb_git_diff_patch_lines(int argc, VALUE *argv, VALUE self)
{
	git_patch *patch;
	size_t lines = 0;
	int options = 0;
	VALUE rb_options;
	Data_Get_Struct(self, git_patch, patch);

	rb_scan_args(argc, argv, "0:", &rb_options);
	if (!NIL_P(rb_options)) {
		if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("exclude_context")))) {
			options |= EXCLUDE_CONTEXT;
		}

		if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("exclude_additions")))) {
			options |= EXCLUDE_ADDITIONS;
		}

		if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("exclude_deletions")))) {
			options |= EXCLUDE_DELETIONS;
		}

		if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("exclude_eofnl")))) {
			options |= EXCLUDE_EOFNL;
		}
	}

	if (options == 0) {
		size_t i = 0, hunks_count = git_patch_num_hunks(patch);
		for (i = 0; i < hunks_count; ++i) {
			lines += git_patch_num_lines_in_hunk(patch, i);
		}
	} else {
		size_t i = 0, hunks_count = git_patch_num_hunks(patch);
		for (i = 0; i < hunks_count; ++i) {
			size_t lines_in_hunk = git_patch_num_lines_in_hunk(patch, i), l = 0;

			for (l = 0; l < lines_in_hunk; ++l) {
				const git_diff_line *line;
				rugged_exception_check(
					git_patch_get_line_in_hunk(&line, patch, i, l)
				);

				switch (line->origin) {
				case GIT_DIFF_LINE_CONTEXT:
					if (options & EXCLUDE_CONTEXT) continue;
					break;

				case GIT_DIFF_LINE_ADDITION:
					if (options & EXCLUDE_ADDITIONS) continue;
					break;

				case GIT_DIFF_LINE_DELETION:
					if (options & EXCLUDE_DELETIONS) continue;
					break;

				case GIT_DIFF_LINE_ADD_EOFNL:
				case GIT_DIFF_LINE_DEL_EOFNL:
					if (options & EXCLUDE_EOFNL) continue;
					break;
				}

				lines += 1;
			}
		}
	}

	return INT2FIX(lines);
}

#statInteger

Returns the number of additions and deletions in the patch.

Returns:

  • (Integer, Integer)


145
146
147
148
149
150
151
152
153
154
# File 'ext/rugged/rugged_patch.c', line 145

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)


351
352
353
354
355
356
357
358
359
360
# File 'ext/rugged/rugged_patch.c', line 351

static VALUE rb_git_diff_patch_to_s(VALUE self)
{
	git_patch *patch;
	VALUE rb_buffer = rb_ary_new();
	Data_Get_Struct(self, git_patch, patch);

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

	return rb_ary_join(rb_buffer, Qnil);
}