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

#include "../include/tree.h"
#include "../include/oid.h"
#include "../include/tree_entry.h"
#include "../include/object.h"
#include "../include/repository.h"

#include <iostream>

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

GitTree::GitTree(git_tree *raw, bool selfFreeing)
{
  this->raw = raw;
  this->selfFreeing = selfFreeing;
}

GitTree::~GitTree()
{
  if (this->selfFreeing) {
    git_tree_free(this->raw);
  }
}

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

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

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

  NODE_SET_PROTOTYPE_METHOD(tpl, "entryById", EntryByid);
  NODE_SET_PROTOTYPE_METHOD(tpl, "_entryByIndex", EntryByindex);
  NODE_SET_PROTOTYPE_METHOD(tpl, "entryByName", EntryByname);
  NODE_SET_PROTOTYPE_METHOD(tpl, "entryByPath", EntryBypath);
  NODE_SET_METHOD(tpl, "entryCmp", EntryCmp);
  NODE_SET_METHOD(tpl, "entryDup", EntryDup);
  NODE_SET_METHOD(tpl, "entryFilemode", EntryFilemode);
  NODE_SET_METHOD(tpl, "entryFilemodeRaw", EntryFilemodeRaw);
  NODE_SET_METHOD(tpl, "entryFree", EntryFree);
  NODE_SET_METHOD(tpl, "entryId", EntryId);
  NODE_SET_METHOD(tpl, "entryName", EntryName);
  NODE_SET_METHOD(tpl, "entryToObject", EntryToObject);
  NODE_SET_METHOD(tpl, "entryType", EntryType);
  NODE_SET_PROTOTYPE_METHOD(tpl, "entryCount", Entrycount);
  NODE_SET_PROTOTYPE_METHOD(tpl, "id", Id);
  NODE_SET_METHOD(tpl, "lookup", Lookup);
  NODE_SET_METHOD(tpl, "lookupPrefix", LookupPrefix);
  NODE_SET_PROTOTYPE_METHOD(tpl, "owner", Owner);

  Local<Function> _constructor_template = tpl->GetFunction();
  NanAssignPersistent(constructor_template, _constructor_template);
  target->Set(NanNew<String>("Tree"), _constructor_template);
}

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

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

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

  NanReturnValue(args.This());
}

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

git_tree *GitTree::GetValue()
{
  return this->raw;
}

git_tree **GitTree::GetRefValue()
{
  return &this->raw;
}


/*
   * @param Oid id
     * @return TreeEntry  result    */
NAN_METHOD(GitTree::EntryByid)
{
  NanEscapableScope();

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

// 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
  const git_tree_entry * result = git_tree_entry_byid(
                                    ObjectWrap::Unwrap<GitTree>(args.This())->GetValue()
                                    ,from_id
                                  );
  if (args[0]->IsString()) {
    free(&from_id);
  }
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

  Handle<Value> to;
// start convert_to_v8 block

  if (result != NULL) {
    // GitTreeEntry result
    to = GitTreeEntry::New((void *)result, false);
  } else {
    to = NanNull();
  }

// end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
   * @param Number idx
     * @return TreeEntry  result    */
NAN_METHOD(GitTree::EntryByindex)
{
  NanEscapableScope();

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

// start convert_from_v8 block
  size_t from_idx;
  from_idx = (size_t)   args[0]->ToNumber()->Value();
// end convert_from_v8 block
  const git_tree_entry * result = git_tree_entry_byindex(
                                    ObjectWrap::Unwrap<GitTree>(args.This())->GetValue()
                                    ,from_idx
                                  );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

  Handle<Value> to;
// start convert_to_v8 block

  if (result != NULL) {
    // GitTreeEntry result
    to = GitTreeEntry::New((void *)result, false);
  } else {
    to = NanNull();
  }

// end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
   * @param String filename
     * @return TreeEntry  result    */
NAN_METHOD(GitTree::EntryByname)
{
  NanEscapableScope();

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

// start convert_from_v8 block
  const char * from_filename;

  String::Utf8Value filename(args[0]->ToString());
  from_filename = (const char *) strdup(*filename);
// end convert_from_v8 block
  const git_tree_entry * result = git_tree_entry_byname(
                                    ObjectWrap::Unwrap<GitTree>(args.This())->GetValue()
                                    ,from_filename
                                  );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

  Handle<Value> to;
// start convert_to_v8 block

  if (result != NULL) {
    // GitTreeEntry result
    to = GitTreeEntry::New((void *)result, false);
  } else {
    to = NanNull();
  }

// end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
    * @param String path
    * @param TreeEntry callback
   */
NAN_METHOD(GitTree::EntryBypath)
{
  NanScope();

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

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

  EntryBypathBaton* baton = new EntryBypathBaton;

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

  baton->root = ObjectWrap::Unwrap<GitTree>(args.This())->GetValue();
// start convert_from_v8 block
  const char * from_path;

  String::Utf8Value path(args[0]->ToString());
  from_path = (const char *) strdup(*path);
// end convert_from_v8 block
  baton->path = from_path;

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

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitTree::EntryBypathWorker::Execute()
{
  int result = git_tree_entry_bypath(
                 &baton->out,baton->root,baton->path    );

  baton->error_code = result;

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

}

void GitTree::EntryBypathWorker::HandleOKCallback()
{
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block

    if (baton->out != NULL) {
      // GitTreeEntry baton->out
      to = GitTreeEntry::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->path);

  delete baton;
}

/*
 * @param TreeEntry e1
   * @param TreeEntry e2
     * @return Number  result    */
NAN_METHOD(GitTree::EntryCmp)
{
  NanEscapableScope();

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

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

// start convert_from_v8 block
  const git_tree_entry * from_e1;
  from_e1 = ObjectWrap::Unwrap<GitTreeEntry>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const git_tree_entry * from_e2;
  from_e2 = ObjectWrap::Unwrap<GitTreeEntry>(args[1]->ToObject())->GetValue();
// end convert_from_v8 block
  int result = git_tree_entry_cmp(
                 from_e1
                 ,from_e2
               );

  Handle<Value> to;
// start convert_to_v8 block
  to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
 * @param TreeEntry dest
   * @param TreeEntry source
     * @return Number  result    */
NAN_METHOD(GitTree::EntryDup)
{
  NanEscapableScope();

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

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

// start convert_from_v8 block
  git_tree_entry ** from_dest;
  from_dest = ObjectWrap::Unwrap<GitTreeEntry>(args[0]->ToObject())->GetRefValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const git_tree_entry * from_source;
  from_source = ObjectWrap::Unwrap<GitTreeEntry>(args[1]->ToObject())->GetValue();
// end convert_from_v8 block
  int result = git_tree_entry_dup(
                 from_dest
                 ,from_source
               );

  Handle<Value> to;
// start convert_to_v8 block
  to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
 * @param TreeEntry entry
     * @return Number  result    */
NAN_METHOD(GitTree::EntryFilemode)
{
  NanEscapableScope();

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

// start convert_from_v8 block
  const git_tree_entry * from_entry;
  from_entry = ObjectWrap::Unwrap<GitTreeEntry>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  git_filemode_t result = git_tree_entry_filemode(
                            from_entry
                          );

  Handle<Value> to;
// start convert_to_v8 block
  to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
 * @param TreeEntry entry
     * @return Number  result    */
NAN_METHOD(GitTree::EntryFilemodeRaw)
{
  NanEscapableScope();

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

// start convert_from_v8 block
  const git_tree_entry * from_entry;
  from_entry = ObjectWrap::Unwrap<GitTreeEntry>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  git_filemode_t result = git_tree_entry_filemode_raw(
                            from_entry
                          );

  Handle<Value> to;
// start convert_to_v8 block
  to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
 * @param TreeEntry entry
     */
NAN_METHOD(GitTree::EntryFree)
{
  NanEscapableScope();

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

// start convert_from_v8 block
  git_tree_entry * from_entry;
  from_entry = ObjectWrap::Unwrap<GitTreeEntry>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  git_tree_entry_free(
    from_entry
  );
  NanReturnUndefined();
}

/*
 * @param TreeEntry entry
     * @return Oid  result    */
NAN_METHOD(GitTree::EntryId)
{
  NanEscapableScope();

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

// start convert_from_v8 block
  const git_tree_entry * from_entry;
  from_entry = ObjectWrap::Unwrap<GitTreeEntry>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  const git_oid * result = git_tree_entry_id(
                             from_entry
                           );
  // 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 TreeEntry entry
     * @return String  result    */
NAN_METHOD(GitTree::EntryName)
{
  NanEscapableScope();

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

// start convert_from_v8 block
  const git_tree_entry * from_entry;
  from_entry = ObjectWrap::Unwrap<GitTreeEntry>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  const char * result = git_tree_entry_name(
                          from_entry
                        );
  // 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 Object object_out
   * @param Repository repo
   * @param TreeEntry entry
     * @return Number  result    */
NAN_METHOD(GitTree::EntryToObject)
{
  NanEscapableScope();

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

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

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

// start convert_from_v8 block
  git_object ** from_object_out;
  from_object_out = ObjectWrap::Unwrap<GitObject>(args[0]->ToObject())->GetRefValue();
// 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 git_tree_entry * from_entry;
  from_entry = ObjectWrap::Unwrap<GitTreeEntry>(args[2]->ToObject())->GetValue();
// end convert_from_v8 block
  int result = git_tree_entry_to_object(
                 from_object_out
                 ,from_repo
                 ,from_entry
               );

  Handle<Value> to;
// start convert_to_v8 block
  to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
 * @param TreeEntry entry
     * @return Number  result    */
NAN_METHOD(GitTree::EntryType)
{
  NanEscapableScope();

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

// start convert_from_v8 block
  const git_tree_entry * from_entry;
  from_entry = ObjectWrap::Unwrap<GitTreeEntry>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  git_otype result = git_tree_entry_type(
                       from_entry
                     );

  Handle<Value> to;
// start convert_to_v8 block
  to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
     * @return Number  result    */
NAN_METHOD(GitTree::Entrycount)
{
  NanEscapableScope();

  size_t result = git_tree_entrycount(
                    ObjectWrap::Unwrap<GitTree>(args.This())->GetValue()
                  );

  Handle<Value> to;
// start convert_to_v8 block
  to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}

/*
     * @return Oid  result    */
NAN_METHOD(GitTree::Id)
{
  NanEscapableScope();

  const git_oid * result = git_tree_id(
                             ObjectWrap::Unwrap<GitTree>(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 Oid id
    * @param Tree callback
   */
NAN_METHOD(GitTree::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 GitTree::LookupWorker::Execute()
{
  int result = git_tree_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 GitTree::LookupWorker::HandleOKCallback()
{
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block

    if (baton->out != NULL) {
      // GitTree baton->out
      to = GitTree::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 Tree callback
   */
NAN_METHOD(GitTree::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 GitTree::LookupPrefixWorker::Execute()
{
  int result = git_tree_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 GitTree::LookupPrefixWorker::HandleOKCallback()
{
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block

    if (baton->out != NULL) {
      // GitTree baton->out
      to = GitTree::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 Repository  result    */
NAN_METHOD(GitTree::Owner)
{
  NanEscapableScope();

  git_repository * result = git_tree_owner(
                              ObjectWrap::Unwrap<GitTree>(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);
}
Persistent<Function> GitTree::constructor_template;
