Method: Rugged.minimize_oid
- Defined in:
- ext/rugged/rugged.c
.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
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);
}
|