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

  #include "../include/oid.h"
  #include "../include/commit.h"
  #include "../include/signature.h"
  #include "../include/tree.h"
  #include "../include/repository.h"
 
#include <iostream>

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

  GitCommit::GitCommit(git_commit *raw, bool selfFreeing) {
    this->raw = raw;
    this->selfFreeing = selfFreeing;
  }

  GitCommit::~GitCommit() {
      if (this->selfFreeing) {
        git_commit_free(this->raw);
      }
   }

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

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

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

          NODE_SET_PROTOTYPE_METHOD(tpl, "amend", Amend);
            NODE_SET_PROTOTYPE_METHOD(tpl, "author", Author);
            NODE_SET_PROTOTYPE_METHOD(tpl, "committer", Committer);
             NODE_SET_METHOD(tpl, "create", Create);
             NODE_SET_METHOD(tpl, "createV", CreateV);
            NODE_SET_PROTOTYPE_METHOD(tpl, "id", Id);
             NODE_SET_METHOD(tpl, "lookup", Lookup);
             NODE_SET_METHOD(tpl, "lookupPrefix", LookupPrefix);
            NODE_SET_PROTOTYPE_METHOD(tpl, "message", Message);
            NODE_SET_PROTOTYPE_METHOD(tpl, "messageEncoding", MessageEncoding);
            NODE_SET_PROTOTYPE_METHOD(tpl, "messageRaw", MessageRaw);
            NODE_SET_PROTOTYPE_METHOD(tpl, "nthGenAncestor", NthGenAncestor);
            NODE_SET_PROTOTYPE_METHOD(tpl, "owner", Owner);
            NODE_SET_PROTOTYPE_METHOD(tpl, "parent", Parent);
            NODE_SET_PROTOTYPE_METHOD(tpl, "parentId", ParentId);
            NODE_SET_PROTOTYPE_METHOD(tpl, "parentcount", Parentcount);
            NODE_SET_PROTOTYPE_METHOD(tpl, "rawHeader", RawHeader);
            NODE_SET_PROTOTYPE_METHOD(tpl, "summary", Summary);
            NODE_SET_PROTOTYPE_METHOD(tpl, "time", Time);
            NODE_SET_PROTOTYPE_METHOD(tpl, "timeOffset", TimeOffset);
            NODE_SET_PROTOTYPE_METHOD(tpl, "tree", Tree);
            NODE_SET_PROTOTYPE_METHOD(tpl, "treeId", TreeId);
    
    Local<Function> _constructor_template = tpl->GetFunction();
    NanAssignPersistent(constructor_template, _constructor_template);
    target->Set(NanNew<String>("Commit"), _constructor_template);
  }

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

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

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

    NanReturnValue(args.This());
  }

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

  git_commit *GitCommit::GetValue() {
    return this->raw;
  }

  git_commit **GitCommit::GetRefValue() {
    return &this->raw;
  }

  
/*
 * @param Oid id
     * @param String update_ref
   * @param Signature author
   * @param Signature committer
   * @param String message_encoding
   * @param String message
   * @param Tree tree
     * @return Number  result    */
NAN_METHOD(GitCommit::Amend) {
  NanEscapableScope();

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

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

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

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

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

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

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

// start convert_from_v8 block
  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
// start convert_from_v8 block
  const char * from_update_ref;

  String::Utf8Value update_ref(args[1]->ToString());
  from_update_ref = (const char *) strdup(*update_ref);
// end convert_from_v8 block
// start convert_from_v8 block
  const git_signature * from_author;
  from_author = ObjectWrap::Unwrap<GitSignature>(args[2]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const git_signature * from_committer;
  from_committer = ObjectWrap::Unwrap<GitSignature>(args[3]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_message_encoding;

  String::Utf8Value message_encoding(args[4]->ToString());
  from_message_encoding = (const char *) strdup(*message_encoding);
// 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
  const git_tree * from_tree;
  from_tree = ObjectWrap::Unwrap<GitTree>(args[6]->ToObject())->GetValue();
// end convert_from_v8 block
  int result = git_commit_amend(
from_id
,ObjectWrap::Unwrap<GitCommit>(args.This())->GetValue()
,from_update_ref
,from_author
,from_committer
,from_message_encoding
,from_message
,from_tree
  );
  if (args[0]->IsString()) {
    free(&from_id);
  }

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

  const git_signature * result = git_commit_author(
ObjectWrap::Unwrap<GitCommit>(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 Signature  result    */
NAN_METHOD(GitCommit::Committer) {
  NanEscapableScope();

  const git_signature * result = git_commit_committer(
ObjectWrap::Unwrap<GitCommit>(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);
}
   
/*
  * @param Repository repo
   * @param String update_ref
   * @param Signature author
   * @param Signature committer
   * @param String message_encoding
   * @param String message
   * @param Tree tree
   * @param Number parent_count
   * @param Array parents
     * @return Oid id    */
NAN_METHOD(GitCommit::Create) {
  NanEscapableScope();

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

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

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

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

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

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

  if (args.Length() == 8 || !args[8]->IsObject()) {
    return NanThrowError("Array parents is required.");
  }

  git_oid *id = (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
// start convert_from_v8 block
  const char * from_update_ref;

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

  String::Utf8Value update_ref(args[1]->ToString());
  from_update_ref = (const char *) strdup(*update_ref);
  }
  else {
    from_update_ref = 0;
  }

// end convert_from_v8 block
// start convert_from_v8 block
  const git_signature * from_author;
  from_author = ObjectWrap::Unwrap<GitSignature>(args[2]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const git_signature * from_committer;
  from_committer = ObjectWrap::Unwrap<GitSignature>(args[3]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_message_encoding;

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

  String::Utf8Value message_encoding(args[4]->ToString());
  from_message_encoding = (const char *) strdup(*message_encoding);
  }
  else {
    from_message_encoding = 0;
  }

// 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
  const git_tree * from_tree;
  from_tree = ObjectWrap::Unwrap<GitTree>(args[6]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  size_t from_parent_count;
  from_parent_count = (size_t)   args[7]->ToNumber()->Value();
// end convert_from_v8 block
// start convert_from_v8 block
  const git_commit ** from_parents;

  Array *tmp_parents = Array::Cast(*args[8]);
  from_parents = (const git_commit **)malloc(tmp_parents->Length() * sizeof(const git_commit *));
      for (unsigned int i = 0; i < tmp_parents->Length(); i++) {
      from_parents[i] = ObjectWrap::Unwrap<GitCommit>(tmp_parents->Get(NanNew<Number>(static_cast<double>(i)))->ToObject())->GetValue();
      }
// end convert_from_v8 block
  int result = git_commit_create(
id
,from_repo
,from_update_ref
,from_author
,from_committer
,from_message_encoding
,from_message
,from_tree
,from_parent_count
,from_parents
  );

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

 // end convert_to_v8 block
  NodeGitPsueodoNanReturnEscapingValue(to);
}
   
/*
 * @param Oid id
   * @param Repository repo
   * @param String update_ref
   * @param Signature author
   * @param Signature committer
   * @param String message_encoding
   * @param String message
   * @param Tree tree
   * @param Number parent_count
     * @return Number  result    */
NAN_METHOD(GitCommit::CreateV) {
  NanEscapableScope();

  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("Repository repo is required.");
  }

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

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

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

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

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

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

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

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

  String::Utf8Value update_ref(args[2]->ToString());
  from_update_ref = (const char *) strdup(*update_ref);
// end convert_from_v8 block
// start convert_from_v8 block
  const git_signature * from_author;
  from_author = ObjectWrap::Unwrap<GitSignature>(args[3]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const git_signature * from_committer;
  from_committer = ObjectWrap::Unwrap<GitSignature>(args[4]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_message_encoding;

  String::Utf8Value message_encoding(args[5]->ToString());
  from_message_encoding = (const char *) strdup(*message_encoding);
// end convert_from_v8 block
// start convert_from_v8 block
  const char * from_message;

  String::Utf8Value message(args[6]->ToString());
  from_message = (const char *) strdup(*message);
// end convert_from_v8 block
// start convert_from_v8 block
  const git_tree * from_tree;
  from_tree = ObjectWrap::Unwrap<GitTree>(args[7]->ToObject())->GetValue();
// end convert_from_v8 block
// start convert_from_v8 block
  size_t from_parent_count;
  from_parent_count = (size_t)   args[8]->ToNumber()->Value();
// end convert_from_v8 block
  int result = git_commit_create_v(
from_id
,from_repo
,from_update_ref
,from_author
,from_committer
,from_message_encoding
,from_message
,from_tree
,from_parent_count
  );
  if (args[0]->IsString()) {
    free(&from_id);
  }

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

  const git_oid * result = git_commit_id(
ObjectWrap::Unwrap<GitCommit>(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 Commit callback
   */
NAN_METHOD(GitCommit::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 GitCommit::LookupWorker::Execute() {
  int result = git_commit_lookup(
&baton->commit,baton->repo,baton->id    );

  baton->error_code = result;

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

}

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

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->commit != NULL) {
    // GitCommit baton->commit
       to = GitCommit::New((void *)baton->commit, 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 Commit callback
   */
NAN_METHOD(GitCommit::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 GitCommit::LookupPrefixWorker::Execute() {
  int result = git_commit_lookup_prefix(
&baton->commit,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 GitCommit::LookupPrefixWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->commit != NULL) {
    // GitCommit baton->commit
       to = GitCommit::New((void *)baton->commit, 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(GitCommit::Message) {
  NanEscapableScope();

  const char * result = git_commit_message(
ObjectWrap::Unwrap<GitCommit>(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(GitCommit::MessageEncoding) {
  NanEscapableScope();

  const char * result = git_commit_message_encoding(
ObjectWrap::Unwrap<GitCommit>(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(GitCommit::MessageRaw) {
  NanEscapableScope();

  const char * result = git_commit_message_raw(
ObjectWrap::Unwrap<GitCommit>(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 n
    * @param Commit callback
   */
NAN_METHOD(GitCommit::NthGenAncestor) {
  NanScope();

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

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

  NthGenAncestorBaton* baton = new NthGenAncestorBaton;

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

  baton->commit = ObjectWrap::Unwrap<GitCommit>(args.This())->GetValue();
// start convert_from_v8 block
  unsigned int from_n;
  from_n = (unsigned int)   args[0]->ToNumber()->Value();
// end convert_from_v8 block
  baton->n = from_n;

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

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitCommit::NthGenAncestorWorker::Execute() {
  int result = git_commit_nth_gen_ancestor(
&baton->ancestor,baton->commit,baton->n    );

  baton->error_code = result;

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

}

void GitCommit::NthGenAncestorWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->ancestor != NULL) {
    // GitCommit baton->ancestor
       to = GitCommit::New((void *)baton->ancestor, 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;
}
   
/*
     * @return Repository  result    */
NAN_METHOD(GitCommit::Owner) {
  NanEscapableScope();

  git_repository * result = git_commit_owner(
ObjectWrap::Unwrap<GitCommit>(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 n
    * @param Commit callback
   */
NAN_METHOD(GitCommit::Parent) {
  NanScope();

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

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

  ParentBaton* baton = new ParentBaton;

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

  baton->commit = ObjectWrap::Unwrap<GitCommit>(args.This())->GetValue();
// start convert_from_v8 block
  unsigned int from_n;
  from_n = (unsigned int)   args[0]->ToNumber()->Value();
// end convert_from_v8 block
  baton->n = from_n;

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

  NanAsyncQueueWorker(worker);
  NanReturnUndefined();
}

void GitCommit::ParentWorker::Execute() {
  int result = git_commit_parent(
&baton->out,baton->commit,baton->n    );

  baton->error_code = result;

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

}

void GitCommit::ParentWorker::HandleOKCallback() {
  TryCatch try_catch;

  if (baton->error_code == GIT_OK) {
    Handle<Value> to;
// start convert_to_v8 block
  
  if (baton->out != NULL) {
    // GitCommit baton->out
       to = GitCommit::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 Number n
     * @return Oid  result    */
NAN_METHOD(GitCommit::ParentId) {
  NanEscapableScope();

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

// start convert_from_v8 block
  unsigned int from_n;
  from_n = (unsigned int)   args[0]->ToNumber()->Value();
// end convert_from_v8 block
  const git_oid * result = git_commit_parent_id(
ObjectWrap::Unwrap<GitCommit>(args.This())->GetValue()
,from_n
  );
  // 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(GitCommit::Parentcount) {
  NanEscapableScope();

  unsigned int result = git_commit_parentcount(
ObjectWrap::Unwrap<GitCommit>(args.This())->GetValue()
  );

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

  const char * result = git_commit_raw_header(
ObjectWrap::Unwrap<GitCommit>(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(GitCommit::Summary) {
  NanEscapableScope();

  const char * result = git_commit_summary(
ObjectWrap::Unwrap<GitCommit>(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 Number  result    */
NAN_METHOD(GitCommit::Time) {
  NanEscapableScope();

  git_time_t result = git_commit_time(
ObjectWrap::Unwrap<GitCommit>(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(GitCommit::TimeOffset) {
  NanEscapableScope();

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

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

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

// start convert_from_v8 block
  git_tree ** from_tree_out;
  from_tree_out = ObjectWrap::Unwrap<GitTree>(args[0]->ToObject())->GetRefValue();
// end convert_from_v8 block
  int result = git_commit_tree(
from_tree_out
,ObjectWrap::Unwrap<GitCommit>(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(GitCommit::TreeId) {
  NanEscapableScope();

  const git_oid * result = git_commit_tree_id(
ObjectWrap::Unwrap<GitCommit>(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);
}
      Persistent<Function> GitCommit::constructor_template;
 