// 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/tag.h"

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

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

  GitTag::GitTag(git_tag *raw, bool selfFreeing) {
    this->raw = raw;
    this->selfFreeing = selfFreeing;
  }

  GitTag::~GitTag() {
      if (this->selfFreeing) {
        git_tag_free(this->raw);
      }
   }

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

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

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

           NODE_SET_METHOD(tpl, "annotationCreate", AnnotationCreate);
             NODE_SET_METHOD(tpl, "create", Create);
             NODE_SET_METHOD(tpl, "createFromBuffer", CreateFrombuffer);
             NODE_SET_METHOD(tpl, "createLightweight", CreateLightweight);
             NODE_SET_METHOD(tpl, "delete", Delete);
            NODE_SET_PROTOTYPE_METHOD(tpl, "id", Id);
             NODE_SET_METHOD(tpl, "list", List);
             NODE_SET_METHOD(tpl, "listMatch", ListMatch);
             NODE_SET_METHOD(tpl, "lookup", Lookup);
             NODE_SET_METHOD(tpl, "lookupPrefix", LookupPrefix);
            NODE_SET_PROTOTYPE_METHOD(tpl, "message", Message);
            NODE_SET_PROTOTYPE_METHOD(tpl, "name", Name);
            NODE_SET_PROTOTYPE_METHOD(tpl, "owner", Owner);
            NODE_SET_PROTOTYPE_METHOD(tpl, "peel", Peel);
            NODE_SET_PROTOTYPE_METHOD(tpl, "tagger", Tagger);
            NODE_SET_PROTOTYPE_METHOD(tpl, "target", Target);
            NODE_SET_PROTOTYPE_METHOD(tpl, "targetId", TargetId);
            NODE_SET_PROTOTYPE_METHOD(tpl, "targetType", TargetType);
    
    Local<Function> _constructor_template = tpl->GetFunction();
    NanAssignPersistent(constructor_template, _constructor_template);
    target->Set(NanNew<String>("Tag"), _constructor_template);
  }

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

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

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

    NanReturnValue(args.This());
  }

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

  git_tag *GitTag::GetValue() {
    return this->raw;
  }

  git_tag **GitTag::GetRefValue() {
    return &this->raw;
  }

  
/*
 * @param Oid oid
   * @param Repository repo
   * @param String tag_name
   * @param Object target
   * @param Signature tagger
   * @param String message
     * @return Number  result    */
NAN_METHOD(GitTag::AnnotationCreate) {
  NanEscapableScope();

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

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

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

  if (args.Length() == 3 || !args[3]->IsObject()) {
    return NanThrowError("Object target is required.");
  }

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

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

// start convert_from_v8 block
  git_oid * from_oid;
  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_oid = oidOut;
  }
  else {
    from_oid = ObjectWrap::Unwrap<GitOid>(args[0]->ToObject())->GetValue();
  }
// end convert_from_v8 block
// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[1]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_tag_name;

  String::Utf8Value tag_name(args[2]->ToString());
  from_tag_name = (const char *) strdup(*tag_name);
// end convert_from_v8 block
// start convert_from_v8 block
  const git_object * from_target;
  from_target = ObjectWrap::Unwrap<GitObject>(args[3]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const git_signature * from_tagger;
  from_tagger = ObjectWrap::Unwrap<GitSignature>(args[4]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_message;

  String::Utf8Value message(args[5]->ToString());
  from_message = (const char *) strdup(*message);
// end convert_from_v8 block
  int result = git_tag_annotation_create(
from_oid
,from_repo
,from_tag_name
,from_target
,from_tagger
,from_message
  );
  if (args[0]->IsString()) {
    free(&from_oid);
  }

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
 * @param Oid oid
   * @param Repository repo
   * @param String tag_name
   * @param Object target
   * @param Signature tagger
   * @param String message
   * @param Number force
     * @return Number  result    */
NAN_METHOD(GitTag::Create) {
  NanEscapableScope();

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

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

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

  if (args.Length() == 3 || !args[3]->IsObject()) {
    return NanThrowError("Object target is required.");
  }

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

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

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

// start convert_from_v8 block
  git_oid * from_oid;
  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_oid = oidOut;
  }
  else {
    from_oid = ObjectWrap::Unwrap<GitOid>(args[0]->ToObject())->GetValue();
  }
// end convert_from_v8 block
// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[1]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_tag_name;

  String::Utf8Value tag_name(args[2]->ToString());
  from_tag_name = (const char *) strdup(*tag_name);
// end convert_from_v8 block
// start convert_from_v8 block
  const git_object * from_target;
  from_target = ObjectWrap::Unwrap<GitObject>(args[3]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const git_signature * from_tagger;
  from_tagger = ObjectWrap::Unwrap<GitSignature>(args[4]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_message;

  String::Utf8Value message(args[5]->ToString());
  from_message = (const char *) strdup(*message);
// end convert_from_v8 block
// start convert_from_v8 block
  int from_force;
  from_force = (int)   args[6]->ToNumber()->Value();
// end convert_from_v8 block
  int result = git_tag_create(
from_oid
,from_repo
,from_tag_name
,from_target
,from_tagger
,from_message
,from_force
  );
  if (args[0]->IsString()) {
    free(&from_oid);
  }

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
 * @param Oid oid
   * @param Repository repo
   * @param String buffer
   * @param Number force
     * @return Number  result    */
NAN_METHOD(GitTag::CreateFrombuffer) {
  NanEscapableScope();

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

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

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

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

// start convert_from_v8 block
  git_oid * from_oid;
  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_oid = oidOut;
  }
  else {
    from_oid = ObjectWrap::Unwrap<GitOid>(args[0]->ToObject())->GetValue();
  }
// end convert_from_v8 block
// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[1]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_buffer;

  String::Utf8Value buffer(args[2]->ToString());
  from_buffer = (const char *) strdup(*buffer);
// end convert_from_v8 block
// start convert_from_v8 block
  int from_force;
  from_force = (int)   args[3]->ToNumber()->Value();
// end convert_from_v8 block
  int result = git_tag_create_frombuffer(
from_oid
,from_repo
,from_buffer
,from_force
  );
  if (args[0]->IsString()) {
    free(&from_oid);
  }

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
 * @param Oid oid
   * @param Repository repo
   * @param String tag_name
   * @param Object target
   * @param Number force
     * @return Number  result    */
NAN_METHOD(GitTag::CreateLightweight) {
  NanEscapableScope();

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

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

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

  if (args.Length() == 3 || !args[3]->IsObject()) {
    return NanThrowError("Object target is required.");
  }

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

// start convert_from_v8 block
  git_oid * from_oid;
  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_oid = oidOut;
  }
  else {
    from_oid = ObjectWrap::Unwrap<GitOid>(args[0]->ToObject())->GetValue();
  }
// end convert_from_v8 block
// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[1]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_tag_name;

  String::Utf8Value tag_name(args[2]->ToString());
  from_tag_name = (const char *) strdup(*tag_name);
// end convert_from_v8 block
// start convert_from_v8 block
  const git_object * from_target;
  from_target = ObjectWrap::Unwrap<GitObject>(args[3]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  int from_force;
  from_force = (int)   args[4]->ToNumber()->Value();
// end convert_from_v8 block
  int result = git_tag_create_lightweight(
from_oid
,from_repo
,from_tag_name
,from_target
,from_force
  );
  if (args[0]->IsString()) {
    free(&from_oid);
  }

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

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

  if (args.Length() == 1 || !args[1]->IsString()) {
    return NanThrowError("String tag_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_tag_name;

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

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

  const git_oid * result = git_tag_id(
ObjectWrap::Unwrap<GitTag>(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);
}
  
/*
  * @param Repository repo
    * @param Array callback
   */
NAN_METHOD(GitTag::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->tag_names = (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 GitTag::ListWorker::Execute() {
  int result = git_tag_list(
baton->tag_names,baton->repo    );

}

void GitTag::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->tag_names->count);
    for (unsigned int i = 0; i < baton->tag_names->count; i++) {
      tmpArray->Set(NanNew<Number>(i), NanNew<String>(baton->tag_names->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->tag_names);
  }

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

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

  delete baton;
}
   
/*
 * @param Strarray tag_names
   * @param String pattern
   * @param Repository repo
     * @return Number  result    */
NAN_METHOD(GitTag::ListMatch) {
  NanEscapableScope();

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

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

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

// start convert_from_v8 block
  git_strarray * from_tag_names;
  from_tag_names = ObjectWrap::Unwrap<GitStrarray>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_pattern;

  String::Utf8Value pattern(args[1]->ToString());
  from_pattern = (const char *) strdup(*pattern);
// end convert_from_v8 block
// start convert_from_v8 block
  git_repository * from_repo;
  from_repo = ObjectWrap::Unwrap<GitRepository>(args[2]->ToObject())->GetValue();
// end convert_from_v8 block
  int result = git_tag_list_match(
from_tag_names
,from_pattern
,from_repo
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
  
/*
  * @param Repository repo
   * @param Oid id
    * @param Tag callback
   */
NAN_METHOD(GitTag::Lookup) {
  NanScope();

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

  if (args.Length() == 1
    || (!args[1]->IsObject() && !args[1]->IsString())) {
    return NanThrowError("Oid id 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 git_oid * from_id;
  if (args[1]->IsString()) {
    // Try and parse in a string to a git_oid
    String::Utf8Value oidString(args[1]->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[1]->ToObject())->GetValue();
  }
// end convert_from_v8 block
  baton->id = from_id;
  baton->idNeedsFree = args[1]->IsString();

  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("id", args[1]->ToObject());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitTag::LookupWorker::Execute() {
  int result = git_tag_lookup(
&baton->out,baton->repo,baton->id    );

  baton->error_code = result;

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

}

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

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->out != NULL) {
    // GitTag baton->out
       to = GitTag::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);
  }

  delete baton;
}
  
/*
  * @param Repository repo
   * @param Oid id
   * @param Number len
    * @param Tag callback
   */
NAN_METHOD(GitTag::LookupPrefix) {
  NanScope();

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

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

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

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

  LookupPrefixBaton* baton = new LookupPrefixBaton;

  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 git_oid * from_id;
  if (args[1]->IsString()) {
    // Try and parse in a string to a git_oid
    String::Utf8Value oidString(args[1]->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[1]->ToObject())->GetValue();
  }
// end convert_from_v8 block
  baton->id = from_id;
  baton->idNeedsFree = args[1]->IsString();
// start convert_from_v8 block
  size_t from_len;
  from_len = (size_t)   args[2]->ToNumber()->Value();
// end convert_from_v8 block
  baton->len = from_len;

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

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitTag::LookupPrefixWorker::Execute() {
  int result = git_tag_lookup_prefix(
&baton->out,baton->repo,baton->id,baton->len    );

  baton->error_code = result;

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

}

void GitTag::LookupPrefixWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->out != NULL) {
    // GitTag baton->out
       to = GitTag::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);
  }

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

  const char * result = git_tag_message(
ObjectWrap::Unwrap<GitTag>(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 String  result    */
NAN_METHOD(GitTag::Name) {
  NanEscapableScope();

  const char * result = git_tag_name(
ObjectWrap::Unwrap<GitTag>(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 Repository  result    */
NAN_METHOD(GitTag::Owner) {
  NanEscapableScope();

  git_repository * result = git_tag_owner(
ObjectWrap::Unwrap<GitTag>(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 Object tag_target_out
       * @return Number  result    */
NAN_METHOD(GitTag::Peel) {
  NanEscapableScope();

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

// start convert_from_v8 block
  git_object ** from_tag_target_out;
  from_tag_target_out = ObjectWrap::Unwrap<GitObject>(args[0]->ToObject())->GetRefValue();
// end convert_from_v8 block
  int result = git_tag_peel(
from_tag_target_out
,ObjectWrap::Unwrap<GitTag>(args.This())->GetValue()
  );

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

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

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

 // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
      * @return Object target_out    */
NAN_METHOD(GitTag::Target) {
  NanEscapableScope();

  git_object * target_out = 0;
  int result = git_tag_target(
&target_out
,ObjectWrap::Unwrap<GitTag>(args.This())->GetValue()
  );

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

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

  const git_oid * result = git_tag_target_id(
ObjectWrap::Unwrap<GitTag>(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(GitTag::TargetType) {
  NanEscapableScope();

  git_otype result = git_tag_target_type(
ObjectWrap::Unwrap<GitTag>(args.This())->GetValue()
  );

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