Class: Rugged::Index

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
ext/rugged/rugged_index.c,
lib/rugged/index.rb,
ext/rugged/rugged_index.c

Overview

Index Entries

Index entries are represented as Hash instances with the following key/value pairs:

path:

The entry’s path in the index.

oid:

The oid of the entry’s git object (blob / tree).

dev:

The device for the index entry.

ino:

The inode for the index entry.

mode:

The current permissions of the index entry.

gid:

Group ID of the index entry’s owner.

uid:

User ID of the index entry’s owner.

file_size:

The index entry’s size, in bytes.

valid:

true if the index entry is valid, false otherwise.

stage:

The current stage of the index entry.

mtime:

A Time instance representing the index entry’s time of last modification.

mtime:

A Time instance representing the index entry’s time of last status change (ie. change of owner, group, mode, etc.).

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new([path]) ⇒ Object

Create a bare index object based on the index file at path.

Any index methods that rely on the ODB or a working directory (e.g. #add) will raise a Rugged::IndexError.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'ext/rugged/rugged_index.c', line 61

static VALUE rb_git_index_new(int argc, VALUE *argv, VALUE klass)
{
	git_index *index;
	int error;

	VALUE rb_path;
	const char *path = NULL;

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

Instance Method Details

#<<(entry) ⇒ nil #<<(path) ⇒ nil #add(entry) ⇒ nil #add(path) ⇒ nil #update(entry) ⇒ nil #update(path) ⇒ nil

Add a new entry to the index or update an existing entry in the index.

If passed a path to an existing, readable file relative to the workdir, creates a new index entry based on this file.

Alternatively, a new index entry can be created by passing a Hash containing all key/value pairs of an index entry.

Any gitignore rules that might match path (or the :path value of the entry hash) are ignored.

If the index entry at path (or :path) currently contains a merge conflict, it will no longer be marked as conflicting and the data about the conflict will be moved into the “resolve undo” (REUC) section of the index.

Overloads:

  • #<<(entry) ⇒ nil

    Returns:

    • (nil)
  • #<<(path) ⇒ nil

    Returns:

    • (nil)
  • #add(entry) ⇒ nil

    Returns:

    • (nil)
  • #add(path) ⇒ nil

    Returns:

    • (nil)
  • #update(entry) ⇒ nil

    Returns:

    • (nil)
  • #update(path) ⇒ nil

    Returns:

    • (nil)


307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'ext/rugged/rugged_index.c', line 307

static VALUE rb_git_index_add(VALUE self, VALUE rb_entry)
{
	git_index *index;
	int error = 0;

	Data_Get_Struct(self, git_index, index);

	if (TYPE(rb_entry) == T_HASH) {
		git_index_entry entry;

		rb_git_indexentry_toC(&entry, rb_entry);
		error = git_index_add(index, &entry);
	}

	else if (TYPE(rb_entry) == T_STRING) {
		error = git_index_add_bypath(index, StringValueCStr(rb_entry));
	}

	else {
		rb_raise(rb_eTypeError,
			"Expecting a hash defining an Index Entry or a path to a file in the repository");
	}

	rugged_exception_check(error);
	return Qnil;
}

#[](path[, stage = 0)) ⇒ nil #[](position) ⇒ nil #get(path[, stage = 0]) ⇒ nil #get(position) ⇒ nil

Return a specific entry in the index.

The first two forms returns entries based on their path in the index and an optional stage, while the last two forms return entries based on their position in the index.

Overloads:

  • #[](path[, stage = 0)) ⇒ nil

    Returns:

    • (nil)
  • #[](position) ⇒ nil

    Returns:

    • (nil)
  • #get(path[, stage = 0]) ⇒ nil

    Returns:

    • (nil)
  • #get(position) ⇒ nil

    Returns:

    • (nil)


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'ext/rugged/rugged_index.c', line 159

static VALUE rb_git_index_get(int argc, VALUE *argv, VALUE self)
{
	git_index *index;
	const git_index_entry *entry = NULL;

	VALUE rb_entry, rb_stage;

	Data_Get_Struct(self, git_index, index);

	rb_scan_args(argc, argv, "11", &rb_entry, &rb_stage);

	if (TYPE(rb_entry) == T_STRING) {
		int stage = 0;

		if (!NIL_P(rb_stage)) {
			Check_Type(rb_stage, T_FIXNUM);
			stage = FIX2INT(rb_stage);
		}

		entry = git_index_get_bypath(index, StringValueCStr(rb_entry), stage);
	}

	else if (TYPE(rb_entry) == T_FIXNUM) {
		if (argc > 1) {
			rb_raise(rb_eArgError,
				"Too many arguments when trying to lookup entry by index");
		}

		entry = git_index_get_byindex(index, FIX2INT(rb_entry));
	} else {
		rb_raise(rb_eArgError,
			"Invalid type for `entry`: expected String or Fixnum");
	}

	return entry ? rb_git_indexentry_fromC(entry) : Qnil;
}

#<<(entry) ⇒ nil #<<(path) ⇒ nil #add(entry) ⇒ nil #add(path) ⇒ nil #update(entry) ⇒ nil #update(path) ⇒ nil

Add a new entry to the index or update an existing entry in the index.

If passed a path to an existing, readable file relative to the workdir, creates a new index entry based on this file.

Alternatively, a new index entry can be created by passing a Hash containing all key/value pairs of an index entry.

Any gitignore rules that might match path (or the :path value of the entry hash) are ignored.

If the index entry at path (or :path) currently contains a merge conflict, it will no longer be marked as conflicting and the data about the conflict will be moved into the “resolve undo” (REUC) section of the index.

Overloads:

  • #<<(entry) ⇒ nil

    Returns:

    • (nil)
  • #<<(path) ⇒ nil

    Returns:

    • (nil)
  • #add(entry) ⇒ nil

    Returns:

    • (nil)
  • #add(path) ⇒ nil

    Returns:

    • (nil)
  • #update(entry) ⇒ nil

    Returns:

    • (nil)
  • #update(path) ⇒ nil

    Returns:

    • (nil)


307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'ext/rugged/rugged_index.c', line 307

static VALUE rb_git_index_add(VALUE self, VALUE rb_entry)
{
	git_index *index;
	int error = 0;

	Data_Get_Struct(self, git_index, index);

	if (TYPE(rb_entry) == T_HASH) {
		git_index_entry entry;

		rb_git_indexentry_toC(&entry, rb_entry);
		error = git_index_add(index, &entry);
	}

	else if (TYPE(rb_entry) == T_STRING) {
		error = git_index_add_bypath(index, StringValueCStr(rb_entry));
	}

	else {
		rb_raise(rb_eTypeError,
			"Expecting a hash defining an Index Entry or a path to a file in the repository");
	}

	rugged_exception_check(error);
	return Qnil;
}

#add_all(pathspec = [][, options]) ⇒ nil #add_all(pathspec = [][, options]) {|path, pathspec| ... } ⇒ nil

Add or update index entries matching files in the working directory.

Searches the working directory for files that pathspec and adds them to index (by updating an existing entry or adding a new entry).

pathspec can either be a String, or an Array of Strings. If pathspec is empty, all entries in the index will be matched.

Files that are ignored due to .gitignore rules will be skipped, unless they’re already have an entry in index.

Files that are marked as the result of a merge request, will have this marking removed and the merge conflict information will be moved into the “resolve undo” (REUC) section of index.

If a block is given, each matched path and the pathspec that matched it will be passed to the block. If the return value of block is falsy, the matching item will not be added to the index.

This method will fail in bare index instances.

The following options can be passed in the options Hash:

:force

If true, any .gitignore rules will be ignored.

:disable_pathspec_match

If true, glob expansion will be disabled and exact matching will be forced.

:check_pathspec

If true, and the :force options is false or not given, exact matches of ignored files or files that are not already in index will raise a Rugged::InvalidError. This emulates git add -A.

Overloads:

  • #add_all(pathspec = [][, options]) ⇒ nil

    Returns:

    • (nil)
  • #add_all(pathspec = [][, options]) {|path, pathspec| ... } ⇒ nil

    Yields:

    • (path, pathspec)

    Returns:

    • (nil)


389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'ext/rugged/rugged_index.c', line 389

static VALUE rb_git_index_add_all(int argc, VALUE *argv, VALUE self)
{
	VALUE rb_pathspecs, rb_options;

	git_index *index;
	git_strarray pathspecs;
	int error, exception = 0;
	unsigned int flags = GIT_INDEX_ADD_DEFAULT;

	Data_Get_Struct(self, git_index, index);

	if (rb_scan_args(argc, argv, "02", &rb_pathspecs, &rb_options) > 1) {
		Check_Type(rb_options, T_HASH);

		if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("force"))))
			flags |= GIT_INDEX_ADD_FORCE;

		if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("disable_pathspec_match"))))
			flags |= GIT_INDEX_ADD_DISABLE_PATHSPEC_MATCH;

		if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("check_pathspec"))))
			flags |= GIT_INDEX_ADD_CHECK_PATHSPEC;
	}

	rugged_rb_ary_to_strarray(rb_pathspecs, &pathspecs);

	error = git_index_add_all(index, &pathspecs, flags,
		rb_block_given_p() ? rugged__index_matched_path_cb : NULL, &exception);

	xfree(pathspecs.strings);

	if (exception)
		rb_jump_tag(exception);

	rugged_exception_check(error);
	return Qnil;
}

#clearnil

Clear the contents (remove all entries) of the index object. Changes are in-memory only and can be saved by calling #write.

Returns:

  • (nil)


87
88
89
90
91
92
93
# File 'ext/rugged/rugged_index.c', line 87

static VALUE rb_git_index_clear(VALUE self)
{
	git_index *index;
	Data_Get_Struct(self, git_index, index);
	git_index_clear(index);
	return Qnil;
}

#conflict_add(conflict) ⇒ nil

Add or update index entries that represent a conflict.

conflict has to be a hash containing :ancestor, :ours and :theirs key/value pairs. Any of those paris can be nil (or left out) to indicate that the file was not present in the respective tree during the merge.

Returns:

  • (nil)


861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
# File 'ext/rugged/rugged_index.c', line 861

static VALUE rb_git_conflict_add(VALUE self, VALUE rb_conflict)
{
	VALUE rb_ancestor, rb_ours, rb_theirs;
	git_index *index;
	git_index_entry ancestor, ours, theirs;
	int error;

	Check_Type(rb_conflict, T_HASH);

	rb_ancestor = rb_hash_aref(rb_conflict, CSTR2SYM("ancestor"));
	rb_ours     = rb_hash_aref(rb_conflict, CSTR2SYM("ours"));
	rb_theirs   = rb_hash_aref(rb_conflict, CSTR2SYM("theirs"));

	if (!NIL_P(rb_ancestor))
		rb_git_indexentry_toC(&ancestor, rb_ancestor);

	if (!NIL_P(rb_ours))
		rb_git_indexentry_toC(&ours, rb_ours);

	if (!NIL_P(rb_theirs))
		rb_git_indexentry_toC(&theirs, rb_theirs);

	Data_Get_Struct(self, git_index, index);

	error = git_index_conflict_add(index,
		NIL_P(rb_ancestor) ? NULL : &ancestor,
		NIL_P(rb_theirs) ? NULL : &ours,
		NIL_P(rb_ours) ? NULL : &theirs);
	rugged_exception_check(error);

	return Qnil;
}

#conflict_cleanupnil

Remove all conflicting entries (entries with a stage greater than 0) from the index.

Returns:

  • (nil)


1111
1112
1113
1114
1115
1116
1117
1118
1119
# File 'ext/rugged/rugged_index.c', line 1111

static VALUE rb_git_conflict_cleanup(VALUE self)
{
	git_index *index;

	Data_Get_Struct(self, git_index, index);
	git_index_conflict_cleanup(index);

	return Qnil;
}

#conflict_get(path) ⇒ nil

Return index entries from the ancestor, our side and their side of the conflict at path.

If :ancestor, :ours or :theirs is nil, that indicates that path did not exist in the respective tree.

Returns nil if no conflict is present at path.

Returns:

  • (nil)


927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
# File 'ext/rugged/rugged_index.c', line 927

static VALUE rb_git_conflict_get(VALUE self, VALUE rb_path)
{
	VALUE rb_result = rb_hash_new();
	git_index *index;
	const git_index_entry *ancestor, *ours, *theirs;
	int error;

	Check_Type(rb_path, T_STRING);

	Data_Get_Struct(self, git_index, index);

	error = git_index_conflict_get(&ancestor, &ours, &theirs, index, StringValueCStr(rb_path));
	if (error == GIT_ENOTFOUND)
		return Qnil;
	else
		rugged_exception_check(error);

	rb_hash_aset(rb_result, CSTR2SYM("ancestor"), rb_git_indexentry_fromC(ancestor));
	rb_hash_aset(rb_result, CSTR2SYM("ours"),     rb_git_indexentry_fromC(ours));
	rb_hash_aset(rb_result, CSTR2SYM("theirs"),   rb_git_indexentry_fromC(theirs));

	return rb_result;
}

#conflict_remove(path) ⇒ nil

Removes the index entries that represent the conflict at path.

Returns:

  • (nil)


900
901
902
903
904
905
906
907
908
909
910
911
912
913
# File 'ext/rugged/rugged_index.c', line 900

static VALUE rb_git_conflict_remove(VALUE self, VALUE rb_path)
{
	git_index *index;
	int error;

	Check_Type(rb_path, T_STRING);

	Data_Get_Struct(self, git_index, index);

	error = git_index_conflict_remove(index, StringValueCStr(rb_path));
	rugged_exception_check(error);

	return Qnil;
}

#conflictsObject

Return all conflicts in index.

Each conflict is represented as a Hash with :ancestor, :ours or :theirs key-value pairs, each containing index entry data.

If the value of the :ancestor, :ours or :theirs key is nil, that indicates that file in conflict did not exists in the respective tree.



1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
# File 'ext/rugged/rugged_index.c', line 1133

static VALUE rb_git_index_conflicts(VALUE self)
{
	VALUE rb_conflicts = rb_ary_new();
	git_index *index;
	git_index_conflict_iterator *iter;
	const git_index_entry *ancestor, *ours, *theirs;
	int error;

	Data_Get_Struct(self, git_index, index);

	error = git_index_conflict_iterator_new(&iter, index);
	rugged_exception_check(error);

	while ((error = git_index_conflict_next(&ancestor, &ours, &theirs, iter)) == GIT_OK) {
		VALUE rb_conflict = rb_hash_new();

		rb_hash_aset(rb_conflict, CSTR2SYM("ancestor"), rb_git_indexentry_fromC(ancestor));
		rb_hash_aset(rb_conflict, CSTR2SYM("ours"),     rb_git_indexentry_fromC(ours));
		rb_hash_aset(rb_conflict, CSTR2SYM("theirs"),   rb_git_indexentry_fromC(theirs));

		rb_ary_push(rb_conflicts, rb_conflict);
	}

	git_index_conflict_iterator_free(iter);

	if (error != GIT_ITEROVER)
		rugged_exception_check(error);

	return rb_conflicts;
}

#conflicts?Boolean

Determines if the index contains entries representing conflicts.

Returns:

  • (Boolean)


843
844
845
846
847
848
# File 'ext/rugged/rugged_index.c', line 843

static VALUE rb_git_index_conflicts_p(VALUE self)
{
	git_index *index;
	Data_Get_Struct(self, git_index, index);
	return git_index_has_conflicts(index) ? Qtrue : Qfalse;
}

#countInteger

Returns the number of entries currently in the index.

Returns:

  • (Integer)


140
141
142
143
144
145
# File 'ext/rugged/rugged_index.c', line 140

static VALUE rb_git_index_count(VALUE self)
{
	git_index *index;
	Data_Get_Struct(self, git_index, index);
	return INT2FIX(git_index_entrycount(index));
}

#diff([options]) ⇒ Object #diff(diffable[, options]) ⇒ Object

The first form returns a diff between the index and the current working directory.

The second form returns a diff between the index and the given diffable object. diffable can either be a Rugged::Commit or a Rugged::Tree.

The index will be used as the “old file” side of the diff, while the working directory or the diffable will be used for the “new file” side.

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.

: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 di ff, 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.

:include_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.



790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
# File 'ext/rugged/rugged_index.c', line 790

static VALUE rb_git_index_diff(int argc, VALUE *argv, VALUE self)
{
	git_index *index;
	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
	git_repository *repo;
	git_diff *diff = NULL;
	VALUE owner, rb_other, rb_options;
	int error;

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

	Data_Get_Struct(self, git_index, index);
	owner = rugged_owner(self);
	Data_Get_Struct(owner, git_repository, repo);

	if (NIL_P(rb_other)) {
		error = git_diff_index_to_workdir(&diff, repo, index, &opts);
	} else {
		// Need to flip the reverse option, so that the index is by default
		// the "old file" side of the diff.
		opts.flags ^= GIT_DIFF_REVERSE;

		if (rb_obj_is_kind_of(rb_other, rb_cRuggedCommit)) {
			git_tree *other_tree;
			git_commit *commit;
			Data_Get_Struct(rb_other, git_commit, commit);
			error = git_commit_tree(&other_tree, commit);

			if (!error)
				error = git_diff_tree_to_index(&diff, repo, other_tree, index, &opts);
		} else if (rb_obj_is_kind_of(rb_other, rb_cRuggedTree)) {
			git_tree *other_tree;
			Data_Get_Struct(rb_other, git_tree, other_tree);
			error = git_diff_tree_to_index(&diff, repo, other_tree, index, &opts);
		} else {
			xfree(opts.pathspec.strings);
			rb_raise(rb_eTypeError, "A Rugged::Commit or Rugged::Tree instance is required");
		}
	}

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

	return rugged_diff_new(rb_cRuggedDiff, self, diff);
}

#each {|entry| ... } ⇒ nil #eachEnumerator

Passes each entry of the index to the given block.

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

Overloads:

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

    Yields:

    • (entry)

    Returns:

    • (nil)
  • #eachEnumerator

    Returns:

    • (Enumerator)


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'ext/rugged/rugged_index.c', line 205

static VALUE rb_git_index_each(VALUE self)
{
	git_index *index;
	unsigned int i, count;

	Data_Get_Struct(self, git_index, index);

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

	count = (unsigned int)git_index_entrycount(index);
	for (i = 0; i < count; ++i) {
		const git_index_entry *entry = git_index_get_byindex(index, i);
		if (entry)
			rb_yield(rb_git_indexentry_fromC(entry));
	}

	return Qnil;
}

#[](path[, stage = 0)) ⇒ nil #[](position) ⇒ nil #get(path[, stage = 0]) ⇒ nil #get(position) ⇒ nil

Return a specific entry in the index.

The first two forms returns entries based on their path in the index and an optional stage, while the last two forms return entries based on their position in the index.

Overloads:

  • #[](path[, stage = 0)) ⇒ nil

    Returns:

    • (nil)
  • #[](position) ⇒ nil

    Returns:

    • (nil)
  • #get(path[, stage = 0]) ⇒ nil

    Returns:

    • (nil)
  • #get(position) ⇒ nil

    Returns:

    • (nil)


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'ext/rugged/rugged_index.c', line 159

static VALUE rb_git_index_get(int argc, VALUE *argv, VALUE self)
{
	git_index *index;
	const git_index_entry *entry = NULL;

	VALUE rb_entry, rb_stage;

	Data_Get_Struct(self, git_index, index);

	rb_scan_args(argc, argv, "11", &rb_entry, &rb_stage);

	if (TYPE(rb_entry) == T_STRING) {
		int stage = 0;

		if (!NIL_P(rb_stage)) {
			Check_Type(rb_stage, T_FIXNUM);
			stage = FIX2INT(rb_stage);
		}

		entry = git_index_get_bypath(index, StringValueCStr(rb_entry), stage);
	}

	else if (TYPE(rb_entry) == T_FIXNUM) {
		if (argc > 1) {
			rb_raise(rb_eArgError,
				"Too many arguments when trying to lookup entry by index");
		}

		entry = git_index_get_byindex(index, FIX2INT(rb_entry));
	} else {
		rb_raise(rb_eArgError,
			"Invalid type for `entry`: expected String or Fixnum");
	}

	return entry ? rb_git_indexentry_fromC(entry) : Qnil;
}

#merge_file(path[, options]) ⇒ nil #merge_file(path) ⇒ nil

Return merge_file (in memory) from the ancestor, our side and their side of the conflict at path.

If :ancestor, :ours or :theirs is nil, that indicates that path did not exist in the respective tree.

Returns nil if no conflict is present at path.

The following options can be passed in the options Hash:

:ancestor_label

The name of the ancestor branch used to decorate conflict markers.

:our_label

The name of our branch used to decorate conflict markers.

:their_label

The name of their branch used to decorate conflict markers.

:favor

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

:style

Specifies the type of merge file to produce. Can be one of ‘:standard`, `:diff3`. Defaults to `:standard`

:simplify

If true, the merge file is simplified by condensing non-alphanumeric regions.

Overloads:

  • #merge_file(path[, options]) ⇒ nil

    Returns:

    • (nil)
  • #merge_file(path) ⇒ nil

    Returns:

    • (nil)


1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
# File 'ext/rugged/rugged_index.c', line 1058

static VALUE rb_git_merge_file(int argc, VALUE *argv, VALUE self)
{
	VALUE rb_path, rb_options;
	VALUE rb_result = rb_hash_new();
	VALUE rb_repo = rugged_owner(self);

	git_repository *repo;
	git_index *index;
	const git_index_entry *ancestor, *ours, *theirs;
	git_merge_file_result merge_file_result = {0};
	git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT;
	int error;

	rb_scan_args(argc, argv, "1:", &rb_path, &rb_options);

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

	Check_Type(rb_path, T_STRING);

	Data_Get_Struct(self, git_index, index);

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

	error = git_index_conflict_get(&ancestor, &ours, &theirs, index, StringValueCStr(rb_path));
	if (error == GIT_ENOTFOUND)
		return Qnil;
	else
		rugged_exception_check(error);

	error = git_merge_file_from_index(&merge_file_result, repo, ancestor, ours, theirs, &opts);
	rugged_exception_check(error);

	rb_hash_aset(rb_result, CSTR2SYM("automergeable"), merge_file_result.automergeable ? Qtrue : Qfalse);
	rb_hash_aset(rb_result, CSTR2SYM("path"),         rb_path);
	rb_hash_aset(rb_result, CSTR2SYM("filemode"),     INT2FIX(merge_file_result.mode));
	rb_hash_aset(rb_result, CSTR2SYM("data"),         rb_str_new(merge_file_result.ptr, merge_file_result.len));

	git_merge_file_result_free(&merge_file_result);

	return rb_result;
}

#read_tree(tree) ⇒ Object

Clear the current index and start the index again on top of tree

Further index operations (add, update, remove, etc) will be considered changes on top of tree.



675
676
677
678
679
680
681
682
683
684
685
686
687
688
# File 'ext/rugged/rugged_index.c', line 675

static VALUE rb_git_index_readtree(VALUE self, VALUE rb_tree)
{
	git_index *index;
	git_tree *tree;
	int error;

	Data_Get_Struct(self, git_index, index);
	Data_Get_Struct(rb_tree, git_tree, tree);

	error = git_index_read_tree(index, tree);
	rugged_exception_check(error);

	return Qnil;
}

#reloadnil

Reloads the index contents from the disk, discarding any changes that have not been saved through #write.

Returns:

  • (nil)


102
103
104
105
106
107
108
109
110
111
112
113
# File 'ext/rugged/rugged_index.c', line 102

static VALUE rb_git_index_read(VALUE self)
{
	git_index *index;
	int error;

	Data_Get_Struct(self, git_index, index);

	error = git_index_read(index, 0);
	rugged_exception_check(error);

	return Qnil;
}

#remove(path[, stage = 0]) ⇒ nil

Removes the entry at the given path with the given stage from the index.

Returns:

  • (nil)


232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'ext/rugged/rugged_index.c', line 232

static VALUE rb_git_index_remove(int argc, VALUE *argv, VALUE self)
{
	git_index *index;
	int error, stage = 0;

	VALUE rb_entry, rb_stage;

	Data_Get_Struct(self, git_index, index);

	if (rb_scan_args(argc, argv, "11", &rb_entry, &rb_stage) > 1) {
		Check_Type(rb_stage, T_FIXNUM);
		stage = FIX2INT(rb_stage);
	}

	Check_Type(rb_entry, T_STRING);

	error = git_index_remove(index, StringValueCStr(rb_entry), stage);
	rugged_exception_check(error);

	return Qnil;
}

#remove_all(pathspec = []) ⇒ nil #remove_all(pathspec = []) {|path, pathspec| ... } ⇒ nil

Remove all matching index entries.

Searches index for entries that match pathspec and removes them from the index.

pathspec can either be a String, or an Array of Strings. If pathspec is empty, all entries in the index will be matched.

If a block is given, each matched path and the pathspec that matched it will be passed to the block. If the return value of block is falsy, the matching item will not be removed from the index.

Overloads:

  • #remove_all(pathspec = []) ⇒ nil

    Returns:

    • (nil)
  • #remove_all(pathspec = []) {|path, pathspec| ... } ⇒ nil

    Yields:

    • (path, pathspec)

    Returns:

    • (nil)


494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'ext/rugged/rugged_index.c', line 494

static VALUE rb_git_index_remove_all(int argc, VALUE *argv, VALUE self)
{
	VALUE rb_pathspecs = rb_ary_new();

	git_index *index;
	git_strarray pathspecs;
	int error, exception = 0;

	Data_Get_Struct(self, git_index, index);

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

	if (NIL_P(rb_pathspecs))
		rb_pathspecs = rb_ary_new();

	rugged_rb_ary_to_strarray(rb_ary_to_ary(rb_pathspecs), &pathspecs);

	error = git_index_remove_all(index, &pathspecs,
		rb_block_given_p() ? rugged__index_matched_path_cb : NULL, &exception);

	xfree(pathspecs.strings);

	if (exception)
		rb_jump_tag(exception);
	rugged_exception_check(error);

	return Qnil;
}

#remove_dir(dir[, stage = 0]) ⇒ nil

Removes all entries under the given dir with the given stage from the index.

Returns:

  • (nil)


261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'ext/rugged/rugged_index.c', line 261

static VALUE rb_git_index_remove_directory(int argc, VALUE *argv, VALUE self)
{
	git_index *index;
	int error, stage = 0;

	VALUE rb_dir, rb_stage;

	Data_Get_Struct(self, git_index, index);

	if (rb_scan_args(argc, argv, "11", &rb_dir, &rb_stage) > 1) {
		Check_Type(rb_stage, T_FIXNUM);
		stage = FIX2INT(rb_stage);
	}

	Check_Type(rb_dir, T_STRING);

	error = git_index_remove_directory(index, StringValueCStr(rb_dir), stage);
	rugged_exception_check(error);

	return Qnil;
}

#to_sObject



5
6
7
8
9
10
11
# File 'lib/rugged/index.rb', line 5

def to_s
  s = "#<Rugged::Index\n"
  self.each do |entry|
    s << "  [#{entry[:stage]}] '#{entry[:path]}'\n"
  end
  s + '>'
end

#<<(entry) ⇒ nil #<<(path) ⇒ nil #add(entry) ⇒ nil #add(path) ⇒ nil #update(entry) ⇒ nil #update(path) ⇒ nil

Add a new entry to the index or update an existing entry in the index.

If passed a path to an existing, readable file relative to the workdir, creates a new index entry based on this file.

Alternatively, a new index entry can be created by passing a Hash containing all key/value pairs of an index entry.

Any gitignore rules that might match path (or the :path value of the entry hash) are ignored.

If the index entry at path (or :path) currently contains a merge conflict, it will no longer be marked as conflicting and the data about the conflict will be moved into the “resolve undo” (REUC) section of the index.

Overloads:

  • #<<(entry) ⇒ nil

    Returns:

    • (nil)
  • #<<(path) ⇒ nil

    Returns:

    • (nil)
  • #add(entry) ⇒ nil

    Returns:

    • (nil)
  • #add(path) ⇒ nil

    Returns:

    • (nil)
  • #update(entry) ⇒ nil

    Returns:

    • (nil)
  • #update(path) ⇒ nil

    Returns:

    • (nil)


307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'ext/rugged/rugged_index.c', line 307

static VALUE rb_git_index_add(VALUE self, VALUE rb_entry)
{
	git_index *index;
	int error = 0;

	Data_Get_Struct(self, git_index, index);

	if (TYPE(rb_entry) == T_HASH) {
		git_index_entry entry;

		rb_git_indexentry_toC(&entry, rb_entry);
		error = git_index_add(index, &entry);
	}

	else if (TYPE(rb_entry) == T_STRING) {
		error = git_index_add_bypath(index, StringValueCStr(rb_entry));
	}

	else {
		rb_raise(rb_eTypeError,
			"Expecting a hash defining an Index Entry or a path to a file in the repository");
	}

	rugged_exception_check(error);
	return Qnil;
}

#update_all(pathspec = []) ⇒ nil #update_all(pathspec = []) {|path, pathspec| ... } ⇒ nil

Update all index entries to match the working directory.

Searches index for entries that match pathspec and synchronizes them with the content of the working directory.

pathspec can either be a String, or an Array of Strings. If pathspec is empty, all entries in the index will be matched.

Entries where the corresponding working directory file no longer exists get deleted, all other matched entries will get updated to reflect their working directory state (the latest version of the a file’s content will automatically be added to the ODB).

If a block is given, each matched path and the pathspec that matched it will be passed to the block. If the return value of block is falsy, the matching item will not be updated in the index.

This method will fail in bare index instances.

Overloads:

  • #update_all(pathspec = []) ⇒ nil

    Returns:

    • (nil)
  • #update_all(pathspec = []) {|path, pathspec| ... } ⇒ nil

    Yields:

    • (path, pathspec)

    Returns:

    • (nil)


451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'ext/rugged/rugged_index.c', line 451

static VALUE rb_git_index_update_all(int argc, VALUE *argv, VALUE self)
{
	VALUE rb_pathspecs = rb_ary_new();

	git_index *index;
	git_strarray pathspecs;
	int error, exception = 0;

	Data_Get_Struct(self, git_index, index);

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

	rugged_rb_ary_to_strarray(rb_pathspecs, &pathspecs);

	error = git_index_update_all(index, &pathspecs,
		rb_block_given_p() ? rugged__index_matched_path_cb : NULL, &exception);

	xfree(pathspecs.strings);

	if (exception)
		rb_jump_tag(exception);
	rugged_exception_check(error);

	return Qnil;
}

#writenil

Writes the index object from memory back to the disk, persisting all changes.

Returns:

  • (nil)


121
122
123
124
125
126
127
128
129
130
131
132
# File 'ext/rugged/rugged_index.c', line 121

static VALUE rb_git_index_write(VALUE self)
{
	git_index *index;
	int error;

	Data_Get_Struct(self, git_index, index);

	error = git_index_write(index);
	rugged_exception_check(error);

	return Qnil;
}

#write_tree([repo]) ⇒ Object

Write the index to a tree, either in the index’s repository, or in the given repo.

If the index contains any files in conflict, writing the tree will fail.

Returns the OID string of the written tree object.



643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'ext/rugged/rugged_index.c', line 643

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

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