Skip to content

Loading…

Add `Rugged::Object.rev_parse` #71

Merged
merged 9 commits into from

4 participants

@brianmario
libgit2 member

Updates the bundled libgit2 so we can wrap git_revparse_single.

As a helper, this is wrapped as Rugged::Repository#rev_parse as well.

The tests for this could probably use some love, but I figured it's already being pretty thoroughly tested by libgit2 as well.

brianmario added some commits
@brianmario brianmario Add `Rugged::Object.rev_parse`
This is wrapped as `Rugged::Repository#rev_parse` as well.
d284c0a
@brianmario brianmario Add `Rugged::Object.rev_parse_oid`
This lets us perform a rev-parse when all we need is the oid of
the found object
20907bf
@brianmario
libgit2 member

I also added Rugged::Object.rev_parse_oid (wrapped in Rugged::Repository as well) so we could perform a rev-parse without needing to inflate a full Ruby object for the target for cases where we just need the object's oid.

It could be argued that Rugged::Object.rev_parse could now be refactored to just use the combination of Rugged::Object.rev_parse_oid and Rugged::Object.lookup. It costs an extra method call if we care about that.

@carlosmn carlosmn commented on an outdated diff
test/object_test.rb
@@ -30,4 +30,17 @@
assert obj.read_raw
end
+ test "can lookup an object by refspec" do
@carlosmn libgit2 member

This is not a refspec. A refspec is what tells fetch where to store the refs it downloads. The documentation calls this an "extended SHA1 syntax", but you can call it whatever you want as long as it's not refspec.

@brianmario libgit2 member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@carlosmn
libgit2 member

:heart:

@aroben aroben commented on an outdated diff
ext/rugged/rugged_object.c
((10 lines not shown))
+
+ Check_Type(rb_spec, T_STRING);
+ spec = RSTRING_PTR(rb_spec);
+
+ if (!rb_obj_is_instance_of(rb_repo, rb_cRuggedRepo))
+ rb_raise(rb_eTypeError, "Expecting a Rugged Repository");
+
+ Data_Get_Struct(rb_repo, git_repository, repo);
+
+ error = git_revparse_single(&object, repo, spec);
+ rugged_exception_check(error);
+
+ if (as_obj)
+ return rugged_object_new(rb_repo, object);
+ else
+ return rugged_create_oid(git_object_id(object));
@aroben
aroben added a note

I've been conditioned to leave out else after return because it's redundant:

if (as_obj)
    return rugged_object_new(…);
return rugged_create_oid(…);

But reasonable people can certainly disagree about this. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@vmg
libgit2 member

rev_parse_oid seems superfluous. Inflating a Ruby object is extremely trivial when compared to the actual lookup cost of the Git object.

@brianmario
libgit2 member

It's less about raw speed (IMO) and more about Ruby's GC, if that matters... I added it for the same reason we added the _oid methods.

@vmg vmg merged commit 8dd5e34 into development
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 13, 2012
  1. @brianmario

    Add `Rugged::Object.rev_parse`

    brianmario committed
    This is wrapped as `Rugged::Repository#rev_parse` as well.
  2. @brianmario

    Add `Rugged::Object.rev_parse_oid`

    brianmario committed
    This lets us perform a rev-parse when all we need is the oid of
    the found object
  3. @brianmario

    whitespace

    brianmario committed
  4. @brianmario

    fix docs

    brianmario committed
  5. @brianmario
  6. @brianmario
  7. @brianmario
  8. @brianmario
  9. @brianmario
View
48 ext/rugged/rugged_object.c
@@ -2,17 +2,17 @@
* The MIT License
*
* Copyright (c) 2011 GitHub, Inc
- *
+ *
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
- *
+ *
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
- *
+ *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -164,7 +164,7 @@ static git_otype class2otype(VALUE klass)
if (klass == rb_cRuggedTree)
return GIT_OBJ_TREE;
-
+
return GIT_OBJ_BAD;
}
@@ -207,6 +207,44 @@ VALUE rb_git_object_lookup(VALUE klass, VALUE rb_repo, VALUE rb_hex)
return rugged_object_new(rb_repo, object);
}
+static VALUE rugged_object_rev_parse(VALUE klass, VALUE rb_repo, VALUE rb_spec, int as_obj)
+{
+ git_object *object;
+ const char *spec;
+ int error;
+ git_repository *repo;
+ VALUE ret;
+
+ Check_Type(rb_spec, T_STRING);
+ spec = RSTRING_PTR(rb_spec);
+
+ if (!rb_obj_is_instance_of(rb_repo, rb_cRuggedRepo))
+ rb_raise(rb_eTypeError, "Expecting a Rugged Repository");
+
+ Data_Get_Struct(rb_repo, git_repository, repo);
+
+ error = git_revparse_single(&object, repo, spec);
+ rugged_exception_check(error);
+
+ if (as_obj) {
+ return rugged_object_new(rb_repo, object);
+ }
+
+ ret = rugged_create_oid(git_object_id(object));
+ git_object_free(object);
+ return ret;
+}
+
+VALUE rb_git_object_rev_parse(VALUE klass, VALUE rb_repo, VALUE rb_spec)
+{
+ return rugged_object_rev_parse(klass, rb_repo, rb_spec, 1);
+}
+
+VALUE rb_git_object_rev_parse_oid(VALUE klass, VALUE rb_repo, VALUE rb_spec)
+{
+ return rugged_object_rev_parse(klass, rb_repo, rb_spec, 0);
+}
+
static VALUE rb_git_object_equal(VALUE self, VALUE other)
{
git_object *a, *b;
@@ -247,6 +285,8 @@ void Init_rugged_object()
{
rb_cRuggedObject = rb_define_class_under(rb_mRugged, "Object", rb_cObject);
rb_define_singleton_method(rb_cRuggedObject, "lookup", rb_git_object_lookup, 2);
+ rb_define_singleton_method(rb_cRuggedObject, "rev_parse", rb_git_object_rev_parse, 2);
+ rb_define_singleton_method(rb_cRuggedObject, "rev_parse_oid", rb_git_object_rev_parse_oid, 2);
rb_define_singleton_method(rb_cRuggedObject, "new", rb_git_object_lookup, 2);
rb_define_method(rb_cRuggedObject, "read_raw", rb_git_object_read_raw, 0);
View
BIN ext/rugged/vendor/libgit2-dist.tar.gz
Binary file not shown.
View
14 lib/rugged/repository.rb
@@ -47,6 +47,20 @@ def lookup(oid)
Rugged::Object.lookup(self, oid)
end
+ # Look up an object by a revision string.
+ #
+ # Returns one of the four classes that inherit from Rugged::Object.
+ def rev_parse(spec)
+ Rugged::Object.rev_parse(self, spec)
+ end
+
+ # Look up an object by a revision string.
+ #
+ # Returns the oid of the matched object as a String
+ def rev_parse_oid(spec)
+ Rugged::Object.rev_parse_oid(self, spec)
+ end
+
# Look up a single reference by name.
#
# Example:
View
13 test/object_test.rb
@@ -30,4 +30,17 @@
assert obj.read_raw
end
+ test "can lookup an object by revision string" do
+ obj = @repo.rev_parse("v1.0")
+ assert "0c37a5391bbff43c37f0d0371823a5509eed5b1d", obj.oid
+ obj = @repo.rev_parse("v1.0^1")
+ assert "8496071c1b46c854b31185ea97743be6a8774479", obj.oid
+ end
+
+ test "can lookup just an object's oid by revision string" do
+ oid = @repo.rev_parse_oid("v1.0")
+ assert "0c37a5391bbff43c37f0d0371823a5509eed5b1d", oid
+ obj = @repo.rev_parse_oid("v1.0^1")
+ assert "8496071c1b46c854b31185ea97743be6a8774479", oid
+ end
end
Something went wrong with that request. Please try again.