Skip to content

Commit f01fd3f

Browse files
committed
rebase: replace RebaseOperation with a hash
This seems more idiomatic. The hash will have the relevant fields for the kind of rebase operation.
1 parent d45e556 commit f01fd3f

File tree

2 files changed

+53
-88
lines changed

2 files changed

+53
-88
lines changed

ext/rugged/rugged_rebase.c

Lines changed: 47 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ extern VALUE rb_cRuggedIndex;
2929
extern VALUE rb_cRuggedRepo;
3030

3131
VALUE rb_cRuggedRebase;
32-
VALUE rb_cRuggedRebaseOperation;
3332

34-
VALUE ruged_rebase_operation_new(git_rebase_operation *operation, VALUE owner);
33+
static VALUE rebase_operation_type(git_rebase_operation *operation);
3534

3635
static void parse_rebase_options(git_rebase_options *ret, VALUE rb_options)
3736
{
@@ -143,16 +142,35 @@ static VALUE rb_git_rebase_new(int argc, VALUE* argv, VALUE klass)
143142

144143
/*
145144
* call-seq:
146-
* Rebase.next() -> RebaseOperation
145+
* Rebase.next() -> operation or nil
147146
*
148-
* Perform the next step in the rebase. The returned operation gives
149-
* its details.
147+
* Perform the next step in the rebase. The returned operation is a
148+
* Hash with its details or nil if there are no more operations to
149+
* perform. The Hash contains some of the following entries:
150+
*
151+
* :type ::
152+
* The type of operation being done. Can be one of +:pick+,
153+
* +:reword+, +:edit+, +:squash+, +:fixup+ or +:exec+.
154+
*
155+
* :id ::
156+
* The id of the commit being cherry-picked. Exists for all but
157+
* +:exec+ operations.
158+
*
159+
* :index ::
160+
* If the rebase is +:inmemory+ this is in the resulting merge
161+
* index. It can be used to resolve merge conflicts during the
162+
* rebase.
163+
*
164+
* :exec ::
165+
* If the operatin is +:exec+ this is what the user asked to be
166+
* executed.
150167
*/
151168
static VALUE rb_git_rebase_next(VALUE self)
152169
{
153170
int error;
154171
git_rebase *rebase;
155172
git_rebase_operation *operation;
173+
VALUE hash, val;
156174

157175
Data_Get_Struct(self, git_rebase, rebase);
158176
error = git_rebase_next(&operation, rebase);
@@ -161,7 +179,29 @@ static VALUE rb_git_rebase_next(VALUE self)
161179

162180
rugged_exception_check(error);
163181

164-
return ruged_rebase_operation_new(operation, self);
182+
/* Create the operation hash out of the relevant details */
183+
hash = rb_hash_new();
184+
185+
val = rebase_operation_type(operation);
186+
rb_hash_aset(hash, CSTR2SYM("type"), val);
187+
188+
if (operation->type != GIT_REBASE_OPERATION_EXEC) {
189+
val = rugged_create_oid(&operation->id);
190+
rb_hash_aset(hash, CSTR2SYM("id"), val);
191+
}
192+
193+
if (operation->index) {
194+
val = Data_Wrap_Struct(rb_cRuggedIndex, NULL, NULL, operation->index);
195+
rugged_set_owner(val, self);
196+
rb_hash_aset(hash, CSTR2SYM("index"), val);
197+
}
198+
199+
if (operation->exec) {
200+
val = rb_str_new_utf8(operation->exec);
201+
rb_hash_aset(hash, CSTR2SYM("exec"), val);
202+
}
203+
204+
return hash;
165205
}
166206

167207
/*
@@ -251,27 +291,9 @@ static VALUE rb_git_rebase_finish(VALUE self, VALUE rb_sig)
251291
return Qnil;
252292
}
253293

254-
VALUE ruged_rebase_operation_new(git_rebase_operation *operation, VALUE owner)
255-
{
256-
VALUE rb_rebase = Data_Wrap_Struct(rb_cRuggedRebaseOperation, NULL, NULL, operation);
257-
rugged_set_owner(rb_rebase, owner);
258-
259-
return rb_rebase;
260-
}
261-
262-
/*
263-
* call-seq:
264-
* RebaseOperation.type -> symbol
265-
*
266-
* The type of operation being done. Can be one of +:pick+,
267-
* +:reword+, +:edit+, +:squash+, +:fixup+ or +:exec+.
268-
*
269-
*/static VALUE rb_git_rebase_operation_type(VALUE self)
294+
static VALUE rebase_operation_type(git_rebase_operation *operation)
270295
{
271296
VALUE rb_type;
272-
git_rebase_operation *operation;
273-
274-
Data_Get_Struct(self, git_rebase_operation, operation);
275297

276298
switch (operation->type) {
277299
case GIT_REBASE_OPERATION_PICK:
@@ -300,58 +322,6 @@ VALUE ruged_rebase_operation_new(git_rebase_operation *operation, VALUE owner)
300322
return rb_type;
301323
}
302324

303-
/*
304-
* call-seq:
305-
* RebaseOperation.id -> oid
306-
*
307-
* The id of the commit being cherry-picked. Exists for all but
308-
* +:exec+ operations.
309-
*/
310-
static VALUE rb_git_rebase_operation_id(VALUE self)
311-
{
312-
git_rebase_operation *operation;
313-
314-
Data_Get_Struct(self, git_rebase_operation, operation);
315-
316-
return rugged_create_oid(&operation->id);
317-
}
318-
319-
/*
320-
* call-seq:
321-
* RebaseOperation.index -> Index
322-
*
323-
* The index that is the result of an opration. Only valid for
324-
* +:inmemory+ rebases.
325-
*/
326-
static VALUE rb_git_rebase_operation_index(VALUE self)
327-
{
328-
git_rebase_operation *operation;
329-
VALUE rb_index;
330-
331-
Data_Get_Struct(self, git_rebase_operation, operation);
332-
333-
rb_index = Data_Wrap_Struct(rb_cRuggedIndex, NULL, NULL, operation->index);
334-
rugged_set_owner(rb_index, self);
335-
336-
return rb_index;
337-
}
338-
339-
/*
340-
* call-seq:
341-
* RebaseOperation.exec -> str
342-
*
343-
* The executable the user has requested to run. Only exists for
344-
* +:exec+ operations.
345-
*/
346-
static VALUE rb_git_rebase_operation_exec(VALUE self)
347-
{
348-
git_rebase_operation *operation;
349-
350-
Data_Get_Struct(self, git_rebase_operation, operation);
351-
352-
return rb_str_new_utf8(operation->exec ? operation->exec : "");
353-
}
354-
355325
void Init_rugged_rebase(void)
356326
{
357327
rb_cRuggedRebase = rb_define_class_under(rb_mRugged, "Rebase", rb_cObject);
@@ -361,12 +331,6 @@ void Init_rugged_rebase(void)
361331
rb_define_method(rb_cRuggedRebase, "commit", rb_git_rebase_commit, -1);
362332
rb_define_method(rb_cRuggedRebase, "abort", rb_git_rebase_abort, 0);
363333
rb_define_method(rb_cRuggedRebase, "finish", rb_git_rebase_finish, 1);
364-
365-
rb_cRuggedRebaseOperation = rb_define_class_under(rb_mRugged, "RebaseOperation", rb_cObject);
366-
rb_define_method(rb_cRuggedRebaseOperation, "type", rb_git_rebase_operation_type, 0);
367-
rb_define_method(rb_cRuggedRebaseOperation, "id", rb_git_rebase_operation_id, 0);
368-
rb_define_method(rb_cRuggedRebaseOperation, "index", rb_git_rebase_operation_index, 0);
369-
rb_define_method(rb_cRuggedRebaseOperation, "exec", rb_git_rebase_operation_exec, 0);
370334
}
371335

372336

test/rebase_test.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ def test_merge_next
1515
rebase = Rugged::Rebase.new(@repo, "refs/heads/beef", "refs/heads/master")
1616

1717
op = rebase.next()
18-
assert_equal :pick, op.type
19-
assert_equal "da9c51a23d02d931a486f45ad18cda05cf5d2b94", op.id
18+
assert_equal :pick, op[:type]
19+
assert_equal "da9c51a23d02d931a486f45ad18cda05cf5d2b94", op[:id]
2020

2121
statuses = []
2222
@repo.status do |file, status|
@@ -50,10 +50,11 @@ def test_inmemory_resolve_conflicts
5050
rebase = Rugged::Rebase.new(@repo, "refs/heads/asparagus", "refs/heads/master", inmemory: true)
5151

5252
op = rebase.next()
53-
idx = op.index
53+
idx = op[:index]
5454

55-
assert_equal :pick, op.type
56-
assert_equal "33f915f9e4dbd9f4b24430e48731a59b45b15500", op.id
55+
assert idx
56+
assert_equal :pick, op[:type]
57+
assert_equal "33f915f9e4dbd9f4b24430e48731a59b45b15500", op[:id]
5758
assert !@repo.index.conflicts?
5859
assert idx.conflicts?
5960

0 commit comments

Comments
 (0)