From: Felipe Contreras Date: Sat, 28 Sep 2013 22:03:26 GMT Subject: [PATCH v2 01/44] Add support for ruby commands Message-ID: <1380405849-13000-2-git-send-email-felipe.contreras@gmail.com> In-Reply-To: <1380405849-13000-1-git-send-email-felipe.contreras@gmail.com> Signed-off-by: Felipe Contreras --- Notes: With this support, third parties would be able to write scripts more easily and access libgit facilities. Also, it could help developers by allowing easier prototyping git ruby > actual < + +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_ruby_init(void) +{ + rb_define_global_function("for_each_ref", git_rb_for_each_ref, 0); +} + +static int run_ruby_command(const char *cmd, int argc, const char **argv) +{ + static char buf[PATH_MAX + 1]; + void *node; + struct stat st; + + ruby_init(); + git_ruby_init(); + + node = ruby_options(argc, (char **)argv); + + ruby_script(cmd); + snprintf(buf, PATH_MAX, "%s/%s", git_exec_path(), "git-rb-setup.rb"); + if (!stat(buf, &st)) + rb_load(rb_str_new2(buf), 0); + + return ruby_run_node(node); +} + +int main(int argc, const char **argv) +{ + return run_ruby_command(argv[1], argc, argv); +} diff --git a/t/t10000-ruby.sh b/t/t10000-ruby.sh new file mode 100755 index 0000000..eb03706 --- /dev/null +++ b/t/t10000-ruby.sh @@ -0,0 +1,42 @@ +#!/bin/sh +# +# Copyright (c) 2013 Felipe Contreras +# + +test_description='test ruby support' + +. ./test-lib.sh + +test_expect_success 'basic support' ' + git ruby > actual <<-EOF && + puts "hello world" + EOF + echo "hello world" > expected && + test_cmp expected actual +' + +test_expect_success 'argument passing' ' + cat > script <<-"EOF" && + p($0) + p(ARGV) + EOF + git ruby script foo bar > actual && + cat > expected <<-EOF && + "script" + ["foo", "bar"] + EOF + test_cmp expected actual +' + +test_expect_success 'test for_each_ref()' ' + test_commit foo && + git ruby > actual <<-EOF && + for_each_ref() do |name, sha1, flags| + puts "%s: %s" % [name, sha1_to_hex(sha1)] + end + EOF + git for-each-ref --format="%(refname): %(objectname)" > expected && + test_cmp expected actual +' + +test_done -- 1.8.4-fc