Module: Rugged

Defined in:
lib/rugged/tag.rb,
lib/rugged/blob.rb,
lib/rugged/diff.rb,
lib/rugged/tree.rb,
lib/rugged/index.rb,
lib/rugged/patch.rb,
lib/rugged/branch.rb,
lib/rugged/commit.rb,
lib/rugged/object.rb,
lib/rugged/remote.rb,
lib/rugged/walker.rb,
lib/rugged/version.rb,
lib/rugged/diff/hunk.rb,
lib/rugged/diff/line.rb,
lib/rugged/reference.rb,
lib/rugged/attributes.rb,
lib/rugged/diff/delta.rb,
lib/rugged/repository.rb,
lib/rugged/credentials.rb,
lib/rugged/submodule_collection.rb,
ext/rugged/rugged.c

Overview

Copyright © the Rugged contributors. All rights reserved.

This file is part of Rugged, distributed under the MIT license. For full terms see the included LICENSE file.

Defined Under Namespace

Modules: Credentials Classes: Backend, Blame, Blob, Branch, BranchCollection, Commit, Config, Diff, Error, Index, Object, OdbObject, Patch, Rebase, Reference, ReferenceCollection, Remote, RemoteCollection, Repository, Settings, Submodule, SubmoduleCollection, Tag, TagCollection, Tree, Walker

Constant Summary collapse

Version =
VERSION = '1.7.1'
SORT_NONE =

Sort the output with the same default time-order method from git. This is the default sorting for new walkers.

INT2FIX(GIT_SORT_NONE)
SORT_TOPO =

Sort the repository contents in topological order (parents before children); this sorting mode can be combined with time sorting to produce git’s “time-order”.

INT2FIX(GIT_SORT_TOPOLOGICAL)
SORT_DATE =

Sort the repository contents by commit time; this sorting mode can be combined with topological sorting.

INT2FIX(GIT_SORT_TIME)
SORT_REVERSE =

Iterate through the repository contents in reverse order; this sorting mode can be combined with any of the above.

INT2FIX(GIT_SORT_REVERSE)

Class Method Summary collapse

Class Method Details

.__cache_usage__Array

Returns an array representing the current bytes in the internal libgit2 cache and the maximum size of the cache.

Returns:

  • (Array)


386
387
388
389
390
391
# File 'ext/rugged/rugged.c', line 386

static VALUE rb_git_cache_usage(VALUE self)
{
	int64_t used, max;
	git_libgit2_opts(GIT_OPT_GET_CACHED_MEMORY, &used, &max);
	return rb_ary_new3(2, LL2NUM(used), LL2NUM(max));
}

.dotgit_attributes?(rb_path) ⇒ Boolean

Returns:

  • (Boolean)


568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'ext/rugged/rugged.c', line 568

static VALUE rb_git_path_is_dotgit_attributes(VALUE self, VALUE rb_path)
{
	const char *path;
	int is_dotgit;

	Check_Type(rb_path, T_STRING);

	path = StringValueCStr(rb_path);

	is_dotgit = git_path_is_gitfile(path, strlen(path), GIT_PATH_GITFILE_GITATTRIBUTES, GIT_PATH_FS_GENERIC);

	return is_dotgit ? Qtrue : Qfalse;
}

.dotgit_ignore?(rb_path) ⇒ Boolean

Returns:

  • (Boolean)


554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'ext/rugged/rugged.c', line 554

static VALUE rb_git_path_is_dotgit_ignore(VALUE self, VALUE rb_path)
{
	const char *path;
	int is_dotgit;

	Check_Type(rb_path, T_STRING);

	path = StringValueCStr(rb_path);

	is_dotgit = git_path_is_gitfile(path, strlen(path), GIT_PATH_GITFILE_GITIGNORE, GIT_PATH_FS_GENERIC);

	return is_dotgit ? Qtrue : Qfalse;
}

.dotgit_modules?(rb_path) ⇒ Boolean

Returns:

  • (Boolean)


540
541
542
543
544
545
546
547
548
549
550
551
552
# File 'ext/rugged/rugged.c', line 540

static VALUE rb_git_path_is_dotgit_modules(VALUE self, VALUE rb_path)
{
	const char *path;
	int is_dotgit;

	Check_Type(rb_path, T_STRING);

	path = StringValueCStr(rb_path);

	is_dotgit = git_path_is_gitfile(path, strlen(path), GIT_PATH_GITFILE_GITMODULES, GIT_PATH_FS_GENERIC);

	return is_dotgit ? Qtrue : Qfalse;
}

.featuresArray

Returns an array representing the features that libgit2 was compiled with — this includes ‘:threads` (thread support), `:https` and `:ssh`.

Rugged.features #=> [:threads, :https]

Returns:

  • (Array)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'ext/rugged/rugged.c', line 102

static VALUE rb_git_features(VALUE self)
{
	VALUE ret_arr = rb_ary_new();

	int caps = git_libgit2_features();

	if (caps & GIT_FEATURE_THREADS)
		rb_ary_push(ret_arr, CSTR2SYM("threads"));

	if (caps & GIT_FEATURE_HTTPS)
		rb_ary_push(ret_arr, CSTR2SYM("https"));

	if (caps & GIT_FEATURE_SSH)
		rb_ary_push(ret_arr, CSTR2SYM("ssh"));

	return ret_arr;
}

.hex_to_raw(oid) ⇒ Object

Turn a string of 40 hexadecimal characters into the buffer of 20 bytes it represents.

Rugged.hex_to_raw('d8786bfc97485e8d7b19b21fb88c8ef1f199fc3f')
#=> "\330xk\374\227H^\215{\031\262\037\270\214\216\361\361\231\374?"


153
154
155
156
157
158
159
160
161
# File 'ext/rugged/rugged.c', line 153

static VALUE rb_git_hex_to_raw(VALUE self, VALUE hex)
{
	git_oid oid;

	Check_Type(hex, T_STRING);
	rugged_exception_check(git_oid_fromstr(&oid, StringValueCStr(hex)));

	return rb_str_new((const char *)oid.id, 20);
}

.libgit2_prereleaseObject

Returns a string with the prerelease string for libgit2. This will be empty for tagged releases.



85
86
87
88
89
90
91
# File 'ext/rugged/rugged.c', line 85

static VALUE rb_git_libgit2_prerelease(VALUE self)
{
	const char *prerelease;

	prerelease = git_libgit2_prerelease();
	return rb_str_new_utf8(prerelease ? prerelease : "");
}

.libgit2_versionObject

Returns an array representing the current libgit2 version in use. Using the array makes it easier for the end-user to take conditional actions based on each respective version attribute: major, minor, rev.

Rugged.libgit2_version #=> [0, 17, 0]


66
67
68
69
70
71
72
73
74
75
76
# File 'ext/rugged/rugged.c', line 66

static VALUE rb_git_libgit2_version(VALUE self)
{
	int major;
	int minor;
	int rev;

	git_libgit2_version(&major, &minor, &rev);

	// We return an array of three elements to represent the version components
	return rb_ary_new3(3, INT2NUM(major), INT2NUM(minor), INT2NUM(rev));
}

.minimize_oid(oid_iterator, min_length = 7) {|short_oid| ... } ⇒ Object .minimize_oid(oid_iterator, min_length = 7) ⇒ Object

Iterate through oid_iterator, which should yield any number of SHA1 OIDs (represented as 40-character hexadecimal strings), and tries to minify them.

Minifying a set of a SHA1 strings means finding the shortest root substring for each string that uniquely identifies it.

If no block is given, the function will return the minimal length as an integer value:

oids = [
  'd8786bfc974aaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
  'd8786bfc974bbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
  'd8786bfc974ccccccccccccccccccccccccccccc',
  '68d041ee999cb07c6496fbdd4f384095de6ca9e1',
]

Rugged.minimize_oids(oids) #=> 12

If a block is given, it will be called with each OID from iterator in its minified form:

Rugged.minimize_oid(oids) { |oid| puts oid }

produces:

d8786bfc974a
d8786bfc974b
d8786bfc974c
68d041ee999c

The optional min_length argument allows you to specify a lower bound for the minified strings; returned strings won’t be shorter than the given value, even if they would still be uniquely represented.

Rugged.minimize_oid(oids, 18) #=> 18

Overloads:

  • .minimize_oid(oid_iterator, min_length = 7) {|short_oid| ... } ⇒ Object

    Yields:

    • (short_oid)


296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'ext/rugged/rugged.c', line 296

static VALUE rb_git_minimize_oid(int argc, VALUE *argv, VALUE self)
{
	git_oid_shorten *shrt;
	int length, minlen = 7;
	VALUE rb_enum, rb_minlen, rb_block;

	rb_scan_args(argc, argv, "11&", &rb_enum, &rb_minlen, &rb_block);

	if (!NIL_P(rb_minlen)) {
		Check_Type(rb_minlen, T_FIXNUM);
		minlen = FIX2INT(rb_minlen);
	}

	if (!rb_respond_to(rb_enum, rb_intern("each")))
		rb_raise(rb_eTypeError, "Expecting an Enumerable instance");

	shrt = git_oid_shorten_new(minlen);

	rb_block_call(rb_enum, rb_intern("each"), 0, NULL, minimize_cb, (VALUE)shrt);
	length = git_oid_shorten_add(shrt, NULL);

	git_oid_shorten_free(shrt);
	rugged_exception_check(length);

	if (!NIL_P(rb_block)) {
		VALUE yield_data[2];

		yield_data[0] = rb_block;
		yield_data[1] = INT2FIX(length);

		rb_block_call(rb_enum, rb_intern("each"), 0, NULL, minimize_yield, (VALUE)yield_data);
		return Qnil;
	}

	return INT2FIX(length);
}

.prettify_message(message, strip_comments = '#') ⇒ Object

Process a commit or tag message into standard form, by stripping trailing spaces and comments, and making sure that the message has a proper header line.



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
# File 'ext/rugged/rugged.c', line 196

static VALUE rb_git_prettify_message(int argc, VALUE *argv, VALUE self)
{
	char comment_char = '#';
	int strip_comments = 1;

	git_buf message = { NULL };
	VALUE rb_message, rb_strip;
	int error;
	VALUE result = Qnil;

	rb_scan_args(argc, argv, "11", &rb_message, &rb_strip);

	Check_Type(rb_message, T_STRING);

	switch (TYPE(rb_strip)) {
	case T_FALSE:
		strip_comments = 0;
		break;

	case T_STRING:
		if (RSTRING_LEN(rb_strip) > 0)
			comment_char = RSTRING_PTR(rb_strip)[0];
		break;

	case T_TRUE:
	case T_NIL:
	default:
		break;
	}

	error = git_message_prettify(&message,
				StringValueCStr(rb_message), strip_comments, comment_char);

	if (!error)
		result = rb_enc_str_new(message.ptr, message.size, rb_utf8_encoding());

	git_buf_dispose(&message);
	rugged_exception_check(error);

	return result;
}

.raw_to_hex(buffer) ⇒ Object

Turn a buffer of 20 bytes (representing a SHA1 OID) into its readable hexadecimal representation.

Rugged.raw_to_hex("\330xk\374\227H^\215{\031\262\037\270\214\216\361\361\231\374?")
#=> "d8786bfc97485e8d7b19b21fb88c8ef1f199fc3f"


173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'ext/rugged/rugged.c', line 173

static VALUE rb_git_raw_to_hex(VALUE self, VALUE raw)
{
	git_oid oid;
	char out[40];

	Check_Type(raw, T_STRING);

	if (RSTRING_LEN(raw) != GIT_OID_RAWSZ)
		rb_raise(rb_eTypeError, "Invalid buffer size for an OID");

	git_oid_fromraw(&oid, (const unsigned char *)RSTRING_PTR(raw));
	git_oid_fmt(out, &oid);

	return rb_usascii_str_new(out, 40);
}

.signature_from_buffer(buffer[, encoding_name]) ⇒ Object

Parse the signature from the given buffer. If an encoding is given, the strings will be tagged with that encoding.

commit.author #=> {:email=>"[email protected]", :time=>Tue Jan 24 05:42:45 UTC 2012, :name=>"Vicent Mart\303\255"}


402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'ext/rugged/rugged.c', line 402

static VALUE rb_git_signature_from_buffer(int argc, VALUE *argv, VALUE self)
{
	VALUE rb_buffer, rb_encoding_name;
	const char *buffer, *encoding_name = NULL;

	rb_scan_args(argc, argv, "11", &rb_buffer, &rb_encoding_name);

	buffer = StringValueCStr(rb_buffer);
	if (!NIL_P(rb_encoding_name))
		encoding_name = StringValueCStr(rb_encoding_name);

	return rugged_signature_from_buffer(buffer, encoding_name);
}

.valid_full_oid?(oid) ⇒ Boolean

Checks to see if a string contains a full 40-character sha1.

Rugged.valid_full_oid?('d8786bfc97485e8d7b19b21fb88c8ef1f199fc3f')
#=> true

Returns:

  • (Boolean)


129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'ext/rugged/rugged.c', line 129

static VALUE rb_git_valid_full_oid(VALUE self, VALUE hex)
{
	git_oid oid;
	int errorcode;

	Check_Type(hex, T_STRING);
	errorcode = git_oid_fromstr(&oid, StringValueCStr(hex));
	if (errorcode < 0) {
		return Qfalse;
	} else {
		return Qtrue;
	}
}