rdavila-rugged 0.24.0b13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +619 -0
  4. data/ext/rugged/extconf.rb +105 -0
  5. data/ext/rugged/rugged.c +527 -0
  6. data/ext/rugged/rugged.h +185 -0
  7. data/ext/rugged/rugged_backend.c +34 -0
  8. data/ext/rugged/rugged_blame.c +292 -0
  9. data/ext/rugged/rugged_blob.c +638 -0
  10. data/ext/rugged/rugged_branch.c +195 -0
  11. data/ext/rugged/rugged_branch_collection.c +408 -0
  12. data/ext/rugged/rugged_commit.c +691 -0
  13. data/ext/rugged/rugged_config.c +404 -0
  14. data/ext/rugged/rugged_cred.c +148 -0
  15. data/ext/rugged/rugged_diff.c +686 -0
  16. data/ext/rugged/rugged_diff_delta.c +105 -0
  17. data/ext/rugged/rugged_diff_hunk.c +103 -0
  18. data/ext/rugged/rugged_diff_line.c +83 -0
  19. data/ext/rugged/rugged_index.c +1255 -0
  20. data/ext/rugged/rugged_note.c +376 -0
  21. data/ext/rugged/rugged_object.c +383 -0
  22. data/ext/rugged/rugged_patch.c +245 -0
  23. data/ext/rugged/rugged_reference.c +396 -0
  24. data/ext/rugged/rugged_reference_collection.c +446 -0
  25. data/ext/rugged/rugged_remote.c +691 -0
  26. data/ext/rugged/rugged_remote_collection.c +457 -0
  27. data/ext/rugged/rugged_repo.c +2669 -0
  28. data/ext/rugged/rugged_revwalk.c +495 -0
  29. data/ext/rugged/rugged_settings.c +155 -0
  30. data/ext/rugged/rugged_signature.c +106 -0
  31. data/ext/rugged/rugged_submodule.c +852 -0
  32. data/ext/rugged/rugged_submodule_collection.c +384 -0
  33. data/ext/rugged/rugged_tag.c +251 -0
  34. data/ext/rugged/rugged_tag_collection.c +347 -0
  35. data/ext/rugged/rugged_tree.c +919 -0
  36. data/lib/rugged.rb +23 -0
  37. data/lib/rugged/attributes.rb +41 -0
  38. data/lib/rugged/blob.rb +28 -0
  39. data/lib/rugged/branch.rb +19 -0
  40. data/lib/rugged/commit.rb +54 -0
  41. data/lib/rugged/console.rb +9 -0
  42. data/lib/rugged/credentials.rb +43 -0
  43. data/lib/rugged/diff.rb +20 -0
  44. data/lib/rugged/diff/delta.rb +53 -0
  45. data/lib/rugged/diff/hunk.rb +18 -0
  46. data/lib/rugged/diff/line.rb +47 -0
  47. data/lib/rugged/index.rb +13 -0
  48. data/lib/rugged/object.rb +7 -0
  49. data/lib/rugged/patch.rb +36 -0
  50. data/lib/rugged/reference.rb +7 -0
  51. data/lib/rugged/remote.rb +4 -0
  52. data/lib/rugged/repository.rb +227 -0
  53. data/lib/rugged/submodule_collection.rb +48 -0
  54. data/lib/rugged/tag.rb +50 -0
  55. data/lib/rugged/tree.rb +38 -0
  56. data/lib/rugged/version.rb +3 -0
  57. data/lib/rugged/walker.rb +5 -0
  58. metadata +146 -0
@@ -0,0 +1,105 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2014 GitHub, Inc
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ * THE SOFTWARE.
23
+ */
24
+
25
+ #include "rugged.h"
26
+
27
+ extern VALUE rb_cRuggedDiff;
28
+ VALUE rb_cRuggedDiffDelta;
29
+
30
+ VALUE rb_git_delta_file_fromC(const git_diff_file *file)
31
+ {
32
+ VALUE rb_file;
33
+
34
+ if (!file)
35
+ return Qnil;
36
+
37
+ rb_file = rb_hash_new();
38
+
39
+ rb_hash_aset(rb_file, CSTR2SYM("oid"), rugged_create_oid(&file->id));
40
+ rb_hash_aset(rb_file, CSTR2SYM("path"), file->path ? rb_str_new2(file->path) : Qnil);
41
+ rb_hash_aset(rb_file, CSTR2SYM("size"), INT2FIX(file->size));
42
+ rb_hash_aset(rb_file, CSTR2SYM("flags"), UINT2NUM(file->flags));
43
+ rb_hash_aset(rb_file, CSTR2SYM("mode"), UINT2NUM(file->mode));
44
+
45
+ return rb_file;
46
+ }
47
+
48
+ static VALUE rb_git_delta_status_fromC(git_delta_t status)
49
+ {
50
+ switch(status) {
51
+ case GIT_DELTA_UNMODIFIED:
52
+ return CSTR2SYM("unmodified");
53
+ case GIT_DELTA_ADDED:
54
+ return CSTR2SYM("added");
55
+ case GIT_DELTA_DELETED:
56
+ return CSTR2SYM("deleted");
57
+ case GIT_DELTA_MODIFIED:
58
+ return CSTR2SYM("modified");
59
+ case GIT_DELTA_RENAMED:
60
+ return CSTR2SYM("renamed");
61
+ case GIT_DELTA_COPIED:
62
+ return CSTR2SYM("copied");
63
+ case GIT_DELTA_IGNORED:
64
+ return CSTR2SYM("ignored");
65
+ case GIT_DELTA_UNTRACKED:
66
+ return CSTR2SYM("untracked");
67
+ case GIT_DELTA_TYPECHANGE:
68
+ return CSTR2SYM("typechange");
69
+ default:
70
+ return CSTR2SYM("unknown");
71
+ }
72
+ }
73
+
74
+ static VALUE rb_git_delta_status_char_fromC(git_delta_t status)
75
+ {
76
+ char status_char[2];
77
+
78
+ status_char[0] = git_diff_status_char(status);
79
+ status_char[1] = '\0';
80
+
81
+ return CSTR2SYM(status_char);
82
+ }
83
+
84
+ VALUE rugged_diff_delta_new(VALUE owner, const git_diff_delta *delta)
85
+ {
86
+ VALUE rb_delta = rb_class_new_instance(0, NULL, rb_cRuggedDiffDelta);
87
+
88
+ rugged_set_owner(rb_delta, owner);
89
+ rb_iv_set(rb_delta, "@old_file", rb_git_delta_file_fromC(&delta->old_file));
90
+ rb_iv_set(rb_delta, "@new_file", rb_git_delta_file_fromC(&delta->new_file));
91
+ rb_iv_set(rb_delta, "@similarity", INT2FIX(delta->similarity));
92
+ rb_iv_set(rb_delta, "@status", rb_git_delta_status_fromC(delta->status));
93
+ rb_iv_set(rb_delta, "@status_char", rb_git_delta_status_char_fromC(delta->status));
94
+ rb_iv_set(rb_delta, "@binary",
95
+ (!(delta->flags & GIT_DIFF_FLAG_NOT_BINARY) &&
96
+ (delta->flags & GIT_DIFF_FLAG_BINARY)) ? Qtrue : Qfalse
97
+ );
98
+
99
+ return rb_delta;
100
+ }
101
+
102
+ void Init_rugged_diff_delta(void)
103
+ {
104
+ rb_cRuggedDiffDelta = rb_define_class_under(rb_cRuggedDiff, "Delta", rb_cObject);
105
+ }
@@ -0,0 +1,103 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2014 GitHub, Inc
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ * THE SOFTWARE.
23
+ */
24
+
25
+ #include "rugged.h"
26
+
27
+ extern VALUE rb_cRuggedDiff;
28
+ VALUE rb_cRuggedDiffHunk;
29
+
30
+
31
+ VALUE rugged_diff_hunk_new(VALUE owner, size_t hunk_idx, const git_diff_hunk *hunk, size_t lines_in_hunk)
32
+ {
33
+ VALUE rb_hunk = rb_class_new_instance(0, NULL, rb_cRuggedDiffHunk);
34
+ rugged_set_owner(rb_hunk, owner);
35
+
36
+ rb_iv_set(rb_hunk, "@header", rb_str_new(hunk->header, hunk->header_len));
37
+ rb_iv_set(rb_hunk, "@line_count", INT2FIX(lines_in_hunk));
38
+ rb_iv_set(rb_hunk, "@hunk_index", INT2FIX(hunk_idx));
39
+
40
+ rb_iv_set(rb_hunk, "@old_start", INT2FIX(hunk->old_start));
41
+ rb_iv_set(rb_hunk, "@old_lines", INT2FIX(hunk->old_lines));
42
+ rb_iv_set(rb_hunk, "@new_start", INT2FIX(hunk->new_start));
43
+ rb_iv_set(rb_hunk, "@new_lines", INT2FIX(hunk->new_lines));
44
+
45
+ return rb_hunk;
46
+ }
47
+
48
+ /*
49
+ * call-seq:
50
+ * hunk.each_line { |line| } -> self
51
+ * hunk.each_line -> Enumerator
52
+ *
53
+ * If given a block, yields each line that is part of the current hunk.
54
+ *
55
+ * If no block is given, an enumerator is returned instead.
56
+ */
57
+ static VALUE rb_git_diff_hunk_each_line(VALUE self)
58
+ {
59
+ git_patch *patch;
60
+ int error = 0, l, lines_count, hunk_idx;
61
+
62
+ if (!rb_block_given_p()) {
63
+ return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_line"), self);
64
+ }
65
+
66
+ Data_Get_Struct(rugged_owner(self), git_patch, patch);
67
+
68
+ lines_count = FIX2INT(rb_iv_get(self, "@line_count"));
69
+ hunk_idx = FIX2INT(rb_iv_get(self, "@hunk_index"));
70
+
71
+ for (l = 0; l < lines_count; ++l) {
72
+ const git_diff_line *line;
73
+ error = git_patch_get_line_in_hunk(&line, patch, hunk_idx, l);
74
+ if (error) break;
75
+
76
+ rb_yield(rugged_diff_line_new(line));
77
+ }
78
+ rugged_exception_check(error);
79
+
80
+ return self;
81
+ }
82
+
83
+ void Init_rugged_diff_hunk(void)
84
+ {
85
+ rb_cRuggedDiffHunk = rb_define_class_under(rb_cRuggedDiff, "Hunk", rb_cObject);
86
+
87
+ rb_include_module(rb_cRuggedDiffHunk, rb_mEnumerable);
88
+
89
+ rb_define_method(rb_cRuggedDiffHunk, "each", rb_git_diff_hunk_each_line, 0);
90
+ rb_define_method(rb_cRuggedDiffHunk, "each_line", rb_git_diff_hunk_each_line, 0);
91
+
92
+ rb_define_attr(rb_cRuggedDiffHunk, "header", 1, 0);
93
+ rb_define_attr(rb_cRuggedDiffHunk, "line_count", 1, 0);
94
+ rb_define_attr(rb_cRuggedDiffHunk, "hunk_index", 1, 0);
95
+
96
+ rb_define_attr(rb_cRuggedDiffHunk, "old_start", 1, 0);
97
+ rb_define_attr(rb_cRuggedDiffHunk, "old_lines", 1, 0);
98
+ rb_define_attr(rb_cRuggedDiffHunk, "new_start", 1, 0);
99
+ rb_define_attr(rb_cRuggedDiffHunk, "new_lines", 1, 0);
100
+
101
+ rb_define_alias(rb_cRuggedDiffHunk, "count", "line_count");
102
+ rb_define_alias(rb_cRuggedDiffHunk, "size", "line_count");
103
+ }
@@ -0,0 +1,83 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2014 GitHub, Inc
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ * THE SOFTWARE.
23
+ */
24
+
25
+ #include "rugged.h"
26
+
27
+ extern VALUE rb_cRuggedDiff;
28
+ VALUE rb_cRuggedDiffLine;
29
+
30
+ VALUE rugged_diff_line_new(const git_diff_line *line)
31
+ {
32
+ VALUE rb_line = rb_class_new_instance(0, NULL, rb_cRuggedDiffLine), rb_line_origin;
33
+
34
+ switch(line->origin) {
35
+ case GIT_DIFF_LINE_CONTEXT:
36
+ rb_line_origin = CSTR2SYM("context");
37
+ break;
38
+ case GIT_DIFF_LINE_ADDITION:
39
+ rb_line_origin = CSTR2SYM("addition");
40
+ break;
41
+ case GIT_DIFF_LINE_DELETION:
42
+ rb_line_origin = CSTR2SYM("deletion");
43
+ break;
44
+ case GIT_DIFF_LINE_CONTEXT_EOFNL: /* neither file has newline at the end */
45
+ rb_line_origin = CSTR2SYM("eof_no_newline");
46
+ break;
47
+ case GIT_DIFF_LINE_ADD_EOFNL: /* added at end of old file */
48
+ rb_line_origin = CSTR2SYM("eof_newline_added");
49
+ break;
50
+ case GIT_DIFF_LINE_DEL_EOFNL: /* removed at end of old file */
51
+ rb_line_origin = CSTR2SYM("eof_newline_removed");
52
+ break;
53
+ case GIT_DIFF_LINE_FILE_HDR:
54
+ rb_line_origin = CSTR2SYM("file_header");
55
+ break;
56
+ case GIT_DIFF_LINE_HUNK_HDR:
57
+ rb_line_origin = CSTR2SYM("hunk_header");
58
+ break;
59
+ case GIT_DIFF_LINE_BINARY:
60
+ rb_line_origin = CSTR2SYM("binary");
61
+ break;
62
+ default:
63
+ /* FIXME: raise here instead? */
64
+ rb_line_origin = CSTR2SYM("unknown");
65
+ }
66
+
67
+ rb_iv_set(rb_line, "@line_origin", rb_line_origin);
68
+ rb_iv_set(rb_line, "@content", rb_str_new(line->content, line->content_len));
69
+ rb_iv_set(rb_line, "@old_lineno", INT2FIX(line->old_lineno));
70
+ rb_iv_set(rb_line, "@new_lineno", INT2FIX(line->new_lineno));
71
+
72
+ if (line->content_offset == -1)
73
+ rb_iv_set(rb_line, "@content_offset", Qnil);
74
+ else
75
+ rb_iv_set(rb_line, "@content_offset", INT2FIX(line->content_offset));
76
+
77
+ return rb_line;
78
+ }
79
+
80
+ void Init_rugged_diff_line(void)
81
+ {
82
+ rb_cRuggedDiffLine = rb_define_class_under(rb_cRuggedDiff, "Line", rb_cObject);
83
+ }
@@ -0,0 +1,1255 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2014 GitHub, Inc
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ * THE SOFTWARE.
23
+ */
24
+
25
+ #include "rugged.h"
26
+
27
+ VALUE rb_cRuggedIndex;
28
+ extern VALUE rb_mRugged;
29
+ extern VALUE rb_cRuggedCommit;
30
+ extern VALUE rb_cRuggedDiff;
31
+ extern VALUE rb_cRuggedTree;
32
+
33
+ static void rb_git_indexentry_toC(git_index_entry *entry, VALUE rb_entry);
34
+ static VALUE rb_git_indexentry_fromC(const git_index_entry *entry);
35
+
36
+ /*
37
+ * Index
38
+ */
39
+
40
+ static void rb_git_index__free(git_index *index)
41
+ {
42
+ git_index_free(index);
43
+ }
44
+
45
+ VALUE rugged_index_new(VALUE klass, VALUE owner, git_index *index)
46
+ {
47
+ VALUE rb_index = Data_Wrap_Struct(klass, NULL, &rb_git_index__free, index);
48
+ rugged_set_owner(rb_index, owner);
49
+ return rb_index;
50
+ }
51
+
52
+ /*
53
+ * call-seq:
54
+ * Index.new([path])
55
+ *
56
+ * Create a bare index object based on the index file at +path+.
57
+ *
58
+ * Any index methods that rely on the ODB or a working directory (e.g. #add)
59
+ * will raise a Rugged::IndexError.
60
+ */
61
+ static VALUE rb_git_index_new(int argc, VALUE *argv, VALUE klass)
62
+ {
63
+ git_index *index;
64
+ int error;
65
+
66
+ VALUE rb_path;
67
+ const char *path = NULL;
68
+
69
+ if (rb_scan_args(argc, argv, "01", &rb_path) == 1) {
70
+ Check_Type(rb_path, T_STRING);
71
+ path = StringValueCStr(rb_path);
72
+ }
73
+
74
+ error = git_index_open(&index, path);
75
+ rugged_exception_check(error);
76
+
77
+ return rugged_index_new(klass, Qnil, index);
78
+ }
79
+
80
+ /*
81
+ * call-seq:
82
+ * index.clear -> nil
83
+ *
84
+ * Clear the contents (remove all entries) of the index object. Changes are in-memory only
85
+ * and can be saved by calling #write.
86
+ */
87
+ static VALUE rb_git_index_clear(VALUE self)
88
+ {
89
+ git_index *index;
90
+ Data_Get_Struct(self, git_index, index);
91
+ git_index_clear(index);
92
+ return Qnil;
93
+ }
94
+
95
+ /*
96
+ * call-seq:
97
+ * index.reload -> nil
98
+ *
99
+ * Reloads the index contents from the disk, discarding any changes that
100
+ * have not been saved through #write.
101
+ */
102
+ static VALUE rb_git_index_read(VALUE self)
103
+ {
104
+ git_index *index;
105
+ int error;
106
+
107
+ Data_Get_Struct(self, git_index, index);
108
+
109
+ error = git_index_read(index, 0);
110
+ rugged_exception_check(error);
111
+
112
+ return Qnil;
113
+ }
114
+
115
+ /*
116
+ * call-seq:
117
+ * index.write -> nil
118
+ *
119
+ * Writes the index object from memory back to the disk, persisting all changes.
120
+ */
121
+ static VALUE rb_git_index_write(VALUE self)
122
+ {
123
+ git_index *index;
124
+ int error;
125
+
126
+ Data_Get_Struct(self, git_index, index);
127
+
128
+ error = git_index_write(index);
129
+ rugged_exception_check(error);
130
+
131
+ return Qnil;
132
+ }
133
+
134
+ /*
135
+ * call-seq:
136
+ * index.count -> int
137
+ *
138
+ * Returns the number of entries currently in the index.
139
+ */
140
+ static VALUE rb_git_index_count(VALUE self)
141
+ {
142
+ git_index *index;
143
+ Data_Get_Struct(self, git_index, index);
144
+ return INT2FIX(git_index_entrycount(index));
145
+ }
146
+
147
+ /*
148
+ * call-seq:
149
+ * index[path[, stage = 0]] -> entry or nil
150
+ * index[position] -> entry or nil
151
+ * index.get(path[, stage = 0]) -> entry or nil
152
+ * index.get(position) -> entry or nil
153
+ *
154
+ * Return a specific entry in the index.
155
+ *
156
+ * The first two forms returns entries based on their +path+ in the index and an optional +stage+,
157
+ * while the last two forms return entries based on their position in the index.
158
+ */
159
+ static VALUE rb_git_index_get(int argc, VALUE *argv, VALUE self)
160
+ {
161
+ git_index *index;
162
+ const git_index_entry *entry = NULL;
163
+
164
+ VALUE rb_entry, rb_stage;
165
+
166
+ Data_Get_Struct(self, git_index, index);
167
+
168
+ rb_scan_args(argc, argv, "11", &rb_entry, &rb_stage);
169
+
170
+ if (TYPE(rb_entry) == T_STRING) {
171
+ int stage = 0;
172
+
173
+ if (!NIL_P(rb_stage)) {
174
+ Check_Type(rb_stage, T_FIXNUM);
175
+ stage = FIX2INT(rb_stage);
176
+ }
177
+
178
+ entry = git_index_get_bypath(index, StringValueCStr(rb_entry), stage);
179
+ }
180
+
181
+ else if (TYPE(rb_entry) == T_FIXNUM) {
182
+ if (argc > 1) {
183
+ rb_raise(rb_eArgError,
184
+ "Too many arguments when trying to lookup entry by index");
185
+ }
186
+
187
+ entry = git_index_get_byindex(index, FIX2INT(rb_entry));
188
+ } else {
189
+ rb_raise(rb_eArgError,
190
+ "Invalid type for `entry`: expected String or Fixnum");
191
+ }
192
+
193
+ return entry ? rb_git_indexentry_fromC(entry) : Qnil;
194
+ }
195
+
196
+ /*
197
+ * call-seq:
198
+ * index.each { |entry| } -> nil
199
+ * index.each -> Enumerator
200
+ *
201
+ * Passes each entry of the index to the given block.
202
+ *
203
+ * If no block is given, an enumerator is returned instead.
204
+ */
205
+ static VALUE rb_git_index_each(VALUE self)
206
+ {
207
+ git_index *index;
208
+ unsigned int i, count;
209
+
210
+ Data_Get_Struct(self, git_index, index);
211
+
212
+ if (!rb_block_given_p())
213
+ return rb_funcall(self, rb_intern("to_enum"), 0);
214
+
215
+ count = (unsigned int)git_index_entrycount(index);
216
+ for (i = 0; i < count; ++i) {
217
+ const git_index_entry *entry = git_index_get_byindex(index, i);
218
+ if (entry)
219
+ rb_yield(rb_git_indexentry_fromC(entry));
220
+ }
221
+
222
+ return Qnil;
223
+ }
224
+
225
+ /*
226
+ * call-seq:
227
+ * index.remove(path[, stage = 0]) -> nil
228
+ *
229
+ * Removes the entry at the given +path+ with the given +stage+
230
+ * from the index.
231
+ */
232
+ static VALUE rb_git_index_remove(int argc, VALUE *argv, VALUE self)
233
+ {
234
+ git_index *index;
235
+ int error, stage = 0;
236
+
237
+ VALUE rb_entry, rb_stage;
238
+
239
+ Data_Get_Struct(self, git_index, index);
240
+
241
+ if (rb_scan_args(argc, argv, "11", &rb_entry, &rb_stage) > 1) {
242
+ Check_Type(rb_stage, T_FIXNUM);
243
+ stage = FIX2INT(rb_stage);
244
+ }
245
+
246
+ Check_Type(rb_entry, T_STRING);
247
+
248
+ error = git_index_remove(index, StringValueCStr(rb_entry), stage);
249
+ rugged_exception_check(error);
250
+
251
+ return Qnil;
252
+ }
253
+
254
+ /*
255
+ * call-seq:
256
+ * index.remove_dir(dir[, stage = 0]) -> nil
257
+ *
258
+ * Removes all entries under the given +dir+ with the given +stage+
259
+ * from the index.
260
+ */
261
+ static VALUE rb_git_index_remove_directory(int argc, VALUE *argv, VALUE self)
262
+ {
263
+ git_index *index;
264
+ int error, stage = 0;
265
+
266
+ VALUE rb_dir, rb_stage;
267
+
268
+ Data_Get_Struct(self, git_index, index);
269
+
270
+ if (rb_scan_args(argc, argv, "11", &rb_dir, &rb_stage) > 1) {
271
+ Check_Type(rb_stage, T_FIXNUM);
272
+ stage = FIX2INT(rb_stage);
273
+ }
274
+
275
+ Check_Type(rb_dir, T_STRING);
276
+
277
+ error = git_index_remove_directory(index, StringValueCStr(rb_dir), stage);
278
+ rugged_exception_check(error);
279
+
280
+ return Qnil;
281
+ }
282
+
283
+ /*
284
+ * call-seq:
285
+ * index << entry -> nil
286
+ * index << path -> nil
287
+ * index.add(entry) -> nil
288
+ * index.add(path) -> nil
289
+ * index.update(entry) -> nil
290
+ * index.update(path) -> nil
291
+ *
292
+ * Add a new entry to the index or update an existing entry in the index.
293
+ *
294
+ * If passed a +path+ to an existing, readable file relative to the workdir,
295
+ * creates a new index entry based on this file.
296
+ *
297
+ * Alternatively, a new index entry can be created by passing a Hash containing
298
+ * all key/value pairs of an index entry.
299
+ *
300
+ * Any gitignore rules that might match +path+ (or the +:path+ value of the
301
+ * entry hash) are ignored.
302
+ *
303
+ * If the index entry at +path+ (or +:path+) currently contains a merge conflict,
304
+ * it will no longer be marked as conflicting and the data about the conflict
305
+ * will be moved into the "resolve undo" (REUC) section of the index.
306
+ */
307
+ static VALUE rb_git_index_add(VALUE self, VALUE rb_entry)
308
+ {
309
+ git_index *index;
310
+ int error = 0;
311
+
312
+ Data_Get_Struct(self, git_index, index);
313
+
314
+ if (TYPE(rb_entry) == T_HASH) {
315
+ git_index_entry entry;
316
+
317
+ rb_git_indexentry_toC(&entry, rb_entry);
318
+ error = git_index_add(index, &entry);
319
+ }
320
+
321
+ else if (TYPE(rb_entry) == T_STRING) {
322
+ error = git_index_add_bypath(index, StringValueCStr(rb_entry));
323
+ }
324
+
325
+ else {
326
+ rb_raise(rb_eTypeError,
327
+ "Expecting a hash defining an Index Entry or a path to a file in the repository");
328
+ }
329
+
330
+ rugged_exception_check(error);
331
+ return Qnil;
332
+ }
333
+
334
+ int rugged__index_matched_path_cb(const char *path, const char *matched_pathspec, void *payload)
335
+ {
336
+ int *exception = (int *)payload;
337
+
338
+ VALUE rb_result, rb_args = rb_ary_new2(2);
339
+ rb_ary_push(rb_args, rb_str_new2(path));
340
+ rb_ary_push(rb_args, matched_pathspec == NULL ? Qnil : rb_str_new2(matched_pathspec));
341
+
342
+ rb_result = rb_protect(rb_yield_splat, rb_args, exception);
343
+
344
+ if (*exception)
345
+ return GIT_ERROR;
346
+
347
+ return RTEST(rb_result) ? 0 : 1;
348
+ }
349
+
350
+ /*
351
+ * call-seq:
352
+ * index.add_all(pathspec = [][, options]) -> nil
353
+ * index.add_all(pathspec = [][, options]) { |path, pathspec| block } -> nil
354
+ *
355
+ * Add or update index entries matching files in the working directory.
356
+ *
357
+ * Searches the working directory for files that +pathspec+ and adds them
358
+ * to +index+ (by updating an existing entry or adding a new entry).
359
+ *
360
+ * +pathspec+ can either be a String, or an Array of Strings.
361
+ * If +pathspec+ is empty, all entries in the index will be matched.
362
+ *
363
+ * Files that are ignored due to +.gitignore+ rules will be skipped,
364
+ * unless they're already have an entry in +index+.
365
+ *
366
+ * Files that are marked as the result of a merge request, will have this
367
+ * marking removed and the merge conflict information will be moved into the
368
+ * "resolve undo" (REUC) section of +index+.
369
+ *
370
+ * If a block is given, each matched +path+ and the +pathspec+ that matched
371
+ * it will be passed to the block. If the return value of +block+ is
372
+ * falsy, the matching item will not be added to the index.
373
+ *
374
+ * This method will fail in bare index instances.
375
+ *
376
+ * The following options can be passed in the +options+ Hash:
377
+ *
378
+ * :force ::
379
+ * If +true+, any +.gitignore+ rules will be ignored.
380
+ *
381
+ * :disable_pathspec_match ::
382
+ * If +true+, glob expansion will be disabled and exact matching will be forced.
383
+ *
384
+ * :check_pathspec ::
385
+ * If +true+, and the +:force+ options is +false+ or not given, exact matches
386
+ * of ignored files or files that are not already in +index+ will raise a
387
+ * Rugged::InvalidError. This emulates <code>git add -A</code>.
388
+ */
389
+ static VALUE rb_git_index_add_all(int argc, VALUE *argv, VALUE self)
390
+ {
391
+ VALUE rb_pathspecs, rb_options;
392
+
393
+ git_index *index;
394
+ git_strarray pathspecs;
395
+ int error, exception = 0;
396
+ unsigned int flags = GIT_INDEX_ADD_DEFAULT;
397
+
398
+ Data_Get_Struct(self, git_index, index);
399
+
400
+ if (rb_scan_args(argc, argv, "02", &rb_pathspecs, &rb_options) > 1) {
401
+ Check_Type(rb_options, T_HASH);
402
+
403
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("force"))))
404
+ flags |= GIT_INDEX_ADD_FORCE;
405
+
406
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("disable_pathspec_match"))))
407
+ flags |= GIT_INDEX_ADD_DISABLE_PATHSPEC_MATCH;
408
+
409
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("check_pathspec"))))
410
+ flags |= GIT_INDEX_ADD_CHECK_PATHSPEC;
411
+ }
412
+
413
+ rugged_rb_ary_to_strarray(rb_pathspecs, &pathspecs);
414
+
415
+ error = git_index_add_all(index, &pathspecs, flags,
416
+ rb_block_given_p() ? rugged__index_matched_path_cb : NULL, &exception);
417
+
418
+ xfree(pathspecs.strings);
419
+
420
+ if (exception)
421
+ rb_jump_tag(exception);
422
+
423
+ rugged_exception_check(error);
424
+ return Qnil;
425
+ }
426
+
427
+ /*
428
+ * call-seq:
429
+ * index.update_all(pathspec = []) -> nil
430
+ * index.update_all(pathspec = []) { |path, pathspec| block } -> nil
431
+ *
432
+ * Update all index entries to match the working directory.
433
+ *
434
+ * Searches +index+ for entries that match +pathspec+ and synchronizes
435
+ * them with the content of the working directory.
436
+ *
437
+ * +pathspec+ can either be a String, or an Array of Strings.
438
+ * If +pathspec+ is empty, all entries in the index will be matched.
439
+ *
440
+ * Entries where the corresponding working directory file no longer exists
441
+ * get deleted, all other matched entries will get updated to reflect their
442
+ * working directory state (the latest version of the a file's content will
443
+ * automatically be added to the ODB).
444
+ *
445
+ * If a block is given, each matched +path+ and the +pathspec+ that matched
446
+ * it will be passed to the block. If the return value of +block+ is
447
+ * falsy, the matching item will not be updated in the index.
448
+ *
449
+ * This method will fail in bare index instances.
450
+ */
451
+ static VALUE rb_git_index_update_all(int argc, VALUE *argv, VALUE self)
452
+ {
453
+ VALUE rb_pathspecs = rb_ary_new();
454
+
455
+ git_index *index;
456
+ git_strarray pathspecs;
457
+ int error, exception = 0;
458
+
459
+ Data_Get_Struct(self, git_index, index);
460
+
461
+ rb_scan_args(argc, argv, "01", &rb_pathspecs);
462
+
463
+ rugged_rb_ary_to_strarray(rb_pathspecs, &pathspecs);
464
+
465
+ error = git_index_update_all(index, &pathspecs,
466
+ rb_block_given_p() ? rugged__index_matched_path_cb : NULL, &exception);
467
+
468
+ xfree(pathspecs.strings);
469
+
470
+ if (exception)
471
+ rb_jump_tag(exception);
472
+ rugged_exception_check(error);
473
+
474
+ return Qnil;
475
+ }
476
+
477
+ /*
478
+ * call-seq:
479
+ * index.remove_all(pathspec = []) -> nil
480
+ * index.remove_all(pathspec = []) { |path, pathspec| block } -> nil
481
+ *
482
+ * Remove all matching index entries.
483
+ *
484
+ * Searches +index+ for entries that match +pathspec+ and removes them
485
+ * from the index.
486
+ *
487
+ * +pathspec+ can either be a String, or an Array of Strings.
488
+ * If +pathspec+ is empty, all entries in the index will be matched.
489
+ *
490
+ * If a block is given, each matched +path+ and the +pathspec+ that matched
491
+ * it will be passed to the block. If the return value of +block+ is
492
+ * falsy, the matching item will not be removed from the index.
493
+ */
494
+ static VALUE rb_git_index_remove_all(int argc, VALUE *argv, VALUE self)
495
+ {
496
+ VALUE rb_pathspecs = rb_ary_new();
497
+
498
+ git_index *index;
499
+ git_strarray pathspecs;
500
+ int error, exception = 0;
501
+
502
+ Data_Get_Struct(self, git_index, index);
503
+
504
+ rb_scan_args(argc, argv, "01", &rb_pathspecs);
505
+
506
+ if (NIL_P(rb_pathspecs))
507
+ rb_pathspecs = rb_ary_new();
508
+
509
+ rugged_rb_ary_to_strarray(rb_ary_to_ary(rb_pathspecs), &pathspecs);
510
+
511
+ error = git_index_remove_all(index, &pathspecs,
512
+ rb_block_given_p() ? rugged__index_matched_path_cb : NULL, &exception);
513
+
514
+ xfree(pathspecs.strings);
515
+
516
+ if (exception)
517
+ rb_jump_tag(exception);
518
+ rugged_exception_check(error);
519
+
520
+ return Qnil;
521
+ }
522
+
523
+ static VALUE rb_git_indexentry_fromC(const git_index_entry *entry)
524
+ {
525
+ VALUE rb_entry, rb_mtime, rb_ctime;
526
+ unsigned int valid, stage;
527
+
528
+ if (!entry)
529
+ return Qnil;
530
+
531
+ rb_entry = rb_hash_new();
532
+
533
+ rb_hash_aset(rb_entry, CSTR2SYM("path"), rb_str_new_utf8(entry->path));
534
+ rb_hash_aset(rb_entry, CSTR2SYM("oid"), rugged_create_oid(&entry->id));
535
+
536
+ rb_hash_aset(rb_entry, CSTR2SYM("dev"), INT2FIX(entry->dev));
537
+ rb_hash_aset(rb_entry, CSTR2SYM("ino"), INT2FIX(entry->ino));
538
+ rb_hash_aset(rb_entry, CSTR2SYM("mode"), INT2FIX(entry->mode));
539
+ rb_hash_aset(rb_entry, CSTR2SYM("gid"), INT2FIX(entry->gid));
540
+ rb_hash_aset(rb_entry, CSTR2SYM("uid"), INT2FIX(entry->uid));
541
+ rb_hash_aset(rb_entry, CSTR2SYM("file_size"), INT2FIX(entry->file_size));
542
+
543
+ valid = (entry->flags & GIT_IDXENTRY_VALID);
544
+ rb_hash_aset(rb_entry, CSTR2SYM("valid"), valid ? Qtrue : Qfalse);
545
+
546
+ stage = (entry->flags & GIT_IDXENTRY_STAGEMASK) >> GIT_IDXENTRY_STAGESHIFT;
547
+ rb_hash_aset(rb_entry, CSTR2SYM("stage"), INT2FIX(stage));
548
+
549
+ rb_mtime = rb_time_new(entry->mtime.seconds, entry->mtime.nanoseconds / 1000);
550
+ rb_ctime = rb_time_new(entry->ctime.seconds, entry->ctime.nanoseconds / 1000);
551
+
552
+ rb_hash_aset(rb_entry, CSTR2SYM("ctime"), rb_ctime);
553
+ rb_hash_aset(rb_entry, CSTR2SYM("mtime"), rb_mtime);
554
+
555
+ return rb_entry;
556
+ }
557
+
558
+ static inline uint32_t
559
+ default_entry_value(VALUE rb_entry, const char *key)
560
+ {
561
+ VALUE val = rb_hash_aref(rb_entry, CSTR2SYM(key));
562
+ if (NIL_P(val))
563
+ return 0;
564
+
565
+ Check_Type(val, T_FIXNUM);
566
+ return FIX2INT(val);
567
+ }
568
+
569
+ static void rb_git_indexentry_toC(git_index_entry *entry, VALUE rb_entry)
570
+ {
571
+ VALUE val;
572
+
573
+ Check_Type(rb_entry, T_HASH);
574
+
575
+ val = rb_hash_aref(rb_entry, CSTR2SYM("path"));
576
+ Check_Type(val, T_STRING);
577
+ entry->path = StringValueCStr(val);
578
+
579
+ val = rb_hash_aref(rb_entry, CSTR2SYM("oid"));
580
+ Check_Type(val, T_STRING);
581
+ rugged_exception_check(
582
+ git_oid_fromstr(&entry->id, StringValueCStr(val))
583
+ );
584
+
585
+ entry->dev = default_entry_value(rb_entry, "dev");
586
+ entry->ino = default_entry_value(rb_entry, "ino");
587
+ entry->mode = default_entry_value(rb_entry, "mode");
588
+ entry->gid = default_entry_value(rb_entry, "gid");
589
+ entry->uid = default_entry_value(rb_entry, "uid");
590
+ entry->file_size = default_entry_value(rb_entry, "file_size");
591
+
592
+ if ((val = rb_hash_aref(rb_entry, CSTR2SYM("mtime"))) != Qnil) {
593
+ if (!rb_obj_is_kind_of(val, rb_cTime))
594
+ rb_raise(rb_eTypeError, ":mtime must be a Time instance");
595
+
596
+ entry->mtime.seconds = NUM2INT(rb_funcall(val, rb_intern("to_i"), 0));
597
+ entry->mtime.nanoseconds = NUM2INT(rb_funcall(val, rb_intern("usec"), 0)) * 1000;
598
+ } else {
599
+ entry->mtime.seconds = entry->mtime.nanoseconds = 0;
600
+ }
601
+
602
+ if ((val = rb_hash_aref(rb_entry, CSTR2SYM("ctime"))) != Qnil) {
603
+ if (!rb_obj_is_kind_of(val, rb_cTime))
604
+ rb_raise(rb_eTypeError, ":ctime must be a Time instance");
605
+
606
+ entry->ctime.seconds = NUM2INT(rb_funcall(val, rb_intern("to_i"), 0));
607
+ entry->ctime.nanoseconds = NUM2INT(rb_funcall(val, rb_intern("usec"), 0)) * 1000;
608
+ } else {
609
+ entry->ctime.seconds = entry->ctime.nanoseconds = 0;
610
+ }
611
+
612
+ entry->flags = 0x0;
613
+ entry->flags_extended = 0x0;
614
+
615
+ val = rb_hash_aref(rb_entry, CSTR2SYM("stage"));
616
+ if (!NIL_P(val)) {
617
+ unsigned int stage = NUM2INT(val);
618
+ entry->flags &= ~GIT_IDXENTRY_STAGEMASK;
619
+ entry->flags |= (stage << GIT_IDXENTRY_STAGESHIFT) & GIT_IDXENTRY_STAGEMASK;
620
+ }
621
+
622
+ val = rb_hash_aref(rb_entry, CSTR2SYM("valid"));
623
+ if (!NIL_P(val)) {
624
+ entry->flags &= ~GIT_IDXENTRY_VALID;
625
+ if (rugged_parse_bool(val))
626
+ entry->flags |= GIT_IDXENTRY_VALID;
627
+ } else {
628
+ entry->flags |= GIT_IDXENTRY_VALID;
629
+ }
630
+ }
631
+
632
+ /*
633
+ * call-seq:
634
+ * index.write_tree([repo]) -> oid
635
+ *
636
+ * Write the index to a tree, either in the index's repository, or in
637
+ * the given +repo+.
638
+ *
639
+ * If the index contains any files in conflict, writing the tree will fail.
640
+ *
641
+ * Returns the OID string of the written tree object.
642
+ */
643
+ static VALUE rb_git_index_writetree(int argc, VALUE *argv, VALUE self)
644
+ {
645
+ git_index *index;
646
+ git_oid tree_oid;
647
+ int error;
648
+ VALUE rb_repo;
649
+
650
+ Data_Get_Struct(self, git_index, index);
651
+
652
+ if (rb_scan_args(argc, argv, "01", &rb_repo) == 1) {
653
+ git_repository *repo = NULL;
654
+ rugged_check_repo(rb_repo);
655
+ Data_Get_Struct(rb_repo, git_repository, repo);
656
+ error = git_index_write_tree_to(&tree_oid, index, repo);
657
+ }
658
+ else {
659
+ error = git_index_write_tree(&tree_oid, index);
660
+ }
661
+
662
+ rugged_exception_check(error);
663
+ return rugged_create_oid(&tree_oid);
664
+ }
665
+
666
+ /*
667
+ * call-seq:
668
+ * index.read_tree(tree)
669
+ *
670
+ * Clear the current index and start the index again on top of +tree+
671
+ *
672
+ * Further index operations (+add+, +update+, +remove+, etc) will
673
+ * be considered changes on top of +tree+.
674
+ */
675
+ static VALUE rb_git_index_readtree(VALUE self, VALUE rb_tree)
676
+ {
677
+ git_index *index;
678
+ git_tree *tree;
679
+ int error;
680
+
681
+ Data_Get_Struct(self, git_index, index);
682
+ Data_Get_Struct(rb_tree, git_tree, tree);
683
+
684
+ if (!rb_obj_is_kind_of(rb_tree, rb_cRuggedTree)) {
685
+ rb_raise(rb_eTypeError, "A Rugged::Tree instance is required");
686
+ }
687
+
688
+ error = git_index_read_tree(index, tree);
689
+ rugged_exception_check(error);
690
+
691
+ return Qnil;
692
+ }
693
+
694
+ /*
695
+ * call-seq:
696
+ * index.diff([options]) -> diff
697
+ * index.diff(diffable[, options]) -> diff
698
+ *
699
+ * The first form returns a diff between the index and the current working
700
+ * directory.
701
+ *
702
+ * The second form returns a diff between the index and the given diffable object.
703
+ * +diffable+ can either be a +Rugged::Commit+ or a +Rugged::Tree+.
704
+ *
705
+ * The index will be used as the "old file" side of the diff, while the working
706
+ * directory or the +diffable+ will be used for the "new file" side.
707
+ *
708
+ * The following options can be passed in the +options+ Hash:
709
+ *
710
+ * :paths ::
711
+ * An array of paths / fnmatch patterns to constrain the diff to a specific
712
+ * set of files. Also see +:disable_pathspec_match+.
713
+ *
714
+ * :max_size ::
715
+ * An integer specifying the maximum byte size of a file before a it will
716
+ * be treated as binary. The default value is 512MB.
717
+ *
718
+ * :context_lines ::
719
+ * The number of unchanged lines that define the boundary of a hunk (and
720
+ * to display before and after the actual changes). The default is 3.
721
+ *
722
+ * :interhunk_lines ::
723
+ * The maximum number of unchanged lines between hunk boundaries before the hunks
724
+ * will be merged into a one. The default is 0.
725
+ *
726
+ * :reverse ::
727
+ * If true, the sides of the diff will be reversed.
728
+ *
729
+ * :force_text ::
730
+ * If true, all files will be treated as text, disabling binary attributes & detection.
731
+ *
732
+ * :ignore_whitespace ::
733
+ * If true, all whitespace will be ignored.
734
+ *
735
+ * :ignore_whitespace_change ::
736
+ * If true, changes in amount of whitespace will be ignored.
737
+ *
738
+ * :ignore_whitespace_eol ::
739
+ * If true, whitespace at end of line will be ignored.
740
+ *
741
+ * :ignore_submodules ::
742
+ * if true, submodules will be excluded from the diff completely.
743
+ *
744
+ * :patience ::
745
+ * If true, the "patience diff" algorithm will be used (currenlty unimplemented).
746
+ *
747
+ * :include_ignored ::
748
+ * If true, ignored files will be included in the diff.
749
+ *
750
+ * :include_untracked ::
751
+ * If true, untracked files will be included in the diff.
752
+ *
753
+ * :include_unmodified ::
754
+ * If true, unmodified files will be included in the diff.
755
+ *
756
+ * :recurse_untracked_dirs ::
757
+ * Even if +:include_untracked+ is true, untracked directories will only be
758
+ * marked with a single entry in the diff. If this flag is set to true,
759
+ * all files under ignored directories will be included in the diff, too.
760
+ *
761
+ * :disable_pathspec_match ::
762
+ * If true, the given +:paths+ will be applied as exact matches, instead of
763
+ * as fnmatch patterns.
764
+ *
765
+ * :deltas_are_icase ::
766
+ * If true, filename comparisons will be made with case-insensitivity.
767
+ *
768
+ * :include_untracked_content ::
769
+ * if true, untracked content will be contained in the the diff patch text.
770
+ *
771
+ * :skip_binary_check ::
772
+ * If true, diff deltas will be generated without spending time on binary
773
+ * detection. This is useful to improve performance in cases where the actual
774
+ * file content difference is not needed.
775
+ *
776
+ * :include_typechange ::
777
+ * If true, type changes for files will not be interpreted as deletion of
778
+ * the "old file" and addition of the "new file", but will generate
779
+ * typechange records.
780
+ *
781
+ * :include_typechange_trees ::
782
+ * Even if +:include_typechange+ is true, blob -> tree changes will still
783
+ * usually be handled as a deletion of the blob. If this flag is set to true,
784
+ * blob -> tree changes will be marked as typechanges.
785
+ *
786
+ * :ignore_filemode ::
787
+ * If true, file mode changes will be ignored.
788
+ *
789
+ * :recurse_ignored_dirs ::
790
+ * Even if +:include_ignored+ is true, ignored directories will only be
791
+ * marked with a single entry in the diff. If this flag is set to true,
792
+ * all files under ignored directories will be included in the diff, too.
793
+ */
794
+ static VALUE rb_git_index_diff(int argc, VALUE *argv, VALUE self)
795
+ {
796
+ git_index *index;
797
+ git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
798
+ git_repository *repo;
799
+ git_diff *diff = NULL;
800
+ VALUE owner, rb_other, rb_options;
801
+ int error;
802
+
803
+ rb_scan_args(argc, argv, "01:", &rb_other, &rb_options);
804
+ rugged_parse_diff_options(&opts, rb_options);
805
+
806
+ Data_Get_Struct(self, git_index, index);
807
+ owner = rugged_owner(self);
808
+ Data_Get_Struct(owner, git_repository, repo);
809
+
810
+ if (NIL_P(rb_other)) {
811
+ error = git_diff_index_to_workdir(&diff, repo, index, &opts);
812
+ } else {
813
+ // Need to flip the reverse option, so that the index is by default
814
+ // the "old file" side of the diff.
815
+ opts.flags ^= GIT_DIFF_REVERSE;
816
+
817
+ if (rb_obj_is_kind_of(rb_other, rb_cRuggedCommit)) {
818
+ git_tree *other_tree;
819
+ git_commit *commit;
820
+ Data_Get_Struct(rb_other, git_commit, commit);
821
+ error = git_commit_tree(&other_tree, commit);
822
+
823
+ if (!error)
824
+ error = git_diff_tree_to_index(&diff, repo, other_tree, index, &opts);
825
+ } else if (rb_obj_is_kind_of(rb_other, rb_cRuggedTree)) {
826
+ git_tree *other_tree;
827
+ Data_Get_Struct(rb_other, git_tree, other_tree);
828
+ error = git_diff_tree_to_index(&diff, repo, other_tree, index, &opts);
829
+ } else {
830
+ xfree(opts.pathspec.strings);
831
+ rb_raise(rb_eTypeError, "A Rugged::Commit or Rugged::Tree instance is required");
832
+ }
833
+ }
834
+
835
+ xfree(opts.pathspec.strings);
836
+ rugged_exception_check(error);
837
+
838
+ return rugged_diff_new(rb_cRuggedDiff, owner, diff);
839
+ }
840
+
841
+ /*
842
+ * call-seq:
843
+ * index.conflicts? -> true or false
844
+ *
845
+ * Determines if the index contains entries representing conflicts.
846
+ */
847
+ static VALUE rb_git_index_conflicts_p(VALUE self)
848
+ {
849
+ git_index *index;
850
+ Data_Get_Struct(self, git_index, index);
851
+ return git_index_has_conflicts(index) ? Qtrue : Qfalse;
852
+ }
853
+
854
+ /*
855
+ * call-seq:
856
+ * index.conflict_add(conflict) -> nil
857
+ *
858
+ * Add or update index entries that represent a conflict.
859
+ *
860
+ * +conflict+ has to be a hash containing +:ancestor+, +:ours+ and
861
+ * +:theirs+ key/value pairs. Any of those paris can be +nil+ (or left out)
862
+ * to indicate that the file was not present in the respective tree during
863
+ * the merge.
864
+ */
865
+ static VALUE rb_git_conflict_add(VALUE self, VALUE rb_conflict)
866
+ {
867
+ VALUE rb_ancestor, rb_ours, rb_theirs;
868
+ git_index *index;
869
+ git_index_entry ancestor, ours, theirs;
870
+ int error;
871
+
872
+ Check_Type(rb_conflict, T_HASH);
873
+
874
+ rb_ancestor = rb_hash_aref(rb_conflict, CSTR2SYM("ancestor"));
875
+ rb_ours = rb_hash_aref(rb_conflict, CSTR2SYM("ours"));
876
+ rb_theirs = rb_hash_aref(rb_conflict, CSTR2SYM("theirs"));
877
+
878
+ if (!NIL_P(rb_ancestor))
879
+ rb_git_indexentry_toC(&ancestor, rb_ancestor);
880
+
881
+ if (!NIL_P(rb_ours))
882
+ rb_git_indexentry_toC(&ours, rb_ours);
883
+
884
+ if (!NIL_P(rb_theirs))
885
+ rb_git_indexentry_toC(&theirs, rb_theirs);
886
+
887
+ Data_Get_Struct(self, git_index, index);
888
+
889
+ error = git_index_conflict_add(index,
890
+ NIL_P(rb_ancestor) ? NULL : &ancestor,
891
+ NIL_P(rb_theirs) ? NULL : &ours,
892
+ NIL_P(rb_ours) ? NULL : &theirs);
893
+ rugged_exception_check(error);
894
+
895
+ return Qnil;
896
+ }
897
+
898
+ /*
899
+ * call-seq:
900
+ * index.conflict_remove(path) -> nil
901
+ *
902
+ * Removes the index entries that represent the conflict at +path+.
903
+ */
904
+ static VALUE rb_git_conflict_remove(VALUE self, VALUE rb_path)
905
+ {
906
+ git_index *index;
907
+ int error;
908
+
909
+ Check_Type(rb_path, T_STRING);
910
+
911
+ Data_Get_Struct(self, git_index, index);
912
+
913
+ error = git_index_conflict_remove(index, StringValueCStr(rb_path));
914
+ rugged_exception_check(error);
915
+
916
+ return Qnil;
917
+ }
918
+
919
+ /*
920
+ * call-seq:
921
+ * index.conflict_get(path) -> conflict or nil
922
+ *
923
+ * Return index entries from the ancestor, our side and their side of
924
+ * the conflict at +path+.
925
+ *
926
+ * If +:ancestor+, +:ours+ or +:theirs+ is +nil+, that indicates that +path+
927
+ * did not exist in the respective tree.
928
+ *
929
+ * Returns nil if no conflict is present at +path+.
930
+ */
931
+ static VALUE rb_git_conflict_get(VALUE self, VALUE rb_path)
932
+ {
933
+ VALUE rb_result = rb_hash_new();
934
+ git_index *index;
935
+ const git_index_entry *ancestor, *ours, *theirs;
936
+ int error;
937
+
938
+ Check_Type(rb_path, T_STRING);
939
+
940
+ Data_Get_Struct(self, git_index, index);
941
+
942
+ error = git_index_conflict_get(&ancestor, &ours, &theirs, index, StringValueCStr(rb_path));
943
+ if (error == GIT_ENOTFOUND)
944
+ return Qnil;
945
+ else
946
+ rugged_exception_check(error);
947
+
948
+ rb_hash_aset(rb_result, CSTR2SYM("ancestor"), rb_git_indexentry_fromC(ancestor));
949
+ rb_hash_aset(rb_result, CSTR2SYM("ours"), rb_git_indexentry_fromC(ours));
950
+ rb_hash_aset(rb_result, CSTR2SYM("theirs"), rb_git_indexentry_fromC(theirs));
951
+
952
+ return rb_result;
953
+ }
954
+
955
+ void rugged_parse_merge_file_options(git_merge_file_options *opts, VALUE rb_options)
956
+ {
957
+ if (!NIL_P(rb_options)) {
958
+ VALUE rb_value;
959
+ Check_Type(rb_options, T_HASH);
960
+
961
+ rb_value = rb_hash_aref(rb_options, CSTR2SYM("ancestor_label"));
962
+ if (!NIL_P(rb_value)) {
963
+ Check_Type(rb_value, T_FIXNUM);
964
+ opts->ancestor_label = StringValueCStr(rb_value);
965
+ }
966
+
967
+ rb_value = rb_hash_aref(rb_options, CSTR2SYM("our_label"));
968
+ if (!NIL_P(rb_value)) {
969
+ Check_Type(rb_value, T_FIXNUM);
970
+ opts->our_label = StringValueCStr(rb_value);
971
+ }
972
+
973
+ rb_value = rb_hash_aref(rb_options, CSTR2SYM("their_label"));
974
+ if (!NIL_P(rb_value)) {
975
+ Check_Type(rb_value, T_FIXNUM);
976
+ opts->their_label = StringValueCStr(rb_value);
977
+ }
978
+
979
+ rb_value = rb_hash_aref(rb_options, CSTR2SYM("favor"));
980
+ if (!NIL_P(rb_value)) {
981
+ ID id_favor;
982
+
983
+ Check_Type(rb_value, T_SYMBOL);
984
+ id_favor = SYM2ID(rb_value);
985
+
986
+ if (id_favor == rb_intern("normal")) {
987
+ opts->favor = GIT_MERGE_FILE_FAVOR_NORMAL;
988
+ } else if (id_favor == rb_intern("ours")) {
989
+ opts->favor = GIT_MERGE_FILE_FAVOR_OURS;
990
+ } else if (id_favor == rb_intern("theirs")) {
991
+ opts->favor = GIT_MERGE_FILE_FAVOR_THEIRS;
992
+ } else if (id_favor == rb_intern("union")) {
993
+ opts->favor = GIT_MERGE_FILE_FAVOR_UNION;
994
+ } else {
995
+ rb_raise(rb_eTypeError,
996
+ "Invalid favor mode. Expected `:normal`, `:ours`, `:theirs` or `:union`");
997
+ }
998
+ }
999
+
1000
+ rb_value = rb_hash_aref(rb_options, CSTR2SYM("style"));
1001
+ if (!NIL_P(rb_value)) {
1002
+ ID id_style;
1003
+
1004
+ Check_Type(rb_value, T_SYMBOL);
1005
+ id_style = SYM2ID(rb_value);
1006
+
1007
+ if (id_style == rb_intern("standard")) {
1008
+ opts->flags |= GIT_MERGE_FILE_STYLE_MERGE;
1009
+ } else if (id_style == rb_intern("diff3")) {
1010
+ opts->flags |= GIT_MERGE_FILE_STYLE_DIFF3;
1011
+ } else {
1012
+ rb_raise(rb_eTypeError,
1013
+ "Invalid style mode. Expected `:standard`, or `:diff3`");
1014
+ }
1015
+ } else {
1016
+ opts->flags |= GIT_MERGE_FILE_STYLE_MERGE;
1017
+ }
1018
+
1019
+ if (RTEST(rb_hash_aref(rb_options, CSTR2SYM("simplify")))) {
1020
+ opts->flags |= GIT_MERGE_FILE_SIMPLIFY_ALNUM;
1021
+ }
1022
+ }
1023
+ }
1024
+
1025
+ /*
1026
+ * call-seq:
1027
+ * index.merge_file(path[, options]) -> merge_file or nil
1028
+ * index.merge_file(path) -> merge_file or nil
1029
+ *
1030
+ * Return merge_file (in memory) from the ancestor, our side and their side of
1031
+ * the conflict at +path+.
1032
+ *
1033
+ * If +:ancestor+, +:ours+ or +:theirs+ is +nil+, that indicates that +path+
1034
+ * did not exist in the respective tree.
1035
+ *
1036
+ * Returns nil if no conflict is present at +path+.
1037
+
1038
+ * The following options can be passed in the +options+ Hash:
1039
+ *
1040
+ * :ancestor_label ::
1041
+ * The name of the ancestor branch used to decorate conflict markers.
1042
+ *
1043
+ * :our_label ::
1044
+ * The name of our branch used to decorate conflict markers.
1045
+ *
1046
+ * :their_label ::
1047
+ * The name of their branch used to decorate conflict markers.
1048
+ *
1049
+ * :favor ::
1050
+ * Specifies how and if conflicts are auto-resolved by favoring a specific
1051
+ * file output. Can be one of `:normal`, `:ours`, `:theirs` or `:union`.
1052
+ * Defaults to `:normal`.
1053
+ *
1054
+ * :style ::
1055
+ * Specifies the type of merge file to produce. Can be one of `:standard`, `:diff3`. Defaults to `:standard`
1056
+ *
1057
+ * :simplify ::
1058
+ * If true, the merge file is simplified by condensing non-alphanumeric regions.
1059
+ *
1060
+ */
1061
+
1062
+ static VALUE rb_git_merge_file(int argc, VALUE *argv, VALUE self)
1063
+ {
1064
+ VALUE rb_path, rb_options;
1065
+ VALUE rb_result = rb_hash_new();
1066
+ VALUE rb_repo = rugged_owner(self);
1067
+
1068
+ git_repository *repo;
1069
+ git_index *index;
1070
+ const git_index_entry *ancestor, *ours, *theirs;
1071
+ git_merge_file_result merge_file_result = {0};
1072
+ git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT;
1073
+ int error;
1074
+
1075
+ rb_scan_args(argc, argv, "1:", &rb_path, &rb_options);
1076
+
1077
+ if (!NIL_P(rb_options)) {
1078
+ Check_Type(rb_options, T_HASH);
1079
+ rugged_parse_merge_file_options(&opts, rb_options);
1080
+ }
1081
+
1082
+ Check_Type(rb_path, T_STRING);
1083
+
1084
+ Data_Get_Struct(self, git_index, index);
1085
+
1086
+ rugged_check_repo(rb_repo);
1087
+ Data_Get_Struct(rb_repo, git_repository, repo);
1088
+
1089
+ error = git_index_conflict_get(&ancestor, &ours, &theirs, index, StringValueCStr(rb_path));
1090
+ if (error == GIT_ENOTFOUND)
1091
+ return Qnil;
1092
+ else
1093
+ rugged_exception_check(error);
1094
+
1095
+ error = git_merge_file_from_index(&merge_file_result, repo, ancestor, ours, theirs, &opts);
1096
+ rugged_exception_check(error);
1097
+
1098
+ rb_hash_aset(rb_result, CSTR2SYM("automergeable"), merge_file_result.automergeable ? Qtrue : Qfalse);
1099
+ rb_hash_aset(rb_result, CSTR2SYM("path"), rb_path);
1100
+ rb_hash_aset(rb_result, CSTR2SYM("filemode"), INT2FIX(merge_file_result.mode));
1101
+ rb_hash_aset(rb_result, CSTR2SYM("data"), rb_str_new(merge_file_result.ptr, merge_file_result.len));
1102
+
1103
+ git_merge_file_result_free(&merge_file_result);
1104
+
1105
+ return rb_result;
1106
+ }
1107
+
1108
+ /*
1109
+ * call-seq:
1110
+ * index.conflict_cleanup -> nil
1111
+ *
1112
+ * Remove all conflicting entries (entries with a stage greater than 0)
1113
+ * from the index.
1114
+ */
1115
+ static VALUE rb_git_conflict_cleanup(VALUE self)
1116
+ {
1117
+ git_index *index;
1118
+
1119
+ Data_Get_Struct(self, git_index, index);
1120
+ git_index_conflict_cleanup(index);
1121
+
1122
+ return Qnil;
1123
+ }
1124
+
1125
+ /*
1126
+ * call-seq:
1127
+ * index.conflicts -> conflicts
1128
+ *
1129
+ * Return all conflicts in +index+.
1130
+ *
1131
+ * Each conflict is represented as a Hash with +:ancestor+, +:ours+ or
1132
+ * +:theirs+ key-value pairs, each containing index entry data.
1133
+ *
1134
+ * If the value of the +:ancestor+, +:ours+ or +:theirs+ key is +nil+,
1135
+ * that indicates that file in conflict did not exists in the respective tree.
1136
+ */
1137
+ static VALUE rb_git_index_conflicts(VALUE self)
1138
+ {
1139
+ VALUE rb_conflicts = rb_ary_new();
1140
+ git_index *index;
1141
+ git_index_conflict_iterator *iter;
1142
+ const git_index_entry *ancestor, *ours, *theirs;
1143
+ int error;
1144
+
1145
+ Data_Get_Struct(self, git_index, index);
1146
+
1147
+ error = git_index_conflict_iterator_new(&iter, index);
1148
+ rugged_exception_check(error);
1149
+
1150
+ while ((error = git_index_conflict_next(&ancestor, &ours, &theirs, iter)) == GIT_OK) {
1151
+ VALUE rb_conflict = rb_hash_new();
1152
+
1153
+ rb_hash_aset(rb_conflict, CSTR2SYM("ancestor"), rb_git_indexentry_fromC(ancestor));
1154
+ rb_hash_aset(rb_conflict, CSTR2SYM("ours"), rb_git_indexentry_fromC(ours));
1155
+ rb_hash_aset(rb_conflict, CSTR2SYM("theirs"), rb_git_indexentry_fromC(theirs));
1156
+
1157
+ rb_ary_push(rb_conflicts, rb_conflict);
1158
+ }
1159
+
1160
+ git_index_conflict_iterator_free(iter);
1161
+
1162
+ if (error != GIT_ITEROVER)
1163
+ rugged_exception_check(error);
1164
+
1165
+ return rb_conflicts;
1166
+ }
1167
+
1168
+ /*
1169
+ * Document-class: Rugged::Index
1170
+ *
1171
+ * == Index Entries
1172
+ *
1173
+ * Index entries are represented as Hash instances with the following key/value pairs:
1174
+ *
1175
+ * path: ::
1176
+ * The entry's path in the index.
1177
+ *
1178
+ * oid: ::
1179
+ * The oid of the entry's git object (blob / tree).
1180
+ *
1181
+ * dev: ::
1182
+ * The device for the index entry.
1183
+ *
1184
+ * ino: ::
1185
+ * The inode for the index entry.
1186
+ *
1187
+ * mode: ::
1188
+ * The current permissions of the index entry.
1189
+ *
1190
+ * gid: ::
1191
+ * Group ID of the index entry's owner.
1192
+ *
1193
+ * uid: ::
1194
+ * User ID of the index entry's owner.
1195
+ *
1196
+ * file_size: ::
1197
+ * The index entry's size, in bytes.
1198
+ *
1199
+ * valid: ::
1200
+ * +true+ if the index entry is valid, +false+ otherwise.
1201
+ *
1202
+ * stage: ::
1203
+ * The current stage of the index entry.
1204
+ *
1205
+ * mtime: ::
1206
+ * A Time instance representing the index entry's time of last modification.
1207
+ *
1208
+ * mtime: ::
1209
+ * A Time instance representing the index entry's time of last status change
1210
+ * (ie. change of owner, group, mode, etc.).
1211
+ */
1212
+ void Init_rugged_index(void)
1213
+ {
1214
+ /*
1215
+ * Index
1216
+ */
1217
+ rb_cRuggedIndex = rb_define_class_under(rb_mRugged, "Index", rb_cObject);
1218
+ rb_define_singleton_method(rb_cRuggedIndex, "new", rb_git_index_new, -1);
1219
+
1220
+ rb_define_method(rb_cRuggedIndex, "count", rb_git_index_count, 0);
1221
+ rb_define_method(rb_cRuggedIndex, "reload", rb_git_index_read, 0);
1222
+ rb_define_method(rb_cRuggedIndex, "clear", rb_git_index_clear, 0);
1223
+ rb_define_method(rb_cRuggedIndex, "write", rb_git_index_write, 0);
1224
+ rb_define_method(rb_cRuggedIndex, "get", rb_git_index_get, -1);
1225
+ rb_define_method(rb_cRuggedIndex, "[]", rb_git_index_get, -1);
1226
+ rb_define_method(rb_cRuggedIndex, "each", rb_git_index_each, 0);
1227
+ rb_define_method(rb_cRuggedIndex, "diff", rb_git_index_diff, -1);
1228
+
1229
+ rb_define_method(rb_cRuggedIndex, "conflicts?", rb_git_index_conflicts_p, 0);
1230
+ rb_define_method(rb_cRuggedIndex, "conflicts", rb_git_index_conflicts, 0);
1231
+ rb_define_method(rb_cRuggedIndex, "conflict_get", rb_git_conflict_get, 1);
1232
+ rb_define_method(rb_cRuggedIndex, "conflict_add", rb_git_conflict_add, 1);
1233
+ rb_define_method(rb_cRuggedIndex, "conflict_remove", rb_git_conflict_remove, 1);
1234
+ rb_define_method(rb_cRuggedIndex, "conflict_cleanup", rb_git_conflict_cleanup, 0);
1235
+
1236
+ rb_define_method(rb_cRuggedIndex, "merge_file", rb_git_merge_file, -1);
1237
+
1238
+ rb_define_method(rb_cRuggedIndex, "add", rb_git_index_add, 1);
1239
+ rb_define_method(rb_cRuggedIndex, "update", rb_git_index_add, 1);
1240
+ rb_define_method(rb_cRuggedIndex, "<<", rb_git_index_add, 1);
1241
+
1242
+ rb_define_method(rb_cRuggedIndex, "remove", rb_git_index_remove, -1);
1243
+ rb_define_method(rb_cRuggedIndex, "remove_dir", rb_git_index_remove_directory, -1);
1244
+
1245
+ rb_define_method(rb_cRuggedIndex, "add_all", rb_git_index_add_all, -1);
1246
+ rb_define_method(rb_cRuggedIndex, "update_all", rb_git_index_update_all, -1);
1247
+ rb_define_method(rb_cRuggedIndex, "remove_all", rb_git_index_remove_all, -1);
1248
+
1249
+ rb_define_method(rb_cRuggedIndex, "write_tree", rb_git_index_writetree, -1);
1250
+ rb_define_method(rb_cRuggedIndex, "read_tree", rb_git_index_readtree, 1);
1251
+
1252
+ rb_const_set(rb_cRuggedIndex, rb_intern("ENTRY_FLAGS_STAGE"), INT2FIX(GIT_IDXENTRY_STAGEMASK));
1253
+ rb_const_set(rb_cRuggedIndex, rb_intern("ENTRY_FLAGS_STAGE_SHIFT"), INT2FIX(GIT_IDXENTRY_STAGESHIFT));
1254
+ rb_const_set(rb_cRuggedIndex, rb_intern("ENTRY_FLAGS_VALID"), INT2FIX(GIT_IDXENTRY_VALID));
1255
+ }