Skip to content

Commit

Permalink
Merge pull request #1218 from implausible/feature/git_remote_ls
Browse files Browse the repository at this point in the history
build safe convenience wrapper for git_remote_ls
  • Loading branch information
implausible committed Feb 23, 2017
2 parents 74a7e07 + 91079d7 commit 5c3b406
Show file tree
Hide file tree
Showing 26 changed files with 388 additions and 62 deletions.
9 changes: 8 additions & 1 deletion generate/input/descriptor.json
Expand Up @@ -1805,6 +1805,9 @@
}
},
"remote": {
"dependencies": [
"../include/remote_head.h"
],
"cType": "git_remote",
"selfFreeing": true,
"functions": {
Expand Down Expand Up @@ -1970,7 +1973,11 @@
}
},
"remote_head": {
"ignore": true
"dependencies": [
"../include/functions/free.h"
],
"freeFunctionName": "git_remote_head_free",
"selfFreeing": true
},
"repository": {
"dependencies": [
Expand Down
61 changes: 61 additions & 0 deletions generate/input/libgit2-supplement.json
Expand Up @@ -126,6 +126,28 @@
},
"group": "rebase"
},
"git_remote_reference_list": {
"args": [
{
"name": "out",
"type": "std::vector<git_remote_head*> *"
},
{
"name": "remote",
"type": "git_remote *"
}
],
"type": "function",
"isManual": true,
"cFile": "generate/templates/manual/remote/ls.cc",
"isAsync": true,
"isPrototypeMethod": true,
"group": "remote",
"return": {
"type": "int",
"isErrorCode": true
}
},
"git_reset": {
"type": "function",
"file": "reset.h",
Expand Down Expand Up @@ -277,6 +299,12 @@
"git_reflog_entry_message"
]
],
[
"remote",
[
"git_remote_reference_list"
]
],
[
"revwalk",
[
Expand Down Expand Up @@ -614,6 +642,39 @@
}
}
],
[
"git_remote_head",
{
"types": "struct",
"fields": [
{
"type": "int",
"name": "local"
},
{
"type": "git_oid",
"name": "oid"
},
{
"type": "git_oid",
"name": "loid"
},
{
"type": "char *",
"name": "name"
},
{
"type": "char *",
"name": "symref_target"
}
],
"used": {
"needs": [
"git_remote_reference_list"
]
}
}
],
[
"git_time_t",
{
Expand Down
4 changes: 2 additions & 2 deletions generate/templates/manual/include/convenient_hunk.h
Expand Up @@ -27,9 +27,9 @@ using namespace v8;
class ConvenientHunk : public Nan::ObjectWrap {
public:
static Nan::Persistent<Function> constructor_template;
static void InitializeComponent (Local<v8::Object> target);
static void InitializeComponent (v8::Local<v8::Object> target);

static Local<v8::Value> New(void *raw);
static v8::Local<v8::Value> New(void *raw);

HunkData *GetValue();
char *GetHeader();
Expand Down
4 changes: 2 additions & 2 deletions generate/templates/manual/include/convenient_patch.h
Expand Up @@ -38,9 +38,9 @@ using namespace v8;
class ConvenientPatch : public Nan::ObjectWrap {
public:
static Nan::Persistent<Function> constructor_template;
static void InitializeComponent (Local<v8::Object> target);
static void InitializeComponent (v8::Local<v8::Object> target);

static Local<v8::Value> New(void *raw);
static v8::Local<v8::Value> New(void *raw);

ConvenientLineStats GetLineStats();
git_delta_t GetStatus();
Expand Down
2 changes: 2 additions & 0 deletions generate/templates/manual/include/functions/copy.h
Expand Up @@ -14,6 +14,8 @@ const git_index_time *git_index_time_dup(const git_index_time *arg);
const git_time *git_time_dup(const git_time *arg);
const git_diff_delta *git_diff_delta_dup(const git_diff_delta *arg);
const git_diff_file *git_diff_file_dup(const git_diff_file *arg);
git_remote_head *git_remote_head_dup(const git_remote_head *src);


void git_time_dup(git_time **out, const git_time *arg);
void git_transfer_progress_dup(git_transfer_progress **out, const git_transfer_progress *arg);
Expand Down
12 changes: 12 additions & 0 deletions generate/templates/manual/include/functions/free.h
@@ -0,0 +1,12 @@
#include <v8.h>

#include <string.h>

#include "git2.h"

#ifndef NODEGIT_FREE_FUNCTIONS
#define NODEGIT_FREE_FUNCTIONS

void git_remote_head_free(git_remote_head *remote_head);

#endif
2 changes: 1 addition & 1 deletion generate/templates/manual/include/git_buf_converter.h
Expand Up @@ -10,7 +10,7 @@ using namespace v8;

class StrArrayConverter {
public:
static git_strarray *Convert (Local<v8::Value> val);
static git_strarray *Convert (v8::Local<v8::Value> val);
};

#endif
4 changes: 2 additions & 2 deletions generate/templates/manual/include/str_array_converter.h
Expand Up @@ -11,11 +11,11 @@ using namespace v8;
class StrArrayConverter {
public:

static git_strarray *Convert (Local<v8::Value> val);
static git_strarray *Convert (v8::Local<v8::Value> val);

private:
static git_strarray *ConvertArray(Array *val);
static git_strarray *ConvertString(Local<String> val);
static git_strarray *ConvertString(v8::Local<String> val);
static git_strarray *AllocStrArray(const size_t count);
static git_strarray *ConstructStrArray(int argc, char** argv);
};
Expand Down
4 changes: 2 additions & 2 deletions generate/templates/manual/include/wrapper.h
Expand Up @@ -17,10 +17,10 @@ class Wrapper : public Nan::ObjectWrap {
public:

static Nan::Persistent<FunctionTemplate> constructor_template;
static void InitializeComponent (Local<v8::Object> target);
static void InitializeComponent (v8::Local<v8::Object> target);

void *GetValue();
static Local<v8::Value> New(const void *raw);
static v8::Local<v8::Value> New(const void *raw);

private:
Wrapper(void *raw);
Expand Down
99 changes: 99 additions & 0 deletions generate/templates/manual/remote/ls.cc
@@ -0,0 +1,99 @@
NAN_METHOD(GitRemote::ReferenceList)
{
if (info.Length() == 0 || !info[0]->IsFunction()) {
return Nan::ThrowError("Callback is required and must be a Function.");
}

ReferenceListBaton* baton = new ReferenceListBaton;

baton->error_code = GIT_OK;
baton->error = NULL;
baton->out = new std::vector<git_remote_head*>;
baton->remote = Nan::ObjectWrap::Unwrap<GitRemote>(info.This())->GetValue();

Nan::Callback *callback = new Nan::Callback(Local<Function>::Cast(info[0]));
ReferenceListWorker *worker = new ReferenceListWorker(baton, callback);
worker->SaveToPersistent("remote", info.This());
Nan::AsyncQueueWorker(worker);
return;
}

void GitRemote::ReferenceListWorker::Execute()
{
giterr_clear();

{
LockMaster lockMaster(
/*asyncAction: */true,
baton->remote
);

const git_remote_head **remote_heads;
size_t num_remote_heads;
baton->error_code = git_remote_ls(
&remote_heads,
&num_remote_heads,
baton->remote
);

if (baton->error_code != GIT_OK) {
baton->error = git_error_dup(giterr_last());
delete baton->out;
baton->out = NULL;
return;
}

baton->out->reserve(num_remote_heads);

for (size_t head_index = 0; head_index < num_remote_heads; ++head_index) {
git_remote_head *remote_head = git_remote_head_dup(remote_heads[head_index]);
baton->out->push_back(remote_head);
}
}
}

void GitRemote::ReferenceListWorker::HandleOKCallback()
{
if (baton->out != NULL)
{
unsigned int size = baton->out->size();
Local<Array> result = Nan::New<Array>(size);
for (unsigned int i = 0; i < size; i++) {
Nan::Set(result, Nan::New<Number>(i), GitRemoteHead::New(baton->out->at(i), true));
}

delete baton->out;

Local<v8::Value> argv[2] = {
Nan::Null(),
result
};
callback->Call(2, argv);
}
else if (baton->error)
{
Local<v8::Value> argv[1] = {
Nan::Error(baton->error->message)
};
callback->Call(1, argv);
if (baton->error->message)
{
free((void *)baton->error->message);
}

free((void *)baton->error);
}
else if (baton->error_code < 0)
{
Local<v8::Object> err = Nan::Error("Reference List has thrown an error.")->ToObject();
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
Local<v8::Value> argv[1] = {
err
};
callback->Call(1, argv);
}
else
{
callback->Call(0, NULL);
}
}
15 changes: 15 additions & 0 deletions generate/templates/manual/src/functions/copy.cc
Expand Up @@ -20,3 +20,18 @@ void git_transfer_progress_dup(git_transfer_progress **out, const git_transfer_p
*out = (git_transfer_progress *)malloc(sizeof(git_transfer_progress));
memcpy(*out, arg, sizeof(git_transfer_progress));
}

git_remote_head *git_remote_head_dup(const git_remote_head *src) {
git_remote_head *dest = (git_remote_head *)malloc(sizeof(git_remote_head));
dest->local = src->local;
git_oid_cpy(&dest->oid, &src->oid);
git_oid_cpy(&dest->loid, &src->loid);

dest->name = src->name
? strdup(src->name)
: NULL;
dest->symref_target = src->symref_target
? strdup(src->symref_target)
: NULL;
return dest;
}
9 changes: 9 additions & 0 deletions generate/templates/manual/src/functions/free.cc
@@ -0,0 +1,9 @@
#include <cstring>

#include "git2.h"

void git_remote_head_free(git_remote_head *remote_head) {
free(remote_head->name);
free(remote_head->symref_target);
free(remote_head);
}

0 comments on commit 5c3b406

Please sign in to comment.