Class: Rugged::Tree

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/rugged/tree.rb,
ext/rugged/rugged_tree.c

Defined Under Namespace

Classes: Builder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

#<=>, #==, #create_note, lookup, new, #notes, #oid, #read_raw, #remove_note, rev_parse, rev_parse_oid, #type

Instance Attribute Details

#ownerObject (readonly) Also known as: repo

Returns the value of attribute owner.



160
161
162
# File 'lib/rugged/tree.rb', line 160

def owner
  @owner
end

Class Method Details

.diff(repo, tree, other_tree = nil, options = {}) ⇒ Object

call-seq:

Tree.diff(repo, tree, diffable[, options]) -> diff

Returns a diff between the ‘tree` and the diffable object that was given. diffable can either be a Rugged::Commit, a Rugged::Tree, a Rugged::Index, or nil.

The tree object will be used as the “old file” side of the diff, while the parent tree or the diffable object will be used for the “new file” side.

If tree or diffable are nil, they will be treated as an empty tree. Passing both as ‘nil` will raise an exception.

The following options can be passed in the options Hash:

:paths

An array of paths / fnmatch patterns to constrain the diff to a specific set of files. Also see :disable_pathspec_match.

:max_size

An integer specifying the maximum byte size of a file before a it will be treated as binary. The default value is 512MB.

:context_lines

The number of unchanged lines that define the boundary of a hunk (and to display before and after the actual changes). The default is 3.

:interhunk_lines

The maximum number of unchanged lines between hunk boundaries before the hunks will be merged into a one. The default is 0.

:old_prefix

The virtual “directory” to prefix to old filenames in hunk headers. The default is “a”.

:new_prefix

The virtual “directory” to prefix to new filenames in hunk headers. The default is “b”.

:reverse

If true, the sides of the diff will be reversed.

:force_text

If true, all files will be treated as text, disabling binary attributes & detection.

:ignore_whitespace

If true, all whitespace will be ignored.

:ignore_whitespace_change

If true, changes in amount of whitespace will be ignored.

:ignore_whitespace_eol

If true, whitespace at end of line will be ignored.

:ignore_submodules

if true, submodules will be excluded from the diff completely.

:patience

If true, the “patience diff” algorithm will be used (currenlty unimplemented).

:include_ignored

If true, ignored files will be included in the diff.

:include_untracked

If true, untracked files will be included in the diff.

:include_unmodified

If true, unmodified files will be included in the diff.

:recurse_untracked_dirs

Even if :include_untracked is true, untracked directories will only be marked with a single entry in the diff. If this flag is set to true, all files under ignored directories will be included in the diff, too.

:disable_pathspec_match

If true, the given :paths will be applied as exact matches, instead of as fnmatch patterns.

:deltas_are_icase

If true, filename comparisons will be made with case-insensitivity.

:show_untracked_content

if true, untracked content will be contained in the the diff patch text.

:skip_binary_check

If true, diff deltas will be generated without spending time on binary detection. This is useful to improve performance in cases where the actual file content difference is not needed.

:include_typechange

If true, type changes for files will not be interpreted as deletion of the “old file” and addition of the “new file”, but will generate typechange records.

:include_typechange_trees

Even if :include_typechange is true, blob -> tree changes will still usually be handled as a deletion of the blob. If this flag is set to true, blob -> tree changes will be marked as typechanges.

:ignore_filemode

If true, file mode changes will be ignored.

:recurse_ignored_dirs

Even if :include_ignored is true, ignored directories will only be marked with a single entry in the diff. If this flag is set to true, all files under ignored directories will be included in the diff, too.

Examples:

# Emulating `git diff <treeish>`
tree = Rugged::Tree.lookup(repo, "d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
diff = tree.diff(repo.index)
diff.merge!(tree.diff)

# Tree-to-Tree Diff
tree = Rugged::Tree.lookup(repo, "d70d245ed97ed2aa596dd1af6536e4bfdb047b69")
other_tree = Rugged::Tree.lookup(repo, "7a9e0b02e63179929fed24f0a3e0f19168114d10")
diff = tree.diff(other_tree)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rugged/tree.rb', line 129

def self.diff(repo, tree, other_tree = nil, options = {})
  if tree && !tree.is_a?(Rugged::Tree)
    raise TypeError, "At least a Rugged::Tree object is required for diffing"
  end

  if other_tree.nil?
    if tree.nil?
      raise TypeError, "Need 'old' or 'new' for diffing"
    else
      diff_tree_to_tree repo, tree, nil, options
    end
  else
    if other_tree.is_a?(::String)
      other_tree = Rugged::Object.rev_parse repo, other_tree
    end

    case other_tree
    when Rugged::Commit
      diff_tree_to_tree repo, tree, other_tree.tree, options
    when Rugged::Tree
      diff_tree_to_tree repo, tree, other_tree, options
    when Rugged::Index
      diff_tree_to_index repo, tree, other_tree, options
    else
      raise TypeError, "A Rugged::Commit, Rugged::Tree or Rugged::Index instance is required"
    end
  end
end

.empty(repo) ⇒ Object

Look up the empty tree in the given repository repo. The empty tree’s id is hard-coded to exist in a repository.

Returns a new instance of the empty tree.



593
594
595
596
597
598
599
600
601
602
603
604
# File 'ext/rugged/rugged_tree.c', line 593

static VALUE rb_git_tree_empty(VALUE self, VALUE rb_repo)
{
	git_repository *repo;
	git_tree *tree;

	rugged_check_repo(rb_repo);
	Data_Get_Struct(rb_repo, git_repository, repo);

	rugged_exception_check(git_tree_lookup(&tree, repo, &empty_tree));

	return rugged_object_new(rb_repo, (git_object *) tree);
}

Instance Method Details

#[](e) ⇒ Object #get_entry(e) ⇒ Object

Return one of the entries from a tree as a Hash. If e is a number, the enth 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


160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'ext/rugged/rugged_tree.c', line 160

static VALUE rb_git_tree_get_entry(VALUE self, VALUE entry_id)
{
	git_tree *tree;
	TypedData_Get_Struct(self, git_tree, &rugged_object_type, 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");
}

#countObject #lengthObject

Return the number of entries contained in the tree.

Note that this only applies to entries in the root of the tree, not any other entries contained in sub-folders.



74
75
76
77
78
79
80
# File 'ext/rugged/rugged_tree.c', line 74

static VALUE rb_git_tree_entrycount(VALUE self)
{
	git_tree *tree;
	TypedData_Get_Struct(self, git_tree, &rugged_object_type, tree);

	return INT2FIX(git_tree_entrycount(tree));
}

#count_recursive(limit = nil) ⇒ Object

‘limit` - The maximum number of blobs to the count in the repository. Rugged will stop walking the tree after `limit` items to avoid long execution times.

Return the number of blobs (up to the limit) contained in the tree and all subtrees.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'ext/rugged/rugged_tree.c', line 113

static VALUE rb_git_tree_entrycount_recursive(int argc, VALUE* argv, VALUE self)
{
	git_tree *tree;
	int error;
	struct rugged_treecount_cb_payload payload;
	VALUE rb_limit;

	TypedData_Get_Struct(self, git_tree, &rugged_object_type, tree);

	rb_scan_args(argc, argv, "01", &rb_limit);

	payload.limit = -1;
	payload.count = 0;

	if (!NIL_P(rb_limit)) {
		Check_Type(rb_limit, T_FIXNUM);
		payload.limit = FIX2INT(rb_limit);
	}


	error = git_tree_walk(tree, GIT_TREEWALK_PRE, &rugged__treecount_cb, (void *)&payload);

	if (error && giterr_last()->klass == GITERR_CALLBACK) {
		giterr_clear();
		error = 0;
	}

	rugged_exception_check(error);

	return INT2FIX(payload.count);
}

#diff(other = nil, options = nil) ⇒ Object



163
164
165
# File 'lib/rugged/tree.rb', line 163

def diff(other = nil, options = nil)
  Tree.diff(repo, self, other, options)
end

#diff_workdir([options]) ⇒ Object

Returns a diff between a tree and the current workdir.

The tree object will be used as the “old file” side of the diff, while the content of the current workdir will be used for the “new file” side.

See Rugged::Tree#diff for a list of options that can be passed.



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'ext/rugged/rugged_tree.c', line 428

static VALUE rb_git_tree_diff_workdir(int argc, VALUE *argv, VALUE self)
{
	git_tree *tree;
	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
	git_repository *repo;
	git_diff *diff;
	VALUE owner, rb_options;
	int error;

	rb_scan_args(argc, argv, "00:", &rb_options);
	rugged_parse_diff_options(&opts, rb_options);

	TypedData_Get_Struct(self, git_tree, &rugged_object_type, tree);
	owner = rugged_owner(self);
	Data_Get_Struct(owner, git_repository, repo);

	error = git_diff_tree_to_workdir(&diff, repo, tree, &opts);

	xfree(opts.pathspec.strings);
	rugged_exception_check(error);

	return rugged_diff_new(rb_cRuggedDiff, owner, diff);
}

#each {|entry| ... } ⇒ Object #eachObject

Call block with each of the entries of the subtree as a Hash. If no block is given, an enumerator 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}
...

Overloads:

  • #each {|entry| ... } ⇒ Object

    Yields:

    • (entry)


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'ext/rugged/rugged_tree.c', line 221

static VALUE rb_git_tree_each(VALUE self)
{
	git_tree *tree;
	size_t i, count;

	RETURN_ENUMERATOR(self, 0, 0);
	TypedData_Get_Struct(self, git_tree, &rugged_object_type, tree);

	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;
}

#each_blobObject

Iterate over the blobs in this tree



186
187
188
189
# File 'lib/rugged/tree.rb', line 186

def each_blob
  return to_enum(__method__) unless block_given?
  self.each { |e| yield e if e[:type] == :blob }
end

#each_treeObject

Iterate over the subtrees in this tree



192
193
194
195
# File 'lib/rugged/tree.rb', line 192

def each_tree
  return to_enum(__method__) unless block_given?
  self.each { |e| yield e if e[:type] == :tree }
end

#[](e) ⇒ Object #get_entry(e) ⇒ Object

Return one of the entries from a tree as a Hash. If e is a number, the enth 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


160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'ext/rugged/rugged_tree.c', line 160

static VALUE rb_git_tree_get_entry(VALUE self, VALUE entry_id)
{
	git_tree *tree;
	TypedData_Get_Struct(self, git_tree, &rugged_object_type, 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");
}

#get_entry_by_oid(rb_oid) ⇒ Object

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}


190
191
192
193
194
195
196
197
198
199
200
# File 'ext/rugged/rugged_tree.c', line 190

static VALUE rb_git_tree_get_entry_by_oid(VALUE self, VALUE rb_oid)
{
	git_tree *tree;
	git_oid oid;
	TypedData_Get_Struct(self, git_tree, &rugged_object_type, 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));
}

#inspectObject



167
168
169
170
171
# File 'lib/rugged/tree.rb', line 167

def inspect
  data = "#<Rugged::Tree:#{object_id} {oid: #{oid}}>\n"
  self.each { |e| data << "  <\"#{e[:name]}\" #{e[:oid]}>\n" }
  data
end

#countObject #lengthObject

Return the number of entries contained in the tree.

Note that this only applies to entries in the root of the tree, not any other entries contained in sub-folders.



74
75
76
77
78
79
80
# File 'ext/rugged/rugged_tree.c', line 74

static VALUE rb_git_tree_entrycount(VALUE self)
{
	git_tree *tree;
	TypedData_Get_Struct(self, git_tree, &rugged_object_type, tree);

	return INT2FIX(git_tree_entrycount(tree));
}

#merge(*args) ⇒ Object

tree.merge(other_tree[, ancestor_tree[, options]]) -> Rugged::Index tree.merge(other_tree[, options]) -> Rugged::Index

Merges two trees and returns the a Rugged::Index object that reflects the result of the merge.

The following options can be passed in the options Hash:

:renames

If true, looking for renames will be enabled (‘–find-renames`), set to false to disable (default true).

:rename_threshold

An integer specifying the minimum similarity of a file to be seen as an eligible rename source (default 50).

:target_limit

An integer specifying the maximum byte size of a file before a it will be treated as binary. The default value is 512MB.

:favor

Specifies how and if conflicts are auto-resolved by favoring a specific file output. Can be one of ‘:normal`, `:ours`, `:theirs` or `:union`.



535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'ext/rugged/rugged_tree.c', line 535

static VALUE rb_git_tree_merge(int argc, VALUE *argv, VALUE self)
{
	VALUE rb_other_tree, rb_ancestor_tree, rb_options;
	VALUE rb_repo = rugged_owner(self);

	git_tree *tree, *other_tree, *ancestor_tree;
	git_repository *repo;
	git_index *index;
	git_merge_options opts = GIT_MERGE_OPTIONS_INIT;
	int error;

	if (rb_scan_args(argc, argv, "12", &rb_other_tree, &rb_ancestor_tree, &rb_options) == 2) {
		if (TYPE(rb_ancestor_tree) == T_HASH) {
			rb_options = rb_ancestor_tree;
			rb_ancestor_tree = Qnil;
		}
	}

	if (!NIL_P(rb_options)) {
		Check_Type(rb_options, T_HASH);
		rugged_parse_merge_options(&opts, rb_options);
	}

	if (!rb_obj_is_kind_of(rb_other_tree, rb_cRuggedTree))
		rb_raise(rb_eTypeError, "Expecting a Rugged::Tree instance");
	else if (!NIL_P(rb_ancestor_tree) && !rb_obj_is_kind_of(rb_ancestor_tree, rb_cRuggedTree))
		rb_raise(rb_eTypeError, "Expecting a Rugged::Tree instance");

	TypedData_Get_Struct(self, git_tree, &rugged_object_type, tree);
	Data_Get_Struct(rb_repo, git_repository, repo);
	TypedData_Get_Struct(rb_other_tree, git_tree, &rugged_object_type, other_tree);

	if (!NIL_P(rb_ancestor_tree))
		TypedData_Get_Struct(rb_ancestor_tree, git_tree, &rugged_object_type, ancestor_tree);
	else
		ancestor_tree = NULL;

	error = git_merge_trees(&index, repo, ancestor_tree, tree, other_tree, &opts);
	if (error == GIT_EMERGECONFLICT)
		return Qnil;

	rugged_exception_check(error);

	return rugged_index_new(rb_cRuggedIndex, rb_repo, index);
}

#path(path) ⇒ Object

Retrieve and return a tree entry by its relative path.



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'ext/rugged/rugged_tree.c', line 324

static VALUE rb_git_tree_path(VALUE self, VALUE rb_path)
{
	int error;
	git_tree *tree;
	git_tree_entry *entry;
	VALUE rb_entry;
	TypedData_Get_Struct(self, git_tree, &rugged_object_type, 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;
}

#update(updates) ⇒ Object

Create a new Rugged::Tree based on the curent one by applying the changes described in updates.

The updates are given as a list of Hash containing:

:action

:upsert or :remove to add/insert an entry, or to remove it resp.

:oid

The oid of the entry. This is ignored for removals.

:filemode

The octal filemode for the entry. This is ignored for remvals.

:path

The path of the entry. This may contain slashes and the intermediate trees will be created.



690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
# File 'ext/rugged/rugged_tree.c', line 690

static VALUE rb_git_tree_update(VALUE self, VALUE rb_updates)
{
	git_repository *repo;
	git_tree *tree = NULL;
	git_tree_update *updates;
	int nupdates, error;
	git_oid id;

	TypedData_Get_Struct(self, git_tree, &rugged_object_type, tree);
	repo = git_tree_owner(tree);

	parse_tree_updates(&updates, &nupdates, rb_updates);

	error = git_tree_create_updated(&id, repo, tree, nupdates, updates);
	xfree(updates);

	rugged_exception_check(error);

	return rugged_create_oid(&id);
}

#walk(mode) {|root, entry| ... } ⇒ Object #walk(mode) ⇒ Enumerator

Walk tree with the given mode (either :preorder or :postorder) and yield to block every entry in tree and all its subtrees, as a Hash. The block also takes a root, the relative path in the traversal, starting from the root of the original tree.

If the block returns a falsy value, that entry and its sub-entries (in the case of a folder) will be skipped for the iteration.

If no block is given, an Enumerator is returned instead.

tree.walk(:postorder) { |root, entry| puts "#{root}#{entry[:name]} [#{entry[:oid]}]" }

generates:

USAGE.rb [02bae86c91f96b5fdb6b1cf06f5aa3612139e318]
ext [23f135b3c576b6ac4785821888991d7089f35db1]
ext/rugged [25c88faa9302e34e16664eb9c990deb2bcf77849]
ext/rugged/extconf.rb [40c1aa8a8cec8ca444ed5758e3f00ecff093070a]
...

Overloads:

  • #walk(mode) {|root, entry| ... } ⇒ Object

    Yields:

  • #walk(mode) ⇒ Enumerator

    Returns:

    • (Enumerator)


286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'ext/rugged/rugged_tree.c', line 286

static VALUE rb_git_tree_walk(VALUE self, VALUE rb_mode)
{
	git_tree *tree;
	int error, mode = 0, exception = 0;
	ID id_mode;

	TypedData_Get_Struct(self, git_tree, &rugged_object_type, tree);

	if (!rb_block_given_p())
		return rb_funcall(self, rb_intern("to_enum"), 2, CSTR2SYM("walk"), rb_mode);

	Check_Type(rb_mode, T_SYMBOL);
	id_mode = SYM2ID(rb_mode);

	if (id_mode == rb_intern("preorder"))
		mode = GIT_TREEWALK_PRE;
	else if (id_mode == rb_intern("postorder"))
		mode = GIT_TREEWALK_POST;
	else
		rb_raise(rb_eTypeError,
				"Invalid iteration mode. Expected `:preorder` or `:postorder`");

	error = git_tree_walk(tree, mode, &rugged__treewalk_cb, (void *)&exception);

	if (exception)
		rb_jump_tag(exception);

	rugged_exception_check(error);

	return Qnil;
}

#walk_blobs(mode = :postorder) ⇒ Object

Walks the tree but only yields blobs



174
175
176
177
# File 'lib/rugged/tree.rb', line 174

def walk_blobs(mode=:postorder)
  return to_enum(__method__) unless block_given?
  self.walk(mode) { |root, e| yield root, e if e[:type] == :blob }
end

#walk_trees(mode = :postorder) ⇒ Object

Walks the tree but only yields subtrees



180
181
182
183
# File 'lib/rugged/tree.rb', line 180

def walk_trees(mode=:postorder)
  return to_enum(__method__) unless block_given?
  self.walk(mode) { |root, e| yield root, e if e[:type] == :tree }
end