This is an example of how to start moving out of Git commands, towards
using Git's internal library.

Signed-off-by: Felipe Contreras <felipe.contre...@gmail.com>
---
 git-request-pull.rb |  8 +++++++-
 ruby.c              | 21 +++++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/git-request-pull.rb b/git-request-pull.rb
index 6a96a98..fc3175c 100644
--- a/git-request-pull.rb
+++ b/git-request-pull.rb
@@ -37,7 +37,13 @@ head = ARGV[2] || 'HEAD'
 status = 0
 branch_name = nil
 
-headref = `git symbolic-ref -q "#{head}"`.chomp
+def get_symbolic_ref(refname)
+  refname, sha1, flags = resolve_ref_unsafe(refname, 0)
+  return nil if (flags & REF_ISSYMREF) == 0
+  return refname
+end
+
+headref = get_symbolic_ref(head)
 
 if system(%[git show-ref -q --verify "#{headref}"])
   branch_name = headref.gsub(/^refs\/heads\//, '')
diff --git a/ruby.c b/ruby.c
index 733215a..b4e874d 100644
--- a/ruby.c
+++ b/ruby.c
@@ -26,9 +26,30 @@ static VALUE git_rb_for_each_ref(void)
        return INT2FIX(r);
 }
 
+static VALUE git_rb_resolve_ref_unsafe(VALUE self, VALUE refname, VALUE 
reading)
+{
+       VALUE a = rb_ary_new2(3);
+       unsigned char sha1[20];
+       int flag;
+       const char *r;
+
+       r = resolve_ref_unsafe(RSTRING_PTR(refname), sha1, FIX2INT(reading), 
&flag);
+       if (!r)
+               return Qnil;
+       rb_ary_store(a, 0, rb_str_new2(r));
+       rb_ary_store(a, 1, sha1_to_str(sha1));
+       rb_ary_store(a, 2, INT2FIX(flag));
+       return a;
+}
+
 static void git_init(void)
 {
+       rb_define_global_const("REF_ISSYMREF", INT2FIX(REF_ISSYMREF));
+       rb_define_global_const("REF_ISPACKED", INT2FIX(REF_ISPACKED));
+       rb_define_global_const("REF_ISBROKEN", INT2FIX(REF_ISBROKEN));
+
        rb_define_global_function("for_each_ref", git_rb_for_each_ref, 0);
+       rb_define_global_function("resolve_ref_unsafe", 
git_rb_resolve_ref_unsafe, 2);
 }
 
 static const char *commands[] = {
-- 
1.8.4-fc

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to