Skip to content

Commit

Permalink
Added tree c++ objects
Browse files Browse the repository at this point in the history
  • Loading branch information
tbranyen committed Mar 5, 2011
1 parent 79bd7da commit 5f8936f
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
#include "obj.h"
#include "commit.h"
#include "revwalk.h"
#include "tree.h"

extern "C" void init(Handle<v8::Object> target) {
HandleScope scope;
Expand All @@ -30,4 +31,5 @@ extern "C" void init(Handle<v8::Object> target) {
Repo::Initialize(target);
Commit::Initialize(target);
RevWalk::Initialize(target);
Tree::Initialize(target);
}
2 changes: 1 addition & 1 deletion src/blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Blob : public EventEmitter {
*
* @return 0 on success; error code otherwise
*/
int Lookup(git_blob **blob, git_repository *repo, const git_oid *id)
int Lookup(git_blob **blob, git_repository *repo, const git_oid *id);

protected:
/**
Expand Down
57 changes: 57 additions & 0 deletions src/tree.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
*/

#include <v8.h>
#include <node.h>
#include <node_events.h>

#include <git2.h>

#include "repo.h"
#include "tree.h"

using namespace v8;
using namespace node;

void Tree::Initialize (Handle<v8::Object> target) {
HandleScope scope;

Local<FunctionTemplate> t = FunctionTemplate::New(New);

constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("Tree"));

target->Set(String::NewSymbol("Tree"), constructor_template->GetFunction());
}

git_tree* Tree::GetValue() {
return this->tree;
}

void Tree::SetValue(git_tree* tree) {
this->tree = tree;
}

int Tree::New(git_repository* repo) {
return git_tree_new(&this->tree, repo);
}

Handle<Value> Tree::New(const Arguments& args) {
HandleScope scope;

Tree *tree = new Tree();

if(args.Length() == 0 || !args[0]->IsObject()) {
return ThrowException(Exception::Error(String::New("Repo is required and must be an Object.")));
}

Repo *repo = ObjectWrap::Unwrap<Repo>(args[0]->ToObject());
int err = tree->New((git_repository *)repo);

tree->Wrap(args.This());

return args.This();
}
Persistent<FunctionTemplate> Tree::constructor_template;
96 changes: 96 additions & 0 deletions src/tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
*/

#ifndef TREE_H
#define TREE_H

#include <v8.h>
#include <node.h>
#include <node_events.h>

#include <git2.h>

#include "repo.h"

using namespace v8;
using namespace node;

/**
* Class wrapper for libgit2 git_tree
*/
class Tree : public EventEmitter {
public:
/**
* v8::FunctionTemplate used to create Node.js constructor
*/
static Persistent<FunctionTemplate> constructor_template;

/**
* Used to intialize the EventEmitter from Node.js
*
* @param target v8::Object the Node.js module object
*/
static void Initialize(Handle<v8::Object> target);

/**
* Creates new internal git_tree reference
*
* @param repo the repo to use when creating the tree.
* @return 0 on success; error code otherwise
*/
int New(git_repository *repo);

/**
* Accessor for Tree
*
* @return the internal git_tree reference
*/
git_tree* GetValue();

/**
* Mutator for Object
*
* @param obj a git_object object
*/
void SetValue(git_tree* tree);

/**
* Lookup a tree object from a repository.
*
* @param tree pointer to the looked up tree
* @param repo the repo to use when locating the tree.
* @param id identity of the tree to locate.
*
* @return 0 on success; error code otherwise
*/
int Lookup(git_tree **tree, git_repository *repo, const git_oid *id);

protected:
/**
* Constructor
*/
Tree() {};

/**
* Deconstructor
*/
~Tree() {};

/**
* Creates a new instance of Tree to Node.js
*
* @param args v8::Arguments function call arguments from Node.js
*
* @return v8::Object args.This()
*/
static Handle<Value> New(const Arguments& args);

private:
/**
* Internal reference to git_tree object
*/
git_tree *tree;
};

#endif
2 changes: 1 addition & 1 deletion wscript
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ def build(bld):

obj = bld.new_task_gen('cxx', 'shlib', 'node_addon')
obj.target = 'nodegit2'
obj.source = 'src/base.cc src/sig.cc src/blob.cc src/error.cc src/obj.cc src/reference.cc src/repo.cc src/commit.cc src/oid.cc src/revwalk.cc'
obj.source = 'src/base.cc src/sig.cc src/blob.cc src/error.cc src/obj.cc src/reference.cc src/repo.cc src/commit.cc src/oid.cc src/revwalk.cc src/tree.cc'
obj.rpath = abspath('build/shared')
obj.uselib = 'GIT2'

0 comments on commit 5f8936f

Please sign in to comment.