git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH/RFC 6/7] ruby: remove one fork

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Sep 21, 2013, 18:48 UTC
Message-ID
<1379789295-18519-7-git-send-email-felipe.contreras@gmail.com>
In-Reply-To
<1379789295-18519-1-git-send-email-felipe.contreras@gmail.com>

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.contreras@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
Previous: Felipe ContrerasNext: Felipe Contreras
Message 7 of 26 in “Support for Ruby”
  1. 0/7 Support for RubyFelipe Contreras, Sep 21, 2013
  2. 1/7 Add support for ruby commandsFelipe Contreras, Sep 21, 2013
  3. 2/7 ruby: add setup scriptFelipe Contreras, Sep 21, 2013
  4. 3/7 ruby: add simple wrappersFelipe Contreras, Sep 21, 2013
  5. 4/7 ruby: rewrite 'request-pull'Felipe Contreras, Sep 21, 2013
  6. 5/7 ruby: rewrite perl scriptFelipe Contreras, Sep 21, 2013
  7. 6/7 ruby: remove one forkFelipe Contreras, Sep 21, 2013
  8. 7/7 ruby: rewrite 'reset'Felipe Contreras, Sep 21, 2013
  9. brian m. carlsonSep 21, 2013
  10. Felipe ContrerasSep 21, 2013
  11. brian m. carlsonSep 21, 2013
  12. Felipe ContrerasSep 22, 2013
  13. Fredrik GustafssonSep 22, 2013
  14. Felipe ContrerasSep 22, 2013
  15. Fredrik GustafssonSep 22, 2013
  16. Felipe ContrerasSep 22, 2013
  17. Patrick DonnellySep 23, 2013
  18. Felipe ContrerasSep 23, 2013
  19. Patrick DonnellySep 23, 2013
  20. Felipe ContrerasSep 23, 2013
  21. Junio C HamanoSep 23, 2013
  22. brian m. carlsonSep 23, 2013
  23. Felipe ContrerasSep 28, 2013
  24. Felipe ContrerasSep 23, 2013
  25. Felipe ContrerasSep 28, 2013
  26. Felipe ContrerasSep 28, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.