示例#1
0
/*
 *	call-seq:
 *		tree[e] -> entry
 *		tree.get_entry(e) -> entry
 *
 *	Return one of the entries from a tree as a +Hash+. If +e+ is a number, the +e+nth entry
 *	from the tree will be returned. If +e+ is a string, the entry with that name
 *	will be returned.
 *
 *	If the entry doesn't exist, +nil+ will be returned.
 *
 *		tree[3] #=> {:name => "foo.txt", :type => :blob, :oid => "d8786bfc97485e8d7b19b21fb88c8ef1f199fc3f", :filemode => 0}
 *
 *		tree['bar.txt'] #=> {:name => "bar.txt", :type => :blob, :oid => "de5ba987198bcf2518885f0fc1350e5172cded78", :filemode => 0}
 *
 *		tree['baz.txt'] #=> nil
 */
static VALUE rb_git_tree_get_entry(VALUE self, VALUE entry_id)
{
	git_tree *tree;
	Data_Get_Struct(self, git_tree, tree);

	if (TYPE(entry_id) == T_FIXNUM)
		return rb_git_treeentry_fromC(git_tree_entry_byindex(tree, FIX2INT(entry_id)));

	else if (TYPE(entry_id) == T_STRING)
		return rb_git_treeentry_fromC(git_tree_entry_byname(tree, StringValueCStr(entry_id)));

	else
		rb_raise(rb_eTypeError, "entry_id must be either an index or a filename");
}
示例#2
0
static int rugged__treewalk_cb(const char *root, const git_tree_entry *entry, void *proc)
{
	rb_funcall((VALUE)proc, rb_intern("call"), 2,
		rugged_str_new2(root, NULL),
		rb_git_treeentry_fromC(entry));

	return GIT_OK;
}
示例#3
0
static VALUE rb_git_treebuilder_get(VALUE self, VALUE path)
{
	git_treebuilder *builder;
	Data_Get_Struct(self, git_treebuilder, builder);

	Check_Type(path, T_STRING);

	return rb_git_treeentry_fromC(git_treebuilder_get(builder, StringValueCStr(path)));
}
示例#4
0
/*
 *  call-seq:
 *    tree.get_entry_by_oid(rb_oid) -> entry
 *
 *  Return one of the entries from a tree as a +Hash+, based off the oid SHA.
 *
 *  If the entry doesn't exist, +nil+ will be returned.
 *
 *  This does a full traversal of the every element in the tree, so this method
 *  is not especially fast.
 *
 *    tree.get_entry_by_oid("d8786bfc97485e8d7b19b21fb88c8ef1f199fc3f")
 *    #=> {:name => "foo.txt", :type => :blob, :oid => "d8786bfc97485e8d7b19b21fb88c8ef1f199fc3f", :filemode => 0}
 *
 */
static VALUE rb_git_tree_get_entry_by_oid(VALUE self, VALUE rb_oid)
{
	git_tree *tree;
	git_oid oid;
	Data_Get_Struct(self, git_tree, tree);

	Check_Type(rb_oid, T_STRING);
	rugged_exception_check(git_oid_fromstr(&oid, StringValueCStr(rb_oid)));

	return rb_git_treeentry_fromC(git_tree_entry_byid(tree, &oid));
}
示例#5
0
static VALUE rb_git_tree_path(VALUE self, VALUE rb_path)
{
  int error;
  git_tree *tree;
  git_tree_entry *entry;
  VALUE rb_entry;
  Data_Get_Struct(self, git_tree, tree);
  Check_Type(rb_path, T_STRING);

  error = git_tree_entry_bypath(&entry, tree, StringValueCStr(rb_path));
  rugged_exception_check(error);

  rb_entry = rb_git_treeentry_fromC(entry);
  git_tree_entry_free(entry);

  return rb_entry;
}
示例#6
0
/*
 *	call-seq:
 *		tree.each { |entry| block }
 *		tree.each -> Iterator
 *
 *	Call +block+ with each of the entries of the subtree as a +Hash+. If no +block+
 *	is given, an +Iterator+ is returned instead.
 *
 *	Note that only the entries in the root of the tree are yielded; if you need to
 *	list also entries in subfolders, use +tree.walk+ instead.
 *
 *		tree.each { |entry| puts entry.inspect }
 *
 *	generates:
 *
 *		{:name => "foo.txt", :type => :blob, :oid => "d8786bfc97485e8d7b19b21fb88c8ef1f199fc3f", :filemode => 0}
 *		{:name => "bar.txt", :type => :blob, :oid => "de5ba987198bcf2518885f0fc1350e5172cded78", :filemode => 0}
 *		...
 */
static VALUE rb_git_tree_each(VALUE self)
{
	git_tree *tree;
	unsigned int i, count;
	Data_Get_Struct(self, git_tree, tree);

	if (!rb_block_given_p())
		return rb_funcall(self, rb_intern("to_enum"), 0);

	count = git_tree_entrycount(tree);

	for (i = 0; i < count; ++i) {
		const git_tree_entry *entry = git_tree_entry_byindex(tree, i);
		rb_yield(rb_git_treeentry_fromC(entry));
	}

	return Qnil;
}
示例#7
0
static int rugged__treewalk_cb(const char *root, const git_tree_entry *entry, void *payload)
{
	int *exception = (int *)payload;

	VALUE rb_result, rb_args = rb_ary_new2(2);

	rb_ary_push(rb_args, rb_str_new_utf8(root));
	rb_ary_push(rb_args, rb_git_treeentry_fromC(entry));

	rb_result = rb_protect(rb_yield_splat, rb_args, exception);

	if (*exception)
		return -1;

	/* skip entry when 'false' is returned */
	if (TYPE(rb_result) == T_FALSE)
		return 1;

	/* otherwise continue normal iteration */
	return 0;
}
示例#8
0
static int treebuilder_cb(const git_tree_entry *entry, void *opaque)
{
	VALUE proc = (VALUE)opaque;
	VALUE ret = rb_funcall(proc, rb_intern("call"), 1, rb_git_treeentry_fromC(entry));
	return rugged_parse_bool(ret);
}