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.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +619 -0
- data/ext/rugged/extconf.rb +105 -0
- data/ext/rugged/rugged.c +527 -0
- data/ext/rugged/rugged.h +185 -0
- data/ext/rugged/rugged_backend.c +34 -0
- data/ext/rugged/rugged_blame.c +292 -0
- data/ext/rugged/rugged_blob.c +638 -0
- data/ext/rugged/rugged_branch.c +195 -0
- data/ext/rugged/rugged_branch_collection.c +408 -0
- data/ext/rugged/rugged_commit.c +691 -0
- data/ext/rugged/rugged_config.c +404 -0
- data/ext/rugged/rugged_cred.c +148 -0
- data/ext/rugged/rugged_diff.c +686 -0
- data/ext/rugged/rugged_diff_delta.c +105 -0
- data/ext/rugged/rugged_diff_hunk.c +103 -0
- data/ext/rugged/rugged_diff_line.c +83 -0
- data/ext/rugged/rugged_index.c +1255 -0
- data/ext/rugged/rugged_note.c +376 -0
- data/ext/rugged/rugged_object.c +383 -0
- data/ext/rugged/rugged_patch.c +245 -0
- data/ext/rugged/rugged_reference.c +396 -0
- data/ext/rugged/rugged_reference_collection.c +446 -0
- data/ext/rugged/rugged_remote.c +691 -0
- data/ext/rugged/rugged_remote_collection.c +457 -0
- data/ext/rugged/rugged_repo.c +2669 -0
- data/ext/rugged/rugged_revwalk.c +495 -0
- data/ext/rugged/rugged_settings.c +155 -0
- data/ext/rugged/rugged_signature.c +106 -0
- data/ext/rugged/rugged_submodule.c +852 -0
- data/ext/rugged/rugged_submodule_collection.c +384 -0
- data/ext/rugged/rugged_tag.c +251 -0
- data/ext/rugged/rugged_tag_collection.c +347 -0
- data/ext/rugged/rugged_tree.c +919 -0
- data/lib/rugged.rb +23 -0
- data/lib/rugged/attributes.rb +41 -0
- data/lib/rugged/blob.rb +28 -0
- data/lib/rugged/branch.rb +19 -0
- data/lib/rugged/commit.rb +54 -0
- data/lib/rugged/console.rb +9 -0
- data/lib/rugged/credentials.rb +43 -0
- data/lib/rugged/diff.rb +20 -0
- data/lib/rugged/diff/delta.rb +53 -0
- data/lib/rugged/diff/hunk.rb +18 -0
- data/lib/rugged/diff/line.rb +47 -0
- data/lib/rugged/index.rb +13 -0
- data/lib/rugged/object.rb +7 -0
- data/lib/rugged/patch.rb +36 -0
- data/lib/rugged/reference.rb +7 -0
- data/lib/rugged/remote.rb +4 -0
- data/lib/rugged/repository.rb +227 -0
- data/lib/rugged/submodule_collection.rb +48 -0
- data/lib/rugged/tag.rb +50 -0
- data/lib/rugged/tree.rb +38 -0
- data/lib/rugged/version.rb +3 -0
- data/lib/rugged/walker.rb +5 -0
- metadata +146 -0
@@ -0,0 +1,446 @@
|
|
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_mRugged;
|
28
|
+
extern VALUE rb_cRuggedRepo;
|
29
|
+
extern VALUE rb_cRuggedReference;
|
30
|
+
|
31
|
+
VALUE rb_cRuggedReferenceCollection;
|
32
|
+
|
33
|
+
/*
|
34
|
+
* call-seq:
|
35
|
+
* ReferenceCollection.new(repo) -> refs
|
36
|
+
*
|
37
|
+
* Creates and returns a new collection of references for the given +repo+.
|
38
|
+
*/
|
39
|
+
static VALUE rb_git_reference_collection_initialize(VALUE self, VALUE repo)
|
40
|
+
{
|
41
|
+
rugged_set_owner(self, repo);
|
42
|
+
return self;
|
43
|
+
}
|
44
|
+
|
45
|
+
/*
|
46
|
+
* call-seq:
|
47
|
+
* references.create(name, oid, options = {}) -> new_ref
|
48
|
+
* references.create(name, target, options = {}) -> new_ref
|
49
|
+
*
|
50
|
+
* Create a symbolic or direct reference on the collection's +repository+ with the given +name+.
|
51
|
+
*
|
52
|
+
* If the second argument is a valid OID, the reference will be created as direct.
|
53
|
+
* Otherwise, it will be assumed the target is the name of another reference.
|
54
|
+
*
|
55
|
+
* The following options can be passed in the +options+ Hash:
|
56
|
+
*
|
57
|
+
* :force ::
|
58
|
+
* Overwrites the reference with the given +name+, if it already exists,
|
59
|
+
* instead of raising an exception.
|
60
|
+
*
|
61
|
+
* If a reference with the given +name+ already exists and +:force+ is not +true+,
|
62
|
+
* an exception will be raised.
|
63
|
+
*/
|
64
|
+
static VALUE rb_git_reference_collection_create(int argc, VALUE *argv, VALUE self)
|
65
|
+
{
|
66
|
+
VALUE rb_repo = rugged_owner(self), rb_name, rb_target, rb_options;
|
67
|
+
git_repository *repo;
|
68
|
+
git_reference *ref;
|
69
|
+
git_oid oid;
|
70
|
+
char *log_message = NULL;
|
71
|
+
int error, force = 0;
|
72
|
+
|
73
|
+
rb_scan_args(argc, argv, "20:", &rb_name, &rb_target, &rb_options);
|
74
|
+
|
75
|
+
rugged_check_repo(rb_repo);
|
76
|
+
Data_Get_Struct(rb_repo, git_repository, repo);
|
77
|
+
Check_Type(rb_name, T_STRING);
|
78
|
+
Check_Type(rb_target, T_STRING);
|
79
|
+
|
80
|
+
if (!NIL_P(rb_options)) {
|
81
|
+
VALUE rb_val = rb_hash_aref(rb_options, CSTR2SYM("message"));
|
82
|
+
if (!NIL_P(rb_val))
|
83
|
+
log_message = StringValueCStr(rb_val);
|
84
|
+
|
85
|
+
force = RTEST(rb_hash_aref(rb_options, CSTR2SYM("force")));
|
86
|
+
}
|
87
|
+
|
88
|
+
if (git_oid_fromstr(&oid, StringValueCStr(rb_target)) == GIT_OK) {
|
89
|
+
error = git_reference_create(
|
90
|
+
&ref, repo, StringValueCStr(rb_name), &oid, force, log_message);
|
91
|
+
} else {
|
92
|
+
error = git_reference_symbolic_create(
|
93
|
+
&ref, repo, StringValueCStr(rb_name), StringValueCStr(rb_target), force, log_message);
|
94
|
+
}
|
95
|
+
|
96
|
+
rugged_exception_check(error);
|
97
|
+
|
98
|
+
return rugged_ref_new(rb_cRuggedReference, rb_repo, ref);
|
99
|
+
}
|
100
|
+
|
101
|
+
/*
|
102
|
+
* call-seq:
|
103
|
+
* references[name] -> new_ref
|
104
|
+
*
|
105
|
+
* Lookup a reference in the collection with the given +name+.
|
106
|
+
*
|
107
|
+
* Returns a new Rugged::Reference object.
|
108
|
+
*/
|
109
|
+
static VALUE rb_git_reference_collection_aref(VALUE self, VALUE rb_name) {
|
110
|
+
VALUE rb_repo = rugged_owner(self);
|
111
|
+
git_repository *repo;
|
112
|
+
git_reference *ref;
|
113
|
+
int error;
|
114
|
+
|
115
|
+
Data_Get_Struct(rb_repo, git_repository, repo);
|
116
|
+
|
117
|
+
error = git_reference_lookup(&ref, repo, StringValueCStr(rb_name));
|
118
|
+
|
119
|
+
if (error == GIT_ENOTFOUND)
|
120
|
+
return Qnil;
|
121
|
+
|
122
|
+
rugged_exception_check(error);
|
123
|
+
|
124
|
+
return rugged_ref_new(rb_cRuggedReference, rb_repo, ref);
|
125
|
+
}
|
126
|
+
|
127
|
+
static VALUE rb_git_reference_collection__each(int argc, VALUE *argv, VALUE self, int only_names)
|
128
|
+
{
|
129
|
+
VALUE rb_glob, rb_repo = rugged_owner(self);
|
130
|
+
git_repository *repo;
|
131
|
+
git_reference_iterator *iter;
|
132
|
+
int error, exception = 0;
|
133
|
+
|
134
|
+
rb_scan_args(argc, argv, "01", &rb_glob);
|
135
|
+
|
136
|
+
if (!rb_block_given_p()) {
|
137
|
+
return rb_funcall(self,
|
138
|
+
rb_intern("to_enum"), 2,
|
139
|
+
only_names ? CSTR2SYM("each_name") : CSTR2SYM("each"),
|
140
|
+
rb_glob);
|
141
|
+
}
|
142
|
+
|
143
|
+
rugged_check_repo(rb_repo);
|
144
|
+
|
145
|
+
Data_Get_Struct(rb_repo, git_repository, repo);
|
146
|
+
|
147
|
+
if (!NIL_P(rb_glob)) {
|
148
|
+
Check_Type(rb_glob, T_STRING);
|
149
|
+
error = git_reference_iterator_glob_new(&iter, repo, StringValueCStr(rb_glob));
|
150
|
+
} else {
|
151
|
+
error = git_reference_iterator_new(&iter, repo);
|
152
|
+
}
|
153
|
+
|
154
|
+
rugged_exception_check(error);
|
155
|
+
|
156
|
+
if (only_names) {
|
157
|
+
const char *ref_name;
|
158
|
+
while (!exception && (error = git_reference_next_name(&ref_name, iter)) == GIT_OK) {
|
159
|
+
rb_protect(rb_yield, rb_str_new_utf8(ref_name), &exception);
|
160
|
+
}
|
161
|
+
} else {
|
162
|
+
git_reference *ref;
|
163
|
+
while (!exception && (error = git_reference_next(&ref, iter)) == GIT_OK) {
|
164
|
+
rb_protect(rb_yield, rugged_ref_new(rb_cRuggedReference, rb_repo, ref), &exception);
|
165
|
+
}
|
166
|
+
}
|
167
|
+
|
168
|
+
git_reference_iterator_free(iter);
|
169
|
+
|
170
|
+
if (exception)
|
171
|
+
rb_jump_tag(exception);
|
172
|
+
|
173
|
+
if (error != GIT_ITEROVER)
|
174
|
+
rugged_exception_check(error);
|
175
|
+
|
176
|
+
return Qnil;
|
177
|
+
}
|
178
|
+
|
179
|
+
/*
|
180
|
+
* call-seq:
|
181
|
+
* references.each(glob = nil) { |ref| block } -> nil
|
182
|
+
* references.each(glob = nil) -> enumerator
|
183
|
+
*
|
184
|
+
* Iterate through all the references in the collection's +repository+. Iteration
|
185
|
+
* can be optionally filtered to the ones matching the given
|
186
|
+
* +glob+, a standard Unix filename glob.
|
187
|
+
*
|
188
|
+
* The given block will be called once with a Rugged::Reference
|
189
|
+
* instance for each reference.
|
190
|
+
*
|
191
|
+
* If no block is given, an enumerator will be returned.
|
192
|
+
*/
|
193
|
+
static VALUE rb_git_reference_collection_each(int argc, VALUE *argv, VALUE self)
|
194
|
+
{
|
195
|
+
return rb_git_reference_collection__each(argc, argv, self, 0);
|
196
|
+
}
|
197
|
+
|
198
|
+
/*
|
199
|
+
* call-seq:
|
200
|
+
* references.each_name(glob = nil) { |ref_name| block } -> nil
|
201
|
+
* references.each_name(glob = nil) -> enumerator
|
202
|
+
*
|
203
|
+
* Iterate through all the reference names in the collection's +repository+. Iteration
|
204
|
+
* can be optionally filtered to the ones matching the given
|
205
|
+
* +glob+, a standard Unix filename glob.
|
206
|
+
*
|
207
|
+
* The given block will be called once with the name of each reference.
|
208
|
+
*
|
209
|
+
* If no block is given, an enumerator will be returned.
|
210
|
+
*/
|
211
|
+
static VALUE rb_git_reference_collection_each_name(int argc, VALUE *argv, VALUE self)
|
212
|
+
{
|
213
|
+
return rb_git_reference_collection__each(argc, argv, self, 1);
|
214
|
+
}
|
215
|
+
|
216
|
+
/*
|
217
|
+
* call-seq:
|
218
|
+
* references.exist?(name) -> true or false
|
219
|
+
* references.exists?(name) -> true or false
|
220
|
+
*
|
221
|
+
* Check if a given reference exists with the given +name+.
|
222
|
+
*/
|
223
|
+
static VALUE rb_git_reference_collection_exist_p(VALUE self, VALUE rb_name_or_ref)
|
224
|
+
{
|
225
|
+
VALUE rb_repo = rugged_owner(self);
|
226
|
+
git_repository *repo;
|
227
|
+
git_reference *ref;
|
228
|
+
int error;
|
229
|
+
|
230
|
+
if (rb_obj_is_kind_of(rb_name_or_ref, rb_cRuggedReference))
|
231
|
+
rb_name_or_ref = rb_funcall(rb_name_or_ref, rb_intern("canonical_name"), 0);
|
232
|
+
|
233
|
+
if (TYPE(rb_name_or_ref) != T_STRING)
|
234
|
+
rb_raise(rb_eTypeError, "Expecting a String or Rugged::Reference instance");
|
235
|
+
|
236
|
+
Data_Get_Struct(rb_repo, git_repository, repo);
|
237
|
+
|
238
|
+
error = git_reference_lookup(&ref, repo, StringValueCStr(rb_name_or_ref));
|
239
|
+
git_reference_free(ref);
|
240
|
+
|
241
|
+
if (error == GIT_ENOTFOUND)
|
242
|
+
return Qfalse;
|
243
|
+
else
|
244
|
+
rugged_exception_check(error);
|
245
|
+
|
246
|
+
return Qtrue;
|
247
|
+
}
|
248
|
+
|
249
|
+
/*
|
250
|
+
* call-seq:
|
251
|
+
* references.rename(old_name, new_name, options = {}) -> new_ref
|
252
|
+
* references.rename(ref, new_name, options = {}) -> new_ref
|
253
|
+
*
|
254
|
+
* Change the name of a reference. If +force+ is +true+, any previously
|
255
|
+
* existing references will be overwritten when renaming.
|
256
|
+
*
|
257
|
+
* Return a new reference object with the new object
|
258
|
+
*
|
259
|
+
* reference.name #=> 'refs/heads/master'
|
260
|
+
* new_ref = references.rename(ref, 'refs/heads/development') #=> <Reference>
|
261
|
+
* new_ref.name #=> 'refs/heads/development'
|
262
|
+
*
|
263
|
+
* The following options can be passed in the +options+ Hash:
|
264
|
+
*
|
265
|
+
* :force ::
|
266
|
+
* Overwrites the reference with the given +name+, if it already exists,
|
267
|
+
* instead of raising an exception.
|
268
|
+
*
|
269
|
+
* If a reference with the given +new_name+ already exists and +:force+ is not +true+,
|
270
|
+
* an exception will be raised.
|
271
|
+
*/
|
272
|
+
static VALUE rb_git_reference_collection_rename(int argc, VALUE *argv, VALUE self)
|
273
|
+
{
|
274
|
+
VALUE rb_new_name, rb_name_or_ref, rb_options;
|
275
|
+
VALUE rb_repo = rugged_owner(self);
|
276
|
+
git_reference *ref, *out = NULL;
|
277
|
+
git_repository *repo;
|
278
|
+
char *log_message = NULL;
|
279
|
+
int error, force = 0;
|
280
|
+
|
281
|
+
rb_scan_args(argc, argv, "20:", &rb_name_or_ref, &rb_new_name, &rb_options);
|
282
|
+
Check_Type(rb_new_name, T_STRING);
|
283
|
+
|
284
|
+
if (rb_obj_is_kind_of(rb_name_or_ref, rb_cRuggedReference))
|
285
|
+
rb_name_or_ref = rb_funcall(rb_name_or_ref, rb_intern("canonical_name"), 0);
|
286
|
+
|
287
|
+
if (TYPE(rb_name_or_ref) != T_STRING)
|
288
|
+
rb_raise(rb_eTypeError, "Expecting a String or Rugged::Reference instance");
|
289
|
+
|
290
|
+
rugged_check_repo(rb_repo);
|
291
|
+
Data_Get_Struct(rb_repo, git_repository, repo);
|
292
|
+
|
293
|
+
if (!NIL_P(rb_options)) {
|
294
|
+
VALUE rb_val = rb_hash_aref(rb_options, CSTR2SYM("message"));
|
295
|
+
if (!NIL_P(rb_val))
|
296
|
+
log_message = StringValueCStr(rb_val);
|
297
|
+
|
298
|
+
force = RTEST(rb_hash_aref(rb_options, CSTR2SYM("force")));
|
299
|
+
}
|
300
|
+
|
301
|
+
if ((error = git_reference_lookup(&ref, repo, StringValueCStr(rb_name_or_ref))) == GIT_OK)
|
302
|
+
error = git_reference_rename(&out, ref, StringValueCStr(rb_new_name), force, log_message);
|
303
|
+
|
304
|
+
git_reference_free(ref);
|
305
|
+
|
306
|
+
rugged_exception_check(error);
|
307
|
+
|
308
|
+
return rugged_ref_new(rb_cRuggedReference, rugged_owner(self), out);
|
309
|
+
}
|
310
|
+
|
311
|
+
/*
|
312
|
+
* call-seq:
|
313
|
+
* references.update(ref, oid) -> new_ref
|
314
|
+
* references.update(name, oid) -> new_ref
|
315
|
+
* references.update(ref, other_ref) -> new_ref
|
316
|
+
* references.update(name, other_ref_name) -> new_ref
|
317
|
+
*
|
318
|
+
* Set the target of a reference. If +ref+ is a direct reference,
|
319
|
+
* the new target must be a +String+ representing a SHA1 OID.
|
320
|
+
*
|
321
|
+
* If +reference+ is symbolic, the new target must be a +String+ with
|
322
|
+
* the name of another reference.
|
323
|
+
*
|
324
|
+
* The original reference is unaltered; a new reference object is
|
325
|
+
* returned with the new target, and the changes are persisted to
|
326
|
+
* disk.
|
327
|
+
*
|
328
|
+
* r1.type #=> :symbolic
|
329
|
+
* references.update(r1, "refs/heads/master") #=> <Reference>
|
330
|
+
*
|
331
|
+
* r2.type #=> :direct
|
332
|
+
* references.update(r2, "de5ba987198bcf2518885f0fc1350e5172cded78") #=> <Reference>
|
333
|
+
*/
|
334
|
+
static VALUE rb_git_reference_collection_update(int argc, VALUE *argv, VALUE self)
|
335
|
+
{
|
336
|
+
VALUE rb_repo = rugged_owner(self), rb_name_or_ref, rb_target, rb_options;
|
337
|
+
git_repository *repo = NULL;
|
338
|
+
git_reference *ref = NULL, *out = NULL;
|
339
|
+
char *log_message = NULL;
|
340
|
+
int error;
|
341
|
+
|
342
|
+
rb_scan_args(argc, argv, "20:", &rb_name_or_ref, &rb_target, &rb_options);
|
343
|
+
|
344
|
+
if (rb_obj_is_kind_of(rb_name_or_ref, rb_cRuggedReference))
|
345
|
+
rb_name_or_ref = rb_funcall(rb_name_or_ref, rb_intern("canonical_name"), 0);
|
346
|
+
|
347
|
+
if (TYPE(rb_name_or_ref) != T_STRING)
|
348
|
+
rb_raise(rb_eTypeError, "Expecting a String or Rugged::Reference instance");
|
349
|
+
|
350
|
+
if (rb_obj_is_kind_of(rb_target, rb_cRuggedReference))
|
351
|
+
rb_target = rb_funcall(rb_target, rb_intern("canonical_name"), 0);
|
352
|
+
|
353
|
+
if (TYPE(rb_target) != T_STRING)
|
354
|
+
rb_raise(rb_eTypeError, "Expecting a String or Rugged::Reference instance");
|
355
|
+
|
356
|
+
if (!NIL_P(rb_options)) {
|
357
|
+
VALUE rb_val = rb_hash_aref(rb_options, CSTR2SYM("message"));
|
358
|
+
if (!NIL_P(rb_val))
|
359
|
+
log_message = StringValueCStr(rb_val);
|
360
|
+
}
|
361
|
+
|
362
|
+
rugged_check_repo(rb_repo);
|
363
|
+
Data_Get_Struct(rb_repo, git_repository, repo);
|
364
|
+
|
365
|
+
error = git_reference_lookup(&ref, repo, StringValueCStr(rb_name_or_ref));
|
366
|
+
rugged_exception_check(error);
|
367
|
+
|
368
|
+
if (git_reference_type(ref) == GIT_REF_OID) {
|
369
|
+
git_oid target;
|
370
|
+
|
371
|
+
error = git_oid_fromstr(&target, StringValueCStr(rb_target));
|
372
|
+
if (error) goto cleanup;
|
373
|
+
|
374
|
+
error = git_reference_set_target(&out, ref, &target, log_message);
|
375
|
+
} else {
|
376
|
+
error = git_reference_symbolic_set_target(&out, ref, StringValueCStr(rb_target), log_message);
|
377
|
+
}
|
378
|
+
|
379
|
+
cleanup:
|
380
|
+
|
381
|
+
git_reference_free(ref);
|
382
|
+
rugged_exception_check(error);
|
383
|
+
|
384
|
+
return rugged_ref_new(rb_cRuggedReference, rb_repo, out);
|
385
|
+
}
|
386
|
+
|
387
|
+
/*
|
388
|
+
* call-seq:
|
389
|
+
* references.delete(ref) -> nil
|
390
|
+
* references.delete(name) -> nil
|
391
|
+
*
|
392
|
+
* Delete specified reference.
|
393
|
+
*
|
394
|
+
* If a Rugged::Reference object was passed, the object will become
|
395
|
+
* invalidated and won't be able to be used for any other operations.
|
396
|
+
*
|
397
|
+
* repo.references.delete("HEAD")
|
398
|
+
* # Reference no longer exists on disk
|
399
|
+
*/
|
400
|
+
static VALUE rb_git_reference_collection_delete(VALUE self, VALUE rb_name_or_ref)
|
401
|
+
{
|
402
|
+
VALUE rb_repo = rugged_owner(self);
|
403
|
+
git_reference *ref;
|
404
|
+
git_repository *repo;
|
405
|
+
int error;
|
406
|
+
|
407
|
+
if (rb_obj_is_kind_of(rb_name_or_ref, rb_cRuggedReference))
|
408
|
+
rb_name_or_ref = rb_funcall(rb_name_or_ref, rb_intern("canonical_name"), 0);
|
409
|
+
|
410
|
+
if (TYPE(rb_name_or_ref) != T_STRING)
|
411
|
+
rb_raise(rb_eTypeError, "Expecting a String or Rugged::Reference instance");
|
412
|
+
|
413
|
+
rugged_check_repo(rb_repo);
|
414
|
+
Data_Get_Struct(rb_repo, git_repository, repo);
|
415
|
+
|
416
|
+
error = git_reference_lookup(&ref, repo, StringValueCStr(rb_name_or_ref));
|
417
|
+
rugged_exception_check(error);
|
418
|
+
|
419
|
+
error = git_reference_delete(ref);
|
420
|
+
git_reference_free(ref);
|
421
|
+
rugged_exception_check(error);
|
422
|
+
|
423
|
+
return Qnil;
|
424
|
+
}
|
425
|
+
|
426
|
+
void Init_rugged_reference_collection(void)
|
427
|
+
{
|
428
|
+
rb_cRuggedReferenceCollection = rb_define_class_under(rb_mRugged, "ReferenceCollection", rb_cObject);
|
429
|
+
rb_include_module(rb_cRuggedReferenceCollection, rb_mEnumerable);
|
430
|
+
|
431
|
+
rb_define_method(rb_cRuggedReferenceCollection, "initialize", rb_git_reference_collection_initialize, 1);
|
432
|
+
|
433
|
+
rb_define_method(rb_cRuggedReferenceCollection, "create", rb_git_reference_collection_create, -1);
|
434
|
+
rb_define_method(rb_cRuggedReferenceCollection, "[]", rb_git_reference_collection_aref, 1);
|
435
|
+
|
436
|
+
rb_define_method(rb_cRuggedReferenceCollection, "each", rb_git_reference_collection_each, -1);
|
437
|
+
rb_define_method(rb_cRuggedReferenceCollection, "each_name", rb_git_reference_collection_each_name, -1);
|
438
|
+
|
439
|
+
rb_define_method(rb_cRuggedReferenceCollection, "exist?", rb_git_reference_collection_exist_p, 1);
|
440
|
+
rb_define_method(rb_cRuggedReferenceCollection, "exists?", rb_git_reference_collection_exist_p, 1);
|
441
|
+
|
442
|
+
rb_define_method(rb_cRuggedReferenceCollection, "move", rb_git_reference_collection_rename, -1);
|
443
|
+
rb_define_method(rb_cRuggedReferenceCollection, "rename", rb_git_reference_collection_rename, -1);
|
444
|
+
rb_define_method(rb_cRuggedReferenceCollection, "update", rb_git_reference_collection_update, -1);
|
445
|
+
rb_define_method(rb_cRuggedReferenceCollection, "delete", rb_git_reference_collection_delete, 1);
|
446
|
+
}
|
@@ -0,0 +1,691 @@
|
|
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_mRugged;
|
28
|
+
extern VALUE rb_cRuggedRepo;
|
29
|
+
extern VALUE rb_eRuggedError;
|
30
|
+
VALUE rb_cRuggedRemote;
|
31
|
+
|
32
|
+
#define RUGGED_REMOTE_CALLBACKS_INIT {1, progress_cb, NULL, credentials_cb, NULL, transfer_progress_cb, update_tips_cb, NULL, NULL, push_update_reference_cb, NULL}
|
33
|
+
|
34
|
+
static int progress_cb(const char *str, int len, void *data)
|
35
|
+
{
|
36
|
+
struct rugged_remote_cb_payload *payload = data;
|
37
|
+
VALUE args = rb_ary_new2(2);
|
38
|
+
|
39
|
+
if (NIL_P(payload->progress))
|
40
|
+
return 0;
|
41
|
+
|
42
|
+
rb_ary_push(args, payload->progress);
|
43
|
+
rb_ary_push(args, rb_str_new(str, len));
|
44
|
+
|
45
|
+
rb_protect(rugged__block_yield_splat, args, &payload->exception);
|
46
|
+
|
47
|
+
return payload->exception ? GIT_ERROR : GIT_OK;
|
48
|
+
}
|
49
|
+
|
50
|
+
static int transfer_progress_cb(const git_transfer_progress *stats, void *data)
|
51
|
+
{
|
52
|
+
struct rugged_remote_cb_payload *payload = data;
|
53
|
+
VALUE args = rb_ary_new2(5);
|
54
|
+
|
55
|
+
if (NIL_P(payload->transfer_progress))
|
56
|
+
return 0;
|
57
|
+
|
58
|
+
rb_ary_push(args, payload->transfer_progress);
|
59
|
+
rb_ary_push(args, UINT2NUM(stats->total_objects));
|
60
|
+
rb_ary_push(args, UINT2NUM(stats->indexed_objects));
|
61
|
+
rb_ary_push(args, UINT2NUM(stats->received_objects));
|
62
|
+
rb_ary_push(args, UINT2NUM(stats->local_objects));
|
63
|
+
rb_ary_push(args, UINT2NUM(stats->total_deltas));
|
64
|
+
rb_ary_push(args, UINT2NUM(stats->indexed_deltas));
|
65
|
+
rb_ary_push(args, INT2FIX(stats->received_bytes));
|
66
|
+
|
67
|
+
rb_protect(rugged__block_yield_splat, args, &payload->exception);
|
68
|
+
|
69
|
+
return payload->exception ? GIT_ERROR : GIT_OK;
|
70
|
+
}
|
71
|
+
|
72
|
+
static int push_update_reference_cb(const char *refname, const char *status, void *data) {
|
73
|
+
struct rugged_remote_cb_payload *payload = data;
|
74
|
+
|
75
|
+
if (status != NULL)
|
76
|
+
rb_hash_aset(payload->result, rb_str_new_utf8(refname), rb_str_new_utf8(status));
|
77
|
+
|
78
|
+
return GIT_OK;
|
79
|
+
}
|
80
|
+
|
81
|
+
static int update_tips_cb(const char *refname, const git_oid *src, const git_oid *dest, void *data)
|
82
|
+
{
|
83
|
+
struct rugged_remote_cb_payload *payload = data;
|
84
|
+
VALUE args = rb_ary_new2(4);
|
85
|
+
|
86
|
+
if (NIL_P(payload->update_tips))
|
87
|
+
return 0;
|
88
|
+
|
89
|
+
rb_ary_push(args, payload->update_tips);
|
90
|
+
rb_ary_push(args, rb_str_new_utf8(refname));
|
91
|
+
rb_ary_push(args, git_oid_iszero(src) ? Qnil : rugged_create_oid(src));
|
92
|
+
rb_ary_push(args, git_oid_iszero(dest) ? Qnil : rugged_create_oid(dest));
|
93
|
+
|
94
|
+
rb_protect(rugged__block_yield_splat, args, &payload->exception);
|
95
|
+
|
96
|
+
return payload->exception ? GIT_ERROR : GIT_OK;
|
97
|
+
}
|
98
|
+
|
99
|
+
struct extract_cred_args
|
100
|
+
{
|
101
|
+
VALUE rb_callback;
|
102
|
+
git_cred **cred;
|
103
|
+
const char *url;
|
104
|
+
const char *username_from_url;
|
105
|
+
unsigned int allowed_types;
|
106
|
+
};
|
107
|
+
|
108
|
+
static VALUE allowed_types_to_rb_ary(int allowed_types) {
|
109
|
+
VALUE rb_allowed_types = rb_ary_new();
|
110
|
+
|
111
|
+
if (allowed_types & GIT_CREDTYPE_USERPASS_PLAINTEXT)
|
112
|
+
rb_ary_push(rb_allowed_types, CSTR2SYM("plaintext"));
|
113
|
+
|
114
|
+
if (allowed_types & GIT_CREDTYPE_SSH_KEY)
|
115
|
+
rb_ary_push(rb_allowed_types, CSTR2SYM("ssh_key"));
|
116
|
+
|
117
|
+
if (allowed_types & GIT_CREDTYPE_DEFAULT)
|
118
|
+
rb_ary_push(rb_allowed_types, CSTR2SYM("default"));
|
119
|
+
|
120
|
+
return rb_allowed_types;
|
121
|
+
}
|
122
|
+
|
123
|
+
static VALUE extract_cred(VALUE data) {
|
124
|
+
struct extract_cred_args *args = (struct extract_cred_args*)data;
|
125
|
+
VALUE rb_url, rb_username_from_url, rb_cred;
|
126
|
+
|
127
|
+
rb_url = args->url ? rb_str_new2(args->url) : Qnil;
|
128
|
+
rb_username_from_url = args->username_from_url ? rb_str_new2(args->username_from_url) : Qnil;
|
129
|
+
|
130
|
+
rb_cred = rb_funcall(args->rb_callback, rb_intern("call"), 3,
|
131
|
+
rb_url, rb_username_from_url, allowed_types_to_rb_ary(args->allowed_types));
|
132
|
+
|
133
|
+
rugged_cred_extract(args->cred, args->allowed_types, rb_cred);
|
134
|
+
|
135
|
+
return Qnil;
|
136
|
+
}
|
137
|
+
|
138
|
+
static int credentials_cb(
|
139
|
+
git_cred **cred,
|
140
|
+
const char *url,
|
141
|
+
const char *username_from_url,
|
142
|
+
unsigned int allowed_types,
|
143
|
+
void *data)
|
144
|
+
{
|
145
|
+
struct rugged_remote_cb_payload *payload = data;
|
146
|
+
struct extract_cred_args args = {
|
147
|
+
payload->credentials, cred, url, username_from_url, allowed_types
|
148
|
+
};
|
149
|
+
|
150
|
+
if (NIL_P(payload->credentials))
|
151
|
+
return GIT_PASSTHROUGH;
|
152
|
+
|
153
|
+
rb_protect(extract_cred, (VALUE)&args, &payload->exception);
|
154
|
+
|
155
|
+
return payload->exception ? GIT_ERROR : GIT_OK;
|
156
|
+
}
|
157
|
+
|
158
|
+
#define CALLABLE_OR_RAISE(ret, rb_options, name) \
|
159
|
+
do { \
|
160
|
+
ret = rb_hash_aref(rb_options, CSTR2SYM(name)); \
|
161
|
+
\
|
162
|
+
if (!NIL_P(ret) && !rb_respond_to(ret, rb_intern("call"))) \
|
163
|
+
rb_raise(rb_eArgError, "Expected a Proc or an object that responds to #call (:" name " )."); \
|
164
|
+
} while (0);
|
165
|
+
|
166
|
+
void rugged_remote_init_callbacks_and_payload_from_options(
|
167
|
+
VALUE rb_options,
|
168
|
+
git_remote_callbacks *callbacks,
|
169
|
+
struct rugged_remote_cb_payload *payload)
|
170
|
+
{
|
171
|
+
git_remote_callbacks prefilled = RUGGED_REMOTE_CALLBACKS_INIT;
|
172
|
+
|
173
|
+
prefilled.payload = payload;
|
174
|
+
memcpy(callbacks, &prefilled, sizeof(git_remote_callbacks));
|
175
|
+
|
176
|
+
if (!NIL_P(rb_options)) {
|
177
|
+
CALLABLE_OR_RAISE(payload->update_tips, rb_options, "update_tips");
|
178
|
+
CALLABLE_OR_RAISE(payload->progress, rb_options, "progress");
|
179
|
+
CALLABLE_OR_RAISE(payload->transfer_progress, rb_options, "transfer_progress");
|
180
|
+
CALLABLE_OR_RAISE(payload->credentials, rb_options, "credentials");
|
181
|
+
}
|
182
|
+
}
|
183
|
+
|
184
|
+
static void init_custom_headers(VALUE rb_options, git_strarray *custom_headers)
|
185
|
+
{
|
186
|
+
if (!NIL_P(rb_options))
|
187
|
+
{
|
188
|
+
VALUE rb_headers = rb_hash_aref(rb_options, CSTR2SYM("headers"));
|
189
|
+
rugged_rb_ary_to_strarray(rb_headers, custom_headers);
|
190
|
+
}
|
191
|
+
}
|
192
|
+
|
193
|
+
static int parse_prune_type(VALUE rb_prune_type)
|
194
|
+
{
|
195
|
+
if (rb_prune_type == Qtrue) {
|
196
|
+
return GIT_FETCH_PRUNE;
|
197
|
+
} else if (rb_prune_type == Qfalse) {
|
198
|
+
return GIT_FETCH_NO_PRUNE;
|
199
|
+
} else if (rb_prune_type == Qnil) {
|
200
|
+
return GIT_FETCH_PRUNE_UNSPECIFIED;
|
201
|
+
} else {
|
202
|
+
rb_raise(rb_eTypeError, "wrong argument type for :prune (expected true, false or nil)");
|
203
|
+
}
|
204
|
+
}
|
205
|
+
|
206
|
+
static void rb_git_remote__free(git_remote *remote)
|
207
|
+
{
|
208
|
+
git_remote_free(remote);
|
209
|
+
}
|
210
|
+
|
211
|
+
VALUE rugged_remote_new(VALUE owner, git_remote *remote)
|
212
|
+
{
|
213
|
+
VALUE rb_remote;
|
214
|
+
|
215
|
+
rb_remote = Data_Wrap_Struct(rb_cRuggedRemote, NULL, &rb_git_remote__free, remote);
|
216
|
+
rugged_set_owner(rb_remote, owner);
|
217
|
+
return rb_remote;
|
218
|
+
}
|
219
|
+
|
220
|
+
static VALUE rugged_rhead_new(const git_remote_head *head)
|
221
|
+
{
|
222
|
+
VALUE rb_head = rb_hash_new();
|
223
|
+
|
224
|
+
rb_hash_aset(rb_head, CSTR2SYM("local?"), head->local ? Qtrue : Qfalse);
|
225
|
+
rb_hash_aset(rb_head, CSTR2SYM("oid"), rugged_create_oid(&head->oid));
|
226
|
+
rb_hash_aset(rb_head, CSTR2SYM("loid"),
|
227
|
+
git_oid_iszero(&head->loid) ? Qnil : rugged_create_oid(&head->loid));
|
228
|
+
rb_hash_aset(rb_head, CSTR2SYM("name"), rb_str_new_utf8(head->name));
|
229
|
+
|
230
|
+
return rb_head;
|
231
|
+
}
|
232
|
+
|
233
|
+
/*
|
234
|
+
* call-seq:
|
235
|
+
* remote.ls(options = {}) -> an_enumerator
|
236
|
+
* remote.ls(options = {}) { |remote_head_hash| block }
|
237
|
+
*
|
238
|
+
* Connects +remote+ to list all references available along with their
|
239
|
+
* associated commit ids.
|
240
|
+
*
|
241
|
+
* The given block is called once for each remote head with a Hash containing the
|
242
|
+
* following keys:
|
243
|
+
*
|
244
|
+
* :local? ::
|
245
|
+
* +true+ if the remote head is available locally, +false+ otherwise.
|
246
|
+
*
|
247
|
+
* :oid ::
|
248
|
+
* The id of the object the remote head is currently pointing to.
|
249
|
+
*
|
250
|
+
* :loid ::
|
251
|
+
* The id of the object the local copy of the remote head is currently
|
252
|
+
* pointing to. Set to +nil+ if there is no local copy of the remote head.
|
253
|
+
*
|
254
|
+
* :name ::
|
255
|
+
* The fully qualified reference name of the remote head.
|
256
|
+
*
|
257
|
+
* If no block is given, an enumerator will be returned.
|
258
|
+
*
|
259
|
+
* The following options can be passed in the +options+ Hash:
|
260
|
+
*
|
261
|
+
* :credentials ::
|
262
|
+
* The credentials to use for the ls operation. Can be either an instance of one
|
263
|
+
* of the Rugged::Credentials types, or a proc returning one of the former.
|
264
|
+
* The proc will be called with the +url+, the +username+ from the url (if applicable) and
|
265
|
+
* a list of applicable credential types.
|
266
|
+
*
|
267
|
+
* :headers ::
|
268
|
+
* Extra HTTP headers to include with the request (only applies to http:// or https:// remotes)
|
269
|
+
*/
|
270
|
+
static VALUE rb_git_remote_ls(int argc, VALUE *argv, VALUE self)
|
271
|
+
{
|
272
|
+
git_remote *remote;
|
273
|
+
git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
|
274
|
+
git_strarray custom_headers = {0};
|
275
|
+
const git_remote_head **heads;
|
276
|
+
|
277
|
+
struct rugged_remote_cb_payload payload = { Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, 0 };
|
278
|
+
|
279
|
+
VALUE rb_options;
|
280
|
+
|
281
|
+
int error;
|
282
|
+
size_t heads_len, i;
|
283
|
+
|
284
|
+
Data_Get_Struct(self, git_remote, remote);
|
285
|
+
|
286
|
+
rb_scan_args(argc, argv, ":", &rb_options);
|
287
|
+
|
288
|
+
if (!rb_block_given_p())
|
289
|
+
return rb_funcall(self, rb_intern("to_enum"), 2, CSTR2SYM("ls"), rb_options);
|
290
|
+
|
291
|
+
rugged_remote_init_callbacks_and_payload_from_options(rb_options, &callbacks, &payload);
|
292
|
+
init_custom_headers(rb_options, &custom_headers);
|
293
|
+
|
294
|
+
if ((error = git_remote_connect(remote, GIT_DIRECTION_FETCH, &callbacks, &custom_headers)) ||
|
295
|
+
(error = git_remote_ls(&heads, &heads_len, remote)))
|
296
|
+
goto cleanup;
|
297
|
+
|
298
|
+
for (i = 0; i < heads_len && !payload.exception; i++)
|
299
|
+
rb_protect(rb_yield, rugged_rhead_new(heads[i]), &payload.exception);
|
300
|
+
|
301
|
+
cleanup:
|
302
|
+
|
303
|
+
git_remote_disconnect(remote);
|
304
|
+
git_strarray_free(&custom_headers);
|
305
|
+
|
306
|
+
if (payload.exception)
|
307
|
+
rb_jump_tag(payload.exception);
|
308
|
+
|
309
|
+
rugged_exception_check(error);
|
310
|
+
|
311
|
+
return Qnil;
|
312
|
+
}
|
313
|
+
|
314
|
+
/*
|
315
|
+
* call-seq:
|
316
|
+
* remote.name() -> string
|
317
|
+
*
|
318
|
+
* Returns the remote's name.
|
319
|
+
*
|
320
|
+
* remote.name #=> "origin"
|
321
|
+
*/
|
322
|
+
static VALUE rb_git_remote_name(VALUE self)
|
323
|
+
{
|
324
|
+
git_remote *remote;
|
325
|
+
const char * name;
|
326
|
+
Data_Get_Struct(self, git_remote, remote);
|
327
|
+
|
328
|
+
name = git_remote_name(remote);
|
329
|
+
|
330
|
+
return name ? rb_str_new_utf8(name) : Qnil;
|
331
|
+
}
|
332
|
+
|
333
|
+
/*
|
334
|
+
* call-seq:
|
335
|
+
* remote.url() -> string
|
336
|
+
*
|
337
|
+
* Returns the remote's url
|
338
|
+
*
|
339
|
+
* remote.url #=> "git://github.com/libgit2/rugged.git"
|
340
|
+
*/
|
341
|
+
static VALUE rb_git_remote_url(VALUE self)
|
342
|
+
{
|
343
|
+
git_remote *remote;
|
344
|
+
Data_Get_Struct(self, git_remote, remote);
|
345
|
+
|
346
|
+
return rb_str_new_utf8(git_remote_url(remote));
|
347
|
+
}
|
348
|
+
|
349
|
+
/*
|
350
|
+
* call-seq:
|
351
|
+
* remote.push_url() -> string or nil
|
352
|
+
*
|
353
|
+
* Returns the remote's url for pushing or nil if no special url for
|
354
|
+
* pushing is set.
|
355
|
+
*
|
356
|
+
* remote.push_url #=> "git://github.com/libgit2/rugged.git"
|
357
|
+
*/
|
358
|
+
static VALUE rb_git_remote_push_url(VALUE self)
|
359
|
+
{
|
360
|
+
git_remote *remote;
|
361
|
+
const char * push_url;
|
362
|
+
|
363
|
+
Data_Get_Struct(self, git_remote, remote);
|
364
|
+
|
365
|
+
push_url = git_remote_pushurl(remote);
|
366
|
+
return push_url ? rb_str_new_utf8(push_url) : Qnil;
|
367
|
+
}
|
368
|
+
|
369
|
+
/*
|
370
|
+
* call-seq:
|
371
|
+
* remote.push_url = url -> url
|
372
|
+
*
|
373
|
+
* Sets the remote's url for pushing without persisting it in the config.
|
374
|
+
* Existing connections will not be updated.
|
375
|
+
*
|
376
|
+
* remote.push_url = 'git@github.com/libgit2/rugged.git' #=> "git@github.com/libgit2/rugged.git"
|
377
|
+
*/
|
378
|
+
static VALUE rb_git_remote_set_push_url(VALUE self, VALUE rb_url)
|
379
|
+
{
|
380
|
+
VALUE rb_repo = rugged_owner(self);
|
381
|
+
git_remote *remote;
|
382
|
+
git_repository *repo;
|
383
|
+
|
384
|
+
rugged_check_repo(rb_repo);
|
385
|
+
Data_Get_Struct(rb_repo, git_repository, repo);
|
386
|
+
|
387
|
+
Check_Type(rb_url, T_STRING);
|
388
|
+
Data_Get_Struct(self, git_remote, remote);
|
389
|
+
|
390
|
+
rugged_exception_check(
|
391
|
+
git_remote_set_pushurl(repo, git_remote_name(remote), StringValueCStr(rb_url))
|
392
|
+
);
|
393
|
+
|
394
|
+
return rb_url;
|
395
|
+
}
|
396
|
+
|
397
|
+
static VALUE rb_git_remote_refspecs(VALUE self, git_direction direction)
|
398
|
+
{
|
399
|
+
git_remote *remote;
|
400
|
+
int error = 0;
|
401
|
+
git_strarray refspecs;
|
402
|
+
VALUE rb_refspec_array;
|
403
|
+
|
404
|
+
Data_Get_Struct(self, git_remote, remote);
|
405
|
+
|
406
|
+
if (direction == GIT_DIRECTION_FETCH)
|
407
|
+
error = git_remote_get_fetch_refspecs(&refspecs, remote);
|
408
|
+
else
|
409
|
+
error = git_remote_get_push_refspecs(&refspecs, remote);
|
410
|
+
|
411
|
+
rugged_exception_check(error);
|
412
|
+
|
413
|
+
rb_refspec_array = rugged_strarray_to_rb_ary(&refspecs);
|
414
|
+
git_strarray_free(&refspecs);
|
415
|
+
return rb_refspec_array;
|
416
|
+
}
|
417
|
+
|
418
|
+
/*
|
419
|
+
* call-seq:
|
420
|
+
* remote.fetch_refspecs -> array
|
421
|
+
*
|
422
|
+
* Get the remote's list of fetch refspecs as +array+.
|
423
|
+
*/
|
424
|
+
static VALUE rb_git_remote_fetch_refspecs(VALUE self)
|
425
|
+
{
|
426
|
+
return rb_git_remote_refspecs(self, GIT_DIRECTION_FETCH);
|
427
|
+
}
|
428
|
+
|
429
|
+
/*
|
430
|
+
* call-seq:
|
431
|
+
* remote.push_refspecs -> array
|
432
|
+
*
|
433
|
+
* Get the remote's list of push refspecs as +array+.
|
434
|
+
*/
|
435
|
+
static VALUE rb_git_remote_push_refspecs(VALUE self)
|
436
|
+
{
|
437
|
+
return rb_git_remote_refspecs(self, GIT_DIRECTION_PUSH);
|
438
|
+
}
|
439
|
+
|
440
|
+
/*
|
441
|
+
* call-seq:
|
442
|
+
* remote.check_connection(direction, options = {}) -> boolean
|
443
|
+
*
|
444
|
+
* Try to connect to the +remote+. Useful to simulate
|
445
|
+
* <tt>git fetch --dry-run</tt> and <tt>git push --dry-run</tt>.
|
446
|
+
*
|
447
|
+
* Returns +true+ if connection is successful, +false+ otherwise.
|
448
|
+
*
|
449
|
+
* +direction+ must be either +:fetch+ or +:push+.
|
450
|
+
*
|
451
|
+
* The following options can be passed in the +options+ Hash:
|
452
|
+
*
|
453
|
+
* +credentials+ ::
|
454
|
+
* The credentials to use for the connection. Can be either an instance of
|
455
|
+
* one of the Rugged::Credentials types, or a proc returning one of the
|
456
|
+
* former.
|
457
|
+
* The proc will be called with the +url+, the +username+ from the url (if
|
458
|
+
* applicable) and a list of applicable credential types.
|
459
|
+
*
|
460
|
+
* :headers ::
|
461
|
+
* Extra HTTP headers to include with the request (only applies to http:// or https:// remotes)
|
462
|
+
*
|
463
|
+
* Example:
|
464
|
+
*
|
465
|
+
* remote = repo.remotes["origin"]
|
466
|
+
* success = remote.check_connection(:fetch)
|
467
|
+
* raise Error("Unable to pull without credentials") unless success
|
468
|
+
*/
|
469
|
+
static VALUE rb_git_remote_check_connection(int argc, VALUE *argv, VALUE self)
|
470
|
+
{
|
471
|
+
git_remote *remote;
|
472
|
+
git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
|
473
|
+
git_strarray custom_headers = {0};
|
474
|
+
struct rugged_remote_cb_payload payload = { Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, 0 };
|
475
|
+
VALUE rb_direction, rb_options;
|
476
|
+
ID id_direction;
|
477
|
+
int error, direction;
|
478
|
+
|
479
|
+
Data_Get_Struct(self, git_remote, remote);
|
480
|
+
rb_scan_args(argc, argv, "01:", &rb_direction, &rb_options);
|
481
|
+
|
482
|
+
Check_Type(rb_direction, T_SYMBOL);
|
483
|
+
id_direction = SYM2ID(rb_direction);
|
484
|
+
if (id_direction == rb_intern("fetch"))
|
485
|
+
direction = GIT_DIRECTION_FETCH;
|
486
|
+
else if (id_direction == rb_intern("push"))
|
487
|
+
direction = GIT_DIRECTION_PUSH;
|
488
|
+
else
|
489
|
+
rb_raise(rb_eTypeError, "Invalid direction. Expected :fetch or :push");
|
490
|
+
|
491
|
+
rugged_remote_init_callbacks_and_payload_from_options(rb_options, &callbacks, &payload);
|
492
|
+
init_custom_headers(rb_options, &custom_headers);
|
493
|
+
|
494
|
+
error = git_remote_connect(remote, direction, &callbacks, &custom_headers);
|
495
|
+
git_remote_disconnect(remote);
|
496
|
+
|
497
|
+
git_strarray_free(&custom_headers);
|
498
|
+
|
499
|
+
if (payload.exception)
|
500
|
+
rb_jump_tag(payload.exception);
|
501
|
+
|
502
|
+
return error ? Qfalse : Qtrue;
|
503
|
+
}
|
504
|
+
|
505
|
+
/*
|
506
|
+
* call-seq:
|
507
|
+
* remote.fetch(refspecs = nil, options = {}) -> hash
|
508
|
+
*
|
509
|
+
* Downloads new data from the remote for the given +refspecs+ and updates tips.
|
510
|
+
*
|
511
|
+
* You can optionally pass in a single or multiple alternative +refspecs+ to use instead of the fetch
|
512
|
+
* refspecs already configured for +remote+.
|
513
|
+
*
|
514
|
+
* Returns a hash containing statistics for the fetch operation.
|
515
|
+
*
|
516
|
+
* The following options can be passed in the +options+ Hash:
|
517
|
+
*
|
518
|
+
* :credentials ::
|
519
|
+
* The credentials to use for the fetch operation. Can be either an instance of one
|
520
|
+
* of the Rugged::Credentials types, or a proc returning one of the former.
|
521
|
+
* The proc will be called with the +url+, the +username+ from the url (if applicable) and
|
522
|
+
* a list of applicable credential types.
|
523
|
+
*
|
524
|
+
* :headers ::
|
525
|
+
* Extra HTTP headers to include with the request (only applies to http:// or https:// remotes)
|
526
|
+
*
|
527
|
+
* :progress ::
|
528
|
+
* A callback that will be executed with the textual progress received from the remote.
|
529
|
+
* This is the text send over the progress side-band (ie. the "counting objects" output).
|
530
|
+
*
|
531
|
+
* :transfer_progress ::
|
532
|
+
* A callback that will be executed to report clone progress information. It will be passed
|
533
|
+
* the amount of +total_objects+, +indexed_objects+, +received_objects+, +local_objects+,
|
534
|
+
* +total_deltas+, +indexed_deltas+ and +received_bytes+.
|
535
|
+
*
|
536
|
+
* :update_tips ::
|
537
|
+
* A callback that will be executed each time a reference is updated locally. It will be
|
538
|
+
* passed the +refname+, +old_oid+ and +new_oid+.
|
539
|
+
*
|
540
|
+
* :message ::
|
541
|
+
* The message to insert into the reflogs. Defaults to "fetch".
|
542
|
+
*
|
543
|
+
* :prune ::
|
544
|
+
* Specifies the prune mode for the fetch. +true+ remove any remote-tracking references that
|
545
|
+
* no longer exist, +false+ do not prune, +nil+ use configured settings Defaults to "nil".
|
546
|
+
*
|
547
|
+
* Example:
|
548
|
+
*
|
549
|
+
* remote = Rugged::Remote.lookup(@repo, 'origin')
|
550
|
+
* remote.fetch({
|
551
|
+
* transfer_progress: lambda { |total_objects, indexed_objects, received_objects, local_objects, total_deltas, indexed_deltas, received_bytes|
|
552
|
+
* # ...
|
553
|
+
* }
|
554
|
+
* })
|
555
|
+
*/
|
556
|
+
static VALUE rb_git_remote_fetch(int argc, VALUE *argv, VALUE self)
|
557
|
+
{
|
558
|
+
git_remote *remote;
|
559
|
+
git_strarray refspecs;
|
560
|
+
git_fetch_options opts = GIT_FETCH_OPTIONS_INIT;
|
561
|
+
const git_transfer_progress *stats;
|
562
|
+
struct rugged_remote_cb_payload payload = { Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, 0 };
|
563
|
+
|
564
|
+
char *log_message = NULL;
|
565
|
+
int error;
|
566
|
+
|
567
|
+
VALUE rb_options, rb_refspecs, rb_result = Qnil;
|
568
|
+
|
569
|
+
rb_scan_args(argc, argv, "01:", &rb_refspecs, &rb_options);
|
570
|
+
|
571
|
+
rugged_rb_ary_to_strarray(rb_refspecs, &refspecs);
|
572
|
+
|
573
|
+
Data_Get_Struct(self, git_remote, remote);
|
574
|
+
|
575
|
+
rugged_remote_init_callbacks_and_payload_from_options(rb_options, &opts.callbacks, &payload);
|
576
|
+
init_custom_headers(rb_options, &opts.custom_headers);
|
577
|
+
|
578
|
+
if (!NIL_P(rb_options)) {
|
579
|
+
VALUE rb_prune_type;
|
580
|
+
VALUE rb_val = rb_hash_aref(rb_options, CSTR2SYM("message"));
|
581
|
+
|
582
|
+
if (!NIL_P(rb_val))
|
583
|
+
log_message = StringValueCStr(rb_val);
|
584
|
+
|
585
|
+
rb_prune_type = rb_hash_aref(rb_options, CSTR2SYM("prune"));
|
586
|
+
opts.prune = parse_prune_type(rb_prune_type);
|
587
|
+
}
|
588
|
+
|
589
|
+
error = git_remote_fetch(remote, &refspecs, &opts, log_message);
|
590
|
+
|
591
|
+
xfree(refspecs.strings);
|
592
|
+
git_strarray_free(&opts.custom_headers);
|
593
|
+
|
594
|
+
if (payload.exception)
|
595
|
+
rb_jump_tag(payload.exception);
|
596
|
+
|
597
|
+
rugged_exception_check(error);
|
598
|
+
|
599
|
+
stats = git_remote_stats(remote);
|
600
|
+
|
601
|
+
rb_result = rb_hash_new();
|
602
|
+
rb_hash_aset(rb_result, CSTR2SYM("total_objects"), UINT2NUM(stats->total_objects));
|
603
|
+
rb_hash_aset(rb_result, CSTR2SYM("indexed_objects"), UINT2NUM(stats->indexed_objects));
|
604
|
+
rb_hash_aset(rb_result, CSTR2SYM("received_objects"), UINT2NUM(stats->received_objects));
|
605
|
+
rb_hash_aset(rb_result, CSTR2SYM("local_objects"), UINT2NUM(stats->local_objects));
|
606
|
+
rb_hash_aset(rb_result, CSTR2SYM("total_deltas"), UINT2NUM(stats->total_deltas));
|
607
|
+
rb_hash_aset(rb_result, CSTR2SYM("indexed_deltas"), UINT2NUM(stats->indexed_deltas));
|
608
|
+
rb_hash_aset(rb_result, CSTR2SYM("received_bytes"), INT2FIX(stats->received_bytes));
|
609
|
+
|
610
|
+
return rb_result;
|
611
|
+
}
|
612
|
+
|
613
|
+
/*
|
614
|
+
* call-seq:
|
615
|
+
* remote.push(refspecs = nil, options = {}) -> hash
|
616
|
+
*
|
617
|
+
* Pushes the given +refspecs+ to the given +remote+. Returns a hash that contains
|
618
|
+
* key-value pairs that reflect pushed refs and error messages, if applicable.
|
619
|
+
*
|
620
|
+
* You can optionally pass in an alternative list of +refspecs+ to use instead of the push
|
621
|
+
* refspecs already configured for +remote+.
|
622
|
+
*
|
623
|
+
* The following options can be passed in the +options+ Hash:
|
624
|
+
*
|
625
|
+
* :credentials ::
|
626
|
+
* The credentials to use for the push operation. Can be either an instance of one
|
627
|
+
* of the Rugged::Credentials types, or a proc returning one of the former.
|
628
|
+
* The proc will be called with the +url+, the +username+ from the url (if applicable) and
|
629
|
+
* a list of applicable credential types.
|
630
|
+
*
|
631
|
+
* :update_tips ::
|
632
|
+
* A callback that will be executed each time a reference is updated remotely. It will be
|
633
|
+
* passed the +refname+, +old_oid+ and +new_oid+.
|
634
|
+
*
|
635
|
+
* :headers ::
|
636
|
+
* Extra HTTP headers to include with the push (only applies to http:// or https:// remotes)
|
637
|
+
*
|
638
|
+
* Example:
|
639
|
+
*
|
640
|
+
* remote = Rugged::Remote.lookup(@repo, 'origin')
|
641
|
+
* remote.push(["refs/heads/master", ":refs/heads/to_be_deleted"])
|
642
|
+
*/
|
643
|
+
static VALUE rb_git_remote_push(int argc, VALUE *argv, VALUE self)
|
644
|
+
{
|
645
|
+
VALUE rb_refspecs, rb_options;
|
646
|
+
|
647
|
+
git_remote *remote;
|
648
|
+
git_strarray refspecs;
|
649
|
+
git_push_options opts = GIT_PUSH_OPTIONS_INIT;
|
650
|
+
|
651
|
+
int error = 0;
|
652
|
+
|
653
|
+
struct rugged_remote_cb_payload payload = { Qnil, Qnil, Qnil, Qnil, Qnil, rb_hash_new(), 0 };
|
654
|
+
|
655
|
+
rb_scan_args(argc, argv, "01:", &rb_refspecs, &rb_options);
|
656
|
+
|
657
|
+
rugged_rb_ary_to_strarray(rb_refspecs, &refspecs);
|
658
|
+
|
659
|
+
Data_Get_Struct(self, git_remote, remote);
|
660
|
+
|
661
|
+
rugged_remote_init_callbacks_and_payload_from_options(rb_options, &opts.callbacks, &payload);
|
662
|
+
init_custom_headers(rb_options, &opts.custom_headers);
|
663
|
+
|
664
|
+
error = git_remote_push(remote, &refspecs, &opts);
|
665
|
+
|
666
|
+
xfree(refspecs.strings);
|
667
|
+
git_strarray_free(&opts.custom_headers);
|
668
|
+
|
669
|
+
if (payload.exception)
|
670
|
+
rb_jump_tag(payload.exception);
|
671
|
+
|
672
|
+
rugged_exception_check(error);
|
673
|
+
|
674
|
+
return payload.result;
|
675
|
+
}
|
676
|
+
|
677
|
+
void Init_rugged_remote(void)
|
678
|
+
{
|
679
|
+
rb_cRuggedRemote = rb_define_class_under(rb_mRugged, "Remote", rb_cObject);
|
680
|
+
|
681
|
+
rb_define_method(rb_cRuggedRemote, "name", rb_git_remote_name, 0);
|
682
|
+
rb_define_method(rb_cRuggedRemote, "url", rb_git_remote_url, 0);
|
683
|
+
rb_define_method(rb_cRuggedRemote, "push_url", rb_git_remote_push_url, 0);
|
684
|
+
rb_define_method(rb_cRuggedRemote, "push_url=", rb_git_remote_set_push_url, 1);
|
685
|
+
rb_define_method(rb_cRuggedRemote, "fetch_refspecs", rb_git_remote_fetch_refspecs, 0);
|
686
|
+
rb_define_method(rb_cRuggedRemote, "push_refspecs", rb_git_remote_push_refspecs, 0);
|
687
|
+
rb_define_method(rb_cRuggedRemote, "ls", rb_git_remote_ls, -1);
|
688
|
+
rb_define_method(rb_cRuggedRemote, "check_connection", rb_git_remote_check_connection, -1);
|
689
|
+
rb_define_method(rb_cRuggedRemote, "fetch", rb_git_remote_fetch, -1);
|
690
|
+
rb_define_method(rb_cRuggedRemote, "push", rb_git_remote_push, -1);
|
691
|
+
}
|