Skip to content

Commit 336c590

Browse files
Merge pull request #516 from spraints/custom-push-headers
Add custom headers to smart http requests
2 parents fb17354 + 796e17b commit 336c590

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

ext/rugged/rugged_remote.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ void rugged_remote_init_callbacks_and_payload_from_options(
181181
}
182182
}
183183

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+
184193
static int parse_prune_type(VALUE rb_prune_type)
185194
{
186195
if (rb_prune_type == Qtrue) {
@@ -254,11 +263,15 @@ static VALUE rugged_rhead_new(const git_remote_head *head)
254263
* of the Rugged::Credentials types, or a proc returning one of the former.
255264
* The proc will be called with the +url+, the +username+ from the url (if applicable) and
256265
* 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)
257269
*/
258270
static VALUE rb_git_remote_ls(int argc, VALUE *argv, VALUE self)
259271
{
260272
git_remote *remote;
261273
git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
274+
git_strarray custom_headers = {0};
262275
const git_remote_head **heads;
263276

264277
struct rugged_remote_cb_payload payload = { Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, 0 };
@@ -276,8 +289,9 @@ static VALUE rb_git_remote_ls(int argc, VALUE *argv, VALUE self)
276289
return rb_funcall(self, rb_intern("to_enum"), 2, CSTR2SYM("ls"), rb_options);
277290

278291
rugged_remote_init_callbacks_and_payload_from_options(rb_options, &callbacks, &payload);
292+
init_custom_headers(rb_options, &custom_headers);
279293

280-
if ((error = git_remote_connect(remote, GIT_DIRECTION_FETCH, &callbacks, NULL)) ||
294+
if ((error = git_remote_connect(remote, GIT_DIRECTION_FETCH, &callbacks, &custom_headers)) ||
281295
(error = git_remote_ls(&heads, &heads_len, remote)))
282296
goto cleanup;
283297

@@ -287,6 +301,7 @@ static VALUE rb_git_remote_ls(int argc, VALUE *argv, VALUE self)
287301
cleanup:
288302

289303
git_remote_disconnect(remote);
304+
git_strarray_free(&custom_headers);
290305

291306
if (payload.exception)
292307
rb_jump_tag(payload.exception);
@@ -442,6 +457,9 @@ static VALUE rb_git_remote_push_refspecs(VALUE self)
442457
* The proc will be called with the +url+, the +username+ from the url (if
443458
* applicable) and a list of applicable credential types.
444459
*
460+
* :headers ::
461+
* Extra HTTP headers to include with the request (only applies to http:// or https:// remotes)
462+
*
445463
* Example:
446464
*
447465
* remote = repo.remotes["origin"]
@@ -452,6 +470,7 @@ static VALUE rb_git_remote_check_connection(int argc, VALUE *argv, VALUE self)
452470
{
453471
git_remote *remote;
454472
git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
473+
git_strarray custom_headers = {0};
455474
struct rugged_remote_cb_payload payload = { Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, 0 };
456475
VALUE rb_direction, rb_options;
457476
ID id_direction;
@@ -470,10 +489,13 @@ static VALUE rb_git_remote_check_connection(int argc, VALUE *argv, VALUE self)
470489
rb_raise(rb_eTypeError, "Invalid direction. Expected :fetch or :push");
471490

472491
rugged_remote_init_callbacks_and_payload_from_options(rb_options, &callbacks, &payload);
492+
init_custom_headers(rb_options, &custom_headers);
473493

474-
error = git_remote_connect(remote, direction, &callbacks, NULL);
494+
error = git_remote_connect(remote, direction, &callbacks, &custom_headers);
475495
git_remote_disconnect(remote);
476496

497+
git_strarray_free(&custom_headers);
498+
477499
if (payload.exception)
478500
rb_jump_tag(payload.exception);
479501

@@ -499,6 +521,9 @@ static VALUE rb_git_remote_check_connection(int argc, VALUE *argv, VALUE self)
499521
* The proc will be called with the +url+, the +username+ from the url (if applicable) and
500522
* a list of applicable credential types.
501523
*
524+
* :headers ::
525+
* Extra HTTP headers to include with the request (only applies to http:// or https:// remotes)
526+
*
502527
* :progress ::
503528
* A callback that will be executed with the textual progress received from the remote.
504529
* This is the text send over the progress side-band (ie. the "counting objects" output).
@@ -548,6 +573,7 @@ static VALUE rb_git_remote_fetch(int argc, VALUE *argv, VALUE self)
548573
Data_Get_Struct(self, git_remote, remote);
549574

550575
rugged_remote_init_callbacks_and_payload_from_options(rb_options, &opts.callbacks, &payload);
576+
init_custom_headers(rb_options, &opts.custom_headers);
551577

552578
if (!NIL_P(rb_options)) {
553579
VALUE rb_val = rb_hash_aref(rb_options, CSTR2SYM("message"));
@@ -561,6 +587,7 @@ static VALUE rb_git_remote_fetch(int argc, VALUE *argv, VALUE self)
561587
error = git_remote_fetch(remote, &refspecs, &opts, log_message);
562588

563589
xfree(refspecs.strings);
590+
git_strarray_free(&opts.custom_headers);
564591

565592
if (payload.exception)
566593
rb_jump_tag(payload.exception);
@@ -603,6 +630,9 @@ static VALUE rb_git_remote_fetch(int argc, VALUE *argv, VALUE self)
603630
* A callback that will be executed each time a reference is updated remotely. It will be
604631
* passed the +refname+, +old_oid+ and +new_oid+.
605632
*
633+
* :headers ::
634+
* Extra HTTP headers to include with the push (only applies to http:// or https:// remotes)
635+
*
606636
* Example:
607637
*
608638
* remote = Rugged::Remote.lookup(@repo, 'origin')
@@ -627,10 +657,12 @@ static VALUE rb_git_remote_push(int argc, VALUE *argv, VALUE self)
627657
Data_Get_Struct(self, git_remote, remote);
628658

629659
rugged_remote_init_callbacks_and_payload_from_options(rb_options, &opts.callbacks, &payload);
660+
init_custom_headers(rb_options, &opts.custom_headers);
630661

631662
error = git_remote_push(remote, &refspecs, &opts);
632663

633664
xfree(refspecs.strings);
665+
git_strarray_free(&opts.custom_headers);
634666

635667
if (payload.exception)
636668
rb_jump_tag(payload.exception);

0 commit comments

Comments
 (0)