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

[PATCH/RFC 3/7] ruby: add simple wrappers

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

So that we can use for_each_ref() inside Ruby, and provide an example script.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 Makefile    |  1 +
 git-refs.rb |  7 +++++++
 ruby.c      | 27 +++++++++++++++++++++++++++
 3 files changed, 35 insertions(+)
 create mode 100644 git-refs.rb
diff --git a/Makefile b/Makefile
index 138f9bf..8a4e48f 100644
--- a/Makefile
+++ b/Makefile
@@ -492,6 +492,7 @@ SCRIPT_PYTHON += git-remote-testpy.py
 SCRIPT_PYTHON += git-p4.py
 
 SCRIPT_RUBY += git-rb-setup.rb
+SCRIPT_RUBY += git-refs.rb
 
 NO_INSTALL += git-remote-testgit
 NO_INSTALL += git-remote-testpy
diff --git a/git-refs.rb b/git-refs.rb
new file mode 100644
index 0000000..b048714
--- /dev/null
+++ b/git-refs.rb
@@ -0,0 +1,7 @@
+#!/usr/bin/env ruby
+
+require_relative 'git-rb-setup'
+
+for_each_ref() do |name, sha1, flags|
+  puts '%s: %s' % [name, sha1_to_hex(sha1)]
+end
diff --git a/ruby.c b/ruby.c
index 5701753..7f0cc9d 100644
--- a/ruby.c
+++ b/ruby.c
@@ -1,12 +1,38 @@
 #include "cache.h"
 #include "exec_cmd.h"
+#include "refs.h"
 
 #undef NORETURN
 #undef PATH_SEP
 
 #include <ruby.h>
 
+static inline VALUE sha1_to_str(const unsigned char *sha1)
+{
+	return rb_str_new((const char *)sha1, 20);
+}
+
+static int for_each_ref_fn(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
+{
+	VALUE r;
+	r = rb_yield_values(3, rb_str_new2(refname), sha1_to_str(sha1), INT2FIX(flags));
+	return r == Qfalse;
+}
+
+static VALUE git_rb_for_each_ref(void)
+{
+	int r;
+	r = for_each_ref(for_each_ref_fn, NULL);
+	return INT2FIX(r);
+}
+
+static void git_init(void)
+{
+	rb_define_global_function("for_each_ref", git_rb_for_each_ref, 0);
+}
+
 static const char *commands[] = {
+	"refs",
 };
 
 static void run_ruby_command(int argc, const char **argv)
@@ -23,6 +49,7 @@ static void run_ruby_command(int argc, const char **argv)
 	snprintf(buf, PATH_MAX, "%s/git-%s.rb", dir, cmd);
 
 	ruby_init();
+	git_init();
 
 	prefix = Qnil;
 	rb_define_variable("$prefix", &prefix);
-- 
1.8.4-fc
Previous: Felipe ContrerasNext: Felipe Contreras
Message 4 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.