Signed-off-by: Felipe Contreras <felipe.contre...@gmail.com>
---

Notes:
    Our first usage of Ruby blocks.
    
    Notice how the code inside the block is in the same scope as the code 
outside.
    
      cb_data = "config"
      git_config() do |key, value|
        puts "%s: %s=%s" % [cb_data, key, value]
      end
    
    This invalidates the need for callback data, because the block has access to
    those variables already.

 ruby.c           | 15 +++++++++++++++
 t/t10000-ruby.sh | 10 ++++++++++
 2 files changed, 25 insertions(+)

diff --git a/ruby.c b/ruby.c
index 0ab56de..36de943 100644
--- a/ruby.c
+++ b/ruby.c
@@ -51,11 +51,26 @@ static VALUE git_rb_dwim_ref(VALUE self, VALUE name)
        return rb_ary_new3(3, sha1_to_str(buf), INT2NUM(r), cstr_to_str(ref));
 }
 
+static int git_config_fn(const char *var, const char *value, void *cb_data)
+{
+       VALUE r;
+       r = rb_yield_values(2, rb_str_new2(var), rb_str_new2(value));
+       return r == Qfalse;
+}
+
+static VALUE git_rb_git_config(VALUE self)
+{
+       int r;
+       r = git_config(git_config_fn, NULL);
+       return INT2FIX(r);
+}
+
 static void git_ruby_init(void)
 {
        rb_define_global_function("setup_git_directory", 
git_rb_setup_git_directory, 0);
        rb_define_global_function("for_each_ref", git_rb_for_each_ref, 0);
        rb_define_global_function("dwim_ref", git_rb_dwim_ref, 1);
+       rb_define_global_function("git_config", git_rb_git_config, 0);
 }
 
 static int run_ruby_command(const char *cmd, int argc, const char **argv)
diff --git a/t/t10000-ruby.sh b/t/t10000-ruby.sh
index d719b30..cdbf5d8 100755
--- a/t/t10000-ruby.sh
+++ b/t/t10000-ruby.sh
@@ -63,4 +63,14 @@ test_expect_success 'test dwim_ref()' '
        test_cmp expected actual
 '
 
+test_expect_success 'test git_config()' '
+       git ruby > actual <<-EOF &&
+       git_config() do |key, value|
+         puts "%s=%s" % [key, value]
+       end
+       EOF
+       git config --list > expected &&
+       test_cmp expected actual
+'
+
 test_done
-- 
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