Skip to content

Commit 3c1b1e7

Browse files
Clean up callback setup.
1 parent 21d549c commit 3c1b1e7

File tree

1 file changed

+34
-38
lines changed

1 file changed

+34
-38
lines changed

ext/rugged/rugged_remote.c

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ extern VALUE rb_cRuggedRepo;
2929
extern VALUE rb_eRuggedError;
3030
VALUE rb_cRuggedRemote;
3131

32-
#define RUGGED_REMOTE_CALLBACKS_INIT {1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, push_update_reference_cb, NULL}
33-
3432
static int progress_cb(const char *str, int len, void *data)
3533
{
3634
struct rugged_remote_cb_payload *payload = data;
@@ -176,52 +174,50 @@ static int credentials_cb(
176174
return payload->exception ? GIT_ERROR : GIT_OK;
177175
}
178176

179-
#define CALLABLE_OR_RAISE(ret, rb_options, name) \
180-
do { \
181-
ret = rb_hash_aref(rb_options, CSTR2SYM(name)); \
182-
\
183-
if (!NIL_P(ret) && !rb_respond_to(ret, rb_intern("call"))) \
177+
#define CALLABLE_OR_RAISE(ret, name) \
178+
do { \
179+
if (!rb_respond_to(ret, rb_intern("call"))) \
184180
rb_raise(rb_eArgError, "Expected a Proc or an object that responds to #call (:" name " )."); \
185181
} while (0);
186182

187-
static void setup_callbacks(git_remote_callbacks *callbacks, VALUE rb_options)
188-
{
189-
if (NIL_P(rb_options))
190-
return;
191-
192-
if (!NIL_P(rb_hash_aref(rb_options, CSTR2SYM("progress"))))
193-
callbacks->sideband_progress = progress_cb;
194-
195-
if (!NIL_P(rb_hash_aref(rb_options, CSTR2SYM("credentials"))))
196-
callbacks->credentials = credentials_cb;
197-
198-
if (!NIL_P(rb_hash_aref(rb_options, CSTR2SYM("certificate_check"))))
199-
callbacks->certificate_check = certificate_check_cb;
200-
201-
if (!NIL_P(rb_hash_aref(rb_options, CSTR2SYM("transfer_progress"))))
202-
callbacks->transfer_progress = transfer_progress_cb;
203-
204-
if (!NIL_P(rb_hash_aref(rb_options, CSTR2SYM("update_tips"))))
205-
callbacks->update_tips = update_tips_cb;
206-
}
207-
208183
void rugged_remote_init_callbacks_and_payload_from_options(
209184
VALUE rb_options,
210185
git_remote_callbacks *callbacks,
211186
struct rugged_remote_cb_payload *payload)
212187
{
213-
git_remote_callbacks prefilled = RUGGED_REMOTE_CALLBACKS_INIT;
214-
215-
prefilled.payload = payload;
216-
memcpy(callbacks, &prefilled, sizeof(git_remote_callbacks));
217-
setup_callbacks(callbacks, rb_options);
188+
callbacks->payload = payload;
189+
callbacks->push_update_reference = push_update_reference_cb;
218190

219191
if (!NIL_P(rb_options)) {
220-
CALLABLE_OR_RAISE(payload->certificate_check, rb_options, "certificate_check");
221-
CALLABLE_OR_RAISE(payload->update_tips, rb_options, "update_tips");
222-
CALLABLE_OR_RAISE(payload->progress, rb_options, "progress");
223-
CALLABLE_OR_RAISE(payload->transfer_progress, rb_options, "transfer_progress");
224-
CALLABLE_OR_RAISE(payload->credentials, rb_options, "credentials");
192+
payload->progress = rb_hash_aref(rb_options, CSTR2SYM("progress"));
193+
if (!NIL_P(payload->progress)) {
194+
CALLABLE_OR_RAISE(payload->progress, "progress");
195+
callbacks->sideband_progress = progress_cb;
196+
}
197+
198+
payload->credentials = rb_hash_aref(rb_options, CSTR2SYM("credentials"));
199+
if (!NIL_P(payload->credentials)) {
200+
CALLABLE_OR_RAISE(payload->credentials, "credentials");
201+
callbacks->credentials = credentials_cb;
202+
}
203+
204+
payload->certificate_check = rb_hash_aref(rb_options, CSTR2SYM("certificate_check"));
205+
if (!NIL_P(payload->certificate_check)) {
206+
CALLABLE_OR_RAISE(payload->certificate_check, "certificate_check");
207+
callbacks->certificate_check = certificate_check_cb;
208+
}
209+
210+
payload->transfer_progress = rb_hash_aref(rb_options, CSTR2SYM("transfer_progress"));
211+
if (!NIL_P(payload->transfer_progress)) {
212+
CALLABLE_OR_RAISE(payload->transfer_progress, "transfer_progress");
213+
callbacks->transfer_progress = transfer_progress_cb;
214+
}
215+
216+
payload->update_tips = rb_hash_aref(rb_options, CSTR2SYM("update_tips"));
217+
if (!NIL_P(payload->update_tips)) {
218+
CALLABLE_OR_RAISE(payload->update_tips, "update_tips");
219+
callbacks->update_tips = update_tips_cb;
220+
}
225221
}
226222
}
227223

0 commit comments

Comments
 (0)