Permalink
Browse files

Bring up to speed with libgit2

  • Loading branch information...
vmg committed Nov 1, 2012
1 parent 34af202 commit e0abb115ea7909dd5201560810aad53e2fc87c04
Showing with 39 additions and 25 deletions.
  1. +8 −0 ext/rugged/rugged.h
  2. +2 −4 ext/rugged/rugged_blob.c
  3. +26 −11 ext/rugged/rugged_index.c
  4. +2 −4 ext/rugged/rugged_object.c
  5. +1 −3 ext/rugged/rugged_remote.c
  6. +0 −3 test/index_test.rb
View
@@ -101,6 +101,14 @@ static inline int rugged_parse_bool(VALUE boolean)
return boolean ? 1 : 0;
}
+extern VALUE rb_cRuggedRepo;
+
+static inline void rugged_check_repo(VALUE rb_repo)
+{
+ if (!rb_obj_is_instance_of(rb_repo, rb_cRuggedRepo))
+ rb_raise(rb_eTypeError, "Expecting a Rugged Repository");
+}
+
/* support for string encodings in 1.9 */
#ifdef HAVE_RUBY_ENCODING_H
# define rugged_str_new(str, len, enc) rb_enc_str_new(str, len, enc)
View
@@ -172,8 +172,7 @@ static VALUE rb_git_blob_create(VALUE self, VALUE rb_repo, VALUE rb_buffer)
git_repository *repo;
Check_Type(rb_buffer, T_STRING);
- if (!rb_obj_is_instance_of(rb_repo, rb_cRuggedRepo))
- rb_raise(rb_eTypeError, "Expecting a Rugged Repository");
+ rugged_check_repo(rb_repo);
Data_Get_Struct(rb_repo, git_repository, repo);
@@ -199,8 +198,7 @@ static VALUE rb_git_blob_writefile(VALUE self, VALUE rb_repo, VALUE rb_path)
git_repository *repo;
Check_Type(rb_path, T_STRING);
- if (!rb_obj_is_instance_of(rb_repo, rb_cRuggedRepo))
- rb_raise(rb_eTypeError, "Expecting a Rugged Repository");
+ rugged_check_repo(rb_repo);
Data_Get_Struct(rb_repo, git_repository, repo);
View
@@ -35,7 +35,7 @@ static VALUE rb_git_reuc_fromC(const git_index_reuc_entry *entry);
* Index
*/
-void rb_git_index__free(git_index *index)
+static void rb_git_index__free(git_index *index)
{
git_index_free(index);
}
@@ -47,14 +47,20 @@ VALUE rugged_index_new(VALUE klass, VALUE owner, git_index *index)
return rb_index;
}
-static VALUE rb_git_index_new(VALUE klass, VALUE path)
+static VALUE rb_git_index_new(int argc, VALUE *argv, VALUE klass)
{
git_index *index;
int error;
- Check_Type(path, T_STRING);
+ VALUE rb_path;
+ const char *path = NULL;
- error = git_index_open(&index, StringValueCStr(path));
+ if (rb_scan_args(argc, argv, "01", &rb_path) == 1) {
+ Check_Type(rb_path, T_STRING);
+ path = StringValueCStr(rb_path);
+ }
+
+ error = git_index_open(&index, path);
rugged_exception_check(error);
return rugged_index_new(klass, Qnil, index);
@@ -320,7 +326,7 @@ static void rb_git_indexentry_toC(git_index_entry *entry, VALUE rb_entry)
}
}
-VALUE rb_git_indexer(VALUE self, VALUE rb_packfile_path)
+static VALUE rb_git_indexer(VALUE self, VALUE rb_packfile_path)
{
int error;
git_indexer *indexer;
@@ -343,17 +349,26 @@ VALUE rb_git_indexer(VALUE self, VALUE rb_packfile_path)
return rb_oid;
}
-VALUE rb_git_index_writetree(VALUE self)
+static VALUE rb_git_index_writetree(int argc, VALUE *argv, VALUE self)
{
git_index *index;
git_oid tree_oid;
int error;
+ VALUE rb_repo;
Data_Get_Struct(self, git_index, index);
- error = git_tree_create_fromindex(&tree_oid, index);
- rugged_exception_check(error);
+ if (rb_scan_args(argc, argv, "01", &rb_repo) == 1) {
+ git_repository *repo = NULL;
+ rugged_check_repo(rb_repo);
+ Data_Get_Struct(rb_repo, git_repository, repo);
+ error = git_index_write_tree_to(&tree_oid, index, repo);
+ }
+ else {
+ error = git_index_write_tree(&tree_oid, index);
+ }
+ rugged_exception_check(error);
return rugged_create_oid(&tree_oid);
}
@@ -366,7 +381,7 @@ VALUE rb_git_index_writetree(VALUE self)
* Further index operations (+add+, +update+, +remove+, etc) will
* be considered changes on top of +tree+.
*/
-VALUE rb_git_index_readtree(VALUE self, VALUE rb_tree)
+static VALUE rb_git_index_readtree(VALUE self, VALUE rb_tree)
{
git_index *index;
git_tree *tree;
@@ -387,7 +402,7 @@ void Init_rugged_index()
* Index
*/
rb_cRuggedIndex = rb_define_class_under(rb_mRugged, "Index", rb_cObject);
- rb_define_singleton_method(rb_cRuggedIndex, "new", rb_git_index_new, 1);
+ rb_define_singleton_method(rb_cRuggedIndex, "new", rb_git_index_new, -1);
rb_define_method(rb_cRuggedIndex, "count", rb_git_index_count, 0);
rb_define_method(rb_cRuggedIndex, "reload", rb_git_index_read, 0);
@@ -403,7 +418,7 @@ void Init_rugged_index()
rb_define_method(rb_cRuggedIndex, "remove", rb_git_index_remove, -1);
- rb_define_method(rb_cRuggedIndex, "write_tree", rb_git_index_writetree, 0);
+ rb_define_method(rb_cRuggedIndex, "write_tree", rb_git_index_writetree, -1);
rb_define_method(rb_cRuggedIndex, "read_tree", rb_git_index_readtree, 1);
rb_define_singleton_method(rb_cRuggedIndex, "index_pack", rb_git_indexer, 1);
@@ -190,8 +190,7 @@ VALUE rb_git_object_lookup(VALUE klass, VALUE rb_repo, VALUE rb_hex)
Check_Type(rb_hex, T_STRING);
oid_length = (int)RSTRING_LEN(rb_hex);
- if (!rb_obj_is_instance_of(rb_repo, rb_cRuggedRepo))
- rb_raise(rb_eTypeError, "Expecting a Rugged Repository");
+ rugged_check_repo(rb_repo);
if (oid_length > GIT_OID_HEXSZ)
rb_raise(rb_eTypeError, "The given OID is too long");
@@ -222,8 +221,7 @@ static VALUE rugged_object_rev_parse(VALUE klass, VALUE rb_repo, VALUE rb_spec,
Check_Type(rb_spec, T_STRING);
spec = RSTRING_PTR(rb_spec);
- if (!rb_obj_is_instance_of(rb_repo, rb_cRuggedRepo))
- rb_raise(rb_eTypeError, "Expecting a Rugged Repository");
+ rugged_check_repo(rb_repo);
Data_Get_Struct(rb_repo, git_repository, repo);
@@ -44,9 +44,7 @@ static VALUE rb_git_remote__new(int argc, VALUE *argv, VALUE klass)
rb_scan_args(argc, argv, "21", &rb_repo, &rb_url, &rb_name);
Check_Type(rb_url, T_STRING);
-
- if (!rb_obj_is_instance_of(rb_repo, rb_cRuggedRepo))
- rb_raise(rb_eTypeError, "Expecting a Rugged Repository");
+ rugged_check_repo(rb_repo);
Data_Get_Struct(rb_repo, git_repository, repo);
View
@@ -164,9 +164,6 @@ def new_index_entry
@index.add('test.txt')
@index.write
- sleep(1) # we need this sleep to sync at the FS level
- # most FSs have 1s granularity on mtimes
-
rindex = Rugged::Index.new(File.join(@tmppath, '/.git/index'))
e = rindex['test.txt']
assert_equal 0, e[:stage]

0 comments on commit e0abb11

Please sign in to comment.