// This is a generated file, modify: generate/templates/class_content.cc.
#include <nan.h>
#include <string.h>

extern "C" {
  #include <git2.h>
 }


#include "../include/functions/copy.h"
#include "../include/macros.h"
#include "../include/reference.h"

  #include "../include/reference.h"
  #include "../include/repository.h"
  #include "../include/oid.h"
  #include "../include/signature.h"
  #include "../include/strarray.h"
  #include "../include/object.h"
 
#include <iostream>

using namespace std;
using namespace v8;
using namespace node;

  GitRefs::GitRefs(git_reference *raw, bool selfFreeing) {
    this->raw = raw;
    this->selfFreeing = selfFreeing;
  }

  GitRefs::~GitRefs() {
      if (this->selfFreeing) {
        git_reference_free(this->raw);
      }
   }

  void GitRefs::InitializeComponent(Handle<v8::Object> target) {
    NanScope();

    Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(JSNewFunction);

    tpl->InstanceTemplate()->SetInternalFieldCount(1);
    tpl->SetClassName(NanNew<String>("Reference"));

          NODE_SET_PROTOTYPE_METHOD(tpl, "cmp", Cmp);
             NODE_SET_METHOD(tpl, "create", Create);
             NODE_SET_METHOD(tpl, "createMatching", CreateMatching);
            NODE_SET_PROTOTYPE_METHOD(tpl, "delete", Delete);
             NODE_SET_METHOD(tpl, "dwim", Dwim);
             NODE_SET_METHOD(tpl, "ensureLog", EnsureLog);
             NODE_SET_METHOD(tpl, "hasLog", HasLog);
            NODE_SET_PROTOTYPE_METHOD(tpl, "isBranch", IsBranch);
            NODE_SET_PROTOTYPE_METHOD(tpl, "isNote", IsNote);
            NODE_SET_PROTOTYPE_METHOD(tpl, "isRemote", IsRemote);
            NODE_SET_PROTOTYPE_METHOD(tpl, "isTag", IsTag);
             NODE_SET_METHOD(tpl, "isValidName", IsValidName);
             NODE_SET_METHOD(tpl, "list", List);
             NODE_SET_METHOD(tpl, "lookup", Lookup);
            NODE_SET_PROTOTYPE_METHOD(tpl, "name", Name);
             NODE_SET_METHOD(tpl, "nameToId", NameToId);
             NODE_SET_METHOD(tpl, "normalizeName", NormalizeName);
            NODE_SET_PROTOTYPE_METHOD(tpl, "owner", Owner);
            NODE_SET_PROTOTYPE_METHOD(tpl, "peel", Peel);
             NODE_SET_METHOD(tpl, "remove", Remove);
            NODE_SET_PROTOTYPE_METHOD(tpl, "rename", Rename);
            NODE_SET_PROTOTYPE_METHOD(tpl, "resolve", Resolve);
            NODE_SET_PROTOTYPE_METHOD(tpl, "setTarget", SetTarget);
            NODE_SET_PROTOTYPE_METHOD(tpl, "shorthand", Shorthand);
             NODE_SET_METHOD(tpl, "symbolicCreate", SymbolicCreate);
             NODE_SET_METHOD(tpl, "symbolicCreateMatching", SymbolicCreateMatching);
            NODE_SET_PROTOTYPE_METHOD(tpl, "symbolicSetTarget", SymbolicSetTarget);
            NODE_SET_PROTOTYPE_METHOD(tpl, "symbolicTarget", SymbolicTarget);
            NODE_SET_PROTOTYPE_METHOD(tpl, "target", Target);
            NODE_SET_PROTOTYPE_METHOD(tpl, "targetPeel", TargetPeel);
            NODE_SET_PROTOTYPE_METHOD(tpl, "type", Type);
    
    Local<Function> _constructor_template = tpl->GetFunction();
    NanAssignPersistent(constructor_template, _constructor_template);
    target->Set(NanNew<String>("Reference"), _constructor_template);
  }

  NAN_METHOD(GitRefs::JSNewFunction) {
    NanScope();

    if (args.Length() == 0 || !args[0]->IsExternal()) {
         return NanThrowError("A new GitRefs cannot be instantiated.");
     }

    GitRefs* object = new GitRefs(static_cast<git_reference *>(Handle<External>::Cast(args[0])->Value()), args[1]->BooleanValue());
    object->Wrap(args.This());

    NanReturnValue(args.This());
  }

  Handle<Value> GitRefs::New(void *raw, bool selfFreeing) {
    NanEscapableScope();
    Handle<Value> argv[2] = { NanNew<External>((void *)raw), NanNew<Boolean>(selfFreeing) };
    return NanEscapeScope(NanNew<Function>(GitRefs::constructor_template)->NewInstance(2, argv));
  }

  git_reference *GitRefs::GetValue() {
    return this->raw;
  }

  git_reference **GitRefs::GetRefValue() {
    return &this->raw;
  }

  
/*
       * @return Number  result    */
NAN_METHOD(GitRefs::Cmp) {
  NanEscapableScope();

  int result = git_reference_cmp(
ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue()
,ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue()
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
  
/*
  * @param Repository repo
   * @param String name
   * @param Oid id
   * @param Number force
   * @param Signature signature
   * @param String log_message
    * @param Reference callback
   */
NAN_METHOD(GitRefs::Create) {
  NanScope();

  if (args.Length() == 0 || !args[0]->IsObject()) {
    return NanThrowError("Repository repo is required.");
  }

  if (args.Length() == 1 || !args[1]->IsString()) {
    return NanThrowError("String name is required.");
  }

  if (args.Length() == 2
    || (!args[2]->IsObject() && !args[2]->IsString())) {
    return NanThrowError("Oid id is required.");
  }

  if (args.Length() == 3 || !args[3]->IsNumber()) {
    return NanThrowError("Number force is required.");
  }

  if (args.Length() == 4 || !args[4]->IsObject()) {
    return NanThrowError("Signature signature is required.");
  }

  if (args.Length() == 5 || !args[5]->IsString()) {
    return NanThrowError("String log_message is required.");
  }

  if (args.Length() == 6 || !args[6]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  CreateBaton* baton = new CreateBaton;

  baton->error_code = GIT_OK;
  baton->error = NULL;

// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->repo = from_repo;
// start convert_from_v8 block
  const char * from_name;

  String::Utf8Value name(args[1]->ToString());
  from_name = (const char *) strdup(*name);
// end convert_from_v8 block
  baton->name = from_name;
// start convert_from_v8 block
  const git_oid * from_id;
  if (args[2]->IsString()) {
    // Try and parse in a string to a git_oid
    String::Utf8Value oidString(args[2]->ToString());
    git_oid *oidOut = (git_oid *)malloc(sizeof(git_oid));

    if (git_oid_fromstr(oidOut, (const char *) strdup(*oidString)) != GIT_OK) {
      free(oidOut);

      if (giterr_last()) {
        return NanThrowError(giterr_last()->message);
      } else {
        return NanThrowError("Unknown Error");
      }
    }

    from_id = oidOut;
  }
  else {
    from_id = ObjectWrap::Unwrap<GitOid>(args[2]->ToObject())->GetValue();
  }
// end convert_from_v8 block
  baton->id = from_id;
  baton->idNeedsFree = args[2]->IsString();
// start convert_from_v8 block
  int from_force;
  from_force = (int)   args[3]->ToNumber()->Value();
// end convert_from_v8 block
  baton->force = from_force;
// start convert_from_v8 block
  const git_signature * from_signature;
  from_signature = ObjectWrap::Unwrap<GitSignature>(args[4]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->signature = from_signature;
// start convert_from_v8 block
  const char * from_log_message;

  String::Utf8Value log_message(args[5]->ToString());
  from_log_message = (const char *) strdup(*log_message);
// end convert_from_v8 block
  baton->log_message = from_log_message;

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[6]));
  CreateWorker *worker = new CreateWorker(baton, callback);
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("repo", args[0]->ToObject());
  if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("name", args[1]->ToObject());
  if (!args[2]->IsUndefined() && !args[2]->IsNull())
    worker->SaveToPersistent("id", args[2]->ToObject());
  if (!args[3]->IsUndefined() && !args[3]->IsNull())
    worker->SaveToPersistent("force", args[3]->ToObject());
  if (!args[4]->IsUndefined() && !args[4]->IsNull())
    worker->SaveToPersistent("signature", args[4]->ToObject());
  if (!args[5]->IsUndefined() && !args[5]->IsNull())
    worker->SaveToPersistent("log_message", args[5]->ToObject());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRefs::CreateWorker::Execute() {
  int result = git_reference_create(
&baton->out,baton->repo,baton->name,baton->id,baton->force,baton->signature,baton->log_message    );

  baton->error_code = result;

  if (result != GIT_OK && giterr_last() != NULL) {
    baton->error = git_error_dup(giterr_last());
  }

}

void GitRefs::CreateWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->out != NULL) {
    // GitRefs baton->out
       to = GitRefs::New((void *)baton->out, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
    Handle<Value> result = to;
    Handle<Value> argv[2] = {
      NanNull(),
      result
    };
    callback->Call(2, argv);
  } else {
    if (baton->error) {
      Handle<Value> argv[1] = {
        NanError(baton->error->message)
      };
      callback->Call(1, argv);
      if (baton->error->message)
        free((void *)baton->error->message);
      free((void *)baton->error);
    } else {
      callback->Call(0, NULL);
    }

  }

  if (try_catch.HasCaught()) {
    node::FatalException(try_catch);
  }

  free((void *)baton->name);
  if (baton->idNeedsFree) {
    baton->idNeedsFree = false;
    free((void *)baton->id);
  }
  free((void *)baton->log_message);

  delete baton;
}
  
/*
  * @param Repository repo
   * @param String name
   * @param Oid id
   * @param Number force
   * @param Oid current_id
   * @param Signature signature
   * @param String log_message
    * @param Reference callback
   */
NAN_METHOD(GitRefs::CreateMatching) {
  NanScope();

  if (args.Length() == 0 || !args[0]->IsObject()) {
    return NanThrowError("Repository repo is required.");
  }

  if (args.Length() == 1 || !args[1]->IsString()) {
    return NanThrowError("String name is required.");
  }

  if (args.Length() == 2
    || (!args[2]->IsObject() && !args[2]->IsString())) {
    return NanThrowError("Oid id is required.");
  }

  if (args.Length() == 3 || !args[3]->IsNumber()) {
    return NanThrowError("Number force is required.");
  }

  if (args.Length() == 4
    || (!args[4]->IsObject() && !args[4]->IsString())) {
    return NanThrowError("Oid current_id is required.");
  }

  if (args.Length() == 5 || !args[5]->IsObject()) {
    return NanThrowError("Signature signature is required.");
  }

  if (args.Length() == 6 || !args[6]->IsString()) {
    return NanThrowError("String log_message is required.");
  }

  if (args.Length() == 7 || !args[7]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  CreateMatchingBaton* baton = new CreateMatchingBaton;

  baton->error_code = GIT_OK;
  baton->error = NULL;

// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->repo = from_repo;
// start convert_from_v8 block
  const char * from_name;

  String::Utf8Value name(args[1]->ToString());
  from_name = (const char *) strdup(*name);
// end convert_from_v8 block
  baton->name = from_name;
// start convert_from_v8 block
  const git_oid * from_id;
  if (args[2]->IsString()) {
    // Try and parse in a string to a git_oid
    String::Utf8Value oidString(args[2]->ToString());
    git_oid *oidOut = (git_oid *)malloc(sizeof(git_oid));

    if (git_oid_fromstr(oidOut, (const char *) strdup(*oidString)) != GIT_OK) {
      free(oidOut);

      if (giterr_last()) {
        return NanThrowError(giterr_last()->message);
      } else {
        return NanThrowError("Unknown Error");
      }
    }

    from_id = oidOut;
  }
  else {
    from_id = ObjectWrap::Unwrap<GitOid>(args[2]->ToObject())->GetValue();
  }
// end convert_from_v8 block
  baton->id = from_id;
  baton->idNeedsFree = args[2]->IsString();
// start convert_from_v8 block
  int from_force;
  from_force = (int)   args[3]->ToNumber()->Value();
// end convert_from_v8 block
  baton->force = from_force;
// start convert_from_v8 block
  const git_oid * from_current_id;
  if (args[4]->IsString()) {
    // Try and parse in a string to a git_oid
    String::Utf8Value oidString(args[4]->ToString());
    git_oid *oidOut = (git_oid *)malloc(sizeof(git_oid));

    if (git_oid_fromstr(oidOut, (const char *) strdup(*oidString)) != GIT_OK) {
      free(oidOut);

      if (giterr_last()) {
        return NanThrowError(giterr_last()->message);
      } else {
        return NanThrowError("Unknown Error");
      }
    }

    from_current_id = oidOut;
  }
  else {
    from_current_id = ObjectWrap::Unwrap<GitOid>(args[4]->ToObject())->GetValue();
  }
// end convert_from_v8 block
  baton->current_id = from_current_id;
  baton->current_idNeedsFree = args[4]->IsString();
// start convert_from_v8 block
  const git_signature * from_signature;
  from_signature = ObjectWrap::Unwrap<GitSignature>(args[5]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->signature = from_signature;
// start convert_from_v8 block
  const char * from_log_message;

  String::Utf8Value log_message(args[6]->ToString());
  from_log_message = (const char *) strdup(*log_message);
// end convert_from_v8 block
  baton->log_message = from_log_message;

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[7]));
  CreateMatchingWorker *worker = new CreateMatchingWorker(baton, callback);
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("repo", args[0]->ToObject());
  if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("name", args[1]->ToObject());
  if (!args[2]->IsUndefined() && !args[2]->IsNull())
    worker->SaveToPersistent("id", args[2]->ToObject());
  if (!args[3]->IsUndefined() && !args[3]->IsNull())
    worker->SaveToPersistent("force", args[3]->ToObject());
  if (!args[4]->IsUndefined() && !args[4]->IsNull())
    worker->SaveToPersistent("current_id", args[4]->ToObject());
  if (!args[5]->IsUndefined() && !args[5]->IsNull())
    worker->SaveToPersistent("signature", args[5]->ToObject());
  if (!args[6]->IsUndefined() && !args[6]->IsNull())
    worker->SaveToPersistent("log_message", args[6]->ToObject());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRefs::CreateMatchingWorker::Execute() {
  int result = git_reference_create_matching(
&baton->out,baton->repo,baton->name,baton->id,baton->force,baton->current_id,baton->signature,baton->log_message    );

  baton->error_code = result;

  if (result != GIT_OK && giterr_last() != NULL) {
    baton->error = git_error_dup(giterr_last());
  }

}

void GitRefs::CreateMatchingWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->out != NULL) {
    // GitRefs baton->out
       to = GitRefs::New((void *)baton->out, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
    Handle<Value> result = to;
    Handle<Value> argv[2] = {
      NanNull(),
      result
    };
    callback->Call(2, argv);
  } else {
    if (baton->error) {
      Handle<Value> argv[1] = {
        NanError(baton->error->message)
      };
      callback->Call(1, argv);
      if (baton->error->message)
        free((void *)baton->error->message);
      free((void *)baton->error);
    } else {
      callback->Call(0, NULL);
    }

  }

  if (try_catch.HasCaught()) {
    node::FatalException(try_catch);
  }

  free((void *)baton->name);
  if (baton->idNeedsFree) {
    baton->idNeedsFree = false;
    free((void *)baton->id);
  }
  if (baton->current_idNeedsFree) {
    baton->current_idNeedsFree = false;
    free((void *)baton->current_id);
  }
  free((void *)baton->log_message);

  delete baton;
}
   
/*
     * @return Number  result    */
NAN_METHOD(GitRefs::Delete) {
  NanEscapableScope();

  int result = git_reference_delete(
ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue()
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
  
/*
  * @param Repository repo
   * @param String shorthand
    * @param Reference callback
   */
NAN_METHOD(GitRefs::Dwim) {
  NanScope();

  if (args.Length() == 0 || !args[0]->IsObject()) {
    return NanThrowError("Repository repo is required.");
  }

  if (args.Length() == 1 || !args[1]->IsString()) {
    return NanThrowError("String shorthand is required.");
  }

  if (args.Length() == 2 || !args[2]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  DwimBaton* baton = new DwimBaton;

  baton->error_code = GIT_OK;
  baton->error = NULL;

// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->repo = from_repo;
// start convert_from_v8 block
  const char * from_shorthand;

  String::Utf8Value shorthand(args[1]->ToString());
  from_shorthand = (const char *) strdup(*shorthand);
// end convert_from_v8 block
  baton->shorthand = from_shorthand;

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[2]));
  DwimWorker *worker = new DwimWorker(baton, callback);
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("repo", args[0]->ToObject());
  if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("shorthand", args[1]->ToObject());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRefs::DwimWorker::Execute() {
  int result = git_reference_dwim(
&baton->out,baton->repo,baton->shorthand    );

  baton->error_code = result;

  if (result != GIT_OK && giterr_last() != NULL) {
    baton->error = git_error_dup(giterr_last());
  }

}

void GitRefs::DwimWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->out != NULL) {
    // GitRefs baton->out
       to = GitRefs::New((void *)baton->out, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
    Handle<Value> result = to;
    Handle<Value> argv[2] = {
      NanNull(),
      result
    };
    callback->Call(2, argv);
  } else {
    if (baton->error) {
      Handle<Value> argv[1] = {
        NanError(baton->error->message)
      };
      callback->Call(1, argv);
      if (baton->error->message)
        free((void *)baton->error->message);
      free((void *)baton->error);
    } else {
      callback->Call(0, NULL);
    }

  }

  if (try_catch.HasCaught()) {
    node::FatalException(try_catch);
  }

  free((void *)baton->shorthand);

  delete baton;
}
   
/*
 * @param Repository repo
   * @param String refname
     * @return Number  result    */
NAN_METHOD(GitRefs::EnsureLog) {
  NanEscapableScope();

  if (args.Length() == 0 || !args[0]->IsObject()) {
    return NanThrowError("Repository repo is required.");
  }

  if (args.Length() == 1 || !args[1]->IsString()) {
    return NanThrowError("String refname is required.");
  }

// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_refname;

  String::Utf8Value refname(args[1]->ToString());
  from_refname = (const char *) strdup(*refname);
// end convert_from_v8 block
  int result = git_reference_ensure_log(
from_repo
,from_refname
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
 * @param Repository repo
   * @param String refname
     * @return Number  result    */
NAN_METHOD(GitRefs::HasLog) {
  NanEscapableScope();

  if (args.Length() == 0 || !args[0]->IsObject()) {
    return NanThrowError("Repository repo is required.");
  }

  if (args.Length() == 1 || !args[1]->IsString()) {
    return NanThrowError("String refname is required.");
  }

// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_refname;

  String::Utf8Value refname(args[1]->ToString());
  from_refname = (const char *) strdup(*refname);
// end convert_from_v8 block
  int result = git_reference_has_log(
from_repo
,from_refname
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
     * @return Number  result    */
NAN_METHOD(GitRefs::IsBranch) {
  NanEscapableScope();

  int result = git_reference_is_branch(
ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue()
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
     * @return Number  result    */
NAN_METHOD(GitRefs::IsNote) {
  NanEscapableScope();

  int result = git_reference_is_note(
ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue()
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
     * @return Number  result    */
NAN_METHOD(GitRefs::IsRemote) {
  NanEscapableScope();

  int result = git_reference_is_remote(
ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue()
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
     * @return Number  result    */
NAN_METHOD(GitRefs::IsTag) {
  NanEscapableScope();

  int result = git_reference_is_tag(
ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue()
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
 * @param String refname
     * @return Number  result    */
NAN_METHOD(GitRefs::IsValidName) {
  NanEscapableScope();

  if (args.Length() == 0 || !args[0]->IsString()) {
    return NanThrowError("String refname is required.");
  }

// start convert_from_v8 block
  const char * from_refname;

  String::Utf8Value refname(args[0]->ToString());
  from_refname = (const char *) strdup(*refname);
// end convert_from_v8 block
  int result = git_reference_is_valid_name(
from_refname
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
  
/*
  * @param Repository repo
    * @param Array callback
   */
NAN_METHOD(GitRefs::List) {
  NanScope();

  if (args.Length() == 0 || !args[0]->IsObject()) {
    return NanThrowError("Repository repo is required.");
  }

  if (args.Length() == 1 || !args[1]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  ListBaton* baton = new ListBaton;

  baton->error_code = GIT_OK;
  baton->error = NULL;

  baton->array = (git_strarray *)malloc(sizeof(git_strarray ));
// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->repo = from_repo;

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[1]));
  ListWorker *worker = new ListWorker(baton, callback);
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("repo", args[0]->ToObject());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRefs::ListWorker::Execute() {
  int result = git_reference_list(
baton->array,baton->repo    );

}

void GitRefs::ListWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
    Local<Array> tmpArray = NanNew<Array>(baton->array->count);
    for (unsigned int i = 0; i < baton->array->count; i++) {
      tmpArray->Set(NanNew<Number>(i), NanNew<String>(baton->array->strings[i]));
    }
 
  to = tmpArray;
 // end convert_to_v8 block
    Handle<Value> result = to;
    Handle<Value> argv[2] = {
      NanNull(),
      result
    };
    callback->Call(2, argv);
  } else {
    if (baton->error) {
      Handle<Value> argv[1] = {
        NanError(baton->error->message)
      };
      callback->Call(1, argv);
      if (baton->error->message)
        free((void *)baton->error->message);
      free((void *)baton->error);
    } else {
      callback->Call(0, NULL);
    }

    free((void*)baton->array);
  }

  if (try_catch.HasCaught()) {
    node::FatalException(try_catch);
  }

  free((void *)baton->array);

  delete baton;
}
  
/*
  * @param Repository repo
   * @param String name
    * @param Reference callback
   */
NAN_METHOD(GitRefs::Lookup) {
  NanScope();

  if (args.Length() == 0 || !args[0]->IsObject()) {
    return NanThrowError("Repository repo is required.");
  }

  if (args.Length() == 1 || !args[1]->IsString()) {
    return NanThrowError("String name is required.");
  }

  if (args.Length() == 2 || !args[2]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  LookupBaton* baton = new LookupBaton;

  baton->error_code = GIT_OK;
  baton->error = NULL;

// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->repo = from_repo;
// start convert_from_v8 block
  const char * from_name;

  String::Utf8Value name(args[1]->ToString());
  from_name = (const char *) strdup(*name);
// end convert_from_v8 block
  baton->name = from_name;

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[2]));
  LookupWorker *worker = new LookupWorker(baton, callback);
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("repo", args[0]->ToObject());
  if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("name", args[1]->ToObject());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRefs::LookupWorker::Execute() {
  int result = git_reference_lookup(
&baton->out,baton->repo,baton->name    );

  baton->error_code = result;

  if (result != GIT_OK && giterr_last() != NULL) {
    baton->error = git_error_dup(giterr_last());
  }

}

void GitRefs::LookupWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->out != NULL) {
    // GitRefs baton->out
       to = GitRefs::New((void *)baton->out, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
    Handle<Value> result = to;
    Handle<Value> argv[2] = {
      NanNull(),
      result
    };
    callback->Call(2, argv);
  } else {
    if (baton->error) {
      Handle<Value> argv[1] = {
        NanError(baton->error->message)
      };
      callback->Call(1, argv);
      if (baton->error->message)
        free((void *)baton->error->message);
      free((void *)baton->error);
    } else {
      callback->Call(0, NULL);
    }

  }

  if (try_catch.HasCaught()) {
    node::FatalException(try_catch);
  }

  free((void *)baton->name);

  delete baton;
}
   
/*
     * @return String  result    */
NAN_METHOD(GitRefs::Name) {
  NanEscapableScope();

  const char * result = git_reference_name(
ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue()
  );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

  Handle<Value> to;
// start convert_to_v8 block
  if (result){
       to = NanNew<String>(result);
   }
  else {
    to = NanNull();
  }

  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
  
/*
  * @param Repository repo
   * @param String name
    * @param Oid callback
   */
NAN_METHOD(GitRefs::NameToId) {
  NanScope();

  if (args.Length() == 0 || !args[0]->IsObject()) {
    return NanThrowError("Repository repo is required.");
  }

  if (args.Length() == 1 || !args[1]->IsString()) {
    return NanThrowError("String name is required.");
  }

  if (args.Length() == 2 || !args[2]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  NameToIdBaton* baton = new NameToIdBaton;

  baton->error_code = GIT_OK;
  baton->error = NULL;

  baton->out = (git_oid *)malloc(sizeof(git_oid ));
// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->repo = from_repo;
// start convert_from_v8 block
  const char * from_name;

  String::Utf8Value name(args[1]->ToString());
  from_name = (const char *) strdup(*name);
// end convert_from_v8 block
  baton->name = from_name;

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[2]));
  NameToIdWorker *worker = new NameToIdWorker(baton, callback);
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("repo", args[0]->ToObject());
  if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("name", args[1]->ToObject());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRefs::NameToIdWorker::Execute() {
  int result = git_reference_name_to_id(
baton->out,baton->repo,baton->name    );

  baton->error_code = result;

  if (result != GIT_OK && giterr_last() != NULL) {
    baton->error = git_error_dup(giterr_last());
  }

}

void GitRefs::NameToIdWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->out != NULL) {
    // GitOid baton->out
       to = GitOid::New((void *)baton->out, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
    Handle<Value> result = to;
    Handle<Value> argv[2] = {
      NanNull(),
      result
    };
    callback->Call(2, argv);
  } else {
    if (baton->error) {
      Handle<Value> argv[1] = {
        NanError(baton->error->message)
      };
      callback->Call(1, argv);
      if (baton->error->message)
        free((void *)baton->error->message);
      free((void *)baton->error);
    } else {
      callback->Call(0, NULL);
    }

  }

  if (try_catch.HasCaught()) {
    node::FatalException(try_catch);
  }

  free((void *)baton->name);

  delete baton;
}
   
/*
 * @param String buffer_out
   * @param Number buffer_size
   * @param String name
   * @param Number flags
     * @return Number  result    */
NAN_METHOD(GitRefs::NormalizeName) {
  NanEscapableScope();

  if (args.Length() == 0 || !args[0]->IsString()) {
    return NanThrowError("String buffer_out is required.");
  }

  if (args.Length() == 1 || !args[1]->IsNumber()) {
    return NanThrowError("Number buffer_size is required.");
  }

  if (args.Length() == 2 || !args[2]->IsString()) {
    return NanThrowError("String name is required.");
  }

  if (args.Length() == 3 || !args[3]->IsNumber()) {
    return NanThrowError("Number flags is required.");
  }

// start convert_from_v8 block
  char * from_buffer_out;

  String::Utf8Value buffer_out(args[0]->ToString());
  from_buffer_out = (char *) strdup(*buffer_out);
// end convert_from_v8 block
// start convert_from_v8 block
  size_t from_buffer_size;
  from_buffer_size = (size_t)   args[1]->ToNumber()->Value();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_name;

  String::Utf8Value name(args[2]->ToString());
  from_name = (const char *) strdup(*name);
// end convert_from_v8 block
// start convert_from_v8 block
  unsigned int from_flags;
  from_flags = (unsigned int)   args[3]->ToNumber()->Value();
// end convert_from_v8 block
  int result = git_reference_normalize_name(
from_buffer_out
,from_buffer_size
,from_name
,from_flags
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
     * @return Repository  result    */
NAN_METHOD(GitRefs::Owner) {
  NanEscapableScope();

  git_repository * result = git_reference_owner(
ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue()
  );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

  Handle<Value> to;
// start convert_to_v8 block
  
  if (result != NULL) {
    // GitRepository result
       to = GitRepository::New((void *)result, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
  
/*
    * @param Number type
    * @param Object callback
   */
NAN_METHOD(GitRefs::Peel) {
  NanScope();

  if (args.Length() == 0 || !args[0]->IsNumber()) {
    return NanThrowError("Number type is required.");
  }

  if (args.Length() == 1 || !args[1]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  PeelBaton* baton = new PeelBaton;

  baton->error_code = GIT_OK;
  baton->error = NULL;

  baton->ref = ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue();
// start convert_from_v8 block
  git_otype from_type;
  from_type = (git_otype)  (int) args[0]->ToNumber()->Value();
// end convert_from_v8 block
  baton->type = from_type;

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[1]));
  PeelWorker *worker = new PeelWorker(baton, callback);
  worker->SaveToPersistent("ref", args.This());
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("type", args[0]->ToObject());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRefs::PeelWorker::Execute() {
  int result = git_reference_peel(
&baton->out,baton->ref,baton->type    );

  baton->error_code = result;

  if (result != GIT_OK && giterr_last() != NULL) {
    baton->error = git_error_dup(giterr_last());
  }

}

void GitRefs::PeelWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->out != NULL) {
    // GitObject baton->out
       to = GitObject::New((void *)baton->out, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
    Handle<Value> result = to;
    Handle<Value> argv[2] = {
      NanNull(),
      result
    };
    callback->Call(2, argv);
  } else {
    if (baton->error) {
      Handle<Value> argv[1] = {
        NanError(baton->error->message)
      };
      callback->Call(1, argv);
      if (baton->error->message)
        free((void *)baton->error->message);
      free((void *)baton->error);
    } else {
      callback->Call(0, NULL);
    }

  }

  if (try_catch.HasCaught()) {
    node::FatalException(try_catch);
  }


  delete baton;
}
   
/*
 * @param Repository repo
   * @param String name
     * @return Number  result    */
NAN_METHOD(GitRefs::Remove) {
  NanEscapableScope();

  if (args.Length() == 0 || !args[0]->IsObject()) {
    return NanThrowError("Repository repo is required.");
  }

  if (args.Length() == 1 || !args[1]->IsString()) {
    return NanThrowError("String name is required.");
  }

// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_name;

  String::Utf8Value name(args[1]->ToString());
  from_name = (const char *) strdup(*name);
// end convert_from_v8 block
  int result = git_reference_remove(
from_repo
,from_name
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
  
/*
    * @param String new_name
   * @param Number force
   * @param Signature signature
   * @param String log_message
    * @param Reference callback
   */
NAN_METHOD(GitRefs::Rename) {
  NanScope();

  if (args.Length() == 0 || !args[0]->IsString()) {
    return NanThrowError("String new_name is required.");
  }

  if (args.Length() == 1 || !args[1]->IsNumber()) {
    return NanThrowError("Number force is required.");
  }

  if (args.Length() == 2 || !args[2]->IsObject()) {
    return NanThrowError("Signature signature is required.");
  }

  if (args.Length() == 3 || !args[3]->IsString()) {
    return NanThrowError("String log_message is required.");
  }

  if (args.Length() == 4 || !args[4]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  RenameBaton* baton = new RenameBaton;

  baton->error_code = GIT_OK;
  baton->error = NULL;

  baton->ref = ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue();
// start convert_from_v8 block
  const char * from_new_name;

  String::Utf8Value new_name(args[0]->ToString());
  from_new_name = (const char *) strdup(*new_name);
// end convert_from_v8 block
  baton->new_name = from_new_name;
// start convert_from_v8 block
  int from_force;
  from_force = (int)   args[1]->ToNumber()->Value();
// end convert_from_v8 block
  baton->force = from_force;
// start convert_from_v8 block
  const git_signature * from_signature;
  from_signature = ObjectWrap::Unwrap<GitSignature>(args[2]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->signature = from_signature;
// start convert_from_v8 block
  const char * from_log_message;

  String::Utf8Value log_message(args[3]->ToString());
  from_log_message = (const char *) strdup(*log_message);
// end convert_from_v8 block
  baton->log_message = from_log_message;

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[4]));
  RenameWorker *worker = new RenameWorker(baton, callback);
  worker->SaveToPersistent("ref", args.This());
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("new_name", args[0]->ToObject());
  if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("force", args[1]->ToObject());
  if (!args[2]->IsUndefined() && !args[2]->IsNull())
    worker->SaveToPersistent("signature", args[2]->ToObject());
  if (!args[3]->IsUndefined() && !args[3]->IsNull())
    worker->SaveToPersistent("log_message", args[3]->ToObject());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRefs::RenameWorker::Execute() {
  int result = git_reference_rename(
&baton->new_ref,baton->ref,baton->new_name,baton->force,baton->signature,baton->log_message    );

  baton->error_code = result;

  if (result != GIT_OK && giterr_last() != NULL) {
    baton->error = git_error_dup(giterr_last());
  }

}

void GitRefs::RenameWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->new_ref != NULL) {
    // GitRefs baton->new_ref
       to = GitRefs::New((void *)baton->new_ref, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
    Handle<Value> result = to;
    Handle<Value> argv[2] = {
      NanNull(),
      result
    };
    callback->Call(2, argv);
  } else {
    if (baton->error) {
      Handle<Value> argv[1] = {
        NanError(baton->error->message)
      };
      callback->Call(1, argv);
      if (baton->error->message)
        free((void *)baton->error->message);
      free((void *)baton->error);
    } else {
      callback->Call(0, NULL);
    }

  }

  if (try_catch.HasCaught()) {
    node::FatalException(try_catch);
  }

  free((void *)baton->new_name);
  free((void *)baton->log_message);

  delete baton;
}
  
/*
     * @param Reference callback
   */
NAN_METHOD(GitRefs::Resolve) {
  NanScope();

  if (args.Length() == 0 || !args[0]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  ResolveBaton* baton = new ResolveBaton;

  baton->error_code = GIT_OK;
  baton->error = NULL;

  baton->ref = ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue();

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[0]));
  ResolveWorker *worker = new ResolveWorker(baton, callback);
  worker->SaveToPersistent("ref", args.This());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRefs::ResolveWorker::Execute() {
  int result = git_reference_resolve(
&baton->out,baton->ref    );

  baton->error_code = result;

  if (result != GIT_OK && giterr_last() != NULL) {
    baton->error = git_error_dup(giterr_last());
  }

}

void GitRefs::ResolveWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->out != NULL) {
    // GitRefs baton->out
       to = GitRefs::New((void *)baton->out, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
    Handle<Value> result = to;
    Handle<Value> argv[2] = {
      NanNull(),
      result
    };
    callback->Call(2, argv);
  } else {
    if (baton->error) {
      Handle<Value> argv[1] = {
        NanError(baton->error->message)
      };
      callback->Call(1, argv);
      if (baton->error->message)
        free((void *)baton->error->message);
      free((void *)baton->error);
    } else {
      callback->Call(0, NULL);
    }

  }

  if (try_catch.HasCaught()) {
    node::FatalException(try_catch);
  }


  delete baton;
}
  
/*
    * @param Oid id
   * @param Signature signature
   * @param String log_message
    * @param Reference callback
   */
NAN_METHOD(GitRefs::SetTarget) {
  NanScope();

  if (args.Length() == 0
    || (!args[0]->IsObject() && !args[0]->IsString())) {
    return NanThrowError("Oid id is required.");
  }

  if (args.Length() == 1 || !args[1]->IsObject()) {
    return NanThrowError("Signature signature is required.");
  }

  if (args.Length() == 2 || !args[2]->IsString()) {
    return NanThrowError("String log_message is required.");
  }

  if (args.Length() == 3 || !args[3]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  SetTargetBaton* baton = new SetTargetBaton;

  baton->error_code = GIT_OK;
  baton->error = NULL;

  baton->ref = ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue();
// start convert_from_v8 block
  const git_oid * from_id;
  if (args[0]->IsString()) {
    // Try and parse in a string to a git_oid
    String::Utf8Value oidString(args[0]->ToString());
    git_oid *oidOut = (git_oid *)malloc(sizeof(git_oid));

    if (git_oid_fromstr(oidOut, (const char *) strdup(*oidString)) != GIT_OK) {
      free(oidOut);

      if (giterr_last()) {
        return NanThrowError(giterr_last()->message);
      } else {
        return NanThrowError("Unknown Error");
      }
    }

    from_id = oidOut;
  }
  else {
    from_id = ObjectWrap::Unwrap<GitOid>(args[0]->ToObject())->GetValue();
  }
// end convert_from_v8 block
  baton->id = from_id;
  baton->idNeedsFree = args[0]->IsString();
// start convert_from_v8 block
  const git_signature * from_signature;
  from_signature = ObjectWrap::Unwrap<GitSignature>(args[1]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->signature = from_signature;
// start convert_from_v8 block
  const char * from_log_message;

  String::Utf8Value log_message(args[2]->ToString());
  from_log_message = (const char *) strdup(*log_message);
// end convert_from_v8 block
  baton->log_message = from_log_message;

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[3]));
  SetTargetWorker *worker = new SetTargetWorker(baton, callback);
  worker->SaveToPersistent("ref", args.This());
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("id", args[0]->ToObject());
  if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("signature", args[1]->ToObject());
  if (!args[2]->IsUndefined() && !args[2]->IsNull())
    worker->SaveToPersistent("log_message", args[2]->ToObject());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRefs::SetTargetWorker::Execute() {
  int result = git_reference_set_target(
&baton->out,baton->ref,baton->id,baton->signature,baton->log_message    );

  baton->error_code = result;

  if (result != GIT_OK && giterr_last() != NULL) {
    baton->error = git_error_dup(giterr_last());
  }

}

void GitRefs::SetTargetWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->out != NULL) {
    // GitRefs baton->out
       to = GitRefs::New((void *)baton->out, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
    Handle<Value> result = to;
    Handle<Value> argv[2] = {
      NanNull(),
      result
    };
    callback->Call(2, argv);
  } else {
    if (baton->error) {
      Handle<Value> argv[1] = {
        NanError(baton->error->message)
      };
      callback->Call(1, argv);
      if (baton->error->message)
        free((void *)baton->error->message);
      free((void *)baton->error);
    } else {
      callback->Call(0, NULL);
    }

  }

  if (try_catch.HasCaught()) {
    node::FatalException(try_catch);
  }

  if (baton->idNeedsFree) {
    baton->idNeedsFree = false;
    free((void *)baton->id);
  }
  free((void *)baton->log_message);

  delete baton;
}
   
/*
     * @return String  result    */
NAN_METHOD(GitRefs::Shorthand) {
  NanEscapableScope();

  const char * result = git_reference_shorthand(
ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue()
  );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

  Handle<Value> to;
// start convert_to_v8 block
  if (result){
       to = NanNew<String>(result);
   }
  else {
    to = NanNull();
  }

  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
  
/*
  * @param Repository repo
   * @param String name
   * @param String target
   * @param Number force
   * @param Signature signature
   * @param String log_message
    * @param Reference callback
   */
NAN_METHOD(GitRefs::SymbolicCreate) {
  NanScope();

  if (args.Length() == 0 || !args[0]->IsObject()) {
    return NanThrowError("Repository repo is required.");
  }

  if (args.Length() == 1 || !args[1]->IsString()) {
    return NanThrowError("String name is required.");
  }

  if (args.Length() == 2 || !args[2]->IsString()) {
    return NanThrowError("String target is required.");
  }

  if (args.Length() == 3 || !args[3]->IsNumber()) {
    return NanThrowError("Number force is required.");
  }

  if (args.Length() == 4 || !args[4]->IsObject()) {
    return NanThrowError("Signature signature is required.");
  }

  if (args.Length() == 5 || !args[5]->IsString()) {
    return NanThrowError("String log_message is required.");
  }

  if (args.Length() == 6 || !args[6]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  SymbolicCreateBaton* baton = new SymbolicCreateBaton;

  baton->error_code = GIT_OK;
  baton->error = NULL;

// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->repo = from_repo;
// start convert_from_v8 block
  const char * from_name;

  String::Utf8Value name(args[1]->ToString());
  from_name = (const char *) strdup(*name);
// end convert_from_v8 block
  baton->name = from_name;
// start convert_from_v8 block
  const char * from_target;

  String::Utf8Value target(args[2]->ToString());
  from_target = (const char *) strdup(*target);
// end convert_from_v8 block
  baton->target = from_target;
// start convert_from_v8 block
  int from_force;
  from_force = (int)   args[3]->ToNumber()->Value();
// end convert_from_v8 block
  baton->force = from_force;
// start convert_from_v8 block
  const git_signature * from_signature;
  from_signature = ObjectWrap::Unwrap<GitSignature>(args[4]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->signature = from_signature;
// start convert_from_v8 block
  const char * from_log_message;

  String::Utf8Value log_message(args[5]->ToString());
  from_log_message = (const char *) strdup(*log_message);
// end convert_from_v8 block
  baton->log_message = from_log_message;

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[6]));
  SymbolicCreateWorker *worker = new SymbolicCreateWorker(baton, callback);
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("repo", args[0]->ToObject());
  if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("name", args[1]->ToObject());
  if (!args[2]->IsUndefined() && !args[2]->IsNull())
    worker->SaveToPersistent("target", args[2]->ToObject());
  if (!args[3]->IsUndefined() && !args[3]->IsNull())
    worker->SaveToPersistent("force", args[3]->ToObject());
  if (!args[4]->IsUndefined() && !args[4]->IsNull())
    worker->SaveToPersistent("signature", args[4]->ToObject());
  if (!args[5]->IsUndefined() && !args[5]->IsNull())
    worker->SaveToPersistent("log_message", args[5]->ToObject());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRefs::SymbolicCreateWorker::Execute() {
  int result = git_reference_symbolic_create(
&baton->out,baton->repo,baton->name,baton->target,baton->force,baton->signature,baton->log_message    );

  baton->error_code = result;

  if (result != GIT_OK && giterr_last() != NULL) {
    baton->error = git_error_dup(giterr_last());
  }

}

void GitRefs::SymbolicCreateWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->out != NULL) {
    // GitRefs baton->out
       to = GitRefs::New((void *)baton->out, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
    Handle<Value> result = to;
    Handle<Value> argv[2] = {
      NanNull(),
      result
    };
    callback->Call(2, argv);
  } else {
    if (baton->error) {
      Handle<Value> argv[1] = {
        NanError(baton->error->message)
      };
      callback->Call(1, argv);
      if (baton->error->message)
        free((void *)baton->error->message);
      free((void *)baton->error);
    } else {
      callback->Call(0, NULL);
    }

  }

  if (try_catch.HasCaught()) {
    node::FatalException(try_catch);
  }

  free((void *)baton->name);
  free((void *)baton->target);
  free((void *)baton->log_message);

  delete baton;
}
  
/*
  * @param Repository repo
   * @param String name
   * @param String target
   * @param Number force
   * @param String current_value
   * @param Signature signature
   * @param String log_message
    * @param Reference callback
   */
NAN_METHOD(GitRefs::SymbolicCreateMatching) {
  NanScope();

  if (args.Length() == 0 || !args[0]->IsObject()) {
    return NanThrowError("Repository repo is required.");
  }

  if (args.Length() == 1 || !args[1]->IsString()) {
    return NanThrowError("String name is required.");
  }

  if (args.Length() == 2 || !args[2]->IsString()) {
    return NanThrowError("String target is required.");
  }

  if (args.Length() == 3 || !args[3]->IsNumber()) {
    return NanThrowError("Number force is required.");
  }

  if (args.Length() == 4 || !args[4]->IsString()) {
    return NanThrowError("String current_value is required.");
  }

  if (args.Length() == 5 || !args[5]->IsObject()) {
    return NanThrowError("Signature signature is required.");
  }

  if (args.Length() == 6 || !args[6]->IsString()) {
    return NanThrowError("String log_message is required.");
  }

  if (args.Length() == 7 || !args[7]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  SymbolicCreateMatchingBaton* baton = new SymbolicCreateMatchingBaton;

  baton->error_code = GIT_OK;
  baton->error = NULL;

// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->repo = from_repo;
// start convert_from_v8 block
  const char * from_name;

  String::Utf8Value name(args[1]->ToString());
  from_name = (const char *) strdup(*name);
// end convert_from_v8 block
  baton->name = from_name;
// start convert_from_v8 block
  const char * from_target;

  String::Utf8Value target(args[2]->ToString());
  from_target = (const char *) strdup(*target);
// end convert_from_v8 block
  baton->target = from_target;
// start convert_from_v8 block
  int from_force;
  from_force = (int)   args[3]->ToNumber()->Value();
// end convert_from_v8 block
  baton->force = from_force;
// start convert_from_v8 block
  const char * from_current_value;

  String::Utf8Value current_value(args[4]->ToString());
  from_current_value = (const char *) strdup(*current_value);
// end convert_from_v8 block
  baton->current_value = from_current_value;
// start convert_from_v8 block
  const git_signature * from_signature;
  from_signature = ObjectWrap::Unwrap<GitSignature>(args[5]->ToObject())->GetValue();
// end convert_from_v8 block
  baton->signature = from_signature;
// start convert_from_v8 block
  const char * from_log_message;

  String::Utf8Value log_message(args[6]->ToString());
  from_log_message = (const char *) strdup(*log_message);
// end convert_from_v8 block
  baton->log_message = from_log_message;

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[7]));
  SymbolicCreateMatchingWorker *worker = new SymbolicCreateMatchingWorker(baton, callback);
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("repo", args[0]->ToObject());
  if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("name", args[1]->ToObject());
  if (!args[2]->IsUndefined() && !args[2]->IsNull())
    worker->SaveToPersistent("target", args[2]->ToObject());
  if (!args[3]->IsUndefined() && !args[3]->IsNull())
    worker->SaveToPersistent("force", args[3]->ToObject());
  if (!args[4]->IsUndefined() && !args[4]->IsNull())
    worker->SaveToPersistent("current_value", args[4]->ToObject());
  if (!args[5]->IsUndefined() && !args[5]->IsNull())
    worker->SaveToPersistent("signature", args[5]->ToObject());
  if (!args[6]->IsUndefined() && !args[6]->IsNull())
    worker->SaveToPersistent("log_message", args[6]->ToObject());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRefs::SymbolicCreateMatchingWorker::Execute() {
  int result = git_reference_symbolic_create_matching(
&baton->out,baton->repo,baton->name,baton->target,baton->force,baton->current_value,baton->signature,baton->log_message    );

  baton->error_code = result;

  if (result != GIT_OK && giterr_last() != NULL) {
    baton->error = git_error_dup(giterr_last());
  }

}

void GitRefs::SymbolicCreateMatchingWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->out != NULL) {
    // GitRefs baton->out
       to = GitRefs::New((void *)baton->out, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
    Handle<Value> result = to;
    Handle<Value> argv[2] = {
      NanNull(),
      result
    };
    callback->Call(2, argv);
  } else {
    if (baton->error) {
      Handle<Value> argv[1] = {
        NanError(baton->error->message)
      };
      callback->Call(1, argv);
      if (baton->error->message)
        free((void *)baton->error->message);
      free((void *)baton->error);
    } else {
      callback->Call(0, NULL);
    }

  }

  if (try_catch.HasCaught()) {
    node::FatalException(try_catch);
  }

  free((void *)baton->name);
  free((void *)baton->target);
  free((void *)baton->current_value);
  free((void *)baton->log_message);

  delete baton;
}
  
/*
    * @param String target
   * @param Signature signature
   * @param String log_message
    * @param Reference callback
   */
NAN_METHOD(GitRefs::SymbolicSetTarget) {
  NanScope();

  if (args.Length() == 0 || !args[0]->IsString()) {
    return NanThrowError("String target is required.");
  }

  if (args.Length() == 3 || !args[3]->IsFunction()) {
    return NanThrowError("Callback is required and must be a Function.");
  }

  SymbolicSetTargetBaton* baton = new SymbolicSetTargetBaton;

  baton->error_code = GIT_OK;
  baton->error = NULL;

  baton->ref = ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue();
// start convert_from_v8 block
  const char * from_target;

  String::Utf8Value target(args[0]->ToString());
  from_target = (const char *) strdup(*target);
// end convert_from_v8 block
  baton->target = from_target;
// start convert_from_v8 block
  const git_signature * from_signature;

  if (args[1]->IsObject()) {
  from_signature = ObjectWrap::Unwrap<GitSignature>(args[1]->ToObject())->GetValue();
  }
  else {
    from_signature = 0;
  }

// end convert_from_v8 block
  baton->signature = from_signature;
// start convert_from_v8 block
  const char * from_log_message;

  if (args[2]->IsString()) {

  String::Utf8Value log_message(args[2]->ToString());
  from_log_message = (const char *) strdup(*log_message);
  }
  else {
    from_log_message = 0;
  }

// end convert_from_v8 block
  baton->log_message = from_log_message;

  NanCallback *callback = new NanCallback(Local<Function>::Cast(args[3]));
  SymbolicSetTargetWorker *worker = new SymbolicSetTargetWorker(baton, callback);
  worker->SaveToPersistent("ref", args.This());
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("target", args[0]->ToObject());
  if (!args[1]->IsUndefined() && !args[1]->IsNull())
    worker->SaveToPersistent("signature", args[1]->ToObject());
  if (!args[2]->IsUndefined() && !args[2]->IsNull())
    worker->SaveToPersistent("log_message", args[2]->ToObject());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitRefs::SymbolicSetTargetWorker::Execute() {
  int result = git_reference_symbolic_set_target(
&baton->out,baton->ref,baton->target,baton->signature,baton->log_message    );

  baton->error_code = result;

  if (result != GIT_OK && giterr_last() != NULL) {
    baton->error = git_error_dup(giterr_last());
  }

}

void GitRefs::SymbolicSetTargetWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->out != NULL) {
    // GitRefs baton->out
       to = GitRefs::New((void *)baton->out, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
    Handle<Value> result = to;
    Handle<Value> argv[2] = {
      NanNull(),
      result
    };
    callback->Call(2, argv);
  } else {
    if (baton->error) {
      Handle<Value> argv[1] = {
        NanError(baton->error->message)
      };
      callback->Call(1, argv);
      if (baton->error->message)
        free((void *)baton->error->message);
      free((void *)baton->error);
    } else {
      callback->Call(0, NULL);
    }

  }

  if (try_catch.HasCaught()) {
    node::FatalException(try_catch);
  }

  free((void *)baton->target);
  free((void *)baton->log_message);

  delete baton;
}
   
/*
     * @return String  result    */
NAN_METHOD(GitRefs::SymbolicTarget) {
  NanEscapableScope();

  const char * result = git_reference_symbolic_target(
ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue()
  );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

  Handle<Value> to;
// start convert_to_v8 block
  if (result){
       to = NanNew<String>(result);
   }
  else {
    to = NanNull();
  }

  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
     * @return Oid  result    */
NAN_METHOD(GitRefs::Target) {
  NanEscapableScope();

  const git_oid * result = git_reference_target(
ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue()
  );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

  Handle<Value> to;
// start convert_to_v8 block
  
  if (result != NULL) {
    // GitOid result
       to = GitOid::New((void *)result, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
     * @return Oid  result    */
NAN_METHOD(GitRefs::TargetPeel) {
  NanEscapableScope();

  const git_oid * result = git_reference_target_peel(
ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue()
  );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

  Handle<Value> to;
// start convert_to_v8 block
  
  if (result != NULL) {
    // GitOid result
       to = GitOid::New((void *)result, false);
   }
  else {
    to = NanNull();
  }

 // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
     * @return Number  result    */
NAN_METHOD(GitRefs::Type) {
  NanEscapableScope();

  git_ref_t result = git_reference_type(
ObjectWrap::Unwrap<GitRefs>(args.This())->GetValue()
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
      Persistent<Function> GitRefs::constructor_template;
 