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

  #include "../include/index.h"
  #include "../include/index_entry.h"
  #include "../include/repository.h"
  #include "../include/tree.h"
  #include "../include/oid.h"
 
#include <iostream>

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

  GitIndex::GitIndex(git_index *raw, bool selfFreeing) {
    this->raw = raw;
    this->selfFreeing = selfFreeing;
  }

  GitIndex::~GitIndex() {
      if (this->selfFreeing) {
        git_index_free(this->raw);
      }
   }

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

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

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

          NODE_SET_PROTOTYPE_METHOD(tpl, "add", Add);
            NODE_SET_PROTOTYPE_METHOD(tpl, "addByPath", AddBypath);
            NODE_SET_PROTOTYPE_METHOD(tpl, "caps", Caps);
            NODE_SET_PROTOTYPE_METHOD(tpl, "clear", Clear);
            NODE_SET_PROTOTYPE_METHOD(tpl, "conflictAdd", ConflictAdd);
            NODE_SET_PROTOTYPE_METHOD(tpl, "conflictCleanup", ConflictCleanup);
            NODE_SET_PROTOTYPE_METHOD(tpl, "conflictRemove", ConflictRemove);
             NODE_SET_METHOD(tpl, "entryStage", EntryStage);
            NODE_SET_PROTOTYPE_METHOD(tpl, "entryCount", Entrycount);
            NODE_SET_PROTOTYPE_METHOD(tpl, "getByIndex", GetByindex);
            NODE_SET_PROTOTYPE_METHOD(tpl, "getByPath", GetBypath);
            NODE_SET_PROTOTYPE_METHOD(tpl, "hasConflicts", HasConflicts);
             NODE_SET_METHOD(tpl, "open", Open);
            NODE_SET_PROTOTYPE_METHOD(tpl, "owner", Owner);
            NODE_SET_PROTOTYPE_METHOD(tpl, "path", Path);
            NODE_SET_PROTOTYPE_METHOD(tpl, "read", Read);
            NODE_SET_PROTOTYPE_METHOD(tpl, "readTree", ReadTree);
            NODE_SET_PROTOTYPE_METHOD(tpl, "remove", Remove);
            NODE_SET_PROTOTYPE_METHOD(tpl, "removeByPath", RemoveBypath);
            NODE_SET_PROTOTYPE_METHOD(tpl, "removeDirectory", RemoveDirectory);
            NODE_SET_PROTOTYPE_METHOD(tpl, "setCaps", SetCaps);
            NODE_SET_PROTOTYPE_METHOD(tpl, "write", Write);
            NODE_SET_PROTOTYPE_METHOD(tpl, "writeTree", WriteTree);
            NODE_SET_PROTOTYPE_METHOD(tpl, "writeTreeTo", WriteTreeTo);
    
    Local<Function> _constructor_template = tpl->GetFunction();
    NanAssignPersistent(constructor_template, _constructor_template);
    target->Set(NanNew<String>("Index"), _constructor_template);
  }

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

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

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

    NanReturnValue(args.This());
  }

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

  git_index *GitIndex::GetValue() {
    return this->raw;
  }

  git_index **GitIndex::GetRefValue() {
    return &this->raw;
  }

  
/*
   * @param IndexEntry source_entry
     * @return Number  result    */
NAN_METHOD(GitIndex::Add) {
  NanEscapableScope();

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

// start convert_from_v8 block
  const git_index_entry * from_source_entry;
  from_source_entry = ObjectWrap::Unwrap<GitIndexEntry>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  int result = git_index_add(
ObjectWrap::Unwrap<GitIndex>(args.This())->GetValue()
,from_source_entry
  );

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

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

// 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
  int result = git_index_add_bypath(
ObjectWrap::Unwrap<GitIndex>(args.This())->GetValue()
,from_path
  );

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

  int result = git_index_caps(
ObjectWrap::Unwrap<GitIndex>(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(GitIndex::Clear) {
  NanEscapableScope();

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

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
   * @param IndexEntry ancestor_entry
   * @param IndexEntry our_entry
   * @param IndexEntry their_entry
     * @return Number  result    */
NAN_METHOD(GitIndex::ConflictAdd) {
  NanEscapableScope();

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

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

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

// start convert_from_v8 block
  const git_index_entry * from_ancestor_entry;
  from_ancestor_entry = ObjectWrap::Unwrap<GitIndexEntry>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const git_index_entry * from_our_entry;
  from_our_entry = ObjectWrap::Unwrap<GitIndexEntry>(args[1]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const git_index_entry * from_their_entry;
  from_their_entry = ObjectWrap::Unwrap<GitIndexEntry>(args[2]->ToObject())->GetValue();
// end convert_from_v8 block
  int result = git_index_conflict_add(
ObjectWrap::Unwrap<GitIndex>(args.This())->GetValue()
,from_ancestor_entry
,from_our_entry
,from_their_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(GitIndex::ConflictCleanup) {
  NanEscapableScope();

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

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

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

// 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
  int result = git_index_conflict_remove(
ObjectWrap::Unwrap<GitIndex>(args.This())->GetValue()
,from_path
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
 * @param IndexEntry entry
     * @return Number  result    */
NAN_METHOD(GitIndex::EntryStage) {
  NanEscapableScope();

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

// start convert_from_v8 block
  const git_index_entry * from_entry;
  from_entry = ObjectWrap::Unwrap<GitIndexEntry>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  int result = git_index_entry_stage(
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(GitIndex::Entrycount) {
  NanEscapableScope();

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

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
   * @param Number n
     * @return IndexEntry  result    */
NAN_METHOD(GitIndex::GetByindex) {
  NanEscapableScope();

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

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

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

 // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
   * @param String path
   * @param Number stage
     * @return IndexEntry  result    */
NAN_METHOD(GitIndex::GetBypath) {
  NanEscapableScope();

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

// 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
// start convert_from_v8 block
  int from_stage;

  if (args[1]->IsNumber()) {
  from_stage = (int)   args[1]->ToNumber()->Value();
  }
  else {
    from_stage = 0;
  }

// end convert_from_v8 block
  const git_index_entry * result = git_index_get_bypath(
ObjectWrap::Unwrap<GitIndex>(args.This())->GetValue()
,from_path
,from_stage
  );
  // null checks on pointers
  if (!result) {
    NodeGitPsueodoNanReturnEscapingValue(NanUndefined());
  }

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

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

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

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
  
/*
  * @param String index_path
    * @param Index callback
   */
NAN_METHOD(GitIndex::Open) {
  NanScope();

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

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

  OpenBaton* baton = new OpenBaton;

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

// start convert_from_v8 block
  const char * from_index_path;

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

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

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitIndex::OpenWorker::Execute() {
  int result = git_index_open(
&baton->out,baton->index_path    );

  baton->error_code = result;

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

}

void GitIndex::OpenWorker::HandleOKCallback() {
  TryCatch try_catch;

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

  delete baton;
}
   
/*
     * @return Repository  result    */
NAN_METHOD(GitIndex::Owner) {
  NanEscapableScope();

  git_repository * result = git_index_owner(
ObjectWrap::Unwrap<GitIndex>(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);
}
   
/*
     * @return String  result    */
NAN_METHOD(GitIndex::Path) {
  NanEscapableScope();

  const char * result = git_index_path(
ObjectWrap::Unwrap<GitIndex>(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 Number force
     * @return Number  result    */
NAN_METHOD(GitIndex::Read) {
  NanEscapableScope();

// start convert_from_v8 block
  int from_force;

  if (args[0]->IsNumber()) {
  from_force = (int)   args[0]->ToNumber()->Value();
  }
  else {
    from_force = 0;
  }

// end convert_from_v8 block
  int result = git_index_read(
ObjectWrap::Unwrap<GitIndex>(args.This())->GetValue()
,from_force
  );

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
   * @param Tree tree
     * @return Number  result    */
NAN_METHOD(GitIndex::ReadTree) {
  NanEscapableScope();

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

// start convert_from_v8 block
  const git_tree * from_tree;
  from_tree = ObjectWrap::Unwrap<GitTree>(args[0]->ToObject())->GetValue();
// end convert_from_v8 block
  int result = git_index_read_tree(
ObjectWrap::Unwrap<GitIndex>(args.This())->GetValue()
,from_tree
  );

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

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

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

// 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
// start convert_from_v8 block
  int from_stage;
  from_stage = (int)   args[1]->ToNumber()->Value();
// end convert_from_v8 block
  int result = git_index_remove(
ObjectWrap::Unwrap<GitIndex>(args.This())->GetValue()
,from_path
,from_stage
  );

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

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

// 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
  int result = git_index_remove_bypath(
ObjectWrap::Unwrap<GitIndex>(args.This())->GetValue()
,from_path
  );

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

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

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

// start convert_from_v8 block
  const char * from_dir;

  String::Utf8Value dir(args[0]->ToString());
  from_dir = (const char *) strdup(*dir);
// end convert_from_v8 block
// start convert_from_v8 block
  int from_stage;
  from_stage = (int)   args[1]->ToNumber()->Value();
// end convert_from_v8 block
  int result = git_index_remove_directory(
ObjectWrap::Unwrap<GitIndex>(args.This())->GetValue()
,from_dir
,from_stage
  );

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

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

// start convert_from_v8 block
  int from_caps;
  from_caps = (int)   args[0]->ToNumber()->Value();
// end convert_from_v8 block
  int result = git_index_set_caps(
ObjectWrap::Unwrap<GitIndex>(args.This())->GetValue()
,from_caps
  );

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

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

  Handle<Value> to;
// start convert_to_v8 block
     to = NanNew<Number>(result);
  // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
  
/*
     * @param Oid callback
   */
NAN_METHOD(GitIndex::WriteTree) {
  NanScope();

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

  WriteTreeBaton* baton = new WriteTreeBaton;

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

  baton->out = (git_oid *)malloc(sizeof(git_oid ));
  baton->index = ObjectWrap::Unwrap<GitIndex>(args.This())->GetValue();

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

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitIndex::WriteTreeWorker::Execute() {
  int result = git_index_write_tree(
baton->out,baton->index    );

  baton->error_code = result;

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

}

void GitIndex::WriteTreeWorker::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);
  }


  delete baton;
}
  
/*
    * @param Repository repo
    * @param Oid callback
   */
NAN_METHOD(GitIndex::WriteTreeTo) {
  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.");
  }

  WriteTreeToBaton* baton = new WriteTreeToBaton;

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

  baton->out = (git_oid *)malloc(sizeof(git_oid ));
  baton->index = ObjectWrap::Unwrap<GitIndex>(args.This())->GetValue();
// 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]));
  WriteTreeToWorker *worker = new WriteTreeToWorker(baton, callback);
  worker->SaveToPersistent("index", args.This());
  if (!args[0]->IsUndefined() && !args[0]->IsNull())
    worker->SaveToPersistent("repo", args[0]->ToObject());

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitIndex::WriteTreeToWorker::Execute() {
  int result = git_index_write_tree_to(
baton->out,baton->index,baton->repo    );

  baton->error_code = result;

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

}

void GitIndex::WriteTreeToWorker::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);
  }


  delete baton;
}
      Persistent<Function> GitIndex::constructor_template;
 