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

[PATCH v2 01/44] Add support for ruby commands

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Sep 28, 2013, 22:03 UTC
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 <felipe.contreras@gmail.com>
---
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 <<EOF &&
      for_each_ref() do |name, sha1, flags|
        puts "%s: %s" % [name, sha1_to_hex(sha1)]
      end
 .gitignore       |  1 +
 Makefile         | 19 ++++++++++++++++++-
 git-rb-setup.rb  | 11 +++++++++++
 ruby.c           | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 t/t10000-ruby.sh | 42 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 128 insertions(+), 1 deletion(-)
 create mode 100644 git-rb-setup.rb
 create mode 100644 ruby.c
 create mode 100755 t/t10000-ruby.sh
diff --git a/.gitignore b/.gitignore
index 6b1fd1b..51b04be 100644
--- a/.gitignore
+++ b/.gitignore
@@ -139,6 +139,7 @@
 /git-rev-parse
 /git-revert
 /git-rm
+/git-ruby
 /git-send-email
 /git-send-pack
 /git-sh-i18n
diff --git a/Makefile b/Makefile
index 3588ca1..2fdee15 100644
--- a/Makefile
+++ b/Makefile
@@ -491,6 +491,8 @@ SCRIPT_PERL += git-svn.perl
 SCRIPT_PYTHON += git-remote-testpy.py
 SCRIPT_PYTHON += git-p4.py
 
+SCRIPT_RUBY += git-rb-setup.rb
+
 NO_INSTALL += git-remote-testgit
 NO_INSTALL += git-remote-testpy
 
@@ -502,6 +504,7 @@ SCRIPT_PYTHON_GEN = $(patsubst %.py,%,$(SCRIPT_PYTHON))
 SCRIPT_SH_INS = $(filter-out $(NO_INSTALL),$(SCRIPT_SH_GEN))
 SCRIPT_PERL_INS = $(filter-out $(NO_INSTALL),$(SCRIPT_PERL_GEN))
 SCRIPT_PYTHON_INS = $(filter-out $(NO_INSTALL),$(SCRIPT_PYTHON_GEN))
+SCRIPT_RUBY_INS = $(filter-out $(NO_INSTALL),$(SCRIPT_RUBY))
 
 # Individual rules to allow e.g.
 # "make -C ../.. SCRIPT_PERL=contrib/foo/bar.perl build-perl-script"
@@ -511,13 +514,15 @@ build-perl-script: $(SCRIPT_PERL_GEN)
 build-sh-script: $(SCRIPT_SH_GEN)
 build-python-script: $(SCRIPT_PYTHON_GEN)
 
-.PHONY: install-perl-script install-sh-script install-python-script
+.PHONY: install-perl-script install-sh-script install-python-script install-ruby-script
 install-sh-script: $(SCRIPT_SH_INS)
 	$(INSTALL) $^ '$(DESTDIR_SQ)$(gitexec_instdir_SQ)'
 install-perl-script: $(SCRIPT_PERL_INS)
 	$(INSTALL) $^ '$(DESTDIR_SQ)$(gitexec_instdir_SQ)'
 install-python-script: $(SCRIPT_PYTHON_INS)
 	$(INSTALL) $^ '$(DESTDIR_SQ)$(gitexec_instdir_SQ)'
+install-ruby-script: $(SCRIPT_RUBY_INS)
+	$(INSTALL) $^ '$(DESTDIR_SQ)$(gitexec_instdir_SQ)'
 
 .PHONY: clean-perl-script clean-sh-script clean-python-script
 clean-sh-script:
@@ -530,6 +535,7 @@ clean-python-script:
 SCRIPTS = $(SCRIPT_SH_INS) \
 	  $(SCRIPT_PERL_INS) \
 	  $(SCRIPT_PYTHON_INS) \
+	  $(SCRIPT_RUBY_INS) \
 	  git-instaweb
 
 ETAGS_TARGET = TAGS
@@ -1502,6 +1508,12 @@ ifneq (,$(XDL_FAST_HASH))
 	BASIC_CFLAGS += -DXDL_FAST_HASH
 endif
 
+ifndef NO_RUBY
+	RUBY_LIBS = $(shell pkg-config --libs ruby-2.0)
+	RUBY_CFLAGS = $(shell pkg-config --cflags ruby-2.0)
+	PROGRAM_OBJS += ruby.o
+endif
+
 ifeq ($(TCLTK_PATH),)
 NO_TCLTK = NoThanks
 endif
@@ -2059,6 +2071,11 @@ git-http-push$X: revision.o http.o http-push.o GIT-LDFLAGS $(GITLIBS)
 	$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
 		$(LIBS) $(CURL_LIBCURL) $(EXPAT_LIBEXPAT)
 
+git-ruby$X: BASIC_CFLAGS += $(RUBY_CFLAGS)
+git-ruby$X: ruby.o GIT-LDFLAGS $(GITLIBS)
+	$(QUIET_LINK)$(CC) $(ALL_CFLAGS) $(RUBY_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
+		$(LIBS) $(RUBY_LIBS)
+
 git-remote-testsvn$X: remote-testsvn.o GIT-LDFLAGS $(GITLIBS) $(VCSSVN_LIB)
 	$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS) \
 	$(VCSSVN_LIB)
diff --git a/git-rb-setup.rb b/git-rb-setup.rb
new file mode 100644
index 0000000..969278a
--- /dev/null
+++ b/git-rb-setup.rb
@@ -0,0 +1,11 @@
+#!/usr/bin/env ruby
+
+def die(*args)
+  fmt = args.shift
+  $stderr.printf("fatal: %s\n" % fmt, *args)
+  exit 128
+end
+
+def sha1_to_hex(sha1)
+  sha1.unpack('H*').first
+end
diff --git a/ruby.c b/ruby.c
new file mode 100644
index 0000000..ee6a0e7
--- /dev/null
+++ b/ruby.c
@@ -0,0 +1,56 @@
+#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_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
Previous: Felipe ContrerasNext: Ramkumar Ramachandra
Message 2 of 62 in “Ruby support”
  1. 00/44 Ruby supportFelipe Contreras, Sep 28, 2013
  2. 01/44 Add support for ruby commandsFelipe Contreras, Sep 28, 2013
  3. Ramkumar RamachandraSep 29, 2013
  4. Felipe ContrerasSep 29, 2013
  5. 02/44 ruby: add support for internal ruby programsFelipe Contreras, Sep 28, 2013
  6. Ramkumar RamachandraSep 29, 2013
  7. Felipe ContrerasSep 29, 2013
  8. Felipe ContrerasSep 29, 2013
  9. 03/44 request-pull: fix annotated tag checkFelipe Contreras, Sep 28, 2013
  10. Ramkumar RamachandraSep 29, 2013
  11. 04/44 request-pull: fix for specific branchFelipe Contreras, Sep 28, 2013
  12. 05/44 request-pull: use appropriate refFelipe Contreras, Sep 28, 2013
  13. 06/44 request-pull: fix exact match checkingFelipe Contreras, Sep 28, 2013
  14. 07/44 request-pull: trivial simplificationFelipe Contreras, Sep 28, 2013
  15. 08/44 request-pull: t: trivial whitespace style fixesFelipe Contreras, Sep 28, 2013
  16. 09/44 request-pull: t: add missing catFelipe Contreras, Sep 28, 2013
  17. 10/44 ruby: rewrite 'request-pull'Felipe Contreras, Sep 28, 2013
  18. Ramkumar RamachandraSep 29, 2013
  19. 11/44 ruby: request-pull: rewrite perl scriptFelipe Contreras, Sep 28, 2013
  20. Ramkumar RamachandraSep 29, 2013
  21. 12/44 ruby: request-pull: trivial simplificationsFelipe Contreras, Sep 28, 2013
  22. 13/44 ruby: bind setup_git_directory()Felipe Contreras, Sep 28, 2013
  23. Ramkumar RamachandraSep 29, 2013
  24. Felipe ContrerasSep 29, 2013
  25. 14/44 ruby: bind dwim_ref()Felipe Contreras, Sep 28, 2013
  26. Ramkumar RamachandraSep 29, 2013
  27. Felipe ContrerasSep 29, 2013
  28. 15/44 ruby: request-pull: use native dwim_ref()Felipe Contreras, Sep 28, 2013
  29. 16/44 ruby: bind git_config()Felipe Contreras, Sep 28, 2013
  30. 17/44 ruby: request-pull: use native git_config()Felipe Contreras, Sep 28, 2013
  31. 18/44 ruby: bind read_ref()/peel_ref()Felipe Contreras, Sep 28, 2013
  32. 19/44 ruby: bind get_sha1()Felipe Contreras, Sep 28, 2013
  33. 20/44 ruby: request-pull: simplify tag fetchingFelipe Contreras, Sep 28, 2013
  34. 21/44 ruby: request-pull: use get_sha1()Felipe Contreras, Sep 28, 2013
  35. 22/44 ruby: add Commit classFelipe Contreras, Sep 28, 2013
  36. 23/44 ruby: bind get_merge_bases()Felipe Contreras, Sep 28, 2013
  37. 24/44 ruby: request-pull: use get_merge_bases()Felipe Contreras, Sep 28, 2013
  38. 25/44 ruby: request-pull: trivial cleanupsFelipe Contreras, Sep 28, 2013
  39. 26/44 ruby: bind remote and transport stuffFelipe Contreras, Sep 28, 2013
  40. 27/44 ruby: request-pull: use native remote and transportFelipe Contreras, Sep 28, 2013
  41. 28/44 ruby: bind find_unique_abbrev()Felipe Contreras, Sep 28, 2013
  42. 29/44 ruby: request-pull: use native commit infoFelipe Contreras, Sep 28, 2013
  43. 30/44 ruby: bind read_sha1_file()Felipe Contreras, Sep 28, 2013
  44. 31/44 ruby: request-pull: use read_sha1_file()Felipe Contreras, Sep 28, 2013
  45. 32/44 revision: add missing includeFelipe Contreras, Sep 28, 2013
  46. 33/44 shortlog: add missing declarationFelipe Contreras, Sep 28, 2013
  47. 34/44 shortlog: split builtin from common codeFelipe Contreras, Sep 28, 2013
  48. 35/44 ruby: add RevInfo and DiffOptionsFelipe Contreras, Sep 28, 2013
  49. 36/44 ruby: bind shortlog()Felipe Contreras, Sep 28, 2013
  50. 37/44 ruby: request-pull: use shortlog()Felipe Contreras, Sep 28, 2013
  51. 38/44 ruby: bind diff_tree_sha1()Felipe Contreras, Sep 28, 2013
  52. 39/44 ruby: bind log_tree_diff_flush()Felipe Contreras, Sep 28, 2013
  53. 40/44 ruby: request-pull: use native diff_tree stuffFelipe Contreras, Sep 28, 2013
  54. 41/44 ruby: request-pull: remove rescue blockFelipe Contreras, Sep 28, 2013
  55. 42/44 ruby: remove GIT_PAGER from environmentFelipe Contreras, Sep 28, 2013
  56. Stefan BellerSep 28, 2013
  57. Felipe ContrerasSep 28, 2013
  58. 43/44 ruby: add simpler option parserFelipe Contreras, Sep 28, 2013
  59. 44/44 request-pull: rewrite to CFelipe Contreras, Sep 28, 2013
  60. Ramkumar RamachandraSep 29, 2013
  61. Ramkumar RamachandraSep 29, 2013
  62. Felipe ContrerasSep 29, 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.