// This is a generated file, modify: generate/templates/templates/struct_content.cc

// generated from struct_content.cc
#include <nan.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif // win32

extern "C" {
  #include <git2.h>
 }

#include <iostream>
#include "../include/nodegit.h"
#include "../include/lock_master.h"
#include "../include/functions/copy.h"
#include "../include/remote_callbacks.h"
#include "nodegit_wrapper.cc"

  #include "../include/credential.h"
  #include "../include/cert.h"
  #include "../include/indexer_progress.h"
  #include "../include/buf.h"
 
using namespace v8;
using namespace node;
using namespace std;

 
ConfigurableGitRemoteCallbacks::ConfigurableGitRemoteCallbacks(nodegit::Context *nodegitContext)
  : nodegit::ConfigurableClassWrapper<GitRemoteCallbacksTraits>(nodegitContext)
{
     git_remote_callbacks  wrappedValue = GIT_REMOTE_CALLBACKS_INIT;
    this->raw = (git_remote_callbacks*) malloc(sizeof(git_remote_callbacks ));
    memcpy(this->raw, &wrappedValue, sizeof(git_remote_callbacks ));
 }

ConfigurableGitRemoteCallbacks::~ConfigurableGitRemoteCallbacks() {
                   }

nodegit::ConfigurableClassWrapper<GitRemoteCallbacksTraits>::v8ConversionResult ConfigurableGitRemoteCallbacks::fromJavascript(nodegit::Context *nodegitContext, v8::Local<v8::Value> input) {
  if (!input->IsObject()) {
    return {
      "Must pass object for ConfigurableGitRemoteCallbacks"
    };
  }

  Nan::HandleScope scope;
  v8::Local<v8::Object> inputObj = input.As<v8::Object>();
  std::shared_ptr<ConfigurableGitRemoteCallbacks> output(new ConfigurableGitRemoteCallbacks(nodegitContext));

  // unpack the data into the correct fields
         {
          v8::Local<v8::Value> maybeNumber = nodegit::safeGetField(inputObj, "version");
          if (!maybeNumber.IsEmpty() && !maybeNumber->IsUndefined() && !maybeNumber->IsNull()) {
            if (!maybeNumber->IsNumber()) {
              return {
                "Must pass Int32 to version"
              };
            }

            output->raw->version = static_cast<unsigned int>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
          {
          v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(inputObj, "sidebandProgress");
          if (!maybeCallback.IsEmpty() && !maybeCallback->IsUndefined() && !maybeCallback->IsNull()) {
            if (!maybeCallback->IsFunction() && !maybeCallback->IsObject()) {
              return {
                "Must pass Function or CallbackSpecifier to sidebandProgress"
              };
            }

            std::unique_ptr<Nan::Callback> callback;
            uint32_t throttle =  0 ;
            bool waitForResult = true;

            if (maybeCallback->IsFunction()) {
              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));
            } else {
              v8::Local<v8::Object> callbackSpecifier = maybeCallback.As<v8::Object>();
              v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(callbackSpecifier, "callback");
              if (maybeCallback.IsEmpty() || !maybeCallback->IsFunction()) {
                return {
                  "Must pass callback to CallbackSpecifier"
                };
              }

              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));

              v8::Local<v8::Value> maybeThrottle = nodegit::safeGetField(callbackSpecifier, "throttle");
              if (!maybeThrottle.IsEmpty() && !maybeThrottle->IsUndefined() && !maybeThrottle->IsNull()) {
                if (!maybeThrottle->IsNumber()) {
                  return {
                    "Must pass zero or positive number as throttle to CallbackSpecifier"
                  };
                }

                throttle = maybeThrottle->Uint32Value(Nan::GetCurrentContext()).FromJust();
              }

              v8::Local<v8::Value> maybeWaitForResult = nodegit::safeGetField(callbackSpecifier, "waitForResult");
              if (!maybeWaitForResult.IsEmpty() && !maybeWaitForResult->IsUndefined() && !maybeWaitForResult->IsNull()) {
                if (!maybeWaitForResult->IsBoolean()) {
                  return {
                    "Must pass a boolean as waitForResult to callbackSpecifier"
                  };
                }

                waitForResult = Nan::To<bool>(maybeWaitForResult).FromJust();
              }
            }

            output->sidebandProgress.SetCallback(std::move(callback), throttle, waitForResult);
            output->raw->sideband_progress = (git_transport_message_cb)sidebandProgress_cppCallback;
          }
        }
          {
          v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(inputObj, "credentials");
          if (!maybeCallback.IsEmpty() && !maybeCallback->IsUndefined() && !maybeCallback->IsNull()) {
            if (!maybeCallback->IsFunction() && !maybeCallback->IsObject()) {
              return {
                "Must pass Function or CallbackSpecifier to credentials"
              };
            }

            std::unique_ptr<Nan::Callback> callback;
            uint32_t throttle =  0 ;
            bool waitForResult = true;

            if (maybeCallback->IsFunction()) {
              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));
            } else {
              v8::Local<v8::Object> callbackSpecifier = maybeCallback.As<v8::Object>();
              v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(callbackSpecifier, "callback");
              if (maybeCallback.IsEmpty() || !maybeCallback->IsFunction()) {
                return {
                  "Must pass callback to CallbackSpecifier"
                };
              }

              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));

              v8::Local<v8::Value> maybeThrottle = nodegit::safeGetField(callbackSpecifier, "throttle");
              if (!maybeThrottle.IsEmpty() && !maybeThrottle->IsUndefined() && !maybeThrottle->IsNull()) {
                if (!maybeThrottle->IsNumber()) {
                  return {
                    "Must pass zero or positive number as throttle to CallbackSpecifier"
                  };
                }

                throttle = maybeThrottle->Uint32Value(Nan::GetCurrentContext()).FromJust();
              }

              v8::Local<v8::Value> maybeWaitForResult = nodegit::safeGetField(callbackSpecifier, "waitForResult");
              if (!maybeWaitForResult.IsEmpty() && !maybeWaitForResult->IsUndefined() && !maybeWaitForResult->IsNull()) {
                if (!maybeWaitForResult->IsBoolean()) {
                  return {
                    "Must pass a boolean as waitForResult to callbackSpecifier"
                  };
                }

                waitForResult = Nan::To<bool>(maybeWaitForResult).FromJust();
              }
            }

            output->credentials.SetCallback(std::move(callback), throttle, waitForResult);
            output->raw->credentials = (git_credential_acquire_cb)credentials_cppCallback;
          }
        }
          {
          v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(inputObj, "certificateCheck");
          if (!maybeCallback.IsEmpty() && !maybeCallback->IsUndefined() && !maybeCallback->IsNull()) {
            if (!maybeCallback->IsFunction() && !maybeCallback->IsObject()) {
              return {
                "Must pass Function or CallbackSpecifier to certificateCheck"
              };
            }

            std::unique_ptr<Nan::Callback> callback;
            uint32_t throttle =  0 ;
            bool waitForResult = true;

            if (maybeCallback->IsFunction()) {
              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));
            } else {
              v8::Local<v8::Object> callbackSpecifier = maybeCallback.As<v8::Object>();
              v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(callbackSpecifier, "callback");
              if (maybeCallback.IsEmpty() || !maybeCallback->IsFunction()) {
                return {
                  "Must pass callback to CallbackSpecifier"
                };
              }

              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));

              v8::Local<v8::Value> maybeThrottle = nodegit::safeGetField(callbackSpecifier, "throttle");
              if (!maybeThrottle.IsEmpty() && !maybeThrottle->IsUndefined() && !maybeThrottle->IsNull()) {
                if (!maybeThrottle->IsNumber()) {
                  return {
                    "Must pass zero or positive number as throttle to CallbackSpecifier"
                  };
                }

                throttle = maybeThrottle->Uint32Value(Nan::GetCurrentContext()).FromJust();
              }

              v8::Local<v8::Value> maybeWaitForResult = nodegit::safeGetField(callbackSpecifier, "waitForResult");
              if (!maybeWaitForResult.IsEmpty() && !maybeWaitForResult->IsUndefined() && !maybeWaitForResult->IsNull()) {
                if (!maybeWaitForResult->IsBoolean()) {
                  return {
                    "Must pass a boolean as waitForResult to callbackSpecifier"
                  };
                }

                waitForResult = Nan::To<bool>(maybeWaitForResult).FromJust();
              }
            }

            output->certificateCheck.SetCallback(std::move(callback), throttle, waitForResult);
            output->raw->certificate_check = (git_transport_certificate_check_cb)certificateCheck_cppCallback;
          }
        }
          {
          v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(inputObj, "transferProgress");
          if (!maybeCallback.IsEmpty() && !maybeCallback->IsUndefined() && !maybeCallback->IsNull()) {
            if (!maybeCallback->IsFunction() && !maybeCallback->IsObject()) {
              return {
                "Must pass Function or CallbackSpecifier to transferProgress"
              };
            }

            std::unique_ptr<Nan::Callback> callback;
            uint32_t throttle = 100 ;
            bool waitForResult = true;

            if (maybeCallback->IsFunction()) {
              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));
            } else {
              v8::Local<v8::Object> callbackSpecifier = maybeCallback.As<v8::Object>();
              v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(callbackSpecifier, "callback");
              if (maybeCallback.IsEmpty() || !maybeCallback->IsFunction()) {
                return {
                  "Must pass callback to CallbackSpecifier"
                };
              }

              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));

              v8::Local<v8::Value> maybeThrottle = nodegit::safeGetField(callbackSpecifier, "throttle");
              if (!maybeThrottle.IsEmpty() && !maybeThrottle->IsUndefined() && !maybeThrottle->IsNull()) {
                if (!maybeThrottle->IsNumber()) {
                  return {
                    "Must pass zero or positive number as throttle to CallbackSpecifier"
                  };
                }

                throttle = maybeThrottle->Uint32Value(Nan::GetCurrentContext()).FromJust();
              }

              v8::Local<v8::Value> maybeWaitForResult = nodegit::safeGetField(callbackSpecifier, "waitForResult");
              if (!maybeWaitForResult.IsEmpty() && !maybeWaitForResult->IsUndefined() && !maybeWaitForResult->IsNull()) {
                if (!maybeWaitForResult->IsBoolean()) {
                  return {
                    "Must pass a boolean as waitForResult to callbackSpecifier"
                  };
                }

                waitForResult = Nan::To<bool>(maybeWaitForResult).FromJust();
              }
            }

            output->transferProgress.SetCallback(std::move(callback), throttle, waitForResult);
            output->raw->transfer_progress = (git_indexer_progress_cb)transferProgress_cppCallback;
          }
        }
          {
          v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(inputObj, "pushTransferProgress");
          if (!maybeCallback.IsEmpty() && !maybeCallback->IsUndefined() && !maybeCallback->IsNull()) {
            if (!maybeCallback->IsFunction() && !maybeCallback->IsObject()) {
              return {
                "Must pass Function or CallbackSpecifier to pushTransferProgress"
              };
            }

            std::unique_ptr<Nan::Callback> callback;
            uint32_t throttle = 100 ;
            bool waitForResult = true;

            if (maybeCallback->IsFunction()) {
              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));
            } else {
              v8::Local<v8::Object> callbackSpecifier = maybeCallback.As<v8::Object>();
              v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(callbackSpecifier, "callback");
              if (maybeCallback.IsEmpty() || !maybeCallback->IsFunction()) {
                return {
                  "Must pass callback to CallbackSpecifier"
                };
              }

              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));

              v8::Local<v8::Value> maybeThrottle = nodegit::safeGetField(callbackSpecifier, "throttle");
              if (!maybeThrottle.IsEmpty() && !maybeThrottle->IsUndefined() && !maybeThrottle->IsNull()) {
                if (!maybeThrottle->IsNumber()) {
                  return {
                    "Must pass zero or positive number as throttle to CallbackSpecifier"
                  };
                }

                throttle = maybeThrottle->Uint32Value(Nan::GetCurrentContext()).FromJust();
              }

              v8::Local<v8::Value> maybeWaitForResult = nodegit::safeGetField(callbackSpecifier, "waitForResult");
              if (!maybeWaitForResult.IsEmpty() && !maybeWaitForResult->IsUndefined() && !maybeWaitForResult->IsNull()) {
                if (!maybeWaitForResult->IsBoolean()) {
                  return {
                    "Must pass a boolean as waitForResult to callbackSpecifier"
                  };
                }

                waitForResult = Nan::To<bool>(maybeWaitForResult).FromJust();
              }
            }

            output->pushTransferProgress.SetCallback(std::move(callback), throttle, waitForResult);
            output->raw->push_transfer_progress = (git_push_transfer_progress_cb)pushTransferProgress_cppCallback;
          }
        }
          {
          v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(inputObj, "pushUpdateReference");
          if (!maybeCallback.IsEmpty() && !maybeCallback->IsUndefined() && !maybeCallback->IsNull()) {
            if (!maybeCallback->IsFunction() && !maybeCallback->IsObject()) {
              return {
                "Must pass Function or CallbackSpecifier to pushUpdateReference"
              };
            }

            std::unique_ptr<Nan::Callback> callback;
            uint32_t throttle =  0 ;
            bool waitForResult = true;

            if (maybeCallback->IsFunction()) {
              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));
            } else {
              v8::Local<v8::Object> callbackSpecifier = maybeCallback.As<v8::Object>();
              v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(callbackSpecifier, "callback");
              if (maybeCallback.IsEmpty() || !maybeCallback->IsFunction()) {
                return {
                  "Must pass callback to CallbackSpecifier"
                };
              }

              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));

              v8::Local<v8::Value> maybeThrottle = nodegit::safeGetField(callbackSpecifier, "throttle");
              if (!maybeThrottle.IsEmpty() && !maybeThrottle->IsUndefined() && !maybeThrottle->IsNull()) {
                if (!maybeThrottle->IsNumber()) {
                  return {
                    "Must pass zero or positive number as throttle to CallbackSpecifier"
                  };
                }

                throttle = maybeThrottle->Uint32Value(Nan::GetCurrentContext()).FromJust();
              }

              v8::Local<v8::Value> maybeWaitForResult = nodegit::safeGetField(callbackSpecifier, "waitForResult");
              if (!maybeWaitForResult.IsEmpty() && !maybeWaitForResult->IsUndefined() && !maybeWaitForResult->IsNull()) {
                if (!maybeWaitForResult->IsBoolean()) {
                  return {
                    "Must pass a boolean as waitForResult to callbackSpecifier"
                  };
                }

                waitForResult = Nan::To<bool>(maybeWaitForResult).FromJust();
              }
            }

            output->pushUpdateReference.SetCallback(std::move(callback), throttle, waitForResult);
            output->raw->push_update_reference = (git_push_update_reference_cb)pushUpdateReference_cppCallback;
          }
        }
          output->raw->payload = (void *)output.get();
          {
          v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(inputObj, "resolveUrl");
          if (!maybeCallback.IsEmpty() && !maybeCallback->IsUndefined() && !maybeCallback->IsNull()) {
            if (!maybeCallback->IsFunction() && !maybeCallback->IsObject()) {
              return {
                "Must pass Function or CallbackSpecifier to resolveUrl"
              };
            }

            std::unique_ptr<Nan::Callback> callback;
            uint32_t throttle =  0 ;
            bool waitForResult = true;

            if (maybeCallback->IsFunction()) {
              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));
            } else {
              v8::Local<v8::Object> callbackSpecifier = maybeCallback.As<v8::Object>();
              v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(callbackSpecifier, "callback");
              if (maybeCallback.IsEmpty() || !maybeCallback->IsFunction()) {
                return {
                  "Must pass callback to CallbackSpecifier"
                };
              }

              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));

              v8::Local<v8::Value> maybeThrottle = nodegit::safeGetField(callbackSpecifier, "throttle");
              if (!maybeThrottle.IsEmpty() && !maybeThrottle->IsUndefined() && !maybeThrottle->IsNull()) {
                if (!maybeThrottle->IsNumber()) {
                  return {
                    "Must pass zero or positive number as throttle to CallbackSpecifier"
                  };
                }

                throttle = maybeThrottle->Uint32Value(Nan::GetCurrentContext()).FromJust();
              }

              v8::Local<v8::Value> maybeWaitForResult = nodegit::safeGetField(callbackSpecifier, "waitForResult");
              if (!maybeWaitForResult.IsEmpty() && !maybeWaitForResult->IsUndefined() && !maybeWaitForResult->IsNull()) {
                if (!maybeWaitForResult->IsBoolean()) {
                  return {
                    "Must pass a boolean as waitForResult to callbackSpecifier"
                  };
                }

                waitForResult = Nan::To<bool>(maybeWaitForResult).FromJust();
              }
            }

            output->resolveUrl.SetCallback(std::move(callback), throttle, waitForResult);
            output->raw->resolve_url = (git_url_resolve_cb)resolveUrl_cppCallback;
          }
        }
    
  return {
    output
  };
}

        ConfigurableGitRemoteCallbacks* ConfigurableGitRemoteCallbacks::sidebandProgress_getInstanceFromBaton(SidebandProgressBaton* baton) {
           return static_cast<ConfigurableGitRemoteCallbacks*>(baton->
                payload
  );
       }

      int ConfigurableGitRemoteCallbacks::sidebandProgress_cppCallback (
          const char * str,           int len,           void * payload        ) {
        SidebandProgressBaton *baton =
          new SidebandProgressBaton(1);

          baton->str = str;
          baton->len = len;
          baton->payload = payload;
 
        ConfigurableGitRemoteCallbacks* instance = sidebandProgress_getInstanceFromBaton(baton);

           int result;

          if (instance->nodegitContext != nodegit::ThreadPool::GetCurrentContext()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->sidebandProgress.WillBeThrottled()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->sidebandProgress.ShouldWaitForResult()) {
            result = baton->ExecuteAsync(sidebandProgress_async, sidebandProgress_cancelAsync);
            delete baton;
          } else {
            result = baton->defaultResult;
            baton->ExecuteAsync(sidebandProgress_async, sidebandProgress_cancelAsync, nodegit::deleteBaton);
          }
          return result;
       }

      void ConfigurableGitRemoteCallbacks::sidebandProgress_cancelAsync(void *untypedBaton) {
        SidebandProgressBaton* baton = static_cast<SidebandProgressBaton*>(untypedBaton);
          baton->result = -1;
         baton->Done();
      }

      void ConfigurableGitRemoteCallbacks::sidebandProgress_async(void *untypedBaton) {
        Nan::HandleScope scope;

        SidebandProgressBaton* baton = static_cast<SidebandProgressBaton*>(untypedBaton);
        ConfigurableGitRemoteCallbacks* instance = sidebandProgress_getInstanceFromBaton(baton);

        if (instance->sidebandProgress.GetCallback()->IsEmpty()) {
            baton->result = baton->defaultResult; // no results acquired
           baton->Done();
          return;
        }

           v8::Local<Value> argv[2] = {
               baton->str == NULL
                ? Nan::EmptyString()
                : Nan::New( baton->str).ToLocalChecked()
 ,                Nan::New(baton->len)
            };
 
        Nan::TryCatch tryCatch;

        Nan::MaybeLocal<v8::Value> maybeResult = (*(instance->sidebandProgress.GetCallback()))(
          baton->GetAsyncResource(),
          2,
          argv
        );
        v8::Local<v8::Value> result;
        if (!maybeResult.IsEmpty()) {
          result = maybeResult.ToLocalChecked();
        }

        if (PromiseCompletion::ForwardIfPromise(result, baton, ConfigurableGitRemoteCallbacks::sidebandProgress_promiseCompleted)) {
          return;
        }

             if (result.IsEmpty() || result->IsNativeError()) {
              baton->result = -1;
            }
            else if (!result->IsNull() && !result->IsUndefined()) {
               if (result->IsNumber()) {
                baton->result = Nan::To<int>(result).FromJust();
              }
              else {
                baton->result = baton->defaultResult;
              }
             }
            else {
              baton->result = baton->defaultResult;
            }
           baton->Done();
       }

      void ConfigurableGitRemoteCallbacks::sidebandProgress_promiseCompleted(bool isFulfilled, nodegit::AsyncBaton *_baton, v8::Local<v8::Value> result) {
        Nan::HandleScope scope;

        SidebandProgressBaton* baton = static_cast<SidebandProgressBaton*>(_baton);
           if (isFulfilled) {
              if (result.IsEmpty() || result->IsNativeError()) {
                baton->result = -1;
              }
              else if (!result->IsNull() && !result->IsUndefined()) {
                 if (result->IsNumber()) {
                  baton->result = Nan::To<int>(result).FromJust();
                }
                else{
                  baton->result = baton->defaultResult;
                }
               }
              else {
                baton->result = baton->defaultResult;
              }
           }
          else {
            // promise was rejected
               ConfigurableGitRemoteCallbacks* instance = static_cast<ConfigurableGitRemoteCallbacks*>(baton->  payload  );
             baton->SetCallbackError(result);
            baton->result = -1;
          }
          baton->Done();
       }
        ConfigurableGitRemoteCallbacks* ConfigurableGitRemoteCallbacks::credentials_getInstanceFromBaton(CredentialsBaton* baton) {
           return static_cast<ConfigurableGitRemoteCallbacks*>(baton->
                  payload
  );
       }

      int ConfigurableGitRemoteCallbacks::credentials_cppCallback (
          git_credential ** credential,           const char * url,           const char * username_from_url,           unsigned int allowed_types,           void * payload        ) {
        CredentialsBaton *baton =
          new CredentialsBaton(1);

          baton->credential = credential;
          baton->url = url;
          baton->username_from_url = username_from_url;
          baton->allowed_types = allowed_types;
          baton->payload = payload;
 
        ConfigurableGitRemoteCallbacks* instance = credentials_getInstanceFromBaton(baton);

           int result;

          if (instance->nodegitContext != nodegit::ThreadPool::GetCurrentContext()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->credentials.WillBeThrottled()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->credentials.ShouldWaitForResult()) {
            result = baton->ExecuteAsync(credentials_async, credentials_cancelAsync);
            delete baton;
          } else {
            result = baton->defaultResult;
            baton->ExecuteAsync(credentials_async, credentials_cancelAsync, nodegit::deleteBaton);
          }
          return result;
       }

      void ConfigurableGitRemoteCallbacks::credentials_cancelAsync(void *untypedBaton) {
        CredentialsBaton* baton = static_cast<CredentialsBaton*>(untypedBaton);
          baton->result = -1;
         baton->Done();
      }

      void ConfigurableGitRemoteCallbacks::credentials_async(void *untypedBaton) {
        Nan::HandleScope scope;

        CredentialsBaton* baton = static_cast<CredentialsBaton*>(untypedBaton);
        ConfigurableGitRemoteCallbacks* instance = credentials_getInstanceFromBaton(baton);

        if (instance->credentials.GetCallback()->IsEmpty()) {
            baton->result = baton->defaultResult; // no results acquired
           baton->Done();
          return;
        }

           v8::Local<Value> argv[3] = {
               baton->url == NULL
                ? Nan::EmptyString()
                : Nan::New( baton->url).ToLocalChecked()
 ,               baton->username_from_url == NULL
                ? Nan::EmptyString()
                : Nan::New( baton->username_from_url).ToLocalChecked()
 ,                Nan::New(baton->allowed_types)
            };
 
        Nan::TryCatch tryCatch;

        Nan::MaybeLocal<v8::Value> maybeResult = (*(instance->credentials.GetCallback()))(
          baton->GetAsyncResource(),
          3,
          argv
        );
        v8::Local<v8::Value> result;
        if (!maybeResult.IsEmpty()) {
          result = maybeResult.ToLocalChecked();
        }

        if (PromiseCompletion::ForwardIfPromise(result, baton, ConfigurableGitRemoteCallbacks::credentials_promiseCompleted)) {
          return;
        }

             if (result.IsEmpty() || result->IsNativeError()) {
              baton->result = -1;
            }
            else if (!result->IsNull() && !result->IsUndefined()) {
              GitCredential* wrapper = Nan::ObjectWrap::Unwrap<GitCredential>(Nan::To<v8::Object>(result).ToLocalChecked());
              wrapper->selfFreeing = false;

              *baton->credential = wrapper->GetValue();
              baton->result = 0;
             }
            else {
              baton->result = baton->defaultResult;
            }
           baton->Done();
       }

      void ConfigurableGitRemoteCallbacks::credentials_promiseCompleted(bool isFulfilled, nodegit::AsyncBaton *_baton, v8::Local<v8::Value> result) {
        Nan::HandleScope scope;

        CredentialsBaton* baton = static_cast<CredentialsBaton*>(_baton);
           if (isFulfilled) {
              if (result.IsEmpty() || result->IsNativeError()) {
                baton->result = -1;
              }
              else if (!result->IsNull() && !result->IsUndefined()) {
                GitCredential* wrapper = Nan::ObjectWrap::Unwrap<GitCredential>(Nan::To<v8::Object>(result).ToLocalChecked());
                wrapper->selfFreeing = false;

                *baton->credential = wrapper->GetValue();
                baton->result = 0;
               }
              else {
                baton->result = baton->defaultResult;
              }
           }
          else {
            // promise was rejected
               ConfigurableGitRemoteCallbacks* instance = static_cast<ConfigurableGitRemoteCallbacks*>(baton->    payload  );
             baton->SetCallbackError(result);
            baton->result = -1;
          }
          baton->Done();
       }
        ConfigurableGitRemoteCallbacks* ConfigurableGitRemoteCallbacks::certificateCheck_getInstanceFromBaton(CertificateCheckBaton* baton) {
           return static_cast<ConfigurableGitRemoteCallbacks*>(baton->
                 payload
  );
       }

      int ConfigurableGitRemoteCallbacks::certificateCheck_cppCallback (
          git_cert * cert,           int valid,           const char * host,           void * payload        ) {
        CertificateCheckBaton *baton =
          new CertificateCheckBaton(1);

          baton->cert = cert;
          baton->valid = valid;
          baton->host = host;
          baton->payload = payload;
 
        ConfigurableGitRemoteCallbacks* instance = certificateCheck_getInstanceFromBaton(baton);

           int result;

          if (instance->nodegitContext != nodegit::ThreadPool::GetCurrentContext()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->certificateCheck.WillBeThrottled()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->certificateCheck.ShouldWaitForResult()) {
            result = baton->ExecuteAsync(certificateCheck_async, certificateCheck_cancelAsync);
            delete baton;
          } else {
            result = baton->defaultResult;
            baton->ExecuteAsync(certificateCheck_async, certificateCheck_cancelAsync, nodegit::deleteBaton);
          }
          return result;
       }

      void ConfigurableGitRemoteCallbacks::certificateCheck_cancelAsync(void *untypedBaton) {
        CertificateCheckBaton* baton = static_cast<CertificateCheckBaton*>(untypedBaton);
          baton->result = -1;
         baton->Done();
      }

      void ConfigurableGitRemoteCallbacks::certificateCheck_async(void *untypedBaton) {
        Nan::HandleScope scope;

        CertificateCheckBaton* baton = static_cast<CertificateCheckBaton*>(untypedBaton);
        ConfigurableGitRemoteCallbacks* instance = certificateCheck_getInstanceFromBaton(baton);

        if (instance->certificateCheck.GetCallback()->IsEmpty()) {
            baton->result = baton->defaultResult; // no results acquired
           baton->Done();
          return;
        }

           v8::Local<Value> argv[3] = {
               GitCert::New(baton->cert, false)
 ,                Nan::New(baton->valid)
 ,               baton->host == NULL
                ? Nan::EmptyString()
                : Nan::New( baton->host).ToLocalChecked()
            };
 
        Nan::TryCatch tryCatch;

        Nan::MaybeLocal<v8::Value> maybeResult = (*(instance->certificateCheck.GetCallback()))(
          baton->GetAsyncResource(),
          3,
          argv
        );
        v8::Local<v8::Value> result;
        if (!maybeResult.IsEmpty()) {
          result = maybeResult.ToLocalChecked();
        }

        if (PromiseCompletion::ForwardIfPromise(result, baton, ConfigurableGitRemoteCallbacks::certificateCheck_promiseCompleted)) {
          return;
        }

             if (result.IsEmpty() || result->IsNativeError()) {
              baton->result = -1;
            }
            else if (!result->IsNull() && !result->IsUndefined()) {
               if (result->IsNumber()) {
                baton->result = Nan::To<int>(result).FromJust();
              }
              else {
                baton->result = baton->defaultResult;
              }
             }
            else {
              baton->result = baton->defaultResult;
            }
           baton->Done();
       }

      void ConfigurableGitRemoteCallbacks::certificateCheck_promiseCompleted(bool isFulfilled, nodegit::AsyncBaton *_baton, v8::Local<v8::Value> result) {
        Nan::HandleScope scope;

        CertificateCheckBaton* baton = static_cast<CertificateCheckBaton*>(_baton);
           if (isFulfilled) {
              if (result.IsEmpty() || result->IsNativeError()) {
                baton->result = -1;
              }
              else if (!result->IsNull() && !result->IsUndefined()) {
                 if (result->IsNumber()) {
                  baton->result = Nan::To<int>(result).FromJust();
                }
                else{
                  baton->result = baton->defaultResult;
                }
               }
              else {
                baton->result = baton->defaultResult;
              }
           }
          else {
            // promise was rejected
               ConfigurableGitRemoteCallbacks* instance = static_cast<ConfigurableGitRemoteCallbacks*>(baton->   payload  );
             baton->SetCallbackError(result);
            baton->result = -1;
          }
          baton->Done();
       }
        ConfigurableGitRemoteCallbacks* ConfigurableGitRemoteCallbacks::transferProgress_getInstanceFromBaton(TransferProgressBaton* baton) {
           return static_cast<ConfigurableGitRemoteCallbacks*>(baton->
               payload
  );
       }

      int ConfigurableGitRemoteCallbacks::transferProgress_cppCallback (
          const git_indexer_progress * stats,           void * payload        ) {
        TransferProgressBaton *baton =
          new TransferProgressBaton(0);

          baton->stats = stats;
          baton->payload = payload;
 
        ConfigurableGitRemoteCallbacks* instance = transferProgress_getInstanceFromBaton(baton);

           int result;

          if (instance->nodegitContext != nodegit::ThreadPool::GetCurrentContext()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->transferProgress.WillBeThrottled()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->transferProgress.ShouldWaitForResult()) {
            result = baton->ExecuteAsync(transferProgress_async, transferProgress_cancelAsync);
            delete baton;
          } else {
            result = baton->defaultResult;
            baton->ExecuteAsync(transferProgress_async, transferProgress_cancelAsync, nodegit::deleteBaton);
          }
          return result;
       }

      void ConfigurableGitRemoteCallbacks::transferProgress_cancelAsync(void *untypedBaton) {
        TransferProgressBaton* baton = static_cast<TransferProgressBaton*>(untypedBaton);
          baton->result = -1;
         baton->Done();
      }

      void ConfigurableGitRemoteCallbacks::transferProgress_async(void *untypedBaton) {
        Nan::HandleScope scope;

        TransferProgressBaton* baton = static_cast<TransferProgressBaton*>(untypedBaton);
        ConfigurableGitRemoteCallbacks* instance = transferProgress_getInstanceFromBaton(baton);

        if (instance->transferProgress.GetCallback()->IsEmpty()) {
            baton->result = baton->defaultResult; // no results acquired
           baton->Done();
          return;
        }

           v8::Local<Value> argv[1] = {
               GitIndexerProgress::New(baton->stats, false)
            };
 
        Nan::TryCatch tryCatch;

        Nan::MaybeLocal<v8::Value> maybeResult = (*(instance->transferProgress.GetCallback()))(
          baton->GetAsyncResource(),
          1,
          argv
        );
        v8::Local<v8::Value> result;
        if (!maybeResult.IsEmpty()) {
          result = maybeResult.ToLocalChecked();
        }

        if (PromiseCompletion::ForwardIfPromise(result, baton, ConfigurableGitRemoteCallbacks::transferProgress_promiseCompleted)) {
          return;
        }

             if (result.IsEmpty() || result->IsNativeError()) {
              baton->result = -1;
            }
            else if (!result->IsNull() && !result->IsUndefined()) {
               if (result->IsNumber()) {
                baton->result = Nan::To<int>(result).FromJust();
              }
              else {
                baton->result = baton->defaultResult;
              }
             }
            else {
              baton->result = baton->defaultResult;
            }
           baton->Done();
       }

      void ConfigurableGitRemoteCallbacks::transferProgress_promiseCompleted(bool isFulfilled, nodegit::AsyncBaton *_baton, v8::Local<v8::Value> result) {
        Nan::HandleScope scope;

        TransferProgressBaton* baton = static_cast<TransferProgressBaton*>(_baton);
           if (isFulfilled) {
              if (result.IsEmpty() || result->IsNativeError()) {
                baton->result = -1;
              }
              else if (!result->IsNull() && !result->IsUndefined()) {
                 if (result->IsNumber()) {
                  baton->result = Nan::To<int>(result).FromJust();
                }
                else{
                  baton->result = baton->defaultResult;
                }
               }
              else {
                baton->result = baton->defaultResult;
              }
           }
          else {
            // promise was rejected
               ConfigurableGitRemoteCallbacks* instance = static_cast<ConfigurableGitRemoteCallbacks*>(baton-> payload  );
             baton->SetCallbackError(result);
            baton->result = -1;
          }
          baton->Done();
       }
        ConfigurableGitRemoteCallbacks* ConfigurableGitRemoteCallbacks::pushTransferProgress_getInstanceFromBaton(PushTransferProgressBaton* baton) {
           return static_cast<ConfigurableGitRemoteCallbacks*>(baton->
                 payload
  );
       }

      int ConfigurableGitRemoteCallbacks::pushTransferProgress_cppCallback (
          unsigned int current,           unsigned int total,           size_t bytes,           void * payload        ) {
        PushTransferProgressBaton *baton =
          new PushTransferProgressBaton(0);

          baton->current = current;
          baton->total = total;
          baton->bytes = bytes;
          baton->payload = payload;
 
        ConfigurableGitRemoteCallbacks* instance = pushTransferProgress_getInstanceFromBaton(baton);

           int result;

          if (instance->nodegitContext != nodegit::ThreadPool::GetCurrentContext()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->pushTransferProgress.WillBeThrottled()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->pushTransferProgress.ShouldWaitForResult()) {
            result = baton->ExecuteAsync(pushTransferProgress_async, pushTransferProgress_cancelAsync);
            delete baton;
          } else {
            result = baton->defaultResult;
            baton->ExecuteAsync(pushTransferProgress_async, pushTransferProgress_cancelAsync, nodegit::deleteBaton);
          }
          return result;
       }

      void ConfigurableGitRemoteCallbacks::pushTransferProgress_cancelAsync(void *untypedBaton) {
        PushTransferProgressBaton* baton = static_cast<PushTransferProgressBaton*>(untypedBaton);
          baton->result = -1;
         baton->Done();
      }

      void ConfigurableGitRemoteCallbacks::pushTransferProgress_async(void *untypedBaton) {
        Nan::HandleScope scope;

        PushTransferProgressBaton* baton = static_cast<PushTransferProgressBaton*>(untypedBaton);
        ConfigurableGitRemoteCallbacks* instance = pushTransferProgress_getInstanceFromBaton(baton);

        if (instance->pushTransferProgress.GetCallback()->IsEmpty()) {
            baton->result = baton->defaultResult; // no results acquired
           baton->Done();
          return;
        }

           v8::Local<Value> argv[3] = {
                Nan::New(baton->current)
 ,                Nan::New(baton->total)
 ,               // HACK: NAN should really have an overload for Nan::New to support size_t
              Nan::New((unsigned int)baton->bytes)
            };
 
        Nan::TryCatch tryCatch;

        Nan::MaybeLocal<v8::Value> maybeResult = (*(instance->pushTransferProgress.GetCallback()))(
          baton->GetAsyncResource(),
          3,
          argv
        );
        v8::Local<v8::Value> result;
        if (!maybeResult.IsEmpty()) {
          result = maybeResult.ToLocalChecked();
        }

        if (PromiseCompletion::ForwardIfPromise(result, baton, ConfigurableGitRemoteCallbacks::pushTransferProgress_promiseCompleted)) {
          return;
        }

             if (result.IsEmpty() || result->IsNativeError()) {
              baton->result = -1;
            }
            else if (!result->IsNull() && !result->IsUndefined()) {
               if (result->IsNumber()) {
                baton->result = Nan::To<int>(result).FromJust();
              }
              else {
                baton->result = baton->defaultResult;
              }
             }
            else {
              baton->result = baton->defaultResult;
            }
           baton->Done();
       }

      void ConfigurableGitRemoteCallbacks::pushTransferProgress_promiseCompleted(bool isFulfilled, nodegit::AsyncBaton *_baton, v8::Local<v8::Value> result) {
        Nan::HandleScope scope;

        PushTransferProgressBaton* baton = static_cast<PushTransferProgressBaton*>(_baton);
           if (isFulfilled) {
              if (result.IsEmpty() || result->IsNativeError()) {
                baton->result = -1;
              }
              else if (!result->IsNull() && !result->IsUndefined()) {
                 if (result->IsNumber()) {
                  baton->result = Nan::To<int>(result).FromJust();
                }
                else{
                  baton->result = baton->defaultResult;
                }
               }
              else {
                baton->result = baton->defaultResult;
              }
           }
          else {
            // promise was rejected
               ConfigurableGitRemoteCallbacks* instance = static_cast<ConfigurableGitRemoteCallbacks*>(baton->   payload  );
             baton->SetCallbackError(result);
            baton->result = -1;
          }
          baton->Done();
       }
        ConfigurableGitRemoteCallbacks* ConfigurableGitRemoteCallbacks::pushUpdateReference_getInstanceFromBaton(PushUpdateReferenceBaton* baton) {
           return static_cast<ConfigurableGitRemoteCallbacks*>(baton->
                data
  );
       }

      int ConfigurableGitRemoteCallbacks::pushUpdateReference_cppCallback (
          const char * refname,           const char * status,           void * data        ) {
        PushUpdateReferenceBaton *baton =
          new PushUpdateReferenceBaton(1);

          baton->refname = refname;
          baton->status = status;
          baton->data = data;
 
        ConfigurableGitRemoteCallbacks* instance = pushUpdateReference_getInstanceFromBaton(baton);

           int result;

          if (instance->nodegitContext != nodegit::ThreadPool::GetCurrentContext()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->pushUpdateReference.WillBeThrottled()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->pushUpdateReference.ShouldWaitForResult()) {
            result = baton->ExecuteAsync(pushUpdateReference_async, pushUpdateReference_cancelAsync);
            delete baton;
          } else {
            result = baton->defaultResult;
            baton->ExecuteAsync(pushUpdateReference_async, pushUpdateReference_cancelAsync, nodegit::deleteBaton);
          }
          return result;
       }

      void ConfigurableGitRemoteCallbacks::pushUpdateReference_cancelAsync(void *untypedBaton) {
        PushUpdateReferenceBaton* baton = static_cast<PushUpdateReferenceBaton*>(untypedBaton);
          baton->result = -1;
         baton->Done();
      }

      void ConfigurableGitRemoteCallbacks::pushUpdateReference_async(void *untypedBaton) {
        Nan::HandleScope scope;

        PushUpdateReferenceBaton* baton = static_cast<PushUpdateReferenceBaton*>(untypedBaton);
        ConfigurableGitRemoteCallbacks* instance = pushUpdateReference_getInstanceFromBaton(baton);

        if (instance->pushUpdateReference.GetCallback()->IsEmpty()) {
            baton->result = baton->defaultResult; // no results acquired
           baton->Done();
          return;
        }

           v8::Local<Value> argv[3] = {
               baton->refname == NULL
                ? Nan::EmptyString()
                : Nan::New( baton->refname).ToLocalChecked()
 ,               baton->status == NULL
                ? Nan::EmptyString()
                : Nan::New( baton->status).ToLocalChecked()
 ,                Nan::New(baton->data)
            };
 
        Nan::TryCatch tryCatch;

        Nan::MaybeLocal<v8::Value> maybeResult = (*(instance->pushUpdateReference.GetCallback()))(
          baton->GetAsyncResource(),
          3,
          argv
        );
        v8::Local<v8::Value> result;
        if (!maybeResult.IsEmpty()) {
          result = maybeResult.ToLocalChecked();
        }

        if (PromiseCompletion::ForwardIfPromise(result, baton, ConfigurableGitRemoteCallbacks::pushUpdateReference_promiseCompleted)) {
          return;
        }

             if (result.IsEmpty() || result->IsNativeError()) {
              baton->result = -1;
            }
            else if (!result->IsNull() && !result->IsUndefined()) {
               if (result->IsNumber()) {
                baton->result = Nan::To<int>(result).FromJust();
              }
              else {
                baton->result = baton->defaultResult;
              }
             }
            else {
              baton->result = baton->defaultResult;
            }
           baton->Done();
       }

      void ConfigurableGitRemoteCallbacks::pushUpdateReference_promiseCompleted(bool isFulfilled, nodegit::AsyncBaton *_baton, v8::Local<v8::Value> result) {
        Nan::HandleScope scope;

        PushUpdateReferenceBaton* baton = static_cast<PushUpdateReferenceBaton*>(_baton);
           if (isFulfilled) {
              if (result.IsEmpty() || result->IsNativeError()) {
                baton->result = -1;
              }
              else if (!result->IsNull() && !result->IsUndefined()) {
                 if (result->IsNumber()) {
                  baton->result = Nan::To<int>(result).FromJust();
                }
                else{
                  baton->result = baton->defaultResult;
                }
               }
              else {
                baton->result = baton->defaultResult;
              }
           }
          else {
            // promise was rejected
               ConfigurableGitRemoteCallbacks* instance = static_cast<ConfigurableGitRemoteCallbacks*>(baton->  data  );
             baton->SetCallbackError(result);
            baton->result = -1;
          }
          baton->Done();
       }
          ConfigurableGitRemoteCallbacks* ConfigurableGitRemoteCallbacks::resolveUrl_getInstanceFromBaton(ResolveUrlBaton* baton) {
           return static_cast<ConfigurableGitRemoteCallbacks*>(baton->
                 payload
  );
       }

      int ConfigurableGitRemoteCallbacks::resolveUrl_cppCallback (
          git_buf * url_resolved,           const char * url,           int direction,           void * payload        ) {
        ResolveUrlBaton *baton =
          new ResolveUrlBaton(-30);

          baton->url_resolved = url_resolved;
          baton->url = url;
          baton->direction = direction;
          baton->payload = payload;
 
        ConfigurableGitRemoteCallbacks* instance = resolveUrl_getInstanceFromBaton(baton);

           int result;

          if (instance->nodegitContext != nodegit::ThreadPool::GetCurrentContext()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->resolveUrl.WillBeThrottled()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->resolveUrl.ShouldWaitForResult()) {
            result = baton->ExecuteAsync(resolveUrl_async, resolveUrl_cancelAsync);
            delete baton;
          } else {
            result = baton->defaultResult;
            baton->ExecuteAsync(resolveUrl_async, resolveUrl_cancelAsync, nodegit::deleteBaton);
          }
          return result;
       }

      void ConfigurableGitRemoteCallbacks::resolveUrl_cancelAsync(void *untypedBaton) {
        ResolveUrlBaton* baton = static_cast<ResolveUrlBaton*>(untypedBaton);
          baton->result = -30;
         baton->Done();
      }

      void ConfigurableGitRemoteCallbacks::resolveUrl_async(void *untypedBaton) {
        Nan::HandleScope scope;

        ResolveUrlBaton* baton = static_cast<ResolveUrlBaton*>(untypedBaton);
        ConfigurableGitRemoteCallbacks* instance = resolveUrl_getInstanceFromBaton(baton);

        if (instance->resolveUrl.GetCallback()->IsEmpty()) {
            baton->result = baton->defaultResult; // no results acquired
           baton->Done();
          return;
        }

           v8::Local<Value> argv[3] = {
               GitBuf::New(baton->url_resolved, false)
 ,               baton->url == NULL
                ? Nan::EmptyString()
                : Nan::New( baton->url).ToLocalChecked()
 ,                Nan::New(baton->direction)
            };
 
        Nan::TryCatch tryCatch;

        Nan::MaybeLocal<v8::Value> maybeResult = (*(instance->resolveUrl.GetCallback()))(
          baton->GetAsyncResource(),
          3,
          argv
        );
        v8::Local<v8::Value> result;
        if (!maybeResult.IsEmpty()) {
          result = maybeResult.ToLocalChecked();
        }

        if (PromiseCompletion::ForwardIfPromise(result, baton, ConfigurableGitRemoteCallbacks::resolveUrl_promiseCompleted)) {
          return;
        }

             if (result.IsEmpty() || result->IsNativeError()) {
              baton->result = -1;
            }
            else if (!result->IsNull() && !result->IsUndefined()) {
               if (result->IsNumber()) {
                baton->result = Nan::To<int>(result).FromJust();
              }
              else {
                baton->result = baton->defaultResult;
              }
             }
            else {
              baton->result = baton->defaultResult;
            }
           baton->Done();
       }

      void ConfigurableGitRemoteCallbacks::resolveUrl_promiseCompleted(bool isFulfilled, nodegit::AsyncBaton *_baton, v8::Local<v8::Value> result) {
        Nan::HandleScope scope;

        ResolveUrlBaton* baton = static_cast<ResolveUrlBaton*>(_baton);
           if (isFulfilled) {
              if (result.IsEmpty() || result->IsNativeError()) {
                baton->result = -1;
              }
              else if (!result->IsNull() && !result->IsUndefined()) {
                 if (result->IsNumber()) {
                  baton->result = Nan::To<int>(result).FromJust();
                }
                else{
                  baton->result = baton->defaultResult;
                }
               }
              else {
                baton->result = baton->defaultResult;
              }
           }
          else {
            // promise was rejected
               ConfigurableGitRemoteCallbacks* instance = static_cast<ConfigurableGitRemoteCallbacks*>(baton->   payload  );
             baton->SetCallbackError(result);
            baton->result = -1;
          }
          baton->Done();
       }
   
// force base class template instantiation, to make sure we get all the
// methods, statics, etc.
template class nodegit::ConfigurableClassWrapper<GitRemoteCallbacksTraits>;
