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

Signed-off-by: Felipe Contreras <felipe.contre...@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

--
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